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