From 68104fd0bc89e4fbbaf300c6e51b8f6849cadac1 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Wed, 28 May 2008 20:09:46 -0700 Subject: [PATCH] Make the Haml parser a little more flexible. --- lib/haml/engine.rb | 3 ++- lib/haml/precompiler.rb | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 87fca1c3..b362b359 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -73,7 +73,8 @@ module Haml raise Haml::Error, "Invalid format #{@options[:format].inspect}" end - @template = template.rstrip + "\n-#\n-#" + @template = (template.rstrip + "\n-#\n-#").split(/\r\n|\r|\n/) + @template_index = 0 @to_close_stack = [] @output_tabs = 0 @template_tabs = 0 diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index e93682ae..a1bdff02 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -113,10 +113,7 @@ END def precompile old_line = Line.new - @template.split(/\r\n|\r|\n/).each_with_index do |text, index| - line = Line.new text.strip, text.lstrip.chomp, index - line.spaces, line.tabs = count_soft_tabs(text) - + while line = next_line if line.text.empty? process_indent(old_line) if flat? && !old_line.text.empty? @@ -713,6 +710,23 @@ END @block_opened = false end + def raw_next_line + text = @template.shift + return unless text + + index = @template_index + @template_index += 1 + + return text, index + end + + def next_line + text, index = raw_next_line + return unless text + + Line.new text.strip, text.lstrip.chomp, index, *count_soft_tabs(text) + end + def contains_interpolation?(str) str.include?('#{') end