Last active
September 24, 2016 00:42
-
-
Save rakuishi/ed39995c208e253e591d27a6d2f4b1c4 to your computer and use it in GitHub Desktop.
WordPress で書かれた記事を Markdown 変換する(物理)
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 | |
$dir = './content/archives/'; | |
$filenames = []; | |
if ($handle = opendir($dir)) { | |
while (false !== ($filename = readdir($handle))) { | |
if ($filename != "." && $filename != ".." && strtolower(substr($filename, strrpos($filename, '.') + 1)) == 'md') { | |
$filenames[] = $filename; | |
} | |
} | |
closedir($handle); | |
} | |
foreach($filenames as $index => $filename) { | |
echo $filename . "\n"; | |
$path = $dir . $filename; | |
$contents = file_get_contents($dir . $filename); | |
$pattern = '/<div class="amazlet-box" style="margin-bottom:0px;"><div class="amazlet-image" style="float:left;margin:0px 12px 1px 0px;"><a href="http:\/\/www\.amazon\.co\.jp\/exec\/obidos\/ASIN\/(.+?)\/rakuishi-22\/ref=nosim\/" name="amazletlink" target="_blank"><img src="(.+?)" alt="(.+?)" style="border: none;" \/><\/a><\/div><div class="amazlet-info" style="line-height:120%; margin-bottom: 10px"><div class="amazlet-name" style="margin-bottom:10px;line-height:120%"><a href="http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/.+?\/rakuishi-22\/ref=nosim\/" name="amazletlink" target="_blank">.+?<\/a><div class="amazlet-powered-date" style="font-size:80%;margin-top:5px;line-height:120%">posted with <a.+?>amazlet<\/a>.+?<\/div><\/div><div class="amazlet-detail">.+?<br \/><\/div><div class="amazlet-sub-info" style="float: left;"><div class="amazlet-link" style="margin-top: 5px"><a href="http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/.+?\/rakuishi-22\/ref=nosim\/" name="amazletlink" target="_blank">Amazon.co.jpで詳細を見る<\/a><\/div><\/div><\/div><div class="amazlet-footer" style="clear: left"><\/div><\/div>/i'; | |
$replacement = '{{<amazon id="$1" title="$3" src="$2">}}'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<a href="https:\/\/itunes\.apple\.com\/jp\/app\/id(\d+)\?mt=.+?" target="_blank" rel="nofollow"><img width="100" class="alignleft.*?" align="left" src="(.+?)".*?><\/a><strong>.*?<a.*?>(.+?)<\/a><\/strong>.*?<br style="clear: both;"><br \/>/i'; | |
$replacement = '{{<app id="$1" title="$3" src="$2">}}'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<a href="https:\/\/itunes\.apple\.com\/jp\/app\/id(\d+)\?mt=.+?" target="_blank" rel="nofollow"><img width="100" class="alignleft.*?" align="left" src="(.+?)".*?><\/a><strong>.*?(.+?)<\/strong>.*?<br style="clear: both;"><br \/>/i'; | |
$replacement = '{{<app id="$1" title="$3" src="$2">}}'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<a href="https:\/\/itunes.apple.com\/jp\/app\/id(\d+)\?mt=.+?" target="_blank" rel="nofollow"><img width="100" class="alignleft.*?" align="left" src="(.+?)".*?><\/a><strong>.*?<a.*?>(.+?)<\/a><\/strong>.*?<br style="clear: both;">/i'; | |
$replacement = '{{<app id="$1" title="$3" src="$2">}}'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<a href="https:\/\/itunes.apple.com\/jp\/app\/id(\d+)\?mt=.+?" target="_blank" rel="nofollow"><img width="100" class="alignleft.*?" align="left" src="(.+?)".*?><\/a><strong>.*?(.+?)<\/strong>.*?<br style="clear: both;">/i'; | |
$replacement = '{{<app id="$1" title="$3" src="$2">}}'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<h2>(.+?)<\/h2>/'; | |
$replacement = '## $1'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<h3>(.+?)<\/h3>/'; | |
$replacement = '## $1'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<a.*?href="(.+?)".*?>(.+?)<\/a>/'; | |
$replacement = '[$2]($1)'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<img .*?src="(.+?)".*?>/'; | |
$replacement = ''; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$pattern = '/<pre><code>(.+?)<\/code><\/pre>/is'; | |
preg_match_all($pattern, $contents, $matches); | |
if (count($matches) && count($matches[0])) { | |
for ($i = 0; $i < count($matches[0]); $i++) { | |
$replacement = $matches[1][$i]; | |
$replacement = str_replace('<', '<', $replacement); | |
$replacement = str_replace('>', '>', $replacement); | |
$replacement = str_replace('"', '"', $replacement); | |
$replacement = '```' . "\n" . $replacement . "\n" . '```'; | |
$replacement = str_replace("\n\n```", "\n```", $replacement); | |
$contents = str_replace($matches[0][$i], $replacement, $contents); | |
} | |
} | |
$pattern = '/<ul>(.+?)<\/ul>/is'; | |
preg_match_all($pattern, $contents, $matches); | |
if (count($matches) && count($matches[0])) { | |
for ($i = 0; $i < count($matches[0]); $i++) { | |
$replacement = $matches[1][$i]; | |
$replacement = str_replace(array('<ul>', '</ul>', '</li>'), '', $replacement); | |
$replacement = str_replace('<li>', '* ', $replacement); | |
$contents = str_replace($matches[0][$i], $replacement, $contents); | |
} | |
} | |
$pattern = '/<ol>(.+?)<\/ol>/is'; | |
preg_match_all($pattern, $contents, $matches); | |
if (count($matches) && count($matches[0])) { | |
for ($i = 0; $i < count($matches[0]); $i++) { | |
$replacement = $matches[1][$i]; | |
$replacement = str_replace(array('<ol>', '</ol>', '</li>'), '', $replacement); | |
$replacement = str_replace('<li>', '1. ', $replacement); | |
$contents = str_replace($matches[0][$i], $replacement, $contents); | |
} | |
} | |
$pattern = '/<code>(.+?)<\/code>/'; | |
$replacement = '`$1`'; | |
$contents = preg_replace($pattern, $replacement, $contents); | |
$contents = str_replace(' title=" ', ' title="', $contents); | |
$contents = str_replace('<br style="clear:both;">', '', $contents); | |
file_put_contents($path, $contents); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment