wireframe-haml 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. data/README.rdoc +332 -0
  2. data/VERSION.yml +4 -0
  3. data/bin/css2sass +7 -0
  4. data/bin/haml +9 -0
  5. data/bin/html2haml +7 -0
  6. data/bin/sass +8 -0
  7. data/lib/haml/buffer.rb +255 -0
  8. data/lib/haml/engine.rb +268 -0
  9. data/lib/haml/error.rb +22 -0
  10. data/lib/haml/exec.rb +395 -0
  11. data/lib/haml/filters.rb +276 -0
  12. data/lib/haml/helpers/action_view_extensions.rb +45 -0
  13. data/lib/haml/helpers/action_view_mods.rb +181 -0
  14. data/lib/haml/helpers.rb +468 -0
  15. data/lib/haml/html.rb +218 -0
  16. data/lib/haml/precompiler.rb +889 -0
  17. data/lib/haml/shared.rb +45 -0
  18. data/lib/haml/template/patch.rb +58 -0
  19. data/lib/haml/template/plugin.rb +72 -0
  20. data/lib/haml/template.rb +51 -0
  21. data/lib/haml/util.rb +77 -0
  22. data/lib/haml/version.rb +47 -0
  23. data/lib/haml.rb +1042 -0
  24. data/lib/sass/css.rb +388 -0
  25. data/lib/sass/engine.rb +499 -0
  26. data/lib/sass/environment.rb +33 -0
  27. data/lib/sass/error.rb +35 -0
  28. data/lib/sass/plugin/merb.rb +56 -0
  29. data/lib/sass/plugin/rails.rb +24 -0
  30. data/lib/sass/plugin.rb +203 -0
  31. data/lib/sass/repl.rb +51 -0
  32. data/lib/sass/script/bool.rb +13 -0
  33. data/lib/sass/script/color.rb +97 -0
  34. data/lib/sass/script/funcall.rb +28 -0
  35. data/lib/sass/script/functions.rb +122 -0
  36. data/lib/sass/script/lexer.rb +152 -0
  37. data/lib/sass/script/literal.rb +60 -0
  38. data/lib/sass/script/number.rb +231 -0
  39. data/lib/sass/script/operation.rb +30 -0
  40. data/lib/sass/script/parser.rb +142 -0
  41. data/lib/sass/script/string.rb +42 -0
  42. data/lib/sass/script/unary_operation.rb +21 -0
  43. data/lib/sass/script/variable.rb +20 -0
  44. data/lib/sass/script.rb +38 -0
  45. data/lib/sass/tree/attr_node.rb +64 -0
  46. data/lib/sass/tree/comment_node.rb +34 -0
  47. data/lib/sass/tree/debug_node.rb +22 -0
  48. data/lib/sass/tree/directive_node.rb +50 -0
  49. data/lib/sass/tree/file_node.rb +27 -0
  50. data/lib/sass/tree/for_node.rb +29 -0
  51. data/lib/sass/tree/if_node.rb +27 -0
  52. data/lib/sass/tree/mixin_def_node.rb +18 -0
  53. data/lib/sass/tree/mixin_node.rb +34 -0
  54. data/lib/sass/tree/node.rb +99 -0
  55. data/lib/sass/tree/rule_node.rb +120 -0
  56. data/lib/sass/tree/variable_node.rb +24 -0
  57. data/lib/sass/tree/while_node.rb +20 -0
  58. data/lib/sass.rb +1062 -0
  59. data/test/benchmark.rb +99 -0
  60. data/test/haml/engine_test.rb +734 -0
  61. data/test/haml/helper_test.rb +224 -0
  62. data/test/haml/html2haml_test.rb +92 -0
  63. data/test/haml/markaby/standard.mab +52 -0
  64. data/test/haml/mocks/article.rb +6 -0
  65. data/test/haml/results/content_for_layout.xhtml +15 -0
  66. data/test/haml/results/eval_suppressed.xhtml +9 -0
  67. data/test/haml/results/filters.xhtml +62 -0
  68. data/test/haml/results/helpers.xhtml +93 -0
  69. data/test/haml/results/helpful.xhtml +10 -0
  70. data/test/haml/results/just_stuff.xhtml +68 -0
  71. data/test/haml/results/list.xhtml +12 -0
  72. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  73. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  74. data/test/haml/results/original_engine.xhtml +20 -0
  75. data/test/haml/results/partial_layout.xhtml +5 -0
  76. data/test/haml/results/partials.xhtml +21 -0
  77. data/test/haml/results/render_layout.xhtml +3 -0
  78. data/test/haml/results/silent_script.xhtml +74 -0
  79. data/test/haml/results/standard.xhtml +42 -0
  80. data/test/haml/results/tag_parsing.xhtml +23 -0
  81. data/test/haml/results/very_basic.xhtml +5 -0
  82. data/test/haml/results/whitespace_handling.xhtml +89 -0
  83. data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
  84. data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
  85. data/test/haml/rhtml/action_view.rhtml +62 -0
  86. data/test/haml/rhtml/standard.rhtml +54 -0
  87. data/test/haml/template_test.rb +204 -0
  88. data/test/haml/templates/_av_partial_1.haml +9 -0
  89. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  90. data/test/haml/templates/_av_partial_2.haml +5 -0
  91. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  92. data/test/haml/templates/_layout.erb +3 -0
  93. data/test/haml/templates/_layout_for_partial.haml +3 -0
  94. data/test/haml/templates/_partial.haml +8 -0
  95. data/test/haml/templates/_text_area.haml +3 -0
  96. data/test/haml/templates/action_view.haml +47 -0
  97. data/test/haml/templates/action_view_ugly.haml +47 -0
  98. data/test/haml/templates/breakage.haml +8 -0
  99. data/test/haml/templates/content_for_layout.haml +10 -0
  100. data/test/haml/templates/eval_suppressed.haml +11 -0
  101. data/test/haml/templates/filters.haml +66 -0
  102. data/test/haml/templates/helpers.haml +95 -0
  103. data/test/haml/templates/helpful.haml +11 -0
  104. data/test/haml/templates/just_stuff.haml +83 -0
  105. data/test/haml/templates/list.haml +12 -0
  106. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  107. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  108. data/test/haml/templates/original_engine.haml +17 -0
  109. data/test/haml/templates/partial_layout.haml +3 -0
  110. data/test/haml/templates/partialize.haml +1 -0
  111. data/test/haml/templates/partials.haml +12 -0
  112. data/test/haml/templates/render_layout.haml +2 -0
  113. data/test/haml/templates/silent_script.haml +40 -0
  114. data/test/haml/templates/standard.haml +42 -0
  115. data/test/haml/templates/standard_ugly.haml +1 -0
  116. data/test/haml/templates/tag_parsing.haml +21 -0
  117. data/test/haml/templates/very_basic.haml +4 -0
  118. data/test/haml/templates/whitespace_handling.haml +87 -0
  119. data/test/linked_rails.rb +12 -0
  120. data/test/sass/css2sass_test.rb +193 -0
  121. data/test/sass/engine_test.rb +786 -0
  122. data/test/sass/functions_test.rb +96 -0
  123. data/test/sass/more_results/more1.css +9 -0
  124. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  125. data/test/sass/more_results/more_import.css +29 -0
  126. data/test/sass/more_templates/_more_partial.sass +2 -0
  127. data/test/sass/more_templates/more1.sass +23 -0
  128. data/test/sass/more_templates/more_import.sass +11 -0
  129. data/test/sass/plugin_test.rb +208 -0
  130. data/test/sass/results/alt.css +4 -0
  131. data/test/sass/results/basic.css +9 -0
  132. data/test/sass/results/compact.css +5 -0
  133. data/test/sass/results/complex.css +87 -0
  134. data/test/sass/results/compressed.css +1 -0
  135. data/test/sass/results/expanded.css +19 -0
  136. data/test/sass/results/import.css +29 -0
  137. data/test/sass/results/line_numbers.css +49 -0
  138. data/test/sass/results/mixins.css +95 -0
  139. data/test/sass/results/multiline.css +24 -0
  140. data/test/sass/results/nested.css +22 -0
  141. data/test/sass/results/parent_ref.css +13 -0
  142. data/test/sass/results/script.css +16 -0
  143. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  144. data/test/sass/results/subdir/subdir.css +3 -0
  145. data/test/sass/results/units.css +11 -0
  146. data/test/sass/script_test.rb +153 -0
  147. data/test/sass/templates/_partial.sass +2 -0
  148. data/test/sass/templates/alt.sass +16 -0
  149. data/test/sass/templates/basic.sass +23 -0
  150. data/test/sass/templates/bork.sass +2 -0
  151. data/test/sass/templates/bork2.sass +2 -0
  152. data/test/sass/templates/compact.sass +17 -0
  153. data/test/sass/templates/complex.sass +309 -0
  154. data/test/sass/templates/compressed.sass +15 -0
  155. data/test/sass/templates/expanded.sass +17 -0
  156. data/test/sass/templates/import.sass +11 -0
  157. data/test/sass/templates/importee.sass +19 -0
  158. data/test/sass/templates/line_numbers.sass +13 -0
  159. data/test/sass/templates/mixins.sass +76 -0
  160. data/test/sass/templates/multiline.sass +20 -0
  161. data/test/sass/templates/nested.sass +25 -0
  162. data/test/sass/templates/parent_ref.sass +25 -0
  163. data/test/sass/templates/script.sass +101 -0
  164. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  165. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  166. data/test/sass/templates/subdir/subdir.sass +6 -0
  167. data/test/sass/templates/units.sass +11 -0
  168. data/test/test_helper.rb +21 -0
  169. metadata +247 -0
@@ -0,0 +1,889 @@
1
+ require 'strscan'
2
+ require 'haml/shared'
3
+
4
+ module Haml
5
+ module Precompiler
6
+ include Haml::Util
7
+
8
+ # Designates an XHTML/XML element.
9
+ ELEMENT = ?%
10
+
11
+ # Designates a <tt><div></tt> element with the given class.
12
+ DIV_CLASS = ?.
13
+
14
+ # Designates a <tt><div></tt> element with the given id.
15
+ DIV_ID = ?#
16
+
17
+ # Designates an XHTML/XML comment.
18
+ COMMENT = ?/
19
+
20
+ # Designates an XHTML doctype or script that is never HTML-escaped.
21
+ DOCTYPE = ?!
22
+
23
+ # Designates script, the result of which is output.
24
+ SCRIPT = ?=
25
+
26
+ # Designates script that is always HTML-escaped.
27
+ SANITIZE = ?&
28
+
29
+ # Designates script, the result of which is flattened and output.
30
+ FLAT_SCRIPT = ?~
31
+
32
+ # Designates script which is run but not output.
33
+ SILENT_SCRIPT = ?-
34
+
35
+ # When following SILENT_SCRIPT, designates a comment that is not output.
36
+ SILENT_COMMENT = ?#
37
+
38
+ # Designates a non-parsed line.
39
+ ESCAPE = ?\\
40
+
41
+ # Designates a block of filtered text.
42
+ FILTER = ?:
43
+
44
+ # Designates a non-parsed line. Not actually a character.
45
+ PLAIN_TEXT = -1
46
+
47
+ # Keeps track of the ASCII values of the characters that begin a
48
+ # specially-interpreted line.
49
+ SPECIAL_CHARACTERS = [
50
+ ELEMENT,
51
+ DIV_CLASS,
52
+ DIV_ID,
53
+ COMMENT,
54
+ DOCTYPE,
55
+ SCRIPT,
56
+ SANITIZE,
57
+ FLAT_SCRIPT,
58
+ SILENT_SCRIPT,
59
+ ESCAPE,
60
+ FILTER
61
+ ]
62
+
63
+ # The value of the character that designates that a line is part
64
+ # of a multiline string.
65
+ MULTILINE_CHAR_VALUE = ?|
66
+
67
+ # Keywords that appear in the middle of a Ruby block with lowered
68
+ # indentation. If a block has been started using indentation,
69
+ # lowering the indentation with one of these won't end the block.
70
+ # For example:
71
+ #
72
+ # - if foo
73
+ # %p yes!
74
+ # - else
75
+ # %p no!
76
+ #
77
+ # The block is ended after <tt>%p no!</tt>, because <tt>else</tt>
78
+ # is a member of this array.
79
+ MID_BLOCK_KEYWORDS = ['else', 'elsif', 'rescue', 'ensure', 'when']
80
+
81
+ # The Regex that matches a Doctype command.
82
+ DOCTYPE_REGEX = /(\d\.\d)?[\s]*([a-z]*)/i
83
+
84
+ # The Regex that matches a literal string or symbol value
85
+ LITERAL_VALUE_REGEX = /^\s*(:(\w*)|(('|")([^\\\#'"]*?)\4))\s*$/
86
+
87
+ private
88
+
89
+ # Returns the precompiled string with the preamble and postamble
90
+ def precompiled_with_ambles(local_names)
91
+ preamble = <<END.gsub("\n", ";")
92
+ extend Haml::Helpers
93
+ _hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect})
94
+ _erbout = _hamlout.buffer
95
+ __in_erb_template = true
96
+ END
97
+ postamble = <<END.gsub("\n", ";")
98
+ @haml_buffer = @haml_buffer.upper
99
+ _erbout
100
+ END
101
+ preamble + locals_code(local_names) + @precompiled + postamble
102
+ end
103
+
104
+ def locals_code(names)
105
+ names = names.keys if Hash == names
106
+
107
+ names.map do |name|
108
+ # Can't use || because someone might explicitly pass in false with a symbol
109
+ sym_local = "_haml_locals[#{name.to_sym.inspect}]"
110
+ str_local = "_haml_locals[#{name.to_s.inspect}]"
111
+ "#{name} = #{sym_local}.nil? ? #{str_local} : #{sym_local}"
112
+ end.join(';') + ';'
113
+ end
114
+
115
+ class Line < Struct.new(:text, :unstripped, :full, :index, :precompiler, :eod)
116
+ alias_method :eod?, :eod
117
+
118
+ def tabs
119
+ line = self
120
+ @tabs ||= precompiler.instance_eval do
121
+ break 0 if line.text.empty? || !(whitespace = line.full[/^\s+/])
122
+
123
+ if @indentation.nil?
124
+ @indentation = whitespace
125
+
126
+ if @indentation.include?(?\s) && @indentation.include?(?\t)
127
+ raise SyntaxError.new("Indentation can't use both tabs and spaces.", line.index)
128
+ end
129
+
130
+ @flat_spaces = @indentation * @template_tabs if flat?
131
+ break 1
132
+ end
133
+
134
+ tabs = whitespace.length / @indentation.length
135
+ break tabs if whitespace == @indentation * tabs
136
+ break @template_tabs if flat? && whitespace =~ /^#{@indentation * @template_tabs}/
137
+
138
+ raise SyntaxError.new(<<END.strip.gsub("\n", ' '), line.index)
139
+ Inconsistent indentation: #{Haml::Shared.human_indentation whitespace, true} used for indentation,
140
+ but the rest of the document was indented using #{Haml::Shared.human_indentation @indentation}.
141
+ END
142
+ end
143
+ end
144
+ end
145
+
146
+ def precompile
147
+ @haml_comment = @dont_indent_next_line = @dont_tab_up_next_text = false
148
+ @indentation = nil
149
+ @line = next_line
150
+ resolve_newlines
151
+ newline
152
+
153
+ raise SyntaxError.new("Indenting at the beginning of the document is illegal.", @line.index) if @line.tabs != 0
154
+
155
+ while next_line
156
+ process_indent(@line) unless @line.text.empty?
157
+
158
+ if flat?
159
+ push_flat(@line)
160
+ @line = @next_line
161
+ newline
162
+ next
163
+ end
164
+
165
+ process_line(@line.text, @line.index) unless @line.text.empty? || @haml_comment
166
+
167
+ if !flat? && @next_line.tabs - @line.tabs > 1
168
+ raise SyntaxError.new("The line was indented #{@next_line.tabs - @line.tabs} levels deeper than the previous line.", @next_line.index)
169
+ end
170
+
171
+ resolve_newlines unless @next_line.eod?
172
+ @line = @next_line
173
+ newline unless @next_line.eod?
174
+ end
175
+
176
+ # Close all the open tags
177
+ close until @to_close_stack.empty?
178
+ flush_merged_text
179
+ end
180
+
181
+ # Processes and deals with lowering indentation.
182
+ def process_indent(line)
183
+ return unless line.tabs <= @template_tabs && @template_tabs > 0
184
+
185
+ to_close = @template_tabs - line.tabs
186
+ to_close.times { |i| close unless to_close - 1 - i == 0 && mid_block_keyword?(line.text) }
187
+ end
188
+
189
+ # Processes a single line of Haml.
190
+ #
191
+ # This method doesn't return anything; it simply processes the line and
192
+ # adds the appropriate code to <tt>@precompiled</tt>.
193
+ def process_line(text, index)
194
+ @index = index + 1
195
+
196
+ case text[0]
197
+ when DIV_CLASS, DIV_ID; render_div(text)
198
+ when ELEMENT; render_tag(text)
199
+ when COMMENT; render_comment(text[1..-1].strip)
200
+ when SANITIZE
201
+ return push_script(unescape_interpolation(text[3..-1].strip), :escape_html => true) if text[1..2] == "=="
202
+ return push_script(text[2..-1].strip, :escape_html => true) if text[1] == SCRIPT
203
+ return push_script(unescape_interpolation(text[1..-1].strip), :escape_html => true) if text[1] == ?\s
204
+ push_plain text
205
+ when SCRIPT
206
+ return push_script(unescape_interpolation(text[2..-1].strip)) if text[1] == SCRIPT
207
+ return push_script(text[1..-1], :escape_html => true) if options[:escape_html]
208
+ push_script(text[1..-1])
209
+ when FLAT_SCRIPT; push_flat_script(text[1..-1])
210
+ when SILENT_SCRIPT
211
+ return start_haml_comment if text[1] == SILENT_COMMENT
212
+
213
+ raise SyntaxError.new(<<END.rstrip, index) if text[1..-1].strip == "end"
214
+ You don't need to use "- end" in Haml. Use indentation instead:
215
+ - if foo?
216
+ %strong Foo!
217
+ - else
218
+ Not foo.
219
+ END
220
+
221
+ push_silent(text[1..-1], true)
222
+ newline_now
223
+
224
+ case_stmt = text[1..-1].split(' ', 2)[0] == "case"
225
+ block = block_opened? && !mid_block_keyword?(text)
226
+ push_and_tabulate([:script]) if block || case_stmt
227
+ push_and_tabulate(:nil) if block && case_stmt
228
+ when FILTER; start_filtered(text[1..-1].downcase)
229
+ when DOCTYPE
230
+ return render_doctype(text) if text[0...3] == '!!!'
231
+ return push_script(unescape_interpolation(text[3..-1].strip)) if text[1..2] == "=="
232
+ return push_script(text[2..-1].strip) if text[1] == SCRIPT
233
+ return push_script(unescape_interpolation(text[1..-1].strip)) if text[1] == ?\s
234
+ push_plain text
235
+ when ESCAPE; push_plain text[1..-1]
236
+ else push_plain text
237
+ end
238
+ end
239
+
240
+ # Returns whether or not the text is a silent script text with one
241
+ # of Ruby's mid-block keywords.
242
+ def mid_block_keyword?(text)
243
+ text.length > 2 && text[0] == SILENT_SCRIPT && MID_BLOCK_KEYWORDS.include?(text[1..-1].split[0])
244
+ end
245
+
246
+ # Evaluates <tt>text</tt> in the context of the scope object, but
247
+ # does not output the result.
248
+ def push_silent(text, can_suppress = false)
249
+ flush_merged_text
250
+ return if can_suppress && options[:suppress_eval]
251
+ @precompiled << "#{text};"
252
+ end
253
+
254
+ # Adds <tt>text</tt> to <tt>@buffer</tt> with appropriate tabulation
255
+ # without parsing it.
256
+ def push_merged_text(text, tab_change = 0, indent = true)
257
+ text = !indent || @dont_indent_next_line || @options[:ugly] ? text : "#{' ' * @output_tabs}#{text}"
258
+ @to_merge << [:text, text, tab_change]
259
+ @dont_indent_next_line = false
260
+ end
261
+
262
+ # Concatenate <tt>text</tt> to <tt>@buffer</tt> without tabulation.
263
+ def concat_merged_text(text)
264
+ @to_merge << [:text, text, 0]
265
+ end
266
+
267
+ def push_text(text, tab_change = 0)
268
+ push_merged_text("#{text}\n", tab_change)
269
+ end
270
+
271
+ def flush_merged_text
272
+ return if @to_merge.empty?
273
+
274
+ text, tab_change = @to_merge.inject(["", 0]) do |(str, mtabs), (type, val, tabs)|
275
+ case type
276
+ when :text
277
+ [str << val.gsub('#{', "\\\#{"), mtabs + tabs]
278
+ when :script
279
+ if mtabs != 0 && !@options[:ugly]
280
+ val = "_hamlout.adjust_tabs(#{mtabs}); " + val
281
+ end
282
+ [str << "\#{#{val}}", 0]
283
+ else
284
+ raise SyntaxError.new("[HAML BUG] Undefined entry in Haml::Precompiler@to_merge.")
285
+ end
286
+ end
287
+
288
+ @precompiled <<
289
+ if @options[:ugly]
290
+ "_erbout << #{unescape_interpolation(text)};"
291
+ else
292
+ "_hamlout.push_text(#{unescape_interpolation(text)}, #{tab_change}, #{@dont_tab_up_next_text.inspect});"
293
+ end
294
+ @to_merge = []
295
+ @dont_tab_up_next_text = false
296
+ end
297
+
298
+ # Renders a block of text as plain text.
299
+ # Also checks for an illegally opened block.
300
+ def push_plain(text)
301
+ if block_opened?
302
+ raise SyntaxError.new("Illegal nesting: nesting within plain text is illegal.", @next_line.index)
303
+ end
304
+
305
+ if contains_interpolation?(text)
306
+ push_script unescape_interpolation(text)
307
+ else
308
+ push_text text
309
+ end
310
+ end
311
+
312
+ # Adds +text+ to <tt>@buffer</tt> while flattening text.
313
+ def push_flat(line)
314
+ text = line.full.dup
315
+ text = "" unless text.gsub!(/^#{@flat_spaces}/, '')
316
+ @filter_buffer << "#{text}\n"
317
+ end
318
+
319
+ # Causes <tt>text</tt> to be evaluated in the context of
320
+ # the scope object and the result to be added to <tt>@buffer</tt>.
321
+ #
322
+ # If <tt>opts[:preserve_script]</tt> is true, Haml::Helpers#find_and_flatten is run on
323
+ # the result before it is added to <tt>@buffer</tt>
324
+ def push_script(text, opts = {})
325
+ raise SyntaxError.new("There's no Ruby code for = to evaluate.") if text.empty?
326
+ return if options[:suppress_eval]
327
+
328
+ args = %w[preserve_script in_tag preserve_tag escape_html nuke_inner_whitespace]
329
+ args.map! {|name| opts[name.to_sym]}
330
+ args << !block_opened? << @options[:ugly]
331
+
332
+ no_format = @options[:ugly] &&
333
+ !(opts[:preserve_script] || opts[:preserve_tag] || opts[:escape_html])
334
+ output_temp = "(haml_very_temp = haml_temp; haml_temp = nil; haml_very_temp)"
335
+ out = "_hamlout.#{static_method_name(:format_script, *args)}(#{output_temp});"
336
+
337
+ # Prerender tabulation unless we're in a tag
338
+ push_merged_text '' unless opts[:in_tag]
339
+
340
+ unless block_opened?
341
+ @to_merge << [:script, no_format ? "#{text}\n" : "haml_temp = #{text}\n#{out}"]
342
+ concat_merged_text("\n") unless opts[:in_tag] || opts[:nuke_inner_whitespace]
343
+ @newlines -= 1
344
+ return
345
+ end
346
+
347
+ flush_merged_text
348
+
349
+ push_silent "haml_temp = #{text}"
350
+ newline_now
351
+ push_and_tabulate([:loud, "_erbout << #{no_format ? "#{output_temp}.to_s;" : out}",
352
+ !(opts[:in_tag] || opts[:nuke_inner_whitespace] || @options[:ugly])])
353
+ end
354
+
355
+ # Causes <tt>text</tt> to be evaluated, and Haml::Helpers#find_and_flatten
356
+ # to be run on it afterwards.
357
+ def push_flat_script(text)
358
+ flush_merged_text
359
+
360
+ raise SyntaxError.new("There's no Ruby code for ~ to evaluate.") if text.empty?
361
+ push_script(text, :preserve_script => true)
362
+ end
363
+
364
+ def start_haml_comment
365
+ return unless block_opened?
366
+
367
+ @haml_comment = true
368
+ push_and_tabulate([:haml_comment])
369
+ end
370
+
371
+ # Closes the most recent item in <tt>@to_close_stack</tt>.
372
+ def close
373
+ tag, *rest = @to_close_stack.pop
374
+ send("close_#{tag}", *rest)
375
+ end
376
+
377
+ # Puts a line in <tt>@precompiled</tt> that will add the closing tag of
378
+ # the most recently opened tag.
379
+ def close_element(value)
380
+ tag, nuke_outer_whitespace, nuke_inner_whitespace = value
381
+ @output_tabs -= 1 unless nuke_inner_whitespace
382
+ @template_tabs -= 1
383
+ rstrip_buffer! if nuke_inner_whitespace
384
+ push_merged_text("</#{tag}>" + (nuke_outer_whitespace ? "" : "\n"),
385
+ nuke_inner_whitespace ? 0 : -1, !nuke_inner_whitespace)
386
+ @dont_indent_next_line = nuke_outer_whitespace
387
+ end
388
+
389
+ # Closes a Ruby block.
390
+ def close_script
391
+ push_silent "end", true
392
+ @template_tabs -= 1
393
+ end
394
+
395
+ # Closes a comment.
396
+ def close_comment(has_conditional)
397
+ @output_tabs -= 1
398
+ @template_tabs -= 1
399
+ close_tag = has_conditional ? "<![endif]-->" : "-->"
400
+ push_text(close_tag, -1)
401
+ end
402
+
403
+ # Closes a loud Ruby block.
404
+ def close_loud(command, add_newline)
405
+ push_silent 'end', true
406
+ @precompiled << command
407
+ @template_tabs -= 1
408
+ concat_merged_text("\n") if add_newline
409
+ end
410
+
411
+ # Closes a filtered block.
412
+ def close_filtered(filter)
413
+ filter.internal_compile(self, @filter_buffer)
414
+ @flat = false
415
+ @flat_spaces = nil
416
+ @filter_buffer = nil
417
+ @template_tabs -= 1
418
+ end
419
+
420
+ def close_haml_comment
421
+ @haml_comment = false
422
+ @template_tabs -= 1
423
+ end
424
+
425
+ def close_nil
426
+ @template_tabs -= 1
427
+ end
428
+
429
+ # Iterates through the classes and ids supplied through <tt>.</tt>
430
+ # and <tt>#</tt> syntax, and returns a hash with them as attributes,
431
+ # that can then be merged with another attributes hash.
432
+ def parse_class_and_id(list)
433
+ attributes = {}
434
+ list.scan(/([#.])([-_a-zA-Z0-9]+)/) do |type, property|
435
+ case type
436
+ when '.'
437
+ if attributes['class']
438
+ attributes['class'] += " "
439
+ else
440
+ attributes['class'] = ""
441
+ end
442
+ attributes['class'] += property
443
+ when '#'; attributes['id'] = property
444
+ end
445
+ end
446
+ attributes
447
+ end
448
+
449
+ def parse_literal_value(text)
450
+ return nil unless text
451
+ text.match(LITERAL_VALUE_REGEX)
452
+
453
+ # $2 holds the value matched by a symbol, but is nil for a string match
454
+ # $5 holds the value matched by a string
455
+ $2 || $5
456
+ end
457
+
458
+ def parse_static_hash(text)
459
+ return {} unless text
460
+
461
+ attributes = {}
462
+ text.split(',').each do |attrib|
463
+ key, value, more = attrib.split('=>')
464
+
465
+ # Make sure the key and value and only the key and value exist
466
+ # Otherwise, it's too complicated or dynamic and we'll defer it to the actual Ruby parser
467
+ key = parse_literal_value key
468
+ value = parse_literal_value value
469
+ return nil if more || key.nil? || value.nil?
470
+
471
+ attributes[key] = value
472
+ end
473
+ text.count("\n").times { newline }
474
+ attributes
475
+ end
476
+
477
+ # This is a class method so it can be accessed from Buffer.
478
+ def self.build_attributes(is_html, attr_wrapper, attributes = {})
479
+ quote_escape = attr_wrapper == '"' ? "&quot;" : "&apos;"
480
+ other_quote_char = attr_wrapper == '"' ? "'" : '"'
481
+
482
+ result = attributes.collect do |attr, value|
483
+ next if value.nil?
484
+
485
+ if value == true
486
+ next " #{attr}" if is_html
487
+ next " #{attr}=#{attr_wrapper}#{attr}#{attr_wrapper}"
488
+ elsif value == false
489
+ next
490
+ end
491
+
492
+ value = Haml::Helpers.preserve(Haml::Helpers.escape_once(value.to_s))
493
+ # We want to decide whether or not to escape quotes
494
+ value.gsub!('&quot;', '"')
495
+ this_attr_wrapper = attr_wrapper
496
+ if value.include? attr_wrapper
497
+ if value.include? other_quote_char
498
+ value = value.gsub(attr_wrapper, quote_escape)
499
+ else
500
+ this_attr_wrapper = other_quote_char
501
+ end
502
+ end
503
+ " #{attr}=#{this_attr_wrapper}#{value}#{this_attr_wrapper}"
504
+ end
505
+ result.compact.sort.join
506
+ end
507
+
508
+ def prerender_tag(name, self_close, attributes)
509
+ attributes_string = Precompiler.build_attributes(html?, @options[:attr_wrapper], attributes)
510
+ "<#{name}#{attributes_string}#{self_close && xhtml? ? ' /' : ''}>"
511
+ end
512
+
513
+ # Parses a line into tag_name, attributes, attributes_hash, object_ref, action, value
514
+ def parse_tag(line)
515
+ raise SyntaxError.new("Invalid tag: \"#{line}\".") unless match = line.scan(/%([-:\w]+)([-\w\.\#]*)(.*)/)[0]
516
+ tag_name, attributes, rest = match
517
+ attributes_hash, rest, last_line = parse_attributes(rest) if rest[0] == ?{
518
+ if rest
519
+ object_ref, rest = balance(rest, ?[, ?]) if rest[0] == ?[
520
+ attributes_hash, rest, last_line = parse_attributes(rest) if rest[0] == ?{ && attributes_hash.nil?
521
+ nuke_whitespace, action, value = rest.scan(/(<>|><|[><])?([=\/\~&!])?(.*)?/)[0]
522
+ nuke_whitespace ||= ''
523
+ nuke_outer_whitespace = nuke_whitespace.include? '>'
524
+ nuke_inner_whitespace = nuke_whitespace.include? '<'
525
+ end
526
+ value = value.to_s.strip
527
+ [tag_name, attributes, attributes_hash, object_ref, nuke_outer_whitespace,
528
+ nuke_inner_whitespace, action, value, last_line || @index]
529
+ end
530
+
531
+ def parse_attributes(line)
532
+ line = line.dup
533
+ last_line = @index
534
+
535
+ begin
536
+ attributes_hash, rest = balance(line, ?{, ?})
537
+ rescue SyntaxError => e
538
+ if line.strip[-1] == ?, && e.message == "Unbalanced brackets."
539
+ line << "\n" << @next_line.text
540
+ last_line += 1
541
+ next_line
542
+ retry
543
+ end
544
+
545
+ raise e
546
+ end
547
+
548
+ attributes_hash = attributes_hash[1...-1] if attributes_hash
549
+ return attributes_hash, rest, last_line
550
+ end
551
+
552
+ # Parses a line that will render as an XHTML tag, and adds the code that will
553
+ # render that tag to <tt>@precompiled</tt>.
554
+ def render_tag(line)
555
+ tag_name, attributes, attributes_hash, object_ref, nuke_outer_whitespace,
556
+ nuke_inner_whitespace, action, value, last_line = parse_tag(line)
557
+
558
+ raise SyntaxError.new("Illegal element: classes and ids must have values.") if attributes =~ /[\.#](\.|#|\z)/
559
+
560
+ # Get rid of whitespace outside of the tag if we need to
561
+ rstrip_buffer! if nuke_outer_whitespace
562
+
563
+ preserve_tag = options[:preserve].include?(tag_name)
564
+ nuke_inner_whitespace ||= preserve_tag
565
+ preserve_tag &&= !options[:ugly]
566
+
567
+ case action
568
+ when '/'; self_closing = true
569
+ when '~'; parse = preserve_script = true
570
+ when '='
571
+ parse = true
572
+ value = unescape_interpolation(value[1..-1].strip) if value[0] == ?=
573
+ when '&', '!'
574
+ if value[0] == ?=
575
+ parse = true
576
+ value =
577
+ if value[1] == ?=
578
+ unescape_interpolation(value[2..-1].strip)
579
+ else
580
+ value[1..-1].strip
581
+ end
582
+ elsif contains_interpolation?(value)
583
+ parse = true
584
+ value = unescape_interpolation(value)
585
+ end
586
+ else
587
+ if contains_interpolation?(value)
588
+ parse = true
589
+ value = unescape_interpolation(value)
590
+ end
591
+ end
592
+
593
+ if parse && @options[:suppress_eval]
594
+ parse = false
595
+ value = ''
596
+ end
597
+
598
+ escape_html = (action == '&' || (action != '!' && @options[:escape_html]))
599
+
600
+ object_ref = "nil" if object_ref.nil? || @options[:suppress_eval]
601
+
602
+ static_attributes = parse_static_hash(attributes_hash) # Try pre-compiling a static attributes hash
603
+ attributes_hash = nil if static_attributes || @options[:suppress_eval]
604
+ attributes = parse_class_and_id(attributes)
605
+ Buffer.merge_attrs(attributes, static_attributes) if static_attributes
606
+
607
+ raise SyntaxError.new("Illegal nesting: nesting within a self-closing tag is illegal.", @next_line.index) if block_opened? && self_closing
608
+ raise SyntaxError.new("Illegal nesting: content can't be both given on the same line as %#{tag_name} and nested within it.", @next_line.index) if block_opened? && !value.empty?
609
+ raise SyntaxError.new("There's no Ruby code for #{action} to evaluate.", last_line - 1) if parse && value.empty?
610
+ raise SyntaxError.new("Self-closing tags can't have content.", last_line - 1) if self_closing && !value.empty?
611
+
612
+ self_closing ||= !!( !block_opened? && value.empty? && @options[:autoclose].include?(tag_name) )
613
+
614
+ dont_indent_next_line =
615
+ (nuke_outer_whitespace && !block_opened?) ||
616
+ (nuke_inner_whitespace && block_opened?)
617
+
618
+ # Check if we can render the tag directly to text and not process it in the buffer
619
+ if object_ref == "nil" && attributes_hash.nil? && !preserve_script
620
+ tag_closed = !block_opened? && !self_closing && !parse
621
+
622
+ open_tag = prerender_tag(tag_name, self_closing, attributes)
623
+ if tag_closed
624
+ open_tag << "#{value}</#{tag_name}>"
625
+ open_tag << "\n" unless nuke_outer_whitespace
626
+ else
627
+ open_tag << "\n" unless parse || nuke_inner_whitespace || (self_closing && nuke_outer_whitespace)
628
+ end
629
+
630
+ push_merged_text(open_tag, tag_closed || self_closing || nuke_inner_whitespace ? 0 : 1,
631
+ !nuke_outer_whitespace)
632
+
633
+ @dont_indent_next_line = dont_indent_next_line
634
+ return if tag_closed
635
+ else
636
+ flush_merged_text
637
+ content = value.empty? || parse ? 'nil' : value.dump
638
+ attributes_hash = ', ' + attributes_hash if attributes_hash
639
+ args = [tag_name, self_closing, !block_opened?, preserve_tag, escape_html,
640
+ attributes, nuke_outer_whitespace, nuke_inner_whitespace
641
+ ].map { |v| v.inspect }.join(', ')
642
+ push_silent "_hamlout.open_tag(#{args}, #{object_ref}, #{content}#{attributes_hash})"
643
+ @dont_tab_up_next_text = @dont_indent_next_line = dont_indent_next_line
644
+ end
645
+
646
+ return if self_closing
647
+
648
+ if value.empty?
649
+ push_and_tabulate([:element, [tag_name, nuke_outer_whitespace, nuke_inner_whitespace]])
650
+ @output_tabs += 1 unless nuke_inner_whitespace
651
+ return
652
+ end
653
+
654
+ if parse
655
+ push_script(value, :preserve_script => preserve_script, :in_tag => true,
656
+ :preserve_tag => preserve_tag, :escape_html => escape_html,
657
+ :nuke_inner_whitespace => nuke_inner_whitespace)
658
+ concat_merged_text("</#{tag_name}>" + (nuke_outer_whitespace ? "" : "\n"))
659
+ end
660
+ end
661
+
662
+ # Renders a line that creates an XHTML tag and has an implicit div because of
663
+ # <tt>.</tt> or <tt>#</tt>.
664
+ def render_div(line)
665
+ render_tag('%div' + line)
666
+ end
667
+
668
+ # Renders an XHTML comment.
669
+ def render_comment(line)
670
+ conditional, line = balance(line, ?[, ?]) if line[0] == ?[
671
+ line.strip!
672
+ conditional << ">" if conditional
673
+
674
+ if block_opened? && !line.empty?
675
+ raise SyntaxError.new('Illegal nesting: nesting within a tag that already has content is illegal.', @next_line.index)
676
+ end
677
+
678
+ open = "<!--#{conditional} "
679
+
680
+ # Render it statically if possible
681
+ unless line.empty?
682
+ return push_text("#{open}#{line} #{conditional ? "<![endif]-->" : "-->"}")
683
+ end
684
+
685
+ push_text(open, 1)
686
+ @output_tabs += 1
687
+ push_and_tabulate([:comment, !conditional.nil?])
688
+ unless line.empty?
689
+ push_text(line)
690
+ close
691
+ end
692
+ end
693
+
694
+ # Renders an XHTML doctype or XML shebang.
695
+ def render_doctype(line)
696
+ raise SyntaxError.new("Illegal nesting: nesting within a header command is illegal.", @next_line.index) if block_opened?
697
+ doctype = text_for_doctype(line)
698
+ push_text doctype if doctype
699
+ end
700
+
701
+ def text_for_doctype(text)
702
+ text = text[3..-1].lstrip.downcase
703
+ if text.index("xml") == 0
704
+ return nil if html?
705
+ wrapper = @options[:attr_wrapper]
706
+ return "<?xml version=#{wrapper}1.0#{wrapper} encoding=#{wrapper}#{text.split(' ')[1] || "utf-8"}#{wrapper} ?>"
707
+ end
708
+
709
+ if html5?
710
+ '<!DOCTYPE html>'
711
+ else
712
+ version, type = text.scan(DOCTYPE_REGEX)[0]
713
+
714
+ if xhtml?
715
+ if version == "1.1"
716
+ '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
717
+ else
718
+ case type
719
+ when "strict"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
720
+ when "frameset"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
721
+ when "mobile"; '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
722
+ when "basic"; '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">'
723
+ else '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
724
+ end
725
+ end
726
+
727
+ elsif html4?
728
+ case type
729
+ when "strict"; '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
730
+ when "frameset"; '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
731
+ else '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
732
+ end
733
+ end
734
+ end
735
+ end
736
+
737
+ # Starts a filtered block.
738
+ def start_filtered(name)
739
+ raise Error.new("Invalid filter name \":#{name}\".") unless name =~ /^\w+$/
740
+ raise Error.new("Filter \"#{name}\" is not defined.") unless filter = Filters.defined[name]
741
+
742
+ push_and_tabulate([:filtered, filter])
743
+ @flat = true
744
+ @filter_buffer = String.new
745
+
746
+ # If we don't know the indentation by now, it'll be set in Line#tabs
747
+ @flat_spaces = @indentation * @template_tabs if @indentation
748
+ end
749
+
750
+ def raw_next_line
751
+ text = @template.shift
752
+ return unless text
753
+
754
+ index = @template_index
755
+ @template_index += 1
756
+
757
+ return text, index
758
+ end
759
+
760
+ def next_line
761
+ text, index = raw_next_line
762
+ return unless text
763
+
764
+ # :eod is a special end-of-document marker
765
+ line =
766
+ if text == :eod
767
+ Line.new '-#', '-#', '-#', index, self, true
768
+ else
769
+ Line.new text.strip, text.lstrip.chomp, text, index, self, false
770
+ end
771
+
772
+ # `flat?' here is a little outdated,
773
+ # so we have to manually check if the previous line closes the flat block.
774
+ unless flat? && (@line.text.empty? || @line.tabs >= @template_tabs)
775
+ if line.text.empty?
776
+ newline
777
+ return next_line
778
+ end
779
+
780
+ handle_multiline(line)
781
+ end
782
+
783
+ @next_line = line
784
+ end
785
+
786
+ def un_next_line(line)
787
+ @template.unshift line
788
+ @template_index -= 1
789
+ end
790
+
791
+ def handle_multiline(line)
792
+ if is_multiline?(line.text)
793
+ line.text.slice!(-1)
794
+ while new_line = raw_next_line.first
795
+ break if new_line == :eod
796
+ newline and next if new_line.strip.empty?
797
+ break unless is_multiline?(new_line.strip)
798
+ line.text << new_line.strip[0...-1]
799
+ newline
800
+ end
801
+ un_next_line new_line
802
+ resolve_newlines
803
+ end
804
+ end
805
+
806
+ # Checks whether or not +line+ is in a multiline sequence.
807
+ def is_multiline?(text)
808
+ text && text.length > 1 && text[-1] == MULTILINE_CHAR_VALUE && text[-2] == ?\s
809
+ end
810
+
811
+ def contains_interpolation?(str)
812
+ str.include?('#{')
813
+ end
814
+
815
+ def unescape_interpolation(str)
816
+ res = ''
817
+ rest = Haml::Shared.handle_interpolation str.dump do |scan|
818
+ escapes = (scan[2].size - 1) / 2
819
+ res << scan.matched[0...-3 - escapes]
820
+ if escapes % 2 == 1
821
+ res << '#{'
822
+ else
823
+ res << '#{' + eval('"' + balance(scan, ?{, ?}, 1)[0][0...-1] + '"') + "}"# Use eval to get rid of string escapes
824
+ end
825
+ end
826
+ res + rest
827
+ end
828
+
829
+ def balance(*args)
830
+ res = Haml::Shared.balance(*args)
831
+ return res if res
832
+ raise SyntaxError.new("Unbalanced brackets.")
833
+ end
834
+
835
+ def block_opened?
836
+ !flat? && @next_line.tabs > @line.tabs
837
+ end
838
+
839
+ # Pushes value onto <tt>@to_close_stack</tt> and increases
840
+ # <tt>@template_tabs</tt>.
841
+ def push_and_tabulate(value)
842
+ @to_close_stack.push(value)
843
+ @template_tabs += 1
844
+ end
845
+
846
+ def flat?
847
+ @flat
848
+ end
849
+
850
+ def newline
851
+ @newlines += 1
852
+ end
853
+
854
+ def newline_now
855
+ @precompiled << "\n"
856
+ @newlines -= 1
857
+ end
858
+
859
+ def resolve_newlines
860
+ return unless @newlines > 0
861
+ @precompiled << "\n" * @newlines
862
+ @newlines = 0
863
+ end
864
+
865
+ # Get rid of and whitespace at the end of the buffer
866
+ # or the merged text
867
+ def rstrip_buffer!
868
+ if @to_merge.empty?
869
+ push_silent("_erbout.rstrip!", false)
870
+ @dont_tab_up_next_text = true
871
+ return
872
+ end
873
+
874
+ last = @to_merge.last
875
+ case last.first
876
+ when :text
877
+ last[1].rstrip!
878
+ if last[1].empty?
879
+ @to_merge.pop
880
+ rstrip_buffer!
881
+ end
882
+ when :script
883
+ last[1].gsub!(/\(haml_temp, (.*?)\);$/, '(haml_temp.rstrip, \1);')
884
+ else
885
+ raise SyntaxError.new("[HAML BUG] Undefined entry in Haml::Precompiler@to_merge.")
886
+ end
887
+ end
888
+ end
889
+ end