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;
}
/*
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

View File

@ -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

View File

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