Class: Herb::Engine::Compiler
Constant Summary
collapse
- EXPRESSION_TOKEN_TYPES =
[:expr, :expr_escaped, :expr_block, :expr_block_escaped].freeze
- TRAILING_WHITESPACE =
/[ \t]+\z/
- TRAILING_INDENTATION =
/\n[ \t]+\z/
- TRAILING_INDENTATION_CAPTURE =
/\n([ \t]+)\z/
- WHITESPACE_ONLY =
/\A[ \t]+\z/
- WHITESPACE_ONLY_CAPTURE =
/\A([ \t]+)\z/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Visitor
#visit, #visit_all, #visit_child_nodes, #visit_erb_node, #visit_erb_render_node, #visit_erb_strict_locals_node, #visit_html_conditional_open_tag_node, #visit_node, #visit_ruby_html_attributes_splat_node, #visit_ruby_parameter_node, #visit_ruby_render_keywords_node, #visit_ruby_render_local_node
#erb_comment?, #erb_graphql?, #erb_output?, #erb_outputs?, #inline_ruby_comment?
Constructor Details
#initialize(engine, options = {}) ⇒ Compiler
Returns a new instance of Compiler.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/herb/engine/compiler.rb', line 16
def initialize(engine, options = {})
super()
@engine = engine
@escape = options.fetch(:escape) { options.fetch(:escape_html, false) }
@tokens = [] @element_stack = [] @context_stack = [:html_content]
@trim_next_whitespace = false
@last_trim_consumed_newline = false
@pending_leading_whitespace = nil
@pending_leading_whitespace_insert_index = 0
@current_element_source = nil
end
|
Instance Attribute Details
#tokens ⇒ Object
Returns the value of attribute tokens.
14
15
16
|
# File 'lib/herb/engine/compiler.rb', line 14
def tokens
@tokens
end
|
Instance Method Details
#generate_output ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/herb/engine/compiler.rb', line 31
def generate_output
optimized_tokens = optimize_tokens(@tokens)
optimized_tokens.each do |type, value, context, escaped|
case type
when :text
@engine.send(:add_text, value)
when :code
@engine.send(:add_code, value)
when :expr, :expr_escaped
indicator = indicator_for(type)
if context_aware_context?(context)
@engine.send(:add_context_aware_expression, indicator, value, context)
else
@engine.send(:add_expression, indicator, value)
end
when :expr_block, :expr_block_escaped
@engine.send(:add_expression_block, indicator_for(type), value)
when :expr_block_end
@engine.send(:add_expression_block_end, value, escaped: escaped)
end
end
end
|
#visit_cdata_node(node) ⇒ Object
206
207
208
209
210
|
# File 'lib/herb/engine/compiler.rb', line 206
def visit_cdata_node(node)
add_text(node.tag_opening.value)
visit_all(node.children)
add_text(node.tag_closing.value)
end
|
#visit_document_node(node) ⇒ Object
56
57
58
|
# File 'lib/herb/engine/compiler.rb', line 56
def visit_document_node(node)
visit_all(node.children)
end
|
#visit_erb_begin_node(node) ⇒ Object
268
269
270
|
# File 'lib/herb/engine/compiler.rb', line 268
def visit_erb_begin_node(node)
visit_erb_control_with_parts(node, :statements, :rescue_clause, :else_clause, :ensure_clause, :end_node)
end
|
#visit_erb_block_end_node(node, escaped: false) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/herb/engine/compiler.rb', line 327
def visit_erb_block_end_node(node, escaped: false)
remove_trailing_whitespace_from_last_token! if left_trim?(node)
code = node.content.value.strip
if at_line_start?
leading_space = extract_and_remove_leading_space!
right_space = " \n"
@tokens << [:expr_block_end, "#{leading_space}#{code}#{right_space}", current_context, escaped]
@trim_next_whitespace = true
else
@tokens << [:expr_block_end, code, current_context, escaped]
end
end
|
#visit_erb_block_node(node) ⇒ Object
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/herb/engine/compiler.rb', line 296
def visit_erb_block_node(node)
opening = node.tag_opening.value
check_for_escaped_erb_tag!(opening)
if opening.include?("=")
should_escape = should_escape_output?(opening)
code = node.content.value.strip
@tokens << if should_escape
[:expr_block_escaped, code, current_context]
else
[:expr_block, code, current_context]
end
@last_trim_consumed_newline = false
@trim_next_whitespace = true if right_trim?(node)
visit_all(node.body)
visit_erb_block_end_node(node.end_node, escaped: should_escape)
else
visit_erb_control_node(node) do
visit_all(node.body)
visit(node.rescue_clause)
visit(node.else_clause)
visit(node.ensure_clause)
visit(node.end_node)
end
end
end
|
#visit_erb_case_match_node(node) ⇒ Object
284
285
286
|
# File 'lib/herb/engine/compiler.rb', line 284
def visit_erb_case_match_node(node)
visit_erb_control_with_parts(node, :conditions, :else_clause, :end_node)
end
|
#visit_erb_case_node(node) ⇒ Object
248
249
250
|
# File 'lib/herb/engine/compiler.rb', line 248
def visit_erb_case_node(node)
visit_erb_control_with_parts(node, :conditions, :else_clause, :end_node)
end
|
#visit_erb_content_node(node) ⇒ Object
212
213
214
215
216
|
# File 'lib/herb/engine/compiler.rb', line 212
def visit_erb_content_node(node)
return if (node)
process_erb_tag(node)
end
|
#visit_erb_control_node(node) ⇒ Object
218
219
220
221
222
223
224
|
# File 'lib/herb/engine/compiler.rb', line 218
def visit_erb_control_node(node, &)
if node.content
apply_trim(node, node.content.value.strip)
end
yield if block_given?
end
|
#visit_erb_control_with_parts(node, *parts) ⇒ Object
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/herb/engine/compiler.rb', line 343
def visit_erb_control_with_parts(node, *parts)
visit_erb_control_node(node) do
parts.each do |part|
value = node.send(part)
case value
when Array
visit_all(value)
when nil
else
visit(value)
end
end
end
end
|
#visit_erb_else_node(node) ⇒ Object
234
235
236
237
238
|
# File 'lib/herb/engine/compiler.rb', line 234
def visit_erb_else_node(node)
visit_erb_control_node(node) do
visit_all(node.statements)
end
end
|
#visit_erb_end_node(node) ⇒ Object
280
281
282
|
# File 'lib/herb/engine/compiler.rb', line 280
def visit_erb_end_node(node)
visit_erb_control_node(node)
end
|
#visit_erb_ensure_node(node) ⇒ Object
276
277
278
|
# File 'lib/herb/engine/compiler.rb', line 276
def visit_erb_ensure_node(node)
visit_erb_control_with_parts(node, :statements)
end
|
#visit_erb_for_node(node) ⇒ Object
256
257
258
|
# File 'lib/herb/engine/compiler.rb', line 256
def visit_erb_for_node(node)
visit_erb_control_with_parts(node, :statements, :end_node)
end
|
#visit_erb_if_node(node) ⇒ Object
226
227
228
229
230
231
232
|
# File 'lib/herb/engine/compiler.rb', line 226
def visit_erb_if_node(node)
visit_erb_control_node(node) do
visit_all(node.statements)
visit(node.subsequent)
visit(node.end_node)
end
end
|
#visit_erb_in_node(node) ⇒ Object
288
289
290
|
# File 'lib/herb/engine/compiler.rb', line 288
def visit_erb_in_node(node)
visit_erb_control_with_parts(node, :statements)
end
|
#visit_erb_open_tag_node(node) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/herb/engine/compiler.rb', line 125
def visit_erb_open_tag_node(node)
tag_name = node.tag_name&.value
if tag_name
is_void = Herb::HTML::Util.void_element?(tag_name)
uses_self_closing = is_void && @current_element_source != "ActionView::Helpers::TagHelper#tag"
add_text("<")
add_text(tag_name)
node.children.each do |child|
visit(child)
end
add_text(uses_self_closing ? " />" : ">")
else
process_erb_tag(node)
end
end
|
#visit_erb_rescue_node(node) ⇒ Object
272
273
274
|
# File 'lib/herb/engine/compiler.rb', line 272
def visit_erb_rescue_node(node)
visit_erb_control_with_parts(node, :statements, :subsequent)
end
|
#visit_erb_unless_node(node) ⇒ Object
240
241
242
243
244
245
246
|
# File 'lib/herb/engine/compiler.rb', line 240
def visit_erb_unless_node(node)
visit_erb_control_node(node) do
visit_all(node.statements)
visit(node.else_clause)
visit(node.end_node)
end
end
|
#visit_erb_until_node(node) ⇒ Object
264
265
266
|
# File 'lib/herb/engine/compiler.rb', line 264
def visit_erb_until_node(node)
visit_erb_control_with_parts(node, :statements, :end_node)
end
|
#visit_erb_when_node(node) ⇒ Object
252
253
254
|
# File 'lib/herb/engine/compiler.rb', line 252
def visit_erb_when_node(node)
visit_erb_control_with_parts(node, :statements)
end
|
#visit_erb_while_node(node) ⇒ Object
260
261
262
|
# File 'lib/herb/engine/compiler.rb', line 260
def visit_erb_while_node(node)
visit_erb_control_with_parts(node, :statements, :end_node)
end
|
#visit_erb_yield_node(node) ⇒ Object
292
293
294
|
# File 'lib/herb/engine/compiler.rb', line 292
def visit_erb_yield_node(node)
process_erb_tag(node, skip_comment_check: true)
end
|
#visit_html_attribute_name_node(node) ⇒ Object
111
112
113
|
# File 'lib/herb/engine/compiler.rb', line 111
def visit_html_attribute_name_node(node)
visit_all(node.children)
end
|
#visit_html_attribute_node(node) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/herb/engine/compiler.rb', line 98
def visit_html_attribute_node(node)
add_whitespace(" ")
visit(node.name)
return unless node.value
has_equals = node.equals.value&.include?("=")
add_text(has_equals ? node.equals.value : "=")
visit(node.value)
end
|
#visit_html_attribute_value_node(node) ⇒ Object
115
116
117
118
119
120
121
122
123
|
# File 'lib/herb/engine/compiler.rb', line 115
def visit_html_attribute_value_node(node)
push_context(:attribute_value)
add_text(node.open_quote&.value || '"') if node.quoted
visit_all(node.children)
add_text(node.close_quote&.value || '"') if node.quoted
pop_context
end
|
#visit_html_close_tag_node(node) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/herb/engine/compiler.rb', line 159
def visit_html_close_tag_node(node)
tag_name = node.tag_name&.value&.downcase
if @engine.content_for_head && tag_name == "head"
escaped_html = @engine.content_for_head.gsub("'", "\\\\'")
@tokens << [:expr, "'#{escaped_html}'.html_safe", current_context]
end
add_text(node.tag_opening&.value)
add_text(node.tag_name&.value)
add_text(node.tag_closing&.value)
end
|
188
189
190
191
192
|
# File 'lib/herb/engine/compiler.rb', line 188
def (node)
add_text(node..value)
visit_all(node.children)
add_text(node..value)
end
|
#visit_html_conditional_element_node(node) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/herb/engine/compiler.rb', line 81
def visit_html_conditional_element_node(node)
with_element_context(node) do
visit(node.open_conditional)
visit_all(node.body)
visit(node.close_conditional)
end
end
|
#visit_html_doctype_node(node) ⇒ Object
194
195
196
197
198
|
# File 'lib/herb/engine/compiler.rb', line 194
def visit_html_doctype_node(node)
add_text(node.tag_opening.value)
visit_all(node.children)
add_text(node.tag_closing.value)
end
|
#visit_html_element_node(node) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/herb/engine/compiler.rb', line 60
def visit_html_element_node(node)
with_element_context(node) do
visit(node.open_tag)
visit_all(node.body)
tag_name = node.tag_name&.value&.downcase
if node.open_tag.is_a?(Herb::AST::ERBOpenTagNode) && tag_name && node.close_tag
if node.close_tag.is_a?(Herb::AST::ERBEndNode)
remove_trailing_whitespace_from_last_token! if left_trim?(node.close_tag)
add_text("</#{tag_name}>")
@trim_next_whitespace = true
else
add_text("</#{tag_name}>")
end
else
visit(node.close_tag)
end
end
end
|
#visit_html_omitted_close_tag_node(node) ⇒ Object
172
173
174
|
# File 'lib/herb/engine/compiler.rb', line 172
def visit_html_omitted_close_tag_node(node)
end
|
#visit_html_open_tag_node(node) ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'lib/herb/engine/compiler.rb', line 89
def visit_html_open_tag_node(node)
add_text(node.tag_opening&.value || "<")
add_text(node.tag_name.value) if node.tag_name
visit_all(node.children)
add_text(node.tag_closing&.value || ">")
end
|
#visit_html_text_node(node) ⇒ Object
176
177
178
|
# File 'lib/herb/engine/compiler.rb', line 176
def visit_html_text_node(node)
add_text(node.content)
end
|
#visit_html_virtual_close_tag_node(node) ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'lib/herb/engine/compiler.rb', line 145
def visit_html_virtual_close_tag_node(node)
tag_name = node.tag_name&.value
return unless tag_name
add_text("</")
add_text(tag_name)
add_text(">")
end
|
#visit_literal_node(node) ⇒ Object
180
181
182
|
# File 'lib/herb/engine/compiler.rb', line 180
def visit_literal_node(node)
add_text(node.content)
end
|
#visit_ruby_literal_node(node) ⇒ Object
155
156
157
|
# File 'lib/herb/engine/compiler.rb', line 155
def visit_ruby_literal_node(node)
add_expression(node.content)
end
|
#visit_whitespace_node(node) ⇒ Object
184
185
186
|
# File 'lib/herb/engine/compiler.rb', line 184
def visit_whitespace_node(node)
add_whitespace(node.value.value)
end
|
#visit_xml_declaration_node(node) ⇒ Object
200
201
202
203
204
|
# File 'lib/herb/engine/compiler.rb', line 200
def visit_xml_declaration_node(node)
add_text(node.tag_opening.value)
visit_all(node.children)
add_text(node.tag_closing.value)
end
|