FEATURE <pre> formatting for markdown with custom brush support (:::php to <pre class="brush: php">)

This commit is contained in:
Ingo Schommer 2011-01-10 09:18:44 +00:00
parent 8ba0bcea69
commit b7478b864d
3 changed files with 41 additions and 49 deletions

View File

@ -48,40 +48,30 @@ class DocumentationParser {
return $html; return $html;
} }
/*
function rewrite_code_blocks($md) { function rewrite_code_blocks($md) {
$tabwidth = (defined('MARKDOWN_TAB_WIDTH')) ? MARKDOWN_TAB_WIDTH : 4; $started = false;
$md = preg_replace_callback('{ $inner = false;
(?:\n\n|\A\n?)
[ ]*(\{[a-zA-Z]*\})? # lang $lines = split("\n", $md);
[ ]* \n # Whitespace and newline following marker. foreach($lines as $i => $line) {
( # $1 = the code block -- one or more lines, starting with a space/tab if(preg_match('/^\t*:::\s*(.*)/', $line, $matches)) {
(?> // first line
[ ]{'.$tabwidth.'} # Lines must start with a tab or a tab-width of spaces $started = true;
.*\n+ $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)
((?=^[ ]{0,'.$tabwidth.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc $lines[$i] = $matches[1];
}xm', $inner = true;
array('DocumentationParser', '_do_code_blocks'), $md); } 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) { static function rewrite_image_links($md, $page) {
// Links with titles // Links with titles

View File

@ -4,23 +4,24 @@
*/ */
class DocumentationParserTest extends SapphireTest { class DocumentationParserTest extends SapphireTest {
// function testRewriteCodeBlocks() { function testRewriteCodeBlocks() {
// $page = new DocumentationPage( $page = new DocumentationPage();
// 'test.md', $page->setRelativePath('test.md');
// new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'), $page->setEntity(new DocumentationEntity('mymodule', '2.4', BASE_PATH . '/sapphiredocs/tests/docs/'));
// 'en', $page->setLang('en');
// '2.4' $page->setVersion('2.4');
// ); $result = DocumentationParser::rewrite_code_blocks($page->getMarkdown());
// $result = DocumentationParser::rewrite_code_blocks($page->getMarkdown()); $expected = <<<HTML
// $expected = <<<HTML <pre class="brush: php">
// <pre class="brush: php"> code block
// code block with multiple
// with multiple lines
// lines and tab indent
// </pre> </pre>
// HTML; HTML;
// $this->assertContains($expected, $result);
// } $this->assertContains($expected, $result);
}
function testImageRewrites() { function testImageRewrites() {
// Page on toplevel // Page on toplevel

View File

@ -15,6 +15,7 @@ test
code block code block
with multiple with multiple
lines lines
and tab indent
Normal text after code block Normal text after code block