aligning new library files
diff --git a/pkgs/markdown/lib/markdown.dart b/pkgs/markdown/lib/markdown.dart index e2d6a88..e85109f 100644 --- a/pkgs/markdown/lib/markdown.dart +++ b/pkgs/markdown/lib/markdown.dart
@@ -5,118 +5,8 @@ /// Parses text in a markdown-like format and renders to HTML. library markdown; -import 'src/markdown/ast.dart'; -import 'src/markdown/block_parser.dart'; -import 'src/markdown/html_renderer.dart'; -import 'src/markdown/inline_parser.dart'; - export 'src/markdown/ast.dart'; +export 'src/markdown/block_parser.dart'; +export 'src/markdown/document.dart'; +export 'src/markdown/html_renderer.dart'; export 'src/markdown/inline_parser.dart'; - -typedef Node Resolver(String name); - -/// Converts the given string of markdown to HTML. -String markdownToHtml(String markdown, {inlineSyntaxes, linkResolver, - bool inlineOnly: false}) { - final document = new Document(inlineSyntaxes: inlineSyntaxes, - linkResolver: linkResolver); - - if (inlineOnly) { - return renderToHtml(document.parseInline(markdown)); - } else { - // Replace windows line endings with unix line endings, and split. - final lines = markdown.replaceAll('\r\n','\n').split('\n'); - document.parseRefLinks(lines); - final blocks = document.parseLines(lines); - return renderToHtml(blocks); - } -} - -/// Replaces `<`, `&`, and `>`, with their HTML entity equivalents. -String escapeHtml(String html) { - return html.replaceAll('&', '&') - .replaceAll('<', '<') - .replaceAll('>', '>'); -} - -/// Maintains the context needed to parse a markdown document. -class Document { - final Map<String, Link> refLinks; - List<InlineSyntax> inlineSyntaxes; - Resolver linkResolver; - - Document({this.inlineSyntaxes, this.linkResolver}) - : refLinks = <String, Link>{}; - - parseRefLinks(List<String> lines) { - // This is a hideous regex. It matches: - // [id]: http:foo.com "some title" - // Where there may whitespace in there, and where the title may be in - // single quotes, double quotes, or parentheses. - final indent = r'^[ ]{0,3}'; // Leading indentation. - final id = r'\[([^\]]+)\]'; // Reference id in [brackets]. - final quote = r'"[^"]+"'; // Title in "double quotes". - final apos = r"'[^']+'"; // Title in 'single quotes'. - final paren = r"\([^)]+\)"; // Title in (parentheses). - final pattern = new RegExp( - '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$'); - - for (int i = 0; i < lines.length; i++) { - final match = pattern.firstMatch(lines[i]); - if (match != null) { - // Parse the link. - var id = match[1]; - var url = match[2]; - var title = match[3]; - - if (title == '') { - // No title. - title = null; - } else { - // Remove "", '', or (). - title = title.substring(1, title.length - 1); - } - - // References are case-insensitive. - id = id.toLowerCase(); - - refLinks[id] = new Link(id, url, title); - - // Remove it from the output. We replace it with a blank line which will - // get consumed by later processing. - lines[i] = ''; - } - } - } - - /// Parse the given [lines] of markdown to a series of AST nodes. - List<Node> parseLines(List<String> lines) { - final parser = new BlockParser(lines, this); - - final blocks = []; - while (!parser.isDone) { - for (final syntax in BlockSyntax.syntaxes) { - if (syntax.canParse(parser)) { - final block = syntax.parse(parser); - if (block != null) blocks.add(block); - break; - } - } - } - - return blocks; - } - - /// Takes a string of raw text and processes all inline markdown tags, - /// returning a list of AST nodes. For example, given ``"*this **is** a* - /// `markdown`"``, returns: - /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. - List<Node> parseInline(String text) => new InlineParser(text, this).parse(); -} - -class Link { - final String id; - final String url; - final String title; - Link(this.id, this.url, this.title); -}
diff --git a/pkgs/markdown/lib/src/markdown/block_parser.dart b/pkgs/markdown/lib/src/markdown/block_parser.dart index 3c20ab8..c34f7db 100644 --- a/pkgs/markdown/lib/src/markdown/block_parser.dart +++ b/pkgs/markdown/lib/src/markdown/block_parser.dart
@@ -5,7 +5,8 @@ library markdown.block_parser; import 'ast.dart'; -import '../../markdown.dart'; +import 'document.dart'; +import 'util.dart'; /// The line contains only whitespace or is empty. final _RE_EMPTY = new RegExp(r'^([ \t]*)$');
diff --git a/pkgs/markdown/lib/src/markdown/document.dart b/pkgs/markdown/lib/src/markdown/document.dart new file mode 100644 index 0000000..b1c1db3 --- /dev/null +++ b/pkgs/markdown/lib/src/markdown/document.dart
@@ -0,0 +1,88 @@ +library markdown.document; + +import 'ast.dart'; +import 'block_parser.dart'; +import 'inline_parser.dart'; +import 'util.dart'; + +/// Maintains the context needed to parse a markdown document. +class Document { + final Map<String, Link> refLinks; + List<InlineSyntax> inlineSyntaxes; + Resolver linkResolver; + + Document({this.inlineSyntaxes, this.linkResolver}) + : refLinks = <String, Link>{}; + + parseRefLinks(List<String> lines) { + // This is a hideous regex. It matches: + // [id]: http:foo.com "some title" + // Where there may whitespace in there, and where the title may be in + // single quotes, double quotes, or parentheses. + final indent = r'^[ ]{0,3}'; // Leading indentation. + final id = r'\[([^\]]+)\]'; // Reference id in [brackets]. + final quote = r'"[^"]+"'; // Title in "double quotes". + final apos = r"'[^']+'"; // Title in 'single quotes'. + final paren = r"\([^)]+\)"; // Title in (parentheses). + final pattern = new RegExp( + '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$'); + + for (int i = 0; i < lines.length; i++) { + final match = pattern.firstMatch(lines[i]); + if (match != null) { + // Parse the link. + var id = match[1]; + var url = match[2]; + var title = match[3]; + + if (title == '') { + // No title. + title = null; + } else { + // Remove "", '', or (). + title = title.substring(1, title.length - 1); + } + + // References are case-insensitive. + id = id.toLowerCase(); + + refLinks[id] = new Link(id, url, title); + + // Remove it from the output. We replace it with a blank line which will + // get consumed by later processing. + lines[i] = ''; + } + } + } + + /// Parse the given [lines] of markdown to a series of AST nodes. + List<Node> parseLines(List<String> lines) { + final parser = new BlockParser(lines, this); + + final blocks = []; + while (!parser.isDone) { + for (final syntax in BlockSyntax.syntaxes) { + if (syntax.canParse(parser)) { + final block = syntax.parse(parser); + if (block != null) blocks.add(block); + break; + } + } + } + + return blocks; + } + + /// Takes a string of raw text and processes all inline markdown tags, + /// returning a list of AST nodes. For example, given ``"*this **is** a* + /// `markdown`"``, returns: + /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. + List<Node> parseInline(String text) => new InlineParser(text, this).parse(); +} + +class Link { + final String id; + final String url; + final String title; + Link(this.id, this.url, this.title); +}
diff --git a/pkgs/markdown/lib/src/markdown/html_renderer.dart b/pkgs/markdown/lib/src/markdown/html_renderer.dart index 3d79b7a..97b900b 100644 --- a/pkgs/markdown/lib/src/markdown/html_renderer.dart +++ b/pkgs/markdown/lib/src/markdown/html_renderer.dart
@@ -5,6 +5,24 @@ library markdown.html_renderer; import 'ast.dart'; +import 'document.dart'; + +/// Converts the given string of markdown to HTML. +String markdownToHtml(String markdown, {inlineSyntaxes, linkResolver, + bool inlineOnly: false}) { + var document = new Document(inlineSyntaxes: inlineSyntaxes, + linkResolver: linkResolver); + + if (inlineOnly) { + return renderToHtml(document.parseInline(markdown)); + } else { + // Replace windows line endings with unix line endings, and split. + var lines = markdown.replaceAll('\r\n','\n').split('\n'); + document.parseRefLinks(lines); + var blocks = document.parseLines(lines); + return renderToHtml(blocks); + } +} String renderToHtml(List<Node> nodes) => new HtmlRenderer().render(nodes);
diff --git a/pkgs/markdown/lib/src/markdown/inline_parser.dart b/pkgs/markdown/lib/src/markdown/inline_parser.dart index f74526f..392ca69 100644 --- a/pkgs/markdown/lib/src/markdown/inline_parser.dart +++ b/pkgs/markdown/lib/src/markdown/inline_parser.dart
@@ -5,7 +5,8 @@ library markdown.inline_parser; import 'ast.dart'; -import '../../markdown.dart'; +import 'document.dart'; +import 'util.dart'; /// Maintains the internal state needed to parse inline span elements in /// markdown.
diff --git a/pkgs/markdown/lib/src/markdown/util.dart b/pkgs/markdown/lib/src/markdown/util.dart new file mode 100644 index 0000000..2d577a6 --- /dev/null +++ b/pkgs/markdown/lib/src/markdown/util.dart
@@ -0,0 +1,12 @@ +library markdown.util; + +import 'ast.dart'; + +/// Replaces `<`, `&`, and `>`, with their HTML entity equivalents. +String escapeHtml(String html) { + return html.replaceAll('&', '&') + .replaceAll('<', '<') + .replaceAll('>', '>'); +} + +typedef Node Resolver(String name);