Skip to content
This repository was archived by the owner on Jan 15, 2020. It is now read-only.

Commit 89a3b3d

Browse files
committed
Feature: fenced code blocks
Fixes #98
1 parent 78cefdc commit 89a3b3d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

build.sh

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ php ${SCRIPT_PATH}/table_responsive.php ${DOC_DIR}
7575
echo "Fixing pipes in tables"
7676
php ${SCRIPT_PATH}/table_fix_pipes.php ${DOC_DIR}
7777

78+
# Fix fenced code blocks
79+
echo "Fixing fenced code blocks"
80+
php ${SCRIPT_PATH}/fenced_code.php ${DOC_DIR}
81+
7882
# Replace landing page content
7983
if [ -e .zf-mkdoc-theme-landing ]; then
8084
echo "Replacing landing page content"

fenced_code.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
'<' => '&lt;',
34+
'>' => '&gt;',
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

Comments
 (0)