This teaches the Haml::Engine to accept a :tab option (defaults to "  ",
two spaces), which can be used to override what kind of tab is used to
indent the output.

Obvious applications are emitting real tabs ("\t"), 4-space or 8-space
tabs, or even no tabs at all (0 spaces). As such I actually think this
is a superior solution to the ":ugly" implementation I posted earlier to
the Haml group, as it is more flexible.

Running the benchmark suite before and after this change show that it
has no impact on performance; I saw only about 1/100th second of jitter
(slower or faster) on most of the benchmarks.

Signed-off-by: Wincent Colaiuta <[EMAIL PROTECTED]>
---

Based on the conversation in the other thread, I actually think I like
this solution better than the one I posted earlier.

 lib/haml/buffer.rb       |    8 ++++----
 lib/haml/engine.rb       |    8 ++++++--
 lib/haml/precompiler.rb  |    4 ++--
 test/haml/engine_test.rb |   35 +++++++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb
index 4bdac1d..e338577 100644
--- a/lib/haml/buffer.rb
+++ b/lib/haml/buffer.rb
@@ -32,7 +32,8 @@ module Haml
     # Creates a new buffer.
     def initialize(options = {})
       @options = {
-        :attr_wrapper => "'"
+        :attr_wrapper => "'",
+        :tab => '  '
       }.merge options
       @buffer = ""
       @tabulation = 0
@@ -47,7 +48,7 @@ module Haml
     def push_text(text, tab_change = 0)
       if(@tabulation > 0)
         # Have to push every line in by the extra user set tabulation
-        text.gsub!(/^/m, '  ' * @tabulation)
+        text.gsub!(/^/m, @options[:tab] * @tabulation)
       end
       
       @buffer << "#{text}"
@@ -152,8 +153,7 @@ module Haml
     # Gets <tt>count</tt> tabs. Mostly for internal use.
     def tabs(count)
       tabs = count + @tabulation
-      '  ' * tabs
-      @@tab_cache[tabs] ||= '  ' * tabs
+      @@tab_cache[tabs] ||= @options[:tab] * tabs
     end
 
     # Takes an array of objects and uses the class and id of the first
diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb
index b54fc38..5a32687 100644
--- a/lib/haml/engine.rb
+++ b/lib/haml/engine.rb
@@ -45,7 +45,8 @@ module Haml
           'redcloth' => Haml::Filters::RedCloth,
           'textile' => Haml::Filters::Textile,
           'markdown' => Haml::Filters::Markdown },
-        :filename => '(haml)'
+        :filename => '(haml)',
+        :tab => '  '
       }
       @options.rec_merge! options
 
@@ -232,7 +233,10 @@ END
     # Returns a hash of options that Haml::Buffer cares about.
     # This should remain loadable form #inspect.
     def options_for_buffer
-      {:attr_wrapper => @options[:attr_wrapper]}
+      {
+        :attr_wrapper => @options[:attr_wrapper],
+        :tab => @options[:tab]
+      }
     end
   end
 end
diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb
index 3625356..fad3dac 100644
--- a/lib/haml/precompiler.rb
+++ b/lib/haml/precompiler.rb
@@ -270,7 +270,7 @@ END
     # Adds <tt>text</tt> to <tt>@buffer</tt> with appropriate tabulation
     # without parsing it.
     def push_merged_text(text, tab_change = 0, try_one_liner = false)
-      @merged_text  << "#{'  ' * @output_tabs}#{text}"
+      @merged_text  << "[EMAIL PROTECTED]:tab] * @output_tabs}#{text}"
       @tab_change   += tab_change
       @try_one_liner = try_one_liner
     end
@@ -389,7 +389,7 @@ END
       if filter == Haml::Filters::Preserve
         push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\";")
       else
-        push_text(filtered.rstrip.gsub("\n", "\n#{'  ' * @output_tabs}"))
+        push_text(filtered.rstrip.gsub("\n", "[EMAIL PROTECTED]:tab] * 
@output_tabs}"))
       end
 
       @filter_buffer = nil
diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb
index a178ecc..c3f5b85 100644
--- a/test/haml/engine_test.rb
+++ b/test/haml/engine_test.rb
@@ -371,4 +371,39 @@ class EngineTest < Test::Unit::TestCase
   def test_render_proc_with_binding
     assert_equal("FOO\n", Haml::Engine.new("= 
upcase").render_proc("foo".instance_eval{binding}).call)
   end
+
+  def test_no_space_tab
+    input     = "#outer\n  #inner\n    %p hello world"
+    actual    = Haml::Engine.new(input, :tab => '').render
+    expected  = "<div id='outer'>\n<div id='inner'>\n<p>hello 
world</p>\n</div>\n</div>\n"
+    assert_equal(expected, actual)
+  end
+
+  def test_two_space_tab
+    input     = "#outer\n  #inner\n    %p hello world"
+    actual    = Haml::Engine.new(input, :tab => '  ').render
+    expected  = "<div id='outer'>\n  <div id='inner'>\n    <p>hello 
world</p>\n  </div>\n</div>\n"
+    assert_equal(expected, actual)
+  end
+
+  def test_two_space_tab
+    input     = "#outer\n  #inner\n    %p hello world"
+    actual    = Haml::Engine.new(input, :tab => "\t").render
+    expected  = "<div id='outer'>\n\t<div id='inner'>\n\t\t<p>hello 
world</p>\n\t</div>\n</div>\n"
+    assert_equal(expected, actual)
+  end
+
+  def test_comment_tab
+    input     = "#outer\n  #inner\n    %p hello world"
+    actual    = Haml::Engine.new(input, :tab => "<!-- tab -->").render
+    expected  = <<-END
+      <div id='outer'>
+      <!-- tab --><div id='inner'>
+      <!-- tab --><!-- tab --><p>hello world</p>
+      <!-- tab --></div>
+      </div>
+    END
+    assert_equal(expected.gsub(/^ {6}/, ''), actual)
+  end
+
 end
-- 
1.5.4.1-dirty


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Haml" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/haml?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to