|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module SyntaxTree |
| 4 | + module XML |
| 5 | + class Format < Visitor |
| 6 | + attr_reader :q |
| 7 | + |
| 8 | + def initialize(q) |
| 9 | + @q = q |
| 10 | + end |
| 11 | + |
| 12 | + # Visit a Token node. |
| 13 | + def visit_token(node) |
| 14 | + q.text(node.value.strip) |
| 15 | + end |
| 16 | + |
| 17 | + # Visit a Document node. |
| 18 | + def visit_document(node) |
| 19 | + child_nodes = |
| 20 | + node |
| 21 | + .child_nodes |
| 22 | + .select do |child_node| |
| 23 | + case child_node |
| 24 | + in Misc[value: Token[type: :whitespace]] |
| 25 | + false |
| 26 | + else |
| 27 | + true |
| 28 | + end |
| 29 | + end |
| 30 | + .sort_by(&:location) |
| 31 | + |
| 32 | + q.seplist(child_nodes, -> { q.breakable(force: true) }) do |child_node| |
| 33 | + visit(child_node) |
| 34 | + end |
| 35 | + |
| 36 | + q.breakable(force: true) |
| 37 | + end |
| 38 | + |
| 39 | + # Visit a Prolog node. |
| 40 | + def visit_prolog(node) |
| 41 | + q.group do |
| 42 | + visit(node.opening) |
| 43 | + |
| 44 | + if node.attributes.any? |
| 45 | + q.indent do |
| 46 | + q.breakable("") |
| 47 | + q.seplist(node.attributes, -> { q.breakable }) do |child_node| |
| 48 | + visit(child_node) |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + q.breakable |
| 54 | + visit(node.closing) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # Visit a Doctype node. |
| 59 | + def visit_doctype(node) |
| 60 | + q.group do |
| 61 | + visit(node.opening) |
| 62 | + q.text(" ") |
| 63 | + visit(node.name) |
| 64 | + |
| 65 | + if node.external_id |
| 66 | + q.text(" ") |
| 67 | + visit(node.external_id) |
| 68 | + end |
| 69 | + |
| 70 | + visit(node.closing) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + # Visit an ExternalID node. |
| 75 | + def visit_external_id(node) |
| 76 | + q.group do |
| 77 | + q.group do |
| 78 | + visit(node.type) |
| 79 | + |
| 80 | + if node.public_id |
| 81 | + q.indent do |
| 82 | + q.breakable |
| 83 | + visit(node.public_id) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + q.indent do |
| 89 | + q.breakable |
| 90 | + visit(node.system_id) |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # Visit an Element node. |
| 96 | + def visit_element(node) |
| 97 | + inner_nodes = |
| 98 | + node.content&.select do |child_node| |
| 99 | + case child_node |
| 100 | + in CharData[value: Token[type: :whitespace]] |
| 101 | + false |
| 102 | + in CharData[value: Token[value:]] if value.strip.empty? |
| 103 | + false |
| 104 | + else |
| 105 | + true |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + case inner_nodes |
| 110 | + in nil |
| 111 | + visit(node.opening_tag) |
| 112 | + in [] |
| 113 | + visit( |
| 114 | + Element::OpeningTag.new( |
| 115 | + opening: node.opening_tag.opening, |
| 116 | + name: node.opening_tag.name, |
| 117 | + attributes: node.opening_tag.attributes, |
| 118 | + closing: |
| 119 | + Token.new( |
| 120 | + type: :close, |
| 121 | + value: "/>", |
| 122 | + location: node.opening_tag.closing.location |
| 123 | + ), |
| 124 | + location: node.opening_tag.location |
| 125 | + ) |
| 126 | + ) |
| 127 | + in [CharData[value: Token[type: :text, value:]]] |
| 128 | + q.group do |
| 129 | + visit(node.opening_tag) |
| 130 | + q.indent do |
| 131 | + q.breakable("") |
| 132 | + format_text(q, value) |
| 133 | + end |
| 134 | + |
| 135 | + q.breakable("") |
| 136 | + visit(node.closing_tag) |
| 137 | + end |
| 138 | + else |
| 139 | + q.group do |
| 140 | + visit(node.opening_tag) |
| 141 | + q.indent do |
| 142 | + q.breakable("") |
| 143 | + |
| 144 | + inner_nodes.each_with_index do |child_node, index| |
| 145 | + if index != 0 |
| 146 | + q.breakable(force: true) |
| 147 | + |
| 148 | + end_line = inner_nodes[index - 1].location.end_line |
| 149 | + start_line = child_node.location.start_line |
| 150 | + q.breakable(force: true) if (start_line - end_line) >= 2 |
| 151 | + end |
| 152 | + |
| 153 | + case child_node |
| 154 | + in CharData[value: Token[type: :text, value:]] |
| 155 | + format_text(q, value) |
| 156 | + else |
| 157 | + visit(child_node) |
| 158 | + end |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + q.breakable(force: true) |
| 163 | + visit(node.closing_tag) |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + # Visit an Element::OpeningTag node. |
| 169 | + def visit_opening_tag(node) |
| 170 | + q.group do |
| 171 | + visit(node.opening) |
| 172 | + visit(node.name) |
| 173 | + |
| 174 | + if node.attributes.any? |
| 175 | + q.indent do |
| 176 | + q.breakable |
| 177 | + q.seplist(node.attributes, -> { q.breakable }) do |child_node| |
| 178 | + visit(child_node) |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + q.breakable(node.closing.value == "/>" ? " " : "") |
| 184 | + visit(node.closing) |
| 185 | + end |
| 186 | + end |
| 187 | + |
| 188 | + # Visit an Element::ClosingTag node. |
| 189 | + def visit_closing_tag(node) |
| 190 | + q.group do |
| 191 | + visit(node.opening) |
| 192 | + visit(node.name) |
| 193 | + visit(node.closing) |
| 194 | + end |
| 195 | + end |
| 196 | + |
| 197 | + # Visit a Reference node. |
| 198 | + def visit_reference(node) |
| 199 | + visit(node.value) |
| 200 | + end |
| 201 | + |
| 202 | + # Visit an Attribute node. |
| 203 | + def visit_attribute(node) |
| 204 | + q.group do |
| 205 | + visit(node.key) |
| 206 | + visit(node.equals) |
| 207 | + visit(node.value) |
| 208 | + end |
| 209 | + end |
| 210 | + |
| 211 | + # Visit a CharData node. |
| 212 | + def visit_char_data(node) |
| 213 | + lines = node.value.value.strip.split("\n") |
| 214 | + |
| 215 | + q.seplist(lines, -> { q.breakable(indent: false) }) do |line| |
| 216 | + q.text(line) |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + # Visit a Misc node. |
| 221 | + def visit_misc(node) |
| 222 | + visit(node.value) |
| 223 | + end |
| 224 | + |
| 225 | + private |
| 226 | + |
| 227 | + # Format a text by splitting nicely at newlines and spaces. |
| 228 | + def format_text(q, value) |
| 229 | + q.seplist( |
| 230 | + value.strip.split("\n"), |
| 231 | + -> { q.breakable(force: true, indent: false) } |
| 232 | + ) do |line| |
| 233 | + q.seplist( |
| 234 | + line.split(/\b(?: +)\b/), |
| 235 | + -> { q.group { q.breakable } } |
| 236 | + ) { |segment| q.text(segment) } |
| 237 | + end |
| 238 | + end |
| 239 | + end |
| 240 | + end |
| 241 | +end |
0 commit comments