Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Parses text in markdown format. Use this entrypoint if you want to parse | 5 /// Parses text in markdown format. Use this entrypoint if you want to parse |
| 6 /// markdown from your own Dart code. To parse markdown by running the script | 6 /// markdown from your own Dart code. To parse markdown by running the script |
| 7 /// directly from the command line, see markdown.dart. | 7 /// directly from the command line, see markdown.dart. |
| 8 #library('markdown'); | 8 #library('markdown'); |
| 9 | 9 |
| 10 #source('ast.dart'); | 10 #source('ast.dart'); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 return renderToHtml(blocks); | 22 return renderToHtml(blocks); |
| 23 } | 23 } |
| 24 | 24 |
| 25 /// Replaces `<`, `&`, and `>`, with their HTML entity equivalents. | 25 /// Replaces `<`, `&`, and `>`, with their HTML entity equivalents. |
| 26 String escapeHtml(String html) { | 26 String escapeHtml(String html) { |
| 27 return html.replaceAll('&', '&') | 27 return html.replaceAll('&', '&') |
| 28 .replaceAll('<', '<') | 28 .replaceAll('<', '<') |
| 29 .replaceAll('>', '>'); | 29 .replaceAll('>', '>'); |
| 30 } | 30 } |
| 31 | 31 |
| 32 var _implicitLinkResolver; | |
| 33 | |
| 34 Node setImplicitLinkResolver(Node resolver(String text)) { | |
|
Jennifer Messerly
2011/11/28 23:08:13
what's the deal here? Is this a frog bug? (maybe a
Bob Nystrom
2011/11/29 02:44:08
As in why didn't I just make it a field? Hmm. I di
| |
| 35 _implicitLinkResolver = resolver; | |
| 36 } | |
| 37 | |
| 32 /// Maintains the context needed to parse a markdown document. | 38 /// Maintains the context needed to parse a markdown document. |
| 33 class Document { | 39 class Document { |
| 34 final Map<String, Link> refLinks; | 40 final Map<String, Link> refLinks; |
| 35 | 41 |
| 36 Document() | 42 Document() |
| 37 : refLinks = <String, Link>{}; | 43 : refLinks = <String, Link>{}; |
| 38 | 44 |
| 39 parseRefLinks(List<String> lines) { | 45 parseRefLinks(List<String> lines) { |
| 40 /// This is a hideous regex. It matches: | 46 // This is a hideous regex. It matches: |
| 41 /// [id]: http:foo.com "some title" | 47 // [id]: http:foo.com "some title" |
| 42 /// Where there may whitespace in there, and where the title may be in | 48 // Where there may whitespace in there, and where the title may be in |
| 43 /// single quotes, double quotes, or parentheses. | 49 // single quotes, double quotes, or parentheses. |
| 44 final indent = @'^[ ]{0,3}'; // Leading indentation. | 50 final indent = @'^[ ]{0,3}'; // Leading indentation. |
| 45 final id = @'\[([^\]]+)\]'; // Reference id in [brackets]. | 51 final id = @'\[([^\]]+)\]'; // Reference id in [brackets]. |
| 46 final quote = @'"[^"]+"'; // Title in "double quotes". | 52 final quote = @'"[^"]+"'; // Title in "double quotes". |
| 47 final apos = @"'[^']+'"; // Title in 'single quotes'. | 53 final apos = @"'[^']+'"; // Title in 'single quotes'. |
| 48 final paren = @"\([^)]+\)"; // Title in (parentheses). | 54 final paren = @"\([^)]+\)"; // Title in (parentheses). |
| 49 final pattern = new RegExp( | 55 final pattern = new RegExp( |
| 50 '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$'); | 56 '$indent$id:\\s+(\\S+)\\s*($quote|$apos|$paren|)\\s*\$'); |
| 51 | 57 |
| 52 for (int i = 0; i < lines.length; i++) { | 58 for (int i = 0; i < lines.length; i++) { |
| 53 final match = pattern.firstMatch(lines[i]); | 59 final match = pattern.firstMatch(lines[i]); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 } | 95 } |
| 90 } | 96 } |
| 91 | 97 |
| 92 return blocks; | 98 return blocks; |
| 93 } | 99 } |
| 94 | 100 |
| 95 /// Takes a string of raw text and processes all inline markdown tags, | 101 /// Takes a string of raw text and processes all inline markdown tags, |
| 96 /// returning a list of AST nodes. For example, given ``"*this **is** a* | 102 /// returning a list of AST nodes. For example, given ``"*this **is** a* |
| 97 /// `markdown`"``, returns: | 103 /// `markdown`"``, returns: |
| 98 /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. | 104 /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. |
| 99 List<Node> parseInline(String text) { | 105 List<Node> parseInline(String text) => new InlineParser(text, this).parse(); |
| 100 return new InlineParser(text, this).parse(); | |
| 101 } | |
| 102 } | 106 } |
| 103 | 107 |
| 104 class Link { | 108 class Link { |
| 105 final String id; | 109 final String id; |
| 106 final String url; | 110 final String url; |
| 107 final String title; | 111 final String title; |
| 108 Link(this.id, this.url, this.title); | 112 Link(this.id, this.url, this.title); |
| 109 } | 113 } |
| OLD | NEW |