From 331f0247aeae646e3b9c7d6060b9f7252692ce7d Mon Sep 17 00:00:00 2001 From: hcatlin Date: Tue, 8 Aug 2006 21:38:50 +0000 Subject: [PATCH] We have 100% passing. Simplified (in a way I didn't initially want) the flattening procedure. However, it helps the tests pass and that's a Good Thing. Now for speed tests and playing with globalize. git-svn-id: svn://hamptoncatlin.com/haml/trunk@17 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/engine.rb | 23 ++++++----------------- lib/haml/helpers.rb | 19 +++++++++++++------ test/engine_test.rb | 8 ++++---- test/helper_test.rb | 7 +++++++ test/results/whitespace_handling.xhtml | 20 +++++++++++++------- 5 files changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 1cda4166..3dbab926 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -10,19 +10,14 @@ module HAML def initialize(base) @base = base - @tab_index = ["", " "] @happy_land = HappyLand.new(@base, @base.assigns) - #pre-build the tab index up to 9 - 20.times do |num| - @tab_index << @tab_index.last + " " - end end def render(template = "", locals = {}) - @result = "" - @to_close_queue = [] - + @result, @to_close_queue = "", [] + #this helps get the right values for helpers. + #though, it is definitely in the "hack" category @base.assigns.each do |key,value| @base.instance_eval("@#{key} = value") end @@ -35,7 +30,7 @@ module HAML if line.strip[0, 3] == "!!!" @result << %|\n| else - count, line = count_levels(line) + count, line = count_soft_tabs(line) #puts count.to_s + "::" + line if count <= @to_close_queue.size && @to_close_queue.size > 0 (@to_close_queue.size - count).times { close_tag } @@ -67,7 +62,7 @@ module HAML end def add_single(line = "") - @result << @tab_index[@to_close_queue.size] + @result << tabs(@to_close_queue.size) @result << line.chomp + "\n" end @@ -149,18 +144,12 @@ module HAML end attributes end - - def count_levels(line) - [line.index(/[^ ]/)/2, line.strip] - end - + def one_liner?(value) ((value.length < 50) && value.scan(/\n/).empty?) end def template_eval(code) - #@base.instance_eval(code) - #render :inline => "<%=#{code}%>" @happy_land.instance_eval(code) end diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 15e7bf82..4433d8b8 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -13,12 +13,19 @@ module HAMLHelpers end def find_and_flatten(input) - s, result = StringScanner.new(input), "" - while s.scan_until(/<(textarea|code|pre)[^>]*>/i) - result += s.pre_match.to_s + s.matched - tag_name = s.matched.scan(/(textarea|code|pre)/).first - result += flatten(s.scan_until(/<\/#{tag_name}>/i)) + sets = input.scan(/<(textarea|code|pre)[^>]*>(.*?)<\/\1>/im) + sets.each do |thing| + input = input.gsub(thing[1], flatten(thing[1])) end - result + s.rest + input end + + def tabs(count) + " " * count + end + + def count_soft_tabs(line) + [line.index(/[^ ]/)/2, line.strip] + end + end diff --git a/test/engine_test.rb b/test/engine_test.rb index 060bd5e2..2d8a3492 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -20,11 +20,11 @@ class HamlTest < Test::Unit::TestCase end def assert_renders_correctly(name) - #tupleize(load_result(name).scan(/\n/), @base.render(name).scan(/\n/)).each do |pair| + tupleize(load_result(name).scan(/\n/), @base.render(name).scan(/\n/)).each do |pair| #test each line to make sure it matches... (helps with error messages to do them seperately) - # assert_equal(pair.first, pair.last) - #end - assert_equal(load_result(name), @base.render(name)) + assert_equal(pair.first, pair.last) + end + #assert_equal(load_result(name), @base.render(name)) end # Make sure our little environment builds diff --git a/test/helper_test.rb b/test/helper_test.rb index d75ce04c..12ae11fe 100644 --- a/test/helper_test.rb +++ b/test/helper_test.rb @@ -22,5 +22,12 @@ class HamlTest < Test::Unit::TestCase find_and_flatten("
\n \n
\n")) assert_equal( "Two lines
a
b
c
", find_and_flatten("Two\nlines
a\nb\nc
")) + assert_equal( "
Two
lines
\n
a
b
c
", + find_and_flatten("
Two\nlines
\n
a\nb\nc
")) + end + + def tabs + assert_equals(" ", tabs(1)) + assert_equals(" ", tabs(5)) end end diff --git a/test/results/whitespace_handling.xhtml b/test/results/whitespace_handling.xhtml index e686f7f4..a49b95ed 100644 --- a/test/results/whitespace_handling.xhtml +++ b/test/results/whitespace_handling.xhtml @@ -2,19 +2,25 @@
-
- -
-
- -
+
+ +
+ +
+ +
+ +
+
+