mack-haml 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/lib/gems.rb +13 -0
  2. data/lib/gems/haml-2.0.4/VERSION +1 -0
  3. data/lib/gems/haml-2.0.4/bin/css2sass +7 -0
  4. data/lib/gems/haml-2.0.4/bin/haml +9 -0
  5. data/lib/gems/haml-2.0.4/bin/html2haml +7 -0
  6. data/lib/gems/haml-2.0.4/bin/sass +8 -0
  7. data/lib/gems/haml-2.0.4/lib/haml.rb +1040 -0
  8. data/lib/gems/haml-2.0.4/lib/haml/buffer.rb +239 -0
  9. data/lib/gems/haml-2.0.4/lib/haml/engine.rb +265 -0
  10. data/lib/gems/haml-2.0.4/lib/haml/error.rb +22 -0
  11. data/lib/gems/haml-2.0.4/lib/haml/exec.rb +364 -0
  12. data/lib/gems/haml-2.0.4/lib/haml/filters.rb +275 -0
  13. data/lib/gems/haml-2.0.4/lib/haml/helpers.rb +453 -0
  14. data/lib/gems/haml-2.0.4/lib/haml/helpers/action_view_extensions.rb +45 -0
  15. data/lib/gems/haml-2.0.4/lib/haml/helpers/action_view_mods.rb +179 -0
  16. data/lib/gems/haml-2.0.4/lib/haml/html.rb +227 -0
  17. data/lib/gems/haml-2.0.4/lib/haml/precompiler.rb +805 -0
  18. data/lib/gems/haml-2.0.4/lib/haml/template.rb +51 -0
  19. data/lib/gems/haml-2.0.4/lib/haml/template/patch.rb +58 -0
  20. data/lib/gems/haml-2.0.4/lib/haml/template/plugin.rb +72 -0
  21. data/lib/gems/haml-2.0.4/lib/sass.rb +863 -0
  22. data/lib/gems/haml-2.0.4/lib/sass/constant.rb +214 -0
  23. data/lib/gems/haml-2.0.4/lib/sass/constant/color.rb +101 -0
  24. data/lib/gems/haml-2.0.4/lib/sass/constant/literal.rb +54 -0
  25. data/lib/gems/haml-2.0.4/lib/sass/constant/nil.rb +9 -0
  26. data/lib/gems/haml-2.0.4/lib/sass/constant/number.rb +87 -0
  27. data/lib/gems/haml-2.0.4/lib/sass/constant/operation.rb +30 -0
  28. data/lib/gems/haml-2.0.4/lib/sass/constant/string.rb +22 -0
  29. data/lib/gems/haml-2.0.4/lib/sass/css.rb +394 -0
  30. data/lib/gems/haml-2.0.4/lib/sass/engine.rb +466 -0
  31. data/lib/gems/haml-2.0.4/lib/sass/error.rb +35 -0
  32. data/lib/gems/haml-2.0.4/lib/sass/plugin.rb +169 -0
  33. data/lib/gems/haml-2.0.4/lib/sass/plugin/merb.rb +56 -0
  34. data/lib/gems/haml-2.0.4/lib/sass/plugin/rails.rb +24 -0
  35. data/lib/gems/haml-2.0.4/lib/sass/tree/attr_node.rb +53 -0
  36. data/lib/gems/haml-2.0.4/lib/sass/tree/comment_node.rb +20 -0
  37. data/lib/gems/haml-2.0.4/lib/sass/tree/directive_node.rb +46 -0
  38. data/lib/gems/haml-2.0.4/lib/sass/tree/node.rb +42 -0
  39. data/lib/gems/haml-2.0.4/lib/sass/tree/rule_node.rb +89 -0
  40. data/lib/gems/haml-2.0.4/lib/sass/tree/value_node.rb +16 -0
  41. data/lib/gems/haml-2.0.4/rails/init.rb +1 -0
  42. data/lib/mack-haml.rb +1 -0
  43. metadata +65 -16
@@ -0,0 +1,453 @@
1
+ require 'haml/helpers/action_view_mods'
2
+ require 'haml/helpers/action_view_extensions'
3
+
4
+ module Haml
5
+ # This module contains various helpful methods to make it easier to do
6
+ # various tasks. Haml::Helpers is automatically included in the context
7
+ # that a Haml template is parsed in, so all these methods are at your
8
+ # disposal from within the template.
9
+ module Helpers
10
+ self.extend self
11
+
12
+ @@action_view_defined = defined?(ActionView)
13
+ @@force_no_action_view = false
14
+
15
+ # Returns whether or not ActionView is installed on the system.
16
+ def self.action_view?
17
+ @@action_view_defined
18
+ end
19
+
20
+ # Note: this does *not* need to be called
21
+ # when using Haml helpers normally
22
+ # in Rails.
23
+ #
24
+ # Initializes the current object
25
+ # as though it were in the same context
26
+ # as a normal ActionView rendering
27
+ # using Haml.
28
+ # This is useful if you want to use the helpers in a context
29
+ # other than the normal setup with ActionView.
30
+ # For example:
31
+ #
32
+ # context = Object.new
33
+ # class << context
34
+ # include Haml::Helpers
35
+ # end
36
+ # context.init_haml_helpers
37
+ # context.haml_tag :p, "Stuff"
38
+ #
39
+ def init_haml_helpers
40
+ @haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer))
41
+ nil
42
+ end
43
+
44
+ # call-seq:
45
+ # non_haml { ... }
46
+ #
47
+ # Runs a block of code in a non-Haml context
48
+ # (i.e. #is_haml? will return false).
49
+ #
50
+ # This is mainly useful for rendering sub-templates such as partials in a non-Haml language,
51
+ # particularly where helpers may behave differently when run from Haml.
52
+ #
53
+ # Note that this is automatically applied to Rails partials.
54
+ def non_haml
55
+ was_active = @haml_buffer.active?
56
+ @haml_buffer.active = false
57
+ res = yield
58
+ @haml_buffer.active = was_active
59
+ res
60
+ end
61
+
62
+ # call-seq:
63
+ # find_and_preserve(input, tags = haml_buffer.options[:preserve])
64
+ # find_and_preserve {...}
65
+ #
66
+ # Uses preserve to convert any newlines inside whitespace-sensitive tags
67
+ # into the HTML entities for endlines.
68
+ # @tags@ is an array of tags to preserve.
69
+ # It defaults to the value of the <tt>:preserve</tt> option.
70
+ def find_and_preserve(input = '', tags = haml_buffer.options[:preserve], &block)
71
+ return find_and_preserve(capture_haml(&block)) if block
72
+
73
+ input = input.to_s
74
+ input.gsub(/<(#{tags.map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im) do
75
+ "<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
76
+ end
77
+ end
78
+
79
+ # call-seq:
80
+ # preserve(input)
81
+ # preserve {...}
82
+ #
83
+ # Takes any string, finds all the endlines and converts them to
84
+ # HTML entities for endlines so they'll render correctly in
85
+ # whitespace-sensitive tags without screwing up the indentation.
86
+ def preserve(input = '', &block)
87
+ return preserve(capture_haml(&block)) if block
88
+
89
+ input.chomp("\n").gsub(/\n/, '&#x000A;').gsub(/\r/, '')
90
+ end
91
+
92
+ alias_method :flatten, :preserve
93
+
94
+ # Takes an Enumerable object and a block
95
+ # and iterates over the object,
96
+ # yielding each element to a Haml block
97
+ # and putting the result into <tt><li></tt> elements.
98
+ # This creates a list of the results of the block.
99
+ # For example:
100
+ #
101
+ # = list_of([['hello'], ['yall']]) do |i|
102
+ # = i[0]
103
+ #
104
+ # Produces:
105
+ #
106
+ # <li>hello</li>
107
+ # <li>yall</li>
108
+ #
109
+ # And
110
+ #
111
+ # = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|
112
+ # %h3= key.humanize
113
+ # %p= val
114
+ #
115
+ # Produces:
116
+ #
117
+ # <li>
118
+ # <h3>Title</h3>
119
+ # <p>All the stuff</p>
120
+ # </li>
121
+ # <li>
122
+ # <h3>Description</h3>
123
+ # <p>A book about all the stuff.</p>
124
+ # </li>
125
+ #
126
+ def list_of(array, &block) # :yields: item
127
+ to_return = array.collect do |i|
128
+ result = capture_haml(i, &block)
129
+
130
+ if result.count("\n") > 1
131
+ result.gsub!("\n", "\n ")
132
+ result = "\n #{result.strip}\n"
133
+ else
134
+ result.strip!
135
+ end
136
+
137
+ "<li>#{result}</li>"
138
+ end
139
+ to_return.join("\n")
140
+ end
141
+
142
+ # Returns a hash containing default assignments for the xmlns and xml:lang
143
+ # attributes of the <tt>html</tt> HTML element.
144
+ # It also takes an optional argument for the value of xml:lang and lang,
145
+ # which defaults to 'en-US'.
146
+ # For example,
147
+ #
148
+ # %html{html_attrs}
149
+ #
150
+ # becomes
151
+ #
152
+ # <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-US' lang='en-US'>
153
+ #
154
+ def html_attrs(lang = 'en-US')
155
+ {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
156
+ end
157
+
158
+ # Increments the number of tabs the buffer automatically adds
159
+ # to the lines of the template.
160
+ # For example:
161
+ #
162
+ # %h1 foo
163
+ # - tab_up
164
+ # %p bar
165
+ # - tab_down
166
+ # %strong baz
167
+ #
168
+ # Produces:
169
+ #
170
+ # <h1>foo</h1>
171
+ # <p>bar</p>
172
+ # <strong>baz</strong>
173
+ #
174
+ def tab_up(i = 1)
175
+ haml_buffer.tabulation += i
176
+ end
177
+
178
+ # Decrements the number of tabs the buffer automatically adds
179
+ # to the lines of the template.
180
+ #
181
+ # See also tab_up.
182
+ def tab_down(i = 1)
183
+ haml_buffer.tabulation -= i
184
+ end
185
+
186
+ # Surrounds the given block of Haml code with the given characters,
187
+ # with no whitespace in between.
188
+ # For example:
189
+ #
190
+ # = surround '(', ')' do
191
+ # %a{:href => "food"} chicken
192
+ #
193
+ # Produces:
194
+ #
195
+ # (<a href='food'>chicken</a>)
196
+ #
197
+ # and
198
+ #
199
+ # = surround '*' do
200
+ # %strong angry
201
+ #
202
+ # Produces:
203
+ #
204
+ # *<strong>angry</strong>*
205
+ #
206
+ def surround(front, back = nil, &block)
207
+ back ||= front
208
+ output = capture_haml(&block)
209
+
210
+ "#{front}#{output.chomp}#{back}\n"
211
+ end
212
+
213
+ # Prepends the given character to the beginning of the Haml block,
214
+ # with no whitespace between.
215
+ # For example:
216
+ #
217
+ # = precede '*' do
218
+ # %span.small Not really
219
+ #
220
+ # Produces:
221
+ #
222
+ # *<span class='small'>Not really</span>
223
+ #
224
+ def precede(char, &block)
225
+ "#{char}#{capture_haml(&block).chomp}\n"
226
+ end
227
+
228
+ # Appends the given character to the end of the Haml block,
229
+ # with no whitespace between.
230
+ # For example:
231
+ #
232
+ # click
233
+ # = succeed '.' do
234
+ # %a{:href=>"thing"} here
235
+ #
236
+ # Produces:
237
+ #
238
+ # click
239
+ # <a href='thing'>here</a>.
240
+ #
241
+ def succeed(char, &block)
242
+ "#{capture_haml(&block).chomp}#{char}\n"
243
+ end
244
+
245
+ # Captures the result of the given block of Haml code,
246
+ # gets rid of the excess indentation,
247
+ # and returns it as a string.
248
+ # For example, after the following,
249
+ #
250
+ # .foo
251
+ # - foo = capture_haml(13) do |a|
252
+ # %p= a
253
+ #
254
+ # the local variable <tt>foo</tt> would be assigned to "<p>13</p>\n".
255
+ #
256
+ def capture_haml(*args, &block)
257
+ buffer = eval('_hamlout', block) rescue haml_buffer
258
+ with_haml_buffer(buffer) do
259
+ position = haml_buffer.buffer.length
260
+
261
+ block.call(*args)
262
+
263
+ captured = haml_buffer.buffer.slice!(position..-1)
264
+
265
+ min_tabs = nil
266
+ captured.each do |line|
267
+ tabs = line.index(/[^ ]/)
268
+ min_tabs ||= tabs
269
+ min_tabs = min_tabs > tabs ? tabs : min_tabs
270
+ end
271
+
272
+ result = captured.map do |line|
273
+ line[min_tabs..-1]
274
+ end
275
+ result.to_s
276
+ end
277
+ end
278
+
279
+ def puts(*args) # :nodoc:
280
+ warn <<END
281
+ DEPRECATION WARNING:
282
+ The Haml #puts helper is deprecated and will be removed in version 2.4.
283
+ Use the #haml_concat helper instead.
284
+ END
285
+ haml_concat *args
286
+ end
287
+
288
+ # Outputs text directly to the Haml buffer, with the proper tabulation
289
+ def haml_concat(text = "")
290
+ haml_buffer.buffer << (' ' * haml_buffer.tabulation) << text.to_s << "\n"
291
+ nil
292
+ end
293
+
294
+ #
295
+ # call-seq:
296
+ # haml_tag(name, *flags, attributes = {}) {...}
297
+ # haml_tag(name, text, *flags, attributes = {}) {...}
298
+ #
299
+ # Creates an HTML tag with the given name and optionally text and attributes.
300
+ # Can take a block that will be executed
301
+ # between when the opening and closing tags are output.
302
+ # If the block is a Haml block or outputs text using haml_concat,
303
+ # the text will be properly indented.
304
+ #
305
+ # <tt>flags</tt> is a list of symbol flags
306
+ # like those that can be put at the end of a Haml tag
307
+ # (<tt>:/</tt>, <tt>:<</tt>, and <tt>:></tt>).
308
+ # Currently, only <tt>:/</tt> and <tt>:<</tt> are supported.
309
+ #
310
+ # For example,
311
+ #
312
+ # haml_tag :table do
313
+ # haml_tag :tr do
314
+ # haml_tag :td, {:class => 'cell'} do
315
+ # haml_tag :strong, "strong!"
316
+ # haml_concat "data"
317
+ # end
318
+ # haml_tag :td do
319
+ # haml_concat "more_data"
320
+ # end
321
+ # end
322
+ # end
323
+ #
324
+ # outputs
325
+ #
326
+ # <table>
327
+ # <tr>
328
+ # <td class='cell'>
329
+ # <strong>
330
+ # strong!
331
+ # </strong>
332
+ # data
333
+ # </td>
334
+ # <td>
335
+ # more_data
336
+ # </td>
337
+ # </tr>
338
+ # </table>
339
+ #
340
+ def haml_tag(name, *rest, &block)
341
+ name = name.to_s
342
+ text = rest.shift.to_s unless [Symbol, Hash, NilClass].any? {|t| rest.first.is_a? t}
343
+ flags = []
344
+ flags << rest.shift while rest.first.is_a? Symbol
345
+ attributes = Haml::Precompiler.build_attributes(haml_buffer.html?,
346
+ haml_buffer.options[:attr_wrapper],
347
+ rest.shift || {})
348
+
349
+ if text.nil? && block.nil? && (haml_buffer.options[:autoclose].include?(name) || flags.include?(:/))
350
+ haml_concat "<#{name}#{attributes} />"
351
+ return nil
352
+ end
353
+
354
+ if flags.include?(:/)
355
+ raise Error.new("Self-closing tags can't have content.") if text
356
+ raise Error.new("Illegal nesting: nesting within a self-closing tag is illegal.") if block
357
+ end
358
+
359
+ tag = "<#{name}#{attributes}>"
360
+ if block.nil?
361
+ tag << text.to_s << "</#{name}>"
362
+ haml_concat tag
363
+ return
364
+ end
365
+
366
+ if text
367
+ raise Error.new("Illegal nesting: content can't be both given to haml_tag :#{name} and nested within it.")
368
+ end
369
+
370
+ if flags.include?(:<)
371
+ tag << capture_haml(&block).strip << "</#{name}>"
372
+ haml_concat tag
373
+ return
374
+ end
375
+
376
+ haml_concat tag
377
+ tab_up
378
+ block.call
379
+ tab_down
380
+ haml_concat "</#{name}>"
381
+ nil
382
+ end
383
+
384
+ # Characters that need to be escaped to HTML entities from user input
385
+ HTML_ESCAPE = { '&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;', '"'=>'&quot;', "'"=>'&#039;', }
386
+
387
+ # Returns a copy of <tt>text</tt> with ampersands, angle brackets and quotes
388
+ # escaped into HTML entities.
389
+ def html_escape(text)
390
+ text.to_s.gsub(/[\"><&]/) { |s| HTML_ESCAPE[s] }
391
+ end
392
+
393
+ # Escapes HTML entities in <tt>text</tt>, but without escaping an ampersand
394
+ # that is already part of an escaped entity.
395
+ def escape_once(text)
396
+ text.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |s| HTML_ESCAPE[s] }
397
+ end
398
+
399
+ # Returns whether or not the current template is a Haml template.
400
+ #
401
+ # This function, unlike other Haml::Helpers functions,
402
+ # also works in other ActionView templates,
403
+ # where it will always return false.
404
+ def is_haml?
405
+ !@haml_buffer.nil? && @haml_buffer.active?
406
+ end
407
+
408
+ private
409
+
410
+ # call-seq:
411
+ # with_haml_buffer(buffer) {...}
412
+ #
413
+ # Runs the block with the given buffer as the currently active buffer.
414
+ def with_haml_buffer(buffer)
415
+ @haml_buffer, old_buffer = buffer, @haml_buffer
416
+ old_buffer.active, was_active = false, old_buffer.active? if old_buffer
417
+ @haml_buffer.active = true
418
+ yield
419
+ ensure
420
+ @haml_buffer.active = false
421
+ old_buffer.active = was_active if old_buffer
422
+ @haml_buffer = old_buffer
423
+ end
424
+
425
+ # Gets a reference to the current Haml::Buffer object.
426
+ def haml_buffer
427
+ @haml_buffer
428
+ end
429
+
430
+ # Gives a proc the same local "_hamlout" and "_erbout" variables
431
+ # that the current template has.
432
+ def haml_bind_proc(&proc)
433
+ _hamlout = haml_buffer
434
+ _erbout = _hamlout.buffer
435
+ proc { |*args| proc.call(*args) }
436
+ end
437
+
438
+ include ActionViewExtensions if self.const_defined? "ActionViewExtensions"
439
+ end
440
+ end
441
+
442
+ class Object
443
+ # Haml overrides various ActionView helpers,
444
+ # which call an #is_haml? method
445
+ # to determine whether or not the current context object
446
+ # is a proper Haml context.
447
+ # Because ActionView helpers may be included in non-ActionView::Base classes,
448
+ # it's a good idea to define is_haml? for all objects.
449
+ def is_haml?
450
+ false
451
+ end
452
+ end
453
+