ENHANCEMENT Started code block parser (still needs work)

This commit is contained in:
Ingo Schommer 2010-08-01 21:13:40 +00:00
parent e1da17e9d9
commit 7516d31ce0
3 changed files with 64 additions and 8 deletions

View File

@ -1,9 +1,5 @@
<?php
/**
* Wrapper for MarkdownUltra parsing in the template and related functionality for
* parsing paths and documents
*
* @package sapphiredocs
*/
@ -32,21 +28,53 @@ class DocumentationParser {
* @return String
*/
public static function parse(DocumentationPage $page, $baselink = null) {
require_once('../sapphiredocs/thirdparty/markdown.php');
$md = $page->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 = "<pre><code>$codeblock\n</code></pre>";
// return "\n\n".$this->hashBlock($codeblock)."\n\n";
// }
static function rewrite_image_links($md, $page) {
// Links with titles
$re = '/

View File

@ -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 = <<<HTML
// <pre class="brush: php">
// code block
// with multiple
// lines
// </pre>
// HTML;
// $this->assertContains($expected, $result);
// }
function testImageRewrites() {
// Page on toplevel
$page = new DocumentationPage(

View File

@ -9,4 +9,14 @@ test
[link: with anchor](/test#anchor)
[link: http](http://silverstripe.org)
[link: api](api:DataObject)
[api:DataObject::$has_one]
[api:DataObject::$has_one]
:::php
code block
with multiple
lines
Normal text after code block
code block
without formatting prefix