From 7516d31ce0252b9f1606aa6a5976c646114bf999 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 1 Aug 2010 21:13:40 +0000 Subject: [PATCH] ENHANCEMENT Started code block parser (still needs work) --- code/DocumentationParser.php | 42 +++++++++++++++++++++++++------ tests/DocumentationParserTest.php | 18 +++++++++++++ tests/docs/en/test.md | 12 ++++++++- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/code/DocumentationParser.php b/code/DocumentationParser.php index e9151ef..2b69744 100644 --- a/code/DocumentationParser.php +++ b/code/DocumentationParser.php @@ -1,9 +1,5 @@ getMarkdown(); // Pre-processing $md = self::rewrite_image_links($md, $page); $md = self::rewrite_relative_links($md, $page, $baselink); $md = self::rewrite_api_links($md, $page); + $md = self::rewrite_code_blocks($md, $page); - + require_once('../sapphiredocs/thirdparty/markdown.php'); $html = Markdown($md); 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); + // + // 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 = "
$codeblock\n
"; + // return "\n\n".$this->hashBlock($codeblock)."\n\n"; + // } + static function rewrite_image_links($md, $page) { // Links with titles $re = '/ diff --git a/tests/DocumentationParserTest.php b/tests/DocumentationParserTest.php index 166a6ca..5de98f1 100644 --- a/tests/DocumentationParserTest.php +++ b/tests/DocumentationParserTest.php @@ -4,6 +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 = << + // code block + // with multiple + // lines + // + // HTML; + // $this->assertContains($expected, $result); + // } + function testImageRewrites() { // Page on toplevel $page = new DocumentationPage( diff --git a/tests/docs/en/test.md b/tests/docs/en/test.md index 3c11f52..8b8919b 100644 --- a/tests/docs/en/test.md +++ b/tests/docs/en/test.md @@ -9,4 +9,14 @@ test [link: with anchor](/test#anchor) [link: http](http://silverstripe.org) [link: api](api:DataObject) -[api:DataObject::$has_one] \ No newline at end of file +[api:DataObject::$has_one] + + :::php + code block + with multiple + lines + +Normal text after code block + + code block + without formatting prefix \ No newline at end of file