Created
January 5, 2021 01:02
-
-
Save sangheonhan/86043354c81e11ce1bf790d07f451140 to your computer and use it in GitHub Desktop.
Markdownify가 변환하지 못하고 남긴 <pre><code> 블럭을 Makrdown으로 변환
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function markdownFiles(string $directory) : \Generator | |
{ | |
$recurDirIter = new RecursiveDirectoryIterator("./posts/"); | |
$recurIterIter = new RecursiveIteratorIterator($recurDirIter); | |
$regexIter = new RegexIterator($recurIterIter, '/^.+\.md$/', \RecursiveRegexIterator::GET_MATCH); | |
foreach ($regexIter as $filePath) { | |
yield $filePath[0]; | |
} | |
} | |
function convertMarkdownFile(string $markdownFile) : void | |
{ | |
$content = file($markdownFile, \FILE_IGNORE_NEW_LINES); | |
$markdownContent = convertContent($content); | |
backupOriginalFile($markdownFile); | |
saveToMakrdownFile($markdownContent, $markdownFile); | |
} | |
function convertContent(array $content) : array | |
{ | |
$convertedContent = []; | |
$inCode = false; | |
foreach ($content as $line) { | |
if ($inCode == false) { | |
if (preg_match('/<pre>\s*<code\s+class\s*=\s*"(language-)?(?<lang>[^"]+)"\s*>(?<code>.*)<\/code>\s*<\/pre>$/i', $line, $m) || | |
preg_match('/<pre>\s+class\s*=\s*"lang:(?<lang>[^"]+)"\s*>(?<code>.*)\s*<\/pre>$/i', $line, $m) | |
) { | |
if (isset($m["lang"]) && strlen($m["lang"]) > 0) { | |
$beginMark = ['```'.$m["lang"]]; | |
$endMark = ['```']; | |
} else { | |
$beginMark = ["`"]; | |
$endMark = ["`"]; | |
} | |
$convertedContent = array_merge( | |
$convertedContent, | |
$beginMark, | |
decodeHtmlEntity([$m["code"]]), | |
$endMark | |
); | |
continue; | |
} | |
if (preg_match('/<pre>\s*<code\s+class\s*=\s*"(language-)?(?<lang>[^"]+)"\s*>(?<code>.*)$/', $line, $m) || | |
preg_match('/<pre\s+class\s*=\s*"lang:(?<lang>[^"]+)"\s*>(?<code>.*)$/', $line, $m) | |
) { | |
$inCode = true; | |
$codeBlock = ["```{$m['lang']}", "{$m['code']}"]; | |
} else { | |
$convertedContent[] = $line; | |
} | |
} else { | |
if (preg_match('/^(?<code>.*)<\/code>\s*<\/pre>$/', $line, $m) || | |
preg_match('/^(?<code>.*)\s*<\/pre>$/', $line, $m) | |
) { | |
$inCode = false; | |
$codeBlock[] = "{$m['code']}"; | |
$codeBlock[] = "```"; | |
$convertedContent = array_merge($convertedContent, decodeHtmlEntity($codeBlock)); | |
unset($codeBlock); | |
} else { | |
$codeBlock[] = $line; | |
} | |
} | |
} | |
return $convertedContent; | |
} | |
function decodeHtmlEntity(array $codeBlock) : array | |
{ | |
return array_map( | |
function ($line) { | |
return html_entity_decode($line, ENT_QUOTES); | |
}, | |
$codeBlock | |
); | |
} | |
function backupOriginalFile(string $markdownFile) : void | |
{ | |
rename($markdownFile, $markdownFile.".saved"); | |
} | |
function saveToMakrdownFile(array $content, string $filePath) : void | |
{ | |
if (file_put_contents($filePath, implode("\n", $content)) === false) { | |
die("Save error."); | |
} | |
} | |
foreach (markdownFiles("./posts/") as $markdownFile) { | |
convertMarkdownFile($markdownFile); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment