public
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/nex3/haml.git
Add limited support for whitespace chomping and self-closing to #haml_tag.
nex3 (author)
Thu Jun 05 18:32:17 -0700 2008
commit  21147927fdaa8787136d54c1db94df0dfb37e070
tree    b5ca320fa601120d603629b741f37579d88f0842
parent  377464e79e6b3633e63ea87bb2863b0fb786eaaa
...
265
266
267
268
269
 
 
270
271
272
...
274
275
276
 
 
 
 
 
277
278
279
...
304
305
306
307
 
308
309
310
311
312
313
314
315
316
317
318
319
 
 
 
 
 
 
 
 
320
321
322
323
 
 
 
 
 
324
325
326
...
332
333
334
 
 
 
 
 
 
335
336
337
...
265
266
267
 
 
268
269
270
271
272
...
274
275
276
277
278
279
280
281
282
283
284
...
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
...
339
340
341
342
343
344
345
346
347
348
349
350
0
@@ -265,8 +265,8 @@ module Haml
0
 
0
     #
0
     # call-seq:
0
- # haml_tag(name, attributes = {}) {...}
0
- # haml_tag(name, text, attributes = {}) {...}
0
+ # haml_tag(name, *flags, attributes = {}) {...}
0
+ # haml_tag(name, text, *flags, attributes = {}) {...}
0
     #
0
     # Creates an HTML tag with the given name and optionally text and attributes.
0
     # Can take a block that will be executed
0
@@ -274,6 +274,11 @@ module Haml
0
     # If the block is a Haml block or outputs text using puts,
0
     # the text will be properly indented.
0
     #
0
+ # <tt>flags</tt> is a list of symbol flags
0
+ # like those that can be put at the end of a Haml tag
0
+ # (<tt>:/</tt>, <tt>:<</tt>, and <tt>:></tt>).
0
+ # Currently, only <tt>:/</tt> and <tt>:<</tt> are supported.
0
+ #
0
     # For example,
0
     #
0
     # haml_tag :table do
0
@@ -304,23 +309,25 @@ module Haml
0
     # </tr>
0
     # </table>
0
     #
0
- def haml_tag(name, attributes = {}, alt_atts = {}, &block)
0
+ def haml_tag(name, *rest, &block)
0
       name = name.to_s
0
-
0
- text = nil
0
- if attributes.is_a? String
0
- text = attributes
0
- attributes = alt_atts
0
- end
0
-
0
- attributes = Haml::Precompiler.build_attributes(
0
- haml_buffer.html?, haml_buffer.options[:attr_wrapper], attributes)
0
-
0
- if text.nil? && block.nil? && haml_buffer.options[:autoclose].include?(name)
0
+ text = rest.shift if rest.first.is_a? String
0
+ flags = []
0
+ flags << rest.shift while rest.first.is_a? Symbol
0
+ attributes = Haml::Precompiler.build_attributes(haml_buffer.html?,
0
+ haml_buffer.options[:attr_wrapper],
0
+ rest.shift || {})
0
+
0
+ if text.nil? && block.nil? && (haml_buffer.options[:autoclose].include?(name) || flags.include?(:/))
0
         puts "<#{name}#{attributes} />"
0
         return nil
0
       end
0
 
0
+ if flags.include?(:/)
0
+ raise Error.new("Self-closing tags can't have content.") if text
0
+ raise Error.new("Illegal nesting: nesting within a self-closing tag is illegal.") if block
0
+ end
0
+
0
       tag = "<#{name}#{attributes}>"
0
       if block.nil?
0
         tag << text.to_s << "</#{name}>"
0
@@ -332,6 +339,12 @@ module Haml
0
         raise Error.new("Illegal nesting: content can't be both given to haml_tag :#{name} and nested within it.")
0
       end
0
 
0
+ if flags.include?(:<)
0
+ tag << capture_haml(&block).strip << "</#{name}>"
0
+ puts tag
0
+ return
0
+ end
0
+
0
       puts tag
0
       tab_up
0
       block.call
...
116
117
118
 
 
 
 
 
 
 
 
119
120
121
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
0
@@ -116,6 +116,14 @@ class HelperTest < Test::Unit::TestCase
0
     assert_raise(Haml::Error) { render("- haml_tag :p, 'foo' do\n bar") }
0
   end
0
 
0
+ def test_haml_tag_flags
0
+ assert_equal("<p />\n", render("- haml_tag :p, :/"))
0
+ assert_equal("<p>kumquat</p>\n", render("- haml_tag :p, :< do\n kumquat"))
0
+
0
+ assert_raise(Haml::Error) { render("- haml_tag :p, 'foo', :/") }
0
+ assert_raise(Haml::Error) { render("- haml_tag :p, :/ do\n foo") }
0
+ end
0
+
0
   def test_is_haml
0
     assert(!ActionView::Base.new.is_haml?)
0
     assert_equal("true\n", render("= is_haml?"))

Comments

  • Rymai Fri Jun 06 12:46:27 -0700 2008

    Great! :)
    Waiting for a solution for the :> flag.
    Thanks ;)

  • nex3 Fri Jun 06 14:48:47 -0700 2008

    Unfortunately, I can’t imagine how :> would be possible, at least without some serious modifications to Buffer that I’m not sure are worth it.