|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause |
| 4 | + * @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com) |
| 5 | + */ |
| 6 | + |
| 7 | +$docPath = isset($argv[1]) ? $argv[1] : 'doc'; |
| 8 | +$docPath = sprintf('%s/%s', getcwd(), $docPath); |
| 9 | +$docPath = realpath($docPath); |
| 10 | + |
| 11 | +$rdi = new RecursiveDirectoryIterator($docPath . '/html'); |
| 12 | +$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::SELF_FIRST); |
| 13 | +$files = new RegexIterator($rii, '/\.html$/', RecursiveRegexIterator::GET_MATCH); |
| 14 | + |
| 15 | +$process = static function () use ($files) { |
| 16 | + $fileInfo = $files->getInnerIterator()->current(); |
| 17 | + if (! $fileInfo->isFile()) { |
| 18 | + return true; |
| 19 | + } |
| 20 | + |
| 21 | + if ($fileInfo->getBasename('.html') === $fileInfo->getBasename()) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + $file = $fileInfo->getRealPath(); |
| 26 | + $html = file_get_contents($file); |
| 27 | + |
| 28 | + if (preg_match_all('#<p>(?:<code>|```)([a-z]+)(\n.*?)(?:</code>|```)</p>#s', $html, $matches)) { |
| 29 | + foreach ($matches[0] as $i => $content) { |
| 30 | + $code = strtr($matches[2][$i], [ |
| 31 | + '<p>' => '', |
| 32 | + '</p>' => '', |
| 33 | + '<' => '<', |
| 34 | + '>' => '>', |
| 35 | + ]); |
| 36 | + |
| 37 | + $html = str_replace( |
| 38 | + $content, |
| 39 | + '<p><pre class=codehilite><code class=language-' . $matches[1][$i] . '>' . $code . '</code></pre></p>', |
| 40 | + $html |
| 41 | + ); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + file_put_contents($file, $html); |
| 46 | + |
| 47 | + return true; |
| 48 | +}; |
| 49 | + |
| 50 | +iterator_apply($files, $process); |
0 commit comments