Adds an "ugly" option to the engine to suppress all indentation.

The "haml" command-line tool has also learned a -t/--style switch:
when set to "ugly" passes the option through to the engine; any
other value is ignored.

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

Ok, this time with tests. Everything passes.

 lib/haml/buffer.rb       |   18 ++++++++++++------
 lib/haml/engine.rb       |    8 ++++++--
 lib/haml/exec.rb         |    5 +++++
 lib/haml/precompiler.rb  |   14 ++++++++++----
 test/haml/engine_test.rb |   14 ++++++++++++++
 5 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb
index 4bdac1d..edacc70 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 => "'",
+        :ugly => false
       }.merge options
       @buffer = ""
       @tabulation = 0
@@ -45,7 +46,7 @@ module Haml
     # Renders +text+ with the proper tabulation. This also deals with
     # making a possible one-line tag one line or not.
     def push_text(text, tab_change = 0)
-      if(@tabulation > 0)
+      if(@tabulation > 0 && [EMAIL PROTECTED]:ugly])
         # Have to push every line in by the extra user set tabulation
         text.gsub!(/^/m, '  ' * @tabulation)
       end
@@ -78,11 +79,11 @@ module Haml
           @buffer << "\n"
         end
         
-        result = result.gsub(/^/m, tabs(tabulation))
+        (result = result.gsub(/^/m, tabs(tabulation))) unless @options[:ugly]
         @buffer << "#{result}\n"
         
         if close_tag
-          @buffer << "#{tabs(tabulation-1)}</#{close_tag}>\n"
+          @buffer << (@options[:ugly] ? "</#{close_tag}>\n" : 
"#{tabs(tabulation-1)}</#{close_tag}>\n")
           @real_tabs -= 1
         end
       end
@@ -108,10 +109,16 @@ module Haml
       else
         str = ">\n"
       end
-      @buffer << 
"#{tabs(tabulation)}<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper],
 attributes)}#{str}"
+      if @options[:ugly]
+        @buffer << 
"<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], 
attributes)}#{str}"
+      else
+        @buffer << 
"#{tabs(tabulation)}<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper],
 attributes)}#{str}"
+      end
       if content
         if Buffer.one_liner?(content)
           @buffer << "#{content}</#{name}>\n"
+        elsif @options[:ugly]
+          @buffer << "\n#{content}\n</#{name}>\n"
         else
           @buffer << 
"\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}</#{name}>\n"
         end
@@ -152,7 +159,6 @@ module Haml
     # Gets <tt>count</tt> tabs. Mostly for internal use.
     def tabs(count)
       tabs = count + @tabulation
-      '  ' * tabs
       @@tab_cache[tabs] ||= '  ' * tabs
     end
 
diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb
index b54fc38..97d0740 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)',
+        :ugly => false
       }
       @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],
+        :ugly => @options[:ugly]
+      }
     end
   end
 end
diff --git a/lib/haml/exec.rb b/lib/haml/exec.rb
index e1ed7a2..0c01d6c 100644
--- a/lib/haml/exec.rb
+++ b/lib/haml/exec.rb
@@ -144,6 +144,11 @@ END
           exit
         end
 
+        opts.on('-t', '--style NAME',
+                'Output style. Can be indented (default) or ugly.') do |name|
+          @options[:for_engine][:ugly] = true if name.to_sym == :ugly
+        end
+
         opts.on('-c', '--check', "Just check syntax, don't evaluate.") do
           @options[:check_syntax] = true
           @options[:output] = StringIO.new
diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb
index 3625356..7daeee8 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  << (@options[:ugly] ? text : "#{'  ' * 
@output_tabs}#{text}")
       @tab_change   += tab_change
       @try_one_liner = try_one_liner
     end
@@ -299,9 +299,13 @@ END
 
     # Adds +text+ to <tt>@buffer</tt> while flattening text.
     def push_flat(line)
-      tabulation = line.spaces - @flat_spaces
-      tabulation = tabulation > -1 ? tabulation : 0
-      @filter_buffer << "#{' ' * tabulation}#{line.unstripped}\n"
+      unless @options[:ugly]
+        tabulation = line.spaces - @flat_spaces
+        tabulation = tabulation > -1 ? tabulation : 0
+        @filter_buffer << "#{' ' * tabulation}#{line.unstripped}\n"
+      else
+        @filter_buffer << "#{line.unstripped}\n"
+      end
     end
 
     # Causes <tt>text</tt> to be evaluated in the context of
@@ -388,6 +392,8 @@ END
 
       if filter == Haml::Filters::Preserve
         push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\";")
+      elsif @options[:ugly]
+        push_text(filtered.rstrip)
       else
         push_text(filtered.rstrip.gsub("\n", "\n#{'  ' * @output_tabs}"))
       end
diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb
index a178ecc..5244024 100644
--- a/test/haml/engine_test.rb
+++ b/test/haml/engine_test.rb
@@ -371,4 +371,18 @@ 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_ugly_true
+    input     = "#outer\n  #inner\n    %p hello world"
+    actual    = Haml::Engine.new(input, :ugly => true).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_ugly_false
+    input     = "#outer\n  #inner\n    %p hello world"
+    actual    = Haml::Engine.new(input, :ugly => false).render
+    expected  = "<div id='outer'>\n  <div id='inner'>\n    <p>hello 
world</p>\n  </div>\n</div>\n"
+    assert_equal(expected, 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