mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
BUGFIX Improved DocumentationParser to avoid empty lines in pre tags, deal with normal markdown code blocks, and html encode lines within pre tags
This commit is contained in:
parent
4abf31e8d0
commit
255f5de162
@ -54,17 +54,29 @@ class DocumentationParser {
|
|||||||
|
|
||||||
$lines = split("\n", $md);
|
$lines = split("\n", $md);
|
||||||
foreach($lines as $i => $line) {
|
foreach($lines as $i => $line) {
|
||||||
if(preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
|
if(!$started && preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
|
||||||
// first line
|
// first line with custom formatting
|
||||||
$started = true;
|
$started = true;
|
||||||
$lines[$i] = sprintf('<pre class="brush: %s">', $matches[1]);
|
$lines[$i] = sprintf('<pre class="brush: %s">', $matches[1]);
|
||||||
} elseif($started && preg_match('/^\t(.*)/', $line, $matches)) {
|
} elseif(preg_match('/^\t(.*)/', $line, $matches)) {
|
||||||
// remove first tab (subsequent tabs are part of the code)
|
// inner line of ::: block, or first line of standard markdown code block
|
||||||
$lines[$i] = $matches[1];
|
// regex removes first tab (any following tabs are part of the code).
|
||||||
|
$lines[$i] = ($started) ? '' : '<pre>' . "\n";
|
||||||
|
$lines[$i] .= htmlentities($matches[1], ENT_COMPAT, 'UTF-8');
|
||||||
$inner = true;
|
$inner = true;
|
||||||
|
$started = true;
|
||||||
} elseif($started && $inner) {
|
} elseif($started && $inner) {
|
||||||
|
// remove any previous blank lines
|
||||||
|
$j = $i-1;
|
||||||
|
while(isset($lines[$j]) && preg_match('/^[\t\s]*$/', $lines[$j])) {
|
||||||
|
unset($lines[$j]);
|
||||||
|
$j--;
|
||||||
|
}
|
||||||
|
|
||||||
// last line, close pre
|
// last line, close pre
|
||||||
$lines[$i] = '</pre>' . "\n" . $line;
|
$lines[$i] = '</pre>' . "\n\n" . $line;
|
||||||
|
|
||||||
|
// reset state
|
||||||
$started = $inner = false;
|
$started = $inner = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,21 @@ code block
|
|||||||
with multiple
|
with multiple
|
||||||
lines
|
lines
|
||||||
and tab indent
|
and tab indent
|
||||||
|
and escaped < brackets
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
Normal text after code block
|
||||||
HTML;
|
HTML;
|
||||||
|
|
||||||
$this->assertContains($expected, $result);
|
$this->assertContains($expected, $result, 'Custom code blocks with ::: prefix');
|
||||||
|
|
||||||
|
$expected = <<<HTML
|
||||||
|
<pre>
|
||||||
|
code block
|
||||||
|
without formatting prefix
|
||||||
|
</pre>
|
||||||
|
HTML;
|
||||||
|
$this->assertContains($expected, $result, 'Traditional markdown code blocks');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testImageRewrites() {
|
function testImageRewrites() {
|
||||||
|
@ -16,7 +16,8 @@ test
|
|||||||
with multiple
|
with multiple
|
||||||
lines
|
lines
|
||||||
and tab indent
|
and tab indent
|
||||||
|
and escaped < brackets
|
||||||
|
|
||||||
Normal text after code block
|
Normal text after code block
|
||||||
|
|
||||||
code block
|
code block
|
||||||
|
Loading…
Reference in New Issue
Block a user