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.ast; | 5 library markdown.src.ast; |
6 | 6 |
7 typedef Node Resolver(String name); | 7 typedef Node Resolver(String name); |
8 | 8 |
9 /// Base class for any AST item. Roughly corresponds to Node in the DOM. Will | 9 /// Base class for any AST item. Roughly corresponds to Node in the DOM. Will |
10 /// be either an Element or Text. | 10 /// be either an Element or Text. |
11 abstract class Node { | 11 abstract class Node { |
12 void accept(NodeVisitor visitor); | 12 void accept(NodeVisitor visitor); |
13 } | 13 } |
14 | 14 |
15 /// A named tag that can contain other nodes. | 15 /// A named tag that can contain other nodes. |
16 class Element implements Node { | 16 class Element implements Node { |
17 final String tag; | 17 final String tag; |
18 final List<Node> children; | 18 final List<Node> children; |
19 final Map<String, String> attributes; | 19 final Map<String, String> attributes; |
20 | 20 |
21 Element(this.tag, this.children) : attributes = <String, String>{}; | 21 Element(this.tag, this.children) : attributes = <String, String>{}; |
22 | 22 |
23 Element.empty(this.tag) | 23 Element.empty(this.tag) |
24 : children = null, | 24 : children = null, |
25 attributes = <String, String>{}; | 25 attributes = {}; |
26 | 26 |
27 Element.withTag(this.tag) | 27 Element.withTag(this.tag) |
28 : children = [], | 28 : children = [], |
29 attributes = <String, String>{}; | 29 attributes = {}; |
30 | 30 |
31 Element.text(this.tag, String text) | 31 Element.text(this.tag, String text) |
32 : children = [new Text(text)], | 32 : children = [new Text(text)], |
33 attributes = <String, String>{}; | 33 attributes = {}; |
34 | 34 |
35 bool get isEmpty => children == null; | 35 bool get isEmpty => children == null; |
36 | 36 |
37 void accept(NodeVisitor visitor) { | 37 void accept(NodeVisitor visitor) { |
38 if (visitor.visitElementBefore(this)) { | 38 if (visitor.visitElementBefore(this)) { |
39 for (final child in children) child.accept(visitor); | 39 for (var child in children) child.accept(visitor); |
40 visitor.visitElementAfter(this); | 40 visitor.visitElementAfter(this); |
41 } | 41 } |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 /// A plain text element. | 45 /// A plain text element. |
46 class Text implements Node { | 46 class Text implements Node { |
47 final String text; | 47 final String text; |
48 Text(this.text); | 48 Text(this.text); |
49 | 49 |
50 void accept(NodeVisitor visitor) => visitor.visitText(this); | 50 void accept(NodeVisitor visitor) => visitor.visitText(this); |
51 } | 51 } |
52 | 52 |
53 /// Visitor pattern for the AST. Renderers or other AST transformers should | 53 /// Visitor pattern for the AST. Renderers or other AST transformers should |
54 /// implement this. | 54 /// implement this. |
55 abstract class NodeVisitor { | 55 abstract class NodeVisitor { |
56 /// Called when a Text node has been reached. | 56 /// Called when a Text node has been reached. |
57 void visitText(Text text); | 57 void visitText(Text text); |
58 | 58 |
59 /// Called when an Element has been reached, before its children have been | 59 /// Called when an Element has been reached, before its children have been |
60 /// visited. Return `false` to skip its children. | 60 /// visited. Return `false` to skip its children. |
61 bool visitElementBefore(Element element); | 61 bool visitElementBefore(Element element); |
62 | 62 |
63 /// Called when an Element has been reached, after its children have been | 63 /// Called when an Element has been reached, after its children have been |
64 /// visited. Will not be called if [visitElementBefore] returns `false`. | 64 /// visited. Will not be called if [visitElementBefore] returns `false`. |
65 void visitElementAfter(Element element); | 65 void visitElementAfter(Element element); |
66 } | 66 } |
OLD | NEW |