Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: utils/markdown/html_renderer.dart

Issue 8680025: First pass at a markdown parser in Dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Review. Add missing file (oops!). Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « utils/markdown/block_parser.dart ('k') | utils/markdown/inline_parser.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: utils/markdown/html_renderer.dart
diff --git a/utils/markdown/html_renderer.dart b/utils/markdown/html_renderer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ec4bebb1d00316bb270b484f635f59d5fdfe4490
--- /dev/null
+++ b/utils/markdown/html_renderer.dart
@@ -0,0 +1,54 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+String renderToHtml(List<Node> nodes) => new HtmlRenderer().render(nodes);
+
+/// Translates a parsed AST to HTML.
+class HtmlRenderer implements NodeVisitor {
+ static final _BLOCK_TAGS = const RegExp(
+ 'blockquote|h1|h2|h3|h4|h5|h6|hr|p|pre');
+
+ StringBuffer buffer;
+
+ HtmlRenderer();
+
+ String render(List<Node> nodes) {
+ buffer = new StringBuffer();
+
+ for (final node in nodes) node.accept(this);
+
+ return buffer.toString();
+ }
+
+ void visitText(Text text) {
+ buffer.add(text.text);
+ }
+
+ bool visitElementBefore(Element element) {
+ // Hackish. Separate block-level elements with newlines.
+ if (!buffer.isEmpty() &&
+ _BLOCK_TAGS.firstMatch(element.tag) != null) {
+ buffer.add('\n');
+ }
+
+ buffer.add('<${element.tag}');
+
+ for (final name in element.attributes.getKeys()) {
+ buffer.add(' $name="${element.attributes[name]}"');
+ }
+
+ if (element.isEmpty) {
+ // Empty element like <hr/>.
+ buffer.add(' />');
+ return false;
+ } else {
+ buffer.add('>');
+ return true;
+ }
+ }
+
+ void visitElementAfter(Element element) {
+ buffer.add('</${element.tag}>');
+ }
+}
« no previous file with comments | « utils/markdown/block_parser.dart ('k') | utils/markdown/inline_parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698