-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtml_renderer.dart
59 lines (46 loc) · 1.56 KB
/
html_renderer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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}');
// Sort the keys so that we generate stable output.
// TODO(rnystrom): This assumes getKeys() returns a fresh mutable
// collection.
final attributeNames = element.attributes.getKeys();
attributeNames.sort((a, b) => a.compareTo(b));
for (final name in attributeNames) {
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}>');
}
}