mirror of
https://github.com/silverstripe/silverstripe-docsviewer
synced 2024-10-22 11:05:56 +02:00
FEATURE <pre> formatting for markdown with custom brush support (:::php to <pre class="brush: php">)
This commit is contained in:
parent
8ba0bcea69
commit
b7478b864d
@ -48,40 +48,30 @@ class DocumentationParser {
|
||||
return $html;
|
||||
}
|
||||
|
||||
/*
|
||||
function rewrite_code_blocks($md) {
|
||||
$tabwidth = (defined('MARKDOWN_TAB_WIDTH')) ? MARKDOWN_TAB_WIDTH : 4;
|
||||
$md = preg_replace_callback('{
|
||||
(?:\n\n|\A\n?)
|
||||
[ ]*(\{[a-zA-Z]*\})? # lang
|
||||
[ ]* \n # Whitespace and newline following marker.
|
||||
( # $1 = the code block -- one or more lines, starting with a space/tab
|
||||
(?>
|
||||
[ ]{'.$tabwidth.'} # Lines must start with a tab or a tab-width of spaces
|
||||
.*\n+
|
||||
)+
|
||||
)
|
||||
((?=^[ ]{0,'.$tabwidth.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
|
||||
}xm',
|
||||
array('DocumentationParser', '_do_code_blocks'), $md);
|
||||
$started = false;
|
||||
$inner = false;
|
||||
|
||||
$lines = split("\n", $md);
|
||||
foreach($lines as $i => $line) {
|
||||
if(preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
|
||||
// first line
|
||||
$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];
|
||||
$inner = true;
|
||||
} elseif($started && $inner) {
|
||||
// last line, close pre
|
||||
$lines[$i] = '</pre>' . "\n" . $line;
|
||||
$started = $inner = false;
|
||||
}
|
||||
}
|
||||
|
||||
return join("\n", $lines);
|
||||
|
||||
return $md;
|
||||
}
|
||||
static function _do_code_blocks($matches) {
|
||||
$tabwidth = (defined('MARKDOWN_TAB_WIDTH')) ? MARKDOWN_TAB_WIDTH : 4;
|
||||
$codeblock = $matches[2];
|
||||
|
||||
// outdent
|
||||
$codeblock = preg_replace('/^(\t|[ ]{1,'.$tabwidth.'})/m', '', $codeblock);
|
||||
$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
|
||||
|
||||
# trim leading newlines and trailing newlines
|
||||
$codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock);
|
||||
|
||||
$codeblock = "<pre><code>$codeblock\n</code></pre>";
|
||||
return "\n\n".$this->hashBlock($codeblock)."\n\n";
|
||||
}
|
||||
*/
|
||||
|
||||
static function rewrite_image_links($md, $page) {
|
||||
// Links with titles
|
||||
|
@ -4,23 +4,24 @@
|
||||
*/
|
||||
class DocumentationParserTest extends SapphireTest {
|
||||
|
||||
// function testRewriteCodeBlocks() {
|
||||
// $page = new DocumentationPage(
|
||||
// 'test.md',
|
||||
// new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'),
|
||||
// 'en',
|
||||
// '2.4'
|
||||
// );
|
||||
// $result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
|
||||
// $expected = <<<HTML
|
||||
// <pre class="brush: php">
|
||||
// code block
|
||||
// with multiple
|
||||
// lines
|
||||
// </pre>
|
||||
// HTML;
|
||||
// $this->assertContains($expected, $result);
|
||||
// }
|
||||
function testRewriteCodeBlocks() {
|
||||
$page = new DocumentationPage();
|
||||
$page->setRelativePath('test.md');
|
||||
$page->setEntity(new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'));
|
||||
$page->setLang('en');
|
||||
$page->setVersion('2.4');
|
||||
$result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
|
||||
$expected = <<<HTML
|
||||
<pre class="brush: php">
|
||||
code block
|
||||
with multiple
|
||||
lines
|
||||
and tab indent
|
||||
</pre>
|
||||
HTML;
|
||||
|
||||
$this->assertContains($expected, $result);
|
||||
}
|
||||
|
||||
function testImageRewrites() {
|
||||
// Page on toplevel
|
||||
|
@ -15,7 +15,8 @@ test
|
||||
code block
|
||||
with multiple
|
||||
lines
|
||||
|
||||
and tab indent
|
||||
|
||||
Normal text after code block
|
||||
|
||||
code block
|
||||
|
Loading…
Reference in New Issue
Block a user