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