From a99fd555c708fd806aee9e7eea29ce48abdc09f7 Mon Sep 17 00:00:00 2001 From: nex3 Date: Sun, 22 Oct 2006 21:42:45 +0000 Subject: [PATCH] Added more functionality to comments (see REFERENCE). Also added a few tests I forgot last time. git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@100 7063305b-7217-0410-af8c-cdc13e5119b9 --- REFERENCE | 31 ++++++++++++++++++++++++++++++ Rakefile | 2 +- lib/haml/buffer.rb | 23 +++++++++++++++++++++- lib/haml/engine.rb | 34 +++++++++++++++++++++++++-------- test/results/just_stuff.xhtml | 17 +++++++++++++++++ test/results/tag_parsing.xhtml | 28 +++++++++++++++++++++++++++ test/template_test.rb | 2 +- test/templates/just_stuff.haml | 12 ++++++++++++ test/templates/tag_parsing.haml | 24 +++++++++++++++++++++++ 9 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 test/results/just_stuff.xhtml create mode 100644 test/results/tag_parsing.xhtml create mode 100644 test/templates/just_stuff.haml create mode 100644 test/templates/tag_parsing.haml diff --git a/REFERENCE b/REFERENCE index d20674f6..3ac7485e 100644 --- a/REFERENCE +++ b/REFERENCE @@ -271,6 +271,37 @@ is compiled to: I like billabongs! + +The forward slash can also wrap indented sections of code. For example: + + / + %p This doesn't render... + %div + %h1 Because it's commented out! + +is compiled to: + + + +You can also use Interet Explorer conditional comments (about)[http://www.quirksmode.org/css/condcom.html] +by enclosing the condition in square brackets after the /. For example: + + /[if IE] + %a{ :href => 'http://www.mozilla.com/en-US/firefox/' } + %h1 Get Firefox + +is compiled to: + + ==== | diff --git a/Rakefile b/Rakefile index 3dc87b8e..d63442e3 100644 --- a/Rakefile +++ b/Rakefile @@ -49,7 +49,7 @@ end rdoc_task = Proc.new do |rdoc| rdoc.title = 'Haml' rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('REFERENCE') rdoc.rdoc_files.include('lib/**/*.rb') rdoc.rdoc_files.exclude('lib/haml/buffer.rb') end diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index 1232a2a5..67527c6d 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -70,7 +70,6 @@ module Haml end end - # Creates a closing tag with the given name. def close_tag(name, tabulation) if @one_liner_pending @@ -81,6 +80,28 @@ module Haml end end + # Opens an XHTML comment. + def open_comment(try_one_line, conditional, tabulation) + conditional << ">" if conditional + @buffer << "#{tabs(tabulation)}" : "-->" + if @one_liner_pending + @buffer << " #{close_tag}\n" + @one_liner_pending = false + else + push_text(close_tag, tabulation) + end + end + private # Gets count tabs. Mostly for internal use. diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 6cd7b349..c621ba1f 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -163,7 +163,7 @@ module Haml # The indentation was increased after silent script, # it must be a block - @to_close_stack.push '_haml_end_block' + @to_close_stack.push [:script] end elsif count <= @to_close_stack.size && @to_close_stack.size > 0 @@ -332,11 +332,14 @@ module Haml # Closes the most recent item in @to_close_stack. def close - tag = @to_close_stack.pop - if tag == '_haml_end_block' + tag, value = @to_close_stack.pop + case tag + when :script close_block - else - close_tag tag + when :comment + close_comment value + when :element + close_tag value end end @@ -351,6 +354,12 @@ module Haml def close_block push_silent "end" end + + # Closes a comment. + def close_comment(has_conditional) + @tabulation -= 1 + push_silent "_hamlout.close_comment(#{has_conditional}, #{@tabulation})" + end # Parses a line that will render as an XHTML tag, and adds the code that will # render that tag to @precompiled. @@ -372,10 +381,10 @@ module Haml attributes_hash = "nil" unless attributes_hash object_ref = "nil" unless object_ref - @precompiled << "_hamlout.open_tag(#{tag_name.inspect}, #{@tabulation}, #{atomic.inspect}, #{value_exists.inspect}, #{attributes.inspect}, #{attributes_hash}, #{object_ref})\n" + push_silent "_hamlout.open_tag(#{tag_name.inspect}, #{@tabulation}, #{atomic.inspect}, #{value_exists.inspect}, #{attributes.inspect}, #{attributes_hash}, #{object_ref})\n" unless atomic - @to_close_stack.push tag_name + @to_close_stack.push [:element, tag_name] @tabulation += 1 if value_exists @@ -398,7 +407,16 @@ module Haml # Renders an XHTML comment. def render_comment(line) - push_text "" + conditional, content = line.scan(/\/(\[[a-zA-Z0-9 ]*\])?(.*)/)[0] + content = content.strip + try_one_line = !content.empty? + push_silent "_hamlout.open_comment(#{try_one_line}, #{conditional.inspect}, #{@tabulation})" + @tabulation += 1 + @to_close_stack.push [:comment, !conditional.nil?] + if try_one_line + push_text content + close + end end end end diff --git a/test/results/just_stuff.xhtml b/test/results/just_stuff.xhtml new file mode 100644 index 00000000..b3f3be80 --- /dev/null +++ b/test/results/just_stuff.xhtml @@ -0,0 +1,17 @@ + + + + + diff --git a/test/results/tag_parsing.xhtml b/test/results/tag_parsing.xhtml new file mode 100644 index 00000000..c32373af --- /dev/null +++ b/test/results/tag_parsing.xhtml @@ -0,0 +1,28 @@ +
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 +
+
+

+

+
a
+
b
+
c
+
d
+
e
+
f
+
g
+
+
+ <{ :a => :b } +
>{ :c => :d }
+
diff --git a/test/template_test.rb b/test/template_test.rb index ffeab294..4ae8e93c 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -39,7 +39,7 @@ class TemplateTest < Test::Unit::TestCase def test_templates_should_render_correctly %w{very_basic standard helpers whitespace_handling original_engine list helpful silent_script - tag_parsing}.each do |template| + tag_parsing just_stuff}.each do |template| assert_renders_correctly template end end diff --git a/test/templates/just_stuff.haml b/test/templates/just_stuff.haml new file mode 100644 index 00000000..852c5af6 --- /dev/null +++ b/test/templates/just_stuff.haml @@ -0,0 +1,12 @@ +/ Short comment +/ This is a really long comment look how long it is it should be on a line of its own don't you think? +/ + This is a block comment + cool, huh? + %strong there can even be sub-tags! + = "Or script!" +/[if lte IE6] conditional comment! +/[if gte IE7] + %p Block conditional comment + %div + %h1 Cool, eh? diff --git a/test/templates/tag_parsing.haml b/test/templates/tag_parsing.haml new file mode 100644 index 00000000..728a7380 --- /dev/null +++ b/test/templates/tag_parsing.haml @@ -0,0 +1,24 @@ +%div.tags + %foo 1 + %FOO 2 + %fooBAR 3 + %fooBar 4 + %foo_bar 5 + %foo-bar 6 + %foo:bar 7 + %foo.bar 8 + %fooBAr_baz:boom_bar 9 + %foo13 10 + %foo2u 11 +%div.classes + %p.foo.bar#baz#boom + .fooBar a + .foo-bar b + .foo_bar c + .FOOBAR d + .foo16 e + .123 f + .foo2u g +%div.broken + %foo<{ :a => :b } + .foo>{ :c => :d }