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);
|
||||
foreach($lines as $i => $line) {
|
||||
if(preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
|
||||
// first line
|
||||
if(!$started && preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
|
||||
// first line with custom formatting
|
||||
$started = true;
|
||||
$lines[$i] = sprintf('<pre class="brush: %s">', $matches[1]);
|
||||
} elseif($started && preg_match('/^\t(.*)/', $line, $matches)) {
|
||||
// remove first tab (subsequent tabs are part of the code)
|
||||
$lines[$i] = $matches[1];
|
||||
} elseif(preg_match('/^\t(.*)/', $line, $matches)) {
|
||||
// inner line of ::: block, or first line of standard markdown code block
|
||||
// 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;
|
||||
$started = true;
|
||||
} 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
|
||||
$lines[$i] = '</pre>' . "\n" . $line;
|
||||
$lines[$i] = '</pre>' . "\n\n" . $line;
|
||||
|
||||
// reset state
|
||||
$started = $inner = false;
|
||||
}
|
||||
}
|
||||
|
@ -17,10 +17,21 @@ code block
|
||||
with multiple
|
||||
lines
|
||||
and tab indent
|
||||
and escaped < brackets
|
||||
</pre>
|
||||
|
||||
Normal text after code block
|
||||
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() {
|
||||
|
@ -16,7 +16,8 @@ test
|
||||
with multiple
|
||||
lines
|
||||
and tab indent
|
||||
|
||||
and escaped < brackets
|
||||
|
||||
Normal text after code block
|
||||
|
||||
code block
|
||||
|
Loading…
Reference in New Issue
Block a user