Closes ticket #6 and means that multiline-pipe based comments are added.

I also broke up the render method a bit to keep it from becoming a *wee* bit too big.

Love,
Hampton.



git-svn-id: svn://hamptoncatlin.com/haml/trunk@47 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
hcatlin 2006-09-22 02:18:43 +00:00
parent e330b4ffc5
commit 8e245a6de7
4 changed files with 68 additions and 25 deletions

View File

@ -8,6 +8,7 @@ module Haml #:nodoc:
# Lines <= the maximum will be rendered on one line,
# i.e. <tt><p>Hello world</p></tt>
ONE_LINER_LENGTH = 50
MULTILINE_CHAR_VALUE = '|'[0]
def initialize(view)
@view = view
@ -38,29 +39,11 @@ module Haml #:nodoc:
# Process each line of the template returning the resulting string
template.each_with_index do |line, index|
count, line = count_soft_tabs(line)
if count && line
if line.strip[0, 3] == '!!!'
@result << %|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n|
else
if count <= @to_close_queue.size && @to_close_queue.size > 0
(@to_close_queue.size - count).times { close_tag }
end
case line.first
when '.', '#'
render_div(line)
when '%'
render_tag(line)
when '/'
render_comment(line)
when '='
add template_eval(line[1, line.length]).to_s
when '~'
add find_and_flatten(template_eval(line[1, line.length])).to_s
else
add line.strip
end
end
surpress_render, line, count = handle_multiline(count, line)
if !surpress_render && count && line
count, line = process_line(count, line)
end
end
@ -71,6 +54,52 @@ module Haml #:nodoc:
@result
end
def process_line(count, line)
if line.strip[0, 3] == '!!!'
@result << %|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n|
else
if count <= @to_close_queue.size && @to_close_queue.size > 0
(@to_close_queue.size - count).times { close_tag }
end
case line.first
when '.', '#'
render_div(line)
when '%'
render_tag(line)
when '/'
render_comment(line)
when '='
add template_eval(line[1, line.length]).to_s
when '~'
add find_and_flatten(template_eval(line[1, line.length])).to_s
else
add line.strip
end
end
return count, line
end
def handle_multiline(count, line)
# The code to handle how a multi-line object should work.
if @multiline_buffer && line[-1] == MULTILINE_CHAR_VALUE # '|' is 124
# A multiline string is active, and is being continued
@multiline_buffer += line[0...-1]
supress_render = true
elsif line[-1] == MULTILINE_CHAR_VALUE
# A multiline string has just been activated, start adding the lines
@multiline_buffer = line[0...-1]
@multiline_count = count
supress_render = true
elsif @multiline_buffer
# A multiline string has just ended, make line into the result
process_line(@multiline_count, @multiline_buffer)
@multiline_buffer = nil
supress_render = false
end
return supress_render, line, count
end
def add(line)
return if line.nil?
line.to_s.each_line do |me|
@ -132,7 +161,7 @@ module Haml #:nodoc:
class_name = object_ref.class.to_s.underscore
attributes.merge!(:id => "#{class_name}_#{object_ref.id}", :class => class_name)
end
if action == '/'
atomic_tag(tag_name, attributes)
elsif action == '=' || action == '~'

View File

@ -12,7 +12,7 @@ class HamlTest < Test::Unit::TestCase
def setup
ActionView::Base.register_template_handler("haml", Haml::Engine)
@base = ActionView::Base.new(File.dirname(__FILE__) + "/../test/templates/")
@base = ActionView::Base.new(File.dirname(__FILE__) + "/templates/")
@engine = Haml::Engine.new(@base)
@base.instance_variable_set("@article", Article.new)
end

View File

@ -13,6 +13,11 @@
20
</div>
<div id='body'> Quotes should be loved! Just like people!</div>
<p>
Holy cow multiline tags! A pipe (|) even!
Wow.
1|2|3
</p>
<div class='of_divs_with_underscore' id='combo'>with this text</div>
<div class='footer'>
<strong class='shout'>

View File

@ -11,6 +11,15 @@
The question is if this would translate! Ahah!
= 1 + 9 + 8 + 2 #numbers should work and this should be ignored
#body= " Quotes should be loved! Just like people!"
%p
= "Holy cow " + |
"multiline " + |
"tags! " + |
"A pipe (|) even!" |
Wow.
= [1, 2, 3].collect { |n| |
n.to_s |
}.join("|") |
#combo.of_divs_with_underscore= @should_eval = "with this text"
.footer
%strong.shout= "This is a really long ruby quote. It should be loved and wrapped because its more than 50 characters. This value may change in the future and this test may look stupid. \nSo, I'm just making it *really* long. God, I hope this works"