Adds support for github style triple backtick code blocks
diff --git a/pkgs/markdown/lib/src/markdown/block_parser.dart b/pkgs/markdown/lib/src/markdown/block_parser.dart index 145743c..e1d04ec 100644 --- a/pkgs/markdown/lib/src/markdown/block_parser.dart +++ b/pkgs/markdown/lib/src/markdown/block_parser.dart
@@ -19,6 +19,9 @@ /// A line indented four spaces. Used for code blocks and lists. final _RE_INDENT = new RegExp(r'^(?: |\t)(.*)$'); +/// GitHub style triple quoted code block. +final _RE_CODE = new RegExp(r'^```(\w*)$'); + /// Three or more hyphens, asterisks or underscores by themselves. Note that /// a line like `----` is valid as both HR and SETEXT. In case of a tie, /// SETEXT should win. @@ -94,6 +97,7 @@ new SetextHeaderSyntax(), new HeaderSyntax(), new CodeBlockSyntax(), + new GitHubCodeBlockSyntax(), new BlockquoteSyntax(), new HorizontalRuleSyntax(), new UnorderedListSyntax(), @@ -234,7 +238,44 @@ childLines.add(''); // Escape the code. - final escaped = Strings.join(childLines, '\n');//classifySource(); + final escaped = childLines.join('n');//classifySource(); + + return new Element.text('pre', escaped); + } +} + +/// Parses preformatted code blocks between two ``` sequences. +class GitHubCodeBlockSyntax extends BlockSyntax { + RegExp get pattern => _RE_CODE; + + List<String> parseChildLines(BlockParser parser) { + final childLines = <String>[]; + parser.advance(); + while (!parser.isDone) { + var match = pattern.firstMatch(parser.current); + if (match == null) { + childLines.add(parser.current); + parser.advance(); + } else { + parser.advance(); + break; + } + } + return childLines; + } + + Node parse(BlockParser parser) { + // Get the syntax identifier, if there is one. + var syntax = pattern.firstMatch(parser.current).group(1); + print('syntax: $syntax'); + + final childLines = parseChildLines(parser); + + // The Markdown tests expect a trailing newline. + childLines.add(''); + + // Escape the code. + final escaped = childLines.join('\n').trim(); return new Element.text('pre', escaped); }