Class: SyntaxTree::Haml::Format
Defined Under Namespace
Classes: Formatter, HTMLAttributesPart, HashAttributesPart, LiteralHashValue, PartList, PlainPart, PrefixPart
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Visitor
#visit
Constructor Details
#initialize(q) ⇒ Format
Returns a new instance of Format.
28
29
30
|
# File 'lib/syntax_tree/haml/format.rb', line 28
def initialize(q)
@q = q
end
|
Instance Attribute Details
#q ⇒ Object
Returns the value of attribute q.
26
27
28
|
# File 'lib/syntax_tree/haml/format.rb', line 26
def q
@q
end
|
Instance Method Details
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/syntax_tree/haml/format.rb', line 33
def (node)
with_children(node) do
q.text("/")
q.text("!") if node.value[:revealed]
if node.value[:conditional]
q.text(node.value[:conditional])
elsif node.value[:text]
q.text(" #{node.value[:text]}")
end
end
end
|
#visit_doctype(node) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/syntax_tree/haml/format.rb', line 47
def visit_doctype(node)
parts = ["!!!"]
parts << if DOCTYPE_TYPES.key?(node.value[:type])
DOCTYPE_TYPES[node.value[:type]]
elsif DOCTYPE_VERSIONS.include?(node.value[:version])
node.value[:version]
else
node.value[:type]
end
parts << node.value[:encoding] if node.value[:encoding]
q.text(parts.join(" "))
end
|
#visit_filter(node) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/syntax_tree/haml/format.rb', line 63
def visit_filter(node)
q.group do
q.text(":")
q.text(node.value[:name])
q.indent do
q.breakable_force
first = true
node.value[:text].each_line(chomp: true) do |line|
if first
first = false
else
q.breakable_force
end
q.text(line)
end
end
end
end
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/syntax_tree/haml/format.rb', line 86
def (node)
q.text("-#")
text = node.value[:text].strip
if text.include?("\n")
q.indent do
q.breakable_force
first = true
text.each_line(chomp: true) do |line|
if first
first = false
else
q.breakable_force
end
q.text(line)
end
end
else
q.text(" #{text}")
end
end
|
#visit_plain(node) ⇒ Object
111
112
113
114
115
116
117
118
119
|
# File 'lib/syntax_tree/haml/format.rb', line 111
def visit_plain(node)
if line = q.literal_lines[node.line]
q.text(line)
else
text = node.value[:text]
q.text("\\") if escaped?(text)
q.text(text)
end
end
|
#visit_root(node) ⇒ Object
Visit the root node of the AST.
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/syntax_tree/haml/format.rb', line 122
def visit_root(node)
previous_line = nil
node.children.each do |child|
q.breakable_force if previous_line && (child.line - previous_line) > 1
previous_line =
child.children.any? ? child.children.last.line : child.line
visit(child)
q.breakable_force
end
end
|
#visit_script(node) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/syntax_tree/haml/format.rb', line 136
def visit_script(node)
with_children(node) do
if line = q.literal_lines[node.line]
q.text(line)
else
q.text("&") if node.value[:escape_html]
q.text(node.value[:preserve] ? "~" : "=")
q.text(" ")
q.text(node.value[:text].strip)
end
end
end
|
#visit_silent_script(node) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/syntax_tree/haml/format.rb', line 150
def visit_silent_script(node)
q.group do
q.text("- ")
q.text(node.value[:text].strip)
node.children.each do |child|
if continuation?(node, child)
q.breakable_force
visit(child)
else
q.indent do
q.breakable_force
visit(child)
end
end
end
end
end
|
#visit_tag(node) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
# File 'lib/syntax_tree/haml/format.rb', line 313
def visit_tag(node)
parts = PartList.new(node)
if node.value[:name] != "div"
parts << PrefixPart.new("%", node.value[:name])
end
if node.value[:attributes].key?("class")
parts << PrefixPart.new(
".",
node.value[:attributes]["class"].tr(" ", ".")
)
end
if node.value[:attributes].key?("id")
parts << PrefixPart.new("#", node.value[:attributes]["id"])
end
if node.value[:dynamic_attributes].new
parts << HTMLAttributesPart.new(node.value[:dynamic_attributes].new)
end
static =
node.value[:attributes].reject do |key, _|
key == "class" || key == "id"
end
parts << HashAttributesPart.new(static) if static.any?
if node.value[:dynamic_attributes].old
parts << PlainPart.new("%div") if parts.empty?
parts << parse_attributes(node.value[:dynamic_attributes].old)
end
if node.value[:object_ref] != :nil
parts << PlainPart.new("%div") if parts.empty?
parts << PlainPart.new(node.value[:object_ref])
end
parts << PlainPart.new(">") if node.value[:nuke_outer_whitespace]
parts << PlainPart.new("<") if node.value[:nuke_inner_whitespace]
parts << PlainPart.new("/") if node.value[:self_closing]
if (value = node.value[:value]) && !value.empty?
with_children(node) do
q.group { parts.format(q) }
q.indent do
q.breakable_empty
if node.value[:parse]
format_tag_value(q, value)
else
q.if_break { q.text("") }.if_flat { q.text(" ") }
q.text(value)
end
end
end
else
with_children(node) { parts.format(q) }
end
end
|