Class: SyntaxTree::XML::Format
- Defined in:
- lib/syntax_tree/xml/format.rb
Instance Attribute Summary collapse
-
#q ⇒ Object
readonly
Returns the value of attribute q.
Instance Method Summary collapse
-
#initialize(q) ⇒ Format
constructor
A new instance of Format.
-
#visit_attribute(node) ⇒ Object
Visit an Attribute node.
-
#visit_char_data(node) ⇒ Object
Visit a CharData node.
-
#visit_closing_tag(node) ⇒ Object
Visit an Element::ClosingTag node.
-
#visit_doctype(node) ⇒ Object
Visit a Doctype node.
-
#visit_document(node) ⇒ Object
Visit a Document node.
-
#visit_element(node) ⇒ Object
Visit an Element node.
-
#visit_external_id(node) ⇒ Object
Visit an ExternalID node.
-
#visit_misc(node) ⇒ Object
Visit a Misc node.
-
#visit_opening_tag(node) ⇒ Object
Visit an Element::OpeningTag node.
-
#visit_prolog(node) ⇒ Object
Visit a Prolog node.
-
#visit_reference(node) ⇒ Object
Visit a Reference node.
-
#visit_token(node) ⇒ Object
Visit a Token node.
Methods inherited from Visitor
Constructor Details
#initialize(q) ⇒ Format
Returns a new instance of Format.
8 9 10 |
# File 'lib/syntax_tree/xml/format.rb', line 8 def initialize(q) @q = q end |
Instance Attribute Details
#q ⇒ Object (readonly)
Returns the value of attribute q.
6 7 8 |
# File 'lib/syntax_tree/xml/format.rb', line 6 def q @q end |
Instance Method Details
#visit_attribute(node) ⇒ Object
Visit an Attribute node.
203 204 205 206 207 208 209 |
# File 'lib/syntax_tree/xml/format.rb', line 203 def visit_attribute(node) q.group do visit(node.key) visit(node.equals) visit(node.value) end end |
#visit_char_data(node) ⇒ Object
Visit a CharData node.
212 213 214 215 216 217 218 |
# File 'lib/syntax_tree/xml/format.rb', line 212 def visit_char_data(node) lines = node.value.value.strip.split("\n") q.seplist(lines, -> { q.breakable(indent: false) }) do |line| q.text(line) end end |
#visit_closing_tag(node) ⇒ Object
Visit an Element::ClosingTag node.
189 190 191 192 193 194 195 |
# File 'lib/syntax_tree/xml/format.rb', line 189 def visit_closing_tag(node) q.group do visit(node.opening) visit(node.name) visit(node.closing) end end |
#visit_doctype(node) ⇒ Object
Visit a Doctype node.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/syntax_tree/xml/format.rb', line 59 def visit_doctype(node) q.group do visit(node.opening) q.text(" ") visit(node.name) if node.external_id q.text(" ") visit(node.external_id) end visit(node.closing) end end |
#visit_document(node) ⇒ Object
Visit a Document node.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/syntax_tree/xml/format.rb', line 18 def visit_document(node) child_nodes = node .child_nodes .select do |child_node| case child_node in Misc[value: Token[type: :whitespace]] false else true end end .sort_by(&:location) q.seplist(child_nodes, -> { q.breakable(force: true) }) do |child_node| visit(child_node) end q.breakable(force: true) end |
#visit_element(node) ⇒ Object
Visit an Element node.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/syntax_tree/xml/format.rb', line 96 def visit_element(node) inner_nodes = node.content&.select do |child_node| case child_node in CharData[value: Token[type: :whitespace]] false in CharData[value: Token[value:]] if value.strip.empty? false else true end end case inner_nodes in nil visit(node.opening_tag) in [] visit( Element::OpeningTag.new( opening: node.opening_tag.opening, name: node.opening_tag.name, attributes: node.opening_tag.attributes, closing: Token.new( type: :close, value: "/>", location: node.opening_tag.closing.location ), location: node.opening_tag.location ) ) in [CharData[value: Token[type: :text, value:]]] q.group do visit(node.opening_tag) q.indent do q.breakable("") format_text(q, value) end q.breakable("") visit(node.closing_tag) end else q.group do visit(node.opening_tag) q.indent do q.breakable("") inner_nodes.each_with_index do |child_node, index| if index != 0 q.breakable(force: true) end_line = inner_nodes[index - 1].location.end_line start_line = child_node.location.start_line q.breakable(force: true) if (start_line - end_line) >= 2 end case child_node in CharData[value: Token[type: :text, value:]] format_text(q, value) else visit(child_node) end end end q.breakable(force: true) visit(node.closing_tag) end end end |
#visit_external_id(node) ⇒ Object
Visit an ExternalID node.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/syntax_tree/xml/format.rb', line 75 def visit_external_id(node) q.group do q.group do visit(node.type) if node.public_id q.indent do q.breakable visit(node.public_id) end end end q.indent do q.breakable visit(node.system_id) end end end |
#visit_misc(node) ⇒ Object
Visit a Misc node.
221 222 223 |
# File 'lib/syntax_tree/xml/format.rb', line 221 def visit_misc(node) visit(node.value) end |
#visit_opening_tag(node) ⇒ Object
Visit an Element::OpeningTag node.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/syntax_tree/xml/format.rb', line 169 def visit_opening_tag(node) q.group do visit(node.opening) visit(node.name) if node.attributes.any? q.indent do q.breakable q.seplist(node.attributes, -> { q.breakable }) do |child_node| visit(child_node) end end end q.breakable(node.closing.value == "/>" ? " " : "") visit(node.closing) end end |
#visit_prolog(node) ⇒ Object
Visit a Prolog node.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/syntax_tree/xml/format.rb', line 40 def visit_prolog(node) q.group do visit(node.opening) if node.attributes.any? q.indent do q.breakable q.seplist(node.attributes, -> { q.breakable }) do |child_node| visit(child_node) end end end q.breakable("") visit(node.closing) end end |
#visit_reference(node) ⇒ Object
Visit a Reference node.
198 199 200 |
# File 'lib/syntax_tree/xml/format.rb', line 198 def visit_reference(node) visit(node.value) end |
#visit_token(node) ⇒ Object
Visit a Token node.
13 14 15 |
# File 'lib/syntax_tree/xml/format.rb', line 13 def visit_token(node) q.text(node.value.strip) end |