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
This commit is contained in:
nex3 2006-10-22 21:42:45 +00:00
parent fe1d80b6a1
commit a99fd555c7
9 changed files with 162 additions and 11 deletions

View File

@ -271,6 +271,37 @@ is compiled to:
<!-- This is the billabong element -->
I like billabongs!
</billabong>
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:
<!--
<p>This doesn't render...</p>
<div>
<h1>Because it's commented out!</h1>
</div>
-->
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 <tt>/</tt>. For example:
/[if IE]
%a{ :href => 'http://www.mozilla.com/en-US/firefox/' }
%h1 Get Firefox
is compiled to:
<!--[if IE]>
<a href='http://www.mozilla.com/en-US/firefox/'>
<h1>Get Firefox</h1>
</a>
<![endif]-->
==== |

View File

@ -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

View File

@ -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)}<!--#{conditional.to_s} "
if try_one_line
@one_liner_pending = true
else
@buffer << "\n"
end
end
# Closes an XHTML comment.
def close_comment(has_conditional, tabulation)
close_tag = has_conditional ? "<![endif]-->" : "-->"
if @one_liner_pending
@buffer << " #{close_tag}\n"
@one_liner_pending = false
else
push_text(close_tag, tabulation)
end
end
private
# Gets <tt>count</tt> tabs. Mostly for internal use.

View File

@ -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 <tt>@to_close_stack</tt>.
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 <tt>@precompiled</tt>.
@ -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 "<!-- #{line[1..line.length].strip} -->"
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

View File

@ -0,0 +1,17 @@
<!-- 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!</strong>
Or script!
-->
<!--[if lte IE6]> conditional comment! <![endif]-->
<!--[if gte IE7]>
<p>Block conditional comment</p>
<div>
<h1>Cool, eh?</h1>
</div>
<![endif]-->

View File

@ -0,0 +1,28 @@
<div class='tags'>
<foo>1</foo>
<FOO>2</FOO>
<fooBAR>3</fooBAR>
<fooBar>4</fooBar>
<foo_bar>5</foo_bar>
<foo-bar>6</foo-bar>
<foo:bar>7</foo:bar>
<foo class='bar'>8</foo>
<fooBAr_baz:boom_bar>9</fooBAr_baz:boom_bar>
<foo13>10</foo13>
<foo2u>11</foo2u>
</div>
<div class='classes'>
<p class='foo bar' id='boom'>
</p>
<div class='fooBar'>a</div>
<div class='foo-bar'>b</div>
<div class='foo_bar'>c</div>
<div class='FOOBAR'>d</div>
<div class='foo'>e</div>
<div>f</div>
<div class='foo'>g</div>
</div>
<div class='broken'>
<foo><{ :a => :b }</foo>
<div class='foo'>>{ :c => :d }</div>
</div>

View File

@ -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

View File

@ -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?

View File

@ -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 }