| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /// Parses text in a markdown-like format and renders to HTML. | |
| 6 library markdown; | |
| 7 | |
| 8 // TODO(rnystrom): Use "package:" URL (#4968). | |
| 9 part 'src/markdown/ast.dart'; | |
| 10 part 'src/markdown/block_parser.dart'; | |
| 11 part 'src/markdown/html_renderer.dart'; | |
| 12 part 'src/markdown/inline_parser.dart'; | |
| 13 | |
| 14 typedef Node Resolver(String name); | |
| 15 | |
| 16 /// Converts the given string of markdown to HTML. | |
| 17 String markdownToHtml(String markdown, {inlineSyntaxes, linkResolver}) { | |
| 18 final document = new Document(inlineSyntaxes: inlineSyntaxes, | |
| 19 linkResolver: linkResolver); | |
| 20 | |
| 21 // Replace windows line endings with unix line endings, and split. | |
| 22 final lines = markdown.replaceAll('\r\n','\n').split('\n'); | |
| 23 document.parseRefLinks(lines); | |
| 24 final blocks = document.parseLines(lines); | |
| 25 return renderToHtml(blocks); | |
| 26 } | |
| 27 | |
| 28 /// Replaces `<`, `&`, and `>`, with their HTML entity equivalents. | |
| 29 String escapeHtml(String html) { | |
| 30 return html.replaceAll('&', '&') | |
| 31 .replaceAll('<', '<') | |
| 32 .replaceAll('>', '>'); | |
| 33 } | |
| 34 | |
| 35 /// Maintains the context needed to parse a markdown document. | |
| 36 class Document { | |
| 37 final Map<String, Link> refLinks; | |
| 38 List<InlineSyntax> inlineSyntaxes; | |
| 39 Resolver linkResolver; | |
| 40 | |
| 41 Document({this.inlineSyntaxes, this.linkResolver}) | |
| 42 : refLinks = <String, Link>{}; | |
| 43 | |
| 44 parseRefLinks(List<String> lines) { | |
| 45 // This is a hideous regex. It matches: | |
| 46 // [id]: http:foo.com "some title" | |
| 47 // Where there may whitespace in there, and where the title may be in | |
| 48 // single quotes, double quotes, or parentheses. | |
| 49 final indent = r'^[ ]{0,3}'; // Leading indentation. | |
| 50 final id = r'\[([^\]]+)\]'; // Reference id in [brackets]. | |
| 51 final quote = r'"[^"]+"'; // Title in "double quotes". | |
| 52 final apos = r"'[^']+'"; // Title in 'single quotes'. | |
| 53 final paren = r"\([^)]+\)"; // Title in (parentheses). | |
| 54 final pattern = new RegExp( | |
| 55 '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$'); | |
| 56 | |
| 57 for (int i = 0; i < lines.length; i++) { | |
| 58 final match = pattern.firstMatch(lines[i]); | |
| 59 if (match != null) { | |
| 60 // Parse the link. | |
| 61 var id = match[1]; | |
| 62 var url = match[2]; | |
| 63 var title = match[3]; | |
| 64 | |
| 65 if (title == '') { | |
| 66 // No title. | |
| 67 title = null; | |
| 68 } else { | |
| 69 // Remove "", '', or (). | |
| 70 title = title.substring(1, title.length - 1); | |
| 71 } | |
| 72 | |
| 73 // References are case-insensitive. | |
| 74 id = id.toLowerCase(); | |
| 75 | |
| 76 refLinks[id] = new Link(id, url, title); | |
| 77 | |
| 78 // Remove it from the output. We replace it with a blank line which will | |
| 79 // get consumed by later processing. | |
| 80 lines[i] = ''; | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 /// Parse the given [lines] of markdown to a series of AST nodes. | |
| 86 List<Node> parseLines(List<String> lines) { | |
| 87 final parser = new BlockParser(lines, this); | |
| 88 | |
| 89 final blocks = []; | |
| 90 while (!parser.isDone) { | |
| 91 for (final syntax in BlockSyntax.syntaxes) { | |
| 92 if (syntax.canParse(parser)) { | |
| 93 final block = syntax.parse(parser); | |
| 94 if (block != null) blocks.add(block); | |
| 95 break; | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 return blocks; | |
| 101 } | |
| 102 | |
| 103 /// Takes a string of raw text and processes all inline markdown tags, | |
| 104 /// returning a list of AST nodes. For example, given ``"*this **is** a* | |
| 105 /// `markdown`"``, returns: | |
| 106 /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. | |
| 107 List<Node> parseInline(String text) => new InlineParser(text, this).parse(); | |
| 108 } | |
| 109 | |
| 110 class Link { | |
| 111 final String id; | |
| 112 final String url; | |
| 113 final String title; | |
| 114 Link(this.id, this.url, this.title); | |
| 115 } | |
| OLD | NEW |