| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 library markdown.document; |
| 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 | 2 |
| 5 /// Parses text in a markdown-like format and renders to HTML. | 3 import 'ast.dart'; |
| 6 library markdown; | 4 import 'block_parser.dart'; |
| 7 | 5 import 'inline_parser.dart'; |
| 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 | 6 |
| 35 /// Maintains the context needed to parse a markdown document. | 7 /// Maintains the context needed to parse a markdown document. |
| 36 class Document { | 8 class Document { |
| 37 final Map<String, Link> refLinks; | 9 final Map<String, Link> refLinks; |
| 38 List<InlineSyntax> inlineSyntaxes; | 10 List<InlineSyntax> inlineSyntaxes; |
| 39 Resolver linkResolver; | 11 Resolver linkResolver; |
| 40 | 12 |
| 41 Document({this.inlineSyntaxes, this.linkResolver}) | 13 Document({this.inlineSyntaxes, this.linkResolver}) |
| 42 : refLinks = <String, Link>{}; | 14 : refLinks = <String, Link>{}; |
| 43 | 15 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. | 78 /// `<em>this <strong>is</strong> a</em> <code>markdown</code>`. |
| 107 List<Node> parseInline(String text) => new InlineParser(text, this).parse(); | 79 List<Node> parseInline(String text) => new InlineParser(text, this).parse(); |
| 108 } | 80 } |
| 109 | 81 |
| 110 class Link { | 82 class Link { |
| 111 final String id; | 83 final String id; |
| 112 final String url; | 84 final String url; |
| 113 final String title; | 85 final String title; |
| 114 Link(this.id, this.url, this.title); | 86 Link(this.id, this.url, this.title); |
| 115 } | 87 } |
| OLD | NEW |