| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 library markdown.html_renderer; | 5 library markdown.src.html_renderer; |
| 6 | 6 |
| 7 import 'ast.dart'; | 7 import 'ast.dart'; |
| 8 import 'document.dart'; | 8 import 'document.dart'; |
| 9 import 'inline_parser.dart'; | 9 import 'inline_parser.dart'; |
| 10 | 10 |
| 11 /// Converts the given string of markdown to HTML. | 11 /// Converts the given string of markdown to HTML. |
| 12 String markdownToHtml(String markdown, {List<InlineSyntax> inlineSyntaxes, | 12 String markdownToHtml(String markdown, |
| 13 Resolver linkResolver, Resolver imageLinkResolver, | 13 {List<InlineSyntax> inlineSyntaxes, |
| 14 Resolver linkResolver, |
| 15 Resolver imageLinkResolver, |
| 14 bool inlineOnly: false}) { | 16 bool inlineOnly: false}) { |
| 15 var document = new Document( | 17 var document = new Document( |
| 16 inlineSyntaxes: inlineSyntaxes, | 18 inlineSyntaxes: inlineSyntaxes, |
| 17 imageLinkResolver: imageLinkResolver, | 19 imageLinkResolver: imageLinkResolver, |
| 18 linkResolver: linkResolver); | 20 linkResolver: linkResolver); |
| 19 | 21 |
| 20 if (inlineOnly) { | 22 if (inlineOnly) return renderToHtml(document.parseInline(markdown)); |
| 21 return renderToHtml(document.parseInline(markdown)); | 23 |
| 22 } else { | 24 // Replace windows line endings with unix line endings, and split. |
| 23 // Replace windows line endings with unix line endings, and split. | 25 var lines = markdown.replaceAll('\r\n', '\n').split('\n'); |
| 24 var lines = markdown.replaceAll('\r\n', '\n').split('\n'); | 26 document.parseRefLinks(lines); |
| 25 document.parseRefLinks(lines); | 27 |
| 26 var blocks = document.parseLines(lines); | 28 return renderToHtml(document.parseLines(lines)); |
| 27 return renderToHtml(blocks); | |
| 28 } | |
| 29 } | 29 } |
| 30 | 30 |
| 31 String renderToHtml(List<Node> nodes) => new HtmlRenderer().render(nodes); | 31 String renderToHtml(List<Node> nodes) => new HtmlRenderer().render(nodes); |
| 32 | 32 |
| 33 /// Translates a parsed AST to HTML. | 33 /// Translates a parsed AST to HTML. |
| 34 class HtmlRenderer implements NodeVisitor { | 34 class HtmlRenderer implements NodeVisitor { |
| 35 static final _BLOCK_TAGS = new RegExp( | 35 static final _blockTags = new RegExp('blockquote|h1|h2|h3|h4|h5|h6|hr|p|pre'); |
| 36 'blockquote|h1|h2|h3|h4|h5|h6|hr|p|pre'); | |
| 37 | 36 |
| 38 StringBuffer buffer; | 37 StringBuffer buffer; |
| 39 | 38 |
| 40 HtmlRenderer(); | 39 HtmlRenderer(); |
| 41 | 40 |
| 42 String render(List<Node> nodes) { | 41 String render(List<Node> nodes) { |
| 43 buffer = new StringBuffer(); | 42 buffer = new StringBuffer(); |
| 44 | 43 |
| 45 for (final node in nodes) node.accept(this); | 44 for (final node in nodes) node.accept(this); |
| 46 | 45 |
| 47 return buffer.toString(); | 46 return buffer.toString(); |
| 48 } | 47 } |
| 49 | 48 |
| 50 void visitText(Text text) { | 49 void visitText(Text text) { |
| 51 buffer.write(text.text); | 50 buffer.write(text.text); |
| 52 } | 51 } |
| 53 | 52 |
| 54 bool visitElementBefore(Element element) { | 53 bool visitElementBefore(Element element) { |
| 55 // Hackish. Separate block-level elements with newlines. | 54 // Hackish. Separate block-level elements with newlines. |
| 56 if (!buffer.isEmpty && _BLOCK_TAGS.firstMatch(element.tag) != null) { | 55 if (!buffer.isEmpty && _blockTags.firstMatch(element.tag) != null) { |
| 57 buffer.write('\n'); | 56 buffer.write('\n'); |
| 58 } | 57 } |
| 59 | 58 |
| 60 buffer.write('<${element.tag}'); | 59 buffer.write('<${element.tag}'); |
| 61 | 60 |
| 62 // Sort the keys so that we generate stable output. | 61 // Sort the keys so that we generate stable output. |
| 63 final attributeNames = element.attributes.keys.toList(); | 62 var attributeNames = element.attributes.keys.toList(); |
| 64 attributeNames.sort((a, b) => a.compareTo(b)); | 63 attributeNames.sort((a, b) => a.compareTo(b)); |
| 65 for (final name in attributeNames) { | 64 |
| 65 for (var name in attributeNames) { |
| 66 buffer.write(' $name="${element.attributes[name]}"'); | 66 buffer.write(' $name="${element.attributes[name]}"'); |
| 67 } | 67 } |
| 68 | 68 |
| 69 if (element.isEmpty) { | 69 if (element.isEmpty) { |
| 70 // Empty element like <hr/>. | 70 // Empty element like <hr/>. |
| 71 buffer.write(' />'); | 71 buffer.write(' />'); |
| 72 return false; | 72 return false; |
| 73 } else { | 73 } else { |
| 74 buffer.write('>'); | 74 buffer.write('>'); |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 void visitElementAfter(Element element) { | 79 void visitElementAfter(Element element) { |
| 80 buffer.write('</${element.tag}>'); | 80 buffer.write('</${element.tag}>'); |
| 81 } | 81 } |
| 82 } | 82 } |
| OLD | NEW |