Cleaning up Precompiler#process_line

git-svn-id: svn://hamptoncatlin.com/haml/trunk@672 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-11-25 20:20:44 +00:00
parent d483dd348a
commit 2f9ab9b106
2 changed files with 37 additions and 57 deletions

View File

@ -187,57 +187,39 @@ END
# #
# This method doesn't return anything; it simply processes the line and # This method doesn't return anything; it simply processes the line and
# adds the appropriate code to <tt>@precompiled</tt>. # adds the appropriate code to <tt>@precompiled</tt>.
def process_line(line, index, block_opened) def process_line(text, index, block_opened)
@index = index + 1 @index = index + 1
@block_opened = block_opened @block_opened = block_opened
case line[0] case text[0]
when DIV_CLASS, DIV_ID when DIV_CLASS, DIV_ID: render_div(text)
render_div(line) when ELEMENT: render_tag(text)
when ELEMENT when COMMENT: render_comment(text)
render_tag(line)
when COMMENT
render_comment(line)
when SCRIPT when SCRIPT
sub_line = line[1..-1] return push_script(unescape_interpolation(text[2..-1].strip), false) if text[1] == SCRIPT
if sub_line[0] == SCRIPT push_script(text[1..-1], false)
push_script(unescape_interpolation(sub_line[1..-1].strip), false) when FLAT_SCRIPT: push_flat_script(text[1..-1])
else
push_script(sub_line, false)
end
when FLAT_SCRIPT
push_flat_script(line[1..-1])
when SILENT_SCRIPT when SILENT_SCRIPT
sub_line = line[1..-1] return start_haml_comment if text[1] == SILENT_COMMENT
unless sub_line[0] == SILENT_COMMENT
mbk = mid_block_keyword?(line) mbk = mid_block_keyword?(text)
push_silent(sub_line, !mbk, true) push_silent(text[1..-1], !mbk, true)
if (@block_opened && !mbk) || line[1..-1].split(' ', 2)[0] == "case" if (@block_opened && !mbk) || text[1..-1].split(' ', 2)[0] == "case"
push_and_tabulate([:script]) push_and_tabulate([:script])
end end
else when FILTER: start_filtered(text[1..-1].downcase)
start_haml_comment
end
when FILTER
name = line[1..-1].downcase
start_filtered(options[:filters][name.to_s] || name)
when DOCTYPE when DOCTYPE
if line[0...3] == '!!!' return render_doctype(text) if text[0...3] == '!!!'
render_doctype(line) push_plain text
else when ESCAPE: push_plain text[1..-1]
push_plain line else push_plain text
end
when ESCAPE
push_plain line[1..-1]
else
push_plain line
end end
end end
# Returns whether or not the line is a silent script line with one # Returns whether or not the text is a silent script text with one
# of Ruby's mid-block keywords. # of Ruby's mid-block keywords.
def mid_block_keyword?(line) def mid_block_keyword?(text)
line.length > 2 && line[0] == SILENT_SCRIPT && MID_BLOCK_KEYWORDS.include?(line[1..-1].split[0]) text.length > 2 && text[0] == SILENT_SCRIPT && MID_BLOCK_KEYWORDS.include?(text[1..-1].split[0])
end end
# Deals with all the logic of figuring out whether a given line is # Deals with all the logic of figuring out whether a given line is
@ -420,13 +402,6 @@ END
# Closes a filtered block. # Closes a filtered block.
def close_filtered(filter) def close_filtered(filter)
@flat_spaces = -1 @flat_spaces = -1
if filter.is_a? String
if filter == 'redcloth' || filter == 'markdown' || filter == 'textile'
raise HamlError.new("You must have the RedCloth gem installed to use #{filter}")
else
raise HamlError.new("Filter \"#{filter}\" is not defined!")
end
else
filtered = filter.new(@filter_buffer).render filtered = filter.new(@filter_buffer).render
unless filter == Haml::Filters::Preserve unless filter == Haml::Filters::Preserve
@ -434,7 +409,6 @@ END
else else
push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\"\n") push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\"\n")
end end
end
@filter_buffer = nil @filter_buffer = nil
@template_tabs -= 1 @template_tabs -= 1
@ -687,10 +661,16 @@ END
end end
# Starts a filtered block. # Starts a filtered block.
def start_filtered(filter) def start_filtered(name)
unless @block_opened raise SyntaxError.new('Filters must have nested text.') unless @block_opened
raise SyntaxError.new('Filters must have nested text.')
unless filter = options[:filters][name]
if filter == 'redcloth' || filter == 'markdown' || filter == 'textile'
raise HamlError.new("You must have the RedCloth gem installed to use \"#{name}\" filter")
end end
raise HamlError.new("\"#{name}\" filter is not defined!")
end
push_and_tabulate([:filtered, filter]) push_and_tabulate([:filtered, filter])
@flat_spaces = @template_tabs * 2 @flat_spaces = @template_tabs * 2
@filter_buffer = String.new @filter_buffer = String.new

View File

@ -106,7 +106,7 @@ class EngineTest < Test::Unit::TestCase
assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true)) assert_equal("", render(":ruby\n puts 'hello'", :suppress_eval => true))
rescue Haml::HamlError => err rescue Haml::HamlError => err
caught = true caught = true
assert_equal('Filter "ruby" is not defined!', err.message) assert_equal('"ruby" filter is not defined!', err.message)
end end
assert(caught, "Rendering a ruby filter without evaluating didn't throw an error!") assert(caught, "Rendering a ruby filter without evaluating didn't throw an error!")
end end