-
-
Notifications
You must be signed in to change notification settings - Fork 571
/
compiler.rb
340 lines (291 loc) · 10.8 KB
/
compiler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# frozen_string_literal: false
require 'haml/attribute_builder'
require 'haml/attribute_compiler'
require 'haml/temple_line_counter'
module Haml
class Compiler
include Haml::Util
attr_accessor :options
def initialize(options)
@options = Options.wrap(options)
@to_merge = []
@temple = [:multi]
@node = nil
@filters = Filters.defined.merge(options[:filters])
@attribute_compiler = AttributeCompiler.new(@options)
end
def call(node)
compile(node)
@temple
end
def compile(node)
parent, @node = @node, node
if node.children.empty?
send(:"compile_#{node.type}")
else
send(:"compile_#{node.type}") {node.children.each {|c| compile c}}
end
ensure
@node = parent
end
private
def compile_root
@output_line = 1
yield if block_given?
flush_merged_text
end
def compile_plain
push_text("#{@node.value[:text]}\n")
end
def nuke_inner_whitespace?(node)
if node.value && node.value[:nuke_inner_whitespace]
true
elsif node.parent
nuke_inner_whitespace?(node.parent)
else
false
end
end
def compile_script(&block)
push_script(@node.value[:text],
:preserve_script => @node.value[:preserve],
:escape_html => @node.value[:escape_html],
:nuke_inner_whitespace => nuke_inner_whitespace?(@node),
&block)
end
def compile_silent_script
return if @options.suppress_eval
push_silent(@node.value[:text])
keyword = @node.value[:keyword]
if block_given?
yield
push_silent("end", :can_suppress) unless @node.value[:dont_push_end]
elsif keyword == "end"
if @node.parent.children.last.equal?(@node)
# Since this "end" is ending the block,
# we don't need to generate an additional one
@node.parent.value[:dont_push_end] = true
end
# Don't restore dont_* for end because it isn't a conditional branch.
end
end
def compile_haml_comment; end
def compile_tag
t = @node.value
# Get rid of whitespace outside of the tag if we need to
rstrip_buffer! if t[:nuke_outer_whitespace]
if @options.suppress_eval
object_ref = :nil
parse = false
value = t[:parse] ? nil : t[:value]
dynamic_attributes = Haml::Parser::DynamicAttributes.new
else
object_ref = t[:object_ref]
parse = t[:parse]
value = t[:value]
dynamic_attributes = t[:dynamic_attributes]
end
if @options[:trace]
t[:attributes].merge!({"data-trace" => @options.filename.split('/views').last << ":" << @node.line.to_s})
end
push_text("<#{t[:name]}")
push_temple(@attribute_compiler.compile(t[:attributes], object_ref, dynamic_attributes))
push_text(
if t[:self_closing] && @options.xhtml?
" />#{"\n".freeze unless t[:nuke_outer_whitespace]}"
else
">#{"\n".freeze unless (t[:self_closing] && @options.html?) ? t[:nuke_outer_whitespace] : (!block_given? || t[:preserve_tag] || t[:nuke_inner_whitespace])}"
end
)
if value && !parse
push_text("#{value}</#{t[:name]}>#{"\n".freeze unless t[:nuke_outer_whitespace]}")
end
return if t[:self_closing]
if value.nil?
yield if block_given?
rstrip_buffer! if t[:nuke_inner_whitespace]
push_text("</#{t[:name]}>#{"\n".freeze unless t[:nuke_outer_whitespace]}")
return
end
if parse
push_script(value, t.merge(:in_tag => true))
push_text("</#{t[:name]}>#{"\n".freeze unless t[:nuke_outer_whitespace]}")
end
end
def compile_comment
condition = "#{@node.value[:conditional]}>" if @node.value[:conditional]
revealed = @node.value[:revealed]
open = "<!--#{condition}#{'<!-->' if revealed}"
close = "#{'<!--' if revealed}#{'<![endif]' if condition}-->"
unless block_given?
push_text("#{open} ")
if @node.value[:parse]
push_script(@node.value[:text], :in_tag => true, :nuke_inner_whitespace => true)
else
push_text(@node.value[:text])
end
push_text(" #{close}\n")
return
end
push_text("#{open}\n")
yield if block_given?
push_text("#{close}\n")
end
def compile_doctype
doctype = text_for_doctype
push_text("#{doctype}\n") if doctype
end
def compile_filter
unless filter = @filters[@node.value[:name]]
name = @node.value[:name]
if ["maruku", "textile"].include?(name)
raise Error.new(Error.message(:install_haml_contrib, name), @node.line - 1)
else
raise Error.new(Error.message(:filter_not_defined, name), @node.line - 1)
end
end
filter.internal_compile(self, @node.value[:text])
end
def text_for_doctype
if @node.value[:type] == "xml"
return nil if @options.html?
wrapper = @options.attr_wrapper
return "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{@node.value[:encoding] || "utf-8"}#{wrapper} ?>"
end
if @options.html5?
'<!DOCTYPE html>'
else
if @options.xhtml?
if @node.value[:version] == "1.1"
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
elsif @node.value[:version] == "5"
'<!DOCTYPE html>'
else
case @node.value[:type]
when "strict"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
when "frameset"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
when "mobile"; '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
when "rdfa"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">'
when "basic"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">'
else '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
end
end
elsif @options.html4?
case @node.value[:type]
when "strict"; '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
when "frameset"; '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
else '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
end
end
end
end
# Evaluates `text` in the context of the scope object, but
# does not output the result.
def push_silent(text, can_suppress = false)
flush_merged_text
return if can_suppress && @options.suppress_eval?
newline = (text == "end") ? ";" : "\n".freeze
@temple << [:code, "#{resolve_newlines}#{text}#{newline}"]
@output_line = @output_line + text.count("\n".freeze) + newline.count("\n".freeze)
end
# Adds `text` to `@buffer`.
def push_text(text)
@to_merge << [:text, text]
end
def push_temple(temple)
flush_merged_text
@temple.concat([[:newline]] * resolve_newlines.count("\n".freeze))
@temple << temple
@output_line += TempleLineCounter.count_lines(temple)
end
def flush_merged_text
return if @to_merge.empty?
@to_merge.each do |type, val|
case type
when :text
@temple << [:static, val]
when :script
@temple << [:dynamic, val]
else
raise SyntaxError.new("[HAML BUG] Undefined entry in Haml::Compiler@to_merge.")
end
end
@to_merge = []
end
# Causes `text` to be evaluated in the context of
# the scope object and the result to be added to `@buffer`.
#
# If `opts[:preserve_script]` is true, Haml::Helpers#find_and_preserve is run on
# the result before it is added to `@buffer`
def push_script(text, opts = {})
return if @options.suppress_eval?
no_format = !(opts[:preserve_script] || opts[:preserve_tag] || opts[:escape_html])
unless block_given?
push_generated_script(no_format ? "(#{text}\n).to_s" : build_script_formatter("(#{text}\n)", opts))
# TODO this \n should be able to be frozen but the following tests fails
#
# bin/rails test test/helper_test.rb:596
# bin/rails test test/engine_test.rb:534
#
# Both appear to be testing outer whitespace nuking but at this point
# of the code there is no nuke_outer_whitespace key and the
# nuke_inner_whitespace key is false.
push_text("\n") unless opts[:in_tag] || opts[:nuke_inner_whitespace]
return
end
flush_merged_text
push_silent "haml_temp = #{text}"
yield
push_silent('end', :can_suppress) unless @node.value[:dont_push_end]
@temple << [:dynamic, no_format ? 'haml_temp.to_s;' : build_script_formatter('haml_temp', opts)]
end
def build_script_formatter(text, opts)
text = "(#{text}).to_s"
if opts[:escape_html]
text = "::Haml::Helpers.html_escape(#{text})"
end
if opts[:nuke_inner_whitespace]
text = "(#{text}).strip"
end
if opts[:preserve_tag]
text = "_hamlout.fix_textareas!(::Haml::Helpers.preserve(#{text}))"
elsif opts[:preserve_script]
text = "_hamlout.fix_textareas!(::Haml::Helpers.find_and_preserve(#{text}, _hamlout.options[:preserve]))"
end
"#{text};"
end
def push_generated_script(text)
@to_merge << [:script, resolve_newlines + text]
@output_line += text.count("\n".freeze)
end
def resolve_newlines
diff = @node.line - @output_line
return "".freeze if diff <= 0
@output_line = @node.line
return "\n".freeze if diff == 1
"\n".freeze * diff
end
# Get rid of and whitespace at the end of the buffer
# or the merged text
def rstrip_buffer!(index = -1)
last = @to_merge[index]
if last.nil?
push_silent("_hamlout.rstrip!", false)
return
end
case last.first
when :text
last[1].rstrip!
if last[1].empty?
@to_merge.slice! index
rstrip_buffer! index
end
when :script
last[1].gsub!(/\(haml_temp, (.*?)\);$/, '(haml_temp.rstrip, \1);')
rstrip_buffer! index - 1
else
raise SyntaxError.new("[HAML BUG] Undefined entry in Haml::Compiler@to_merge.")
end
end
end
end