public
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/nex3/haml.git
Search Repo:
Add "ugly" option to Haml::Engine

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 <win@wincent.com>
wincent (author)
Wed Feb 13 01:30:21 -0800 2008
nex3 (committer)
Wed Feb 13 18:23:06 -0800 2008
commit  f7f11e202b55c5043f77160bf3ecc63b37b66691
tree    da0f1ab32b58ffb4a6b83ab9b1460064092b1e5f
parent  9bf42750cd5f241b9bf83d5ee1a8c58dc58a30ae
...
32
33
34
35
 
 
36
37
38
...
45
46
47
48
 
49
50
51
...
78
79
80
81
 
82
83
84
85
 
86
87
88
...
108
109
110
111
 
 
 
 
 
112
113
114
 
 
115
116
117
...
152
153
154
155
156
157
158
...
32
33
34
 
35
36
37
38
39
...
46
47
48
 
49
50
51
52
...
79
80
81
 
82
83
84
85
 
86
87
88
89
...
109
110
111
 
112
113
114
115
116
117
118
119
120
121
122
123
124
...
159
160
161
 
162
163
164
0
@@ -32,7 +32,8 @@ module Haml
0
     # Creates a new buffer.
0
     def initialize(options = {})
0
       @options = {
0
- :attr_wrapper => "'"
0
+ :attr_wrapper => "'",
0
+ :ugly => false
0
       }.merge options
0
       @buffer = ""
0
       @tabulation = 0
0
@@ -45,7 +46,7 @@ module Haml
0
     # Renders +text+ with the proper tabulation. This also deals with
0
     # making a possible one-line tag one line or not.
0
     def push_text(text, tab_change = 0)
0
- if(@tabulation > 0)
0
+ if(@tabulation > 0 && !@options[:ugly])
0
         # Have to push every line in by the extra user set tabulation
0
         text.gsub!(/^/m, ' ' * @tabulation)
0
       end
0
@@ -78,11 +79,11 @@ module Haml
0
           @buffer << "\n"
0
         end
0
         
0
- result = result.gsub(/^/m, tabs(tabulation))
0
+ (result = result.gsub(/^/m, tabs(tabulation))) unless @options[:ugly]
0
         @buffer << "#{result}\n"
0
         
0
         if close_tag
0
- @buffer << "#{tabs(tabulation-1)}</#{close_tag}>\n"
0
+ @buffer << (@options[:ugly] ? "</#{close_tag}>\n" : "#{tabs(tabulation-1)}</#{close_tag}>\n")
0
           @real_tabs -= 1
0
         end
0
       end
0
@@ -108,10 +109,16 @@ module Haml
0
       else
0
         str = ">\n"
0
       end
0
- @buffer << "#{tabs(tabulation)}<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{str}"
0
+ if @options[:ugly]
0
+ @buffer << "<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{str}"
0
+ else
0
+ @buffer << "#{tabs(tabulation)}<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{str}"
0
+ end
0
       if content
0
         if Buffer.one_liner?(content)
0
           @buffer << "#{content}</#{name}>\n"
0
+ elsif @options[:ugly]
0
+ @buffer << "\n#{content}\n</#{name}>\n"
0
         else
0
           @buffer << "\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}</#{name}>\n"
0
         end
0
@@ -152,7 +159,6 @@ module Haml
0
     # Gets <tt>count</tt> tabs. Mostly for internal use.
0
     def tabs(count)
0
       tabs = count + @tabulation
0
- ' ' * tabs
0
       @@tab_cache[tabs] ||= ' ' * tabs
0
     end
0
 
...
45
46
47
48
 
 
49
50
51
...
232
233
234
235
 
 
 
 
236
237
238
...
45
46
47
 
48
49
50
51
52
...
233
234
235
 
236
237
238
239
240
241
242
0
@@ -45,7 +45,8 @@ module Haml
0
           'redcloth' => Haml::Filters::RedCloth,
0
           'textile' => Haml::Filters::Textile,
0
           'markdown' => Haml::Filters::Markdown },
0
- :filename => '(haml)'
0
+ :filename => '(haml)',
0
+ :ugly => false
0
       }
0
       @options.rec_merge! options
0
 
0
@@ -232,7 +233,10 @@ END
0
     # Returns a hash of options that Haml::Buffer cares about.
0
     # This should remain loadable form #inspect.
0
     def options_for_buffer
0
- {:attr_wrapper => @options[:attr_wrapper]}
0
+ {
0
+ :attr_wrapper => @options[:attr_wrapper],
0
+ :ugly => @options[:ugly]
0
+ }
0
     end
0
   end
0
 end
...
144
145
146
 
 
 
 
 
147
148
149
...
144
145
146
147
148
149
150
151
152
153
154
0
@@ -144,6 +144,11 @@ END
0
           exit
0
         end
0
 
0
+ opts.on('-t', '--style NAME',
0
+ 'Output style. Can be indented (default) or ugly.') do |name|
0
+ @options[:for_engine][:ugly] = true if name.to_sym == :ugly
0
+ end
0
+
0
         opts.on('-c', '--check', "Just check syntax, don't evaluate.") do
0
           @options[:check_syntax] = true
0
           @options[:output] = StringIO.new
...
270
271
272
273
 
274
275
276
...
299
300
301
302
303
304
 
 
 
 
 
 
 
305
306
307
...
388
389
390
 
 
391
392
393
...
270
271
272
 
273
274
275
276
...
299
300
301
 
 
 
302
303
304
305
306
307
308
309
310
311
...
392
393
394
395
396
397
398
399
0
@@ -270,7 +270,7 @@ END
0
     # Adds <tt>text</tt> to <tt>@buffer</tt> with appropriate tabulation
0
     # without parsing it.
0
     def push_merged_text(text, tab_change = 0, try_one_liner = false)
0
- @merged_text << "#{' ' * @output_tabs}#{text}"
0
+ @merged_text << (@options[:ugly] ? text : "#{' ' * @output_tabs}#{text}")
0
       @tab_change += tab_change
0
       @try_one_liner = try_one_liner
0
     end
0
@@ -299,9 +299,13 @@ END
0
 
0
     # Adds +text+ to <tt>@buffer</tt> while flattening text.
0
     def push_flat(line)
0
- tabulation = line.spaces - @flat_spaces
0
- tabulation = tabulation > -1 ? tabulation : 0
0
- @filter_buffer << "#{' ' * tabulation}#{line.unstripped}\n"
0
+ unless @options[:ugly]
0
+ tabulation = line.spaces - @flat_spaces
0
+ tabulation = tabulation > -1 ? tabulation : 0
0
+ @filter_buffer << "#{' ' * tabulation}#{line.unstripped}\n"
0
+ else
0
+ @filter_buffer << "#{line.unstripped}\n"
0
+ end
0
     end
0
 
0
     # Causes <tt>text</tt> to be evaluated in the context of
0
@@ -388,6 +392,8 @@ END
0
 
0
       if filter == Haml::Filters::Preserve
0
         push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\";")
0
+ elsif @options[:ugly]
0
+ push_text(filtered.rstrip)
0
       else
0
         push_text(filtered.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))
0
       end
...
383
384
385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
0
@@ -383,4 +383,18 @@ class EngineTest < Test::Unit::TestCase
0
   def test_render_proc_with_binding
0
     assert_equal("FOO\n", Haml::Engine.new("= upcase").render_proc("foo".instance_eval{binding}).call)
0
   end
0
+
0
+ def test_ugly_true
0
+ input = "#outer\n #inner\n %p hello world"
0
+ actual = Haml::Engine.new(input, :ugly => true).render
0
+ expected = "<div id='outer'>\n<div id='inner'>\n<p>hello world</p>\n</div>\n</div>\n"
0
+ assert_equal(expected, actual)
0
+ end
0
+
0
+ def test_ugly_false
0
+ input = "#outer\n #inner\n %p hello world"
0
+ actual = Haml::Engine.new(input, :ugly => false).render
0
+ expected = "<div id='outer'>\n <div id='inner'>\n <p>hello world</p>\n </div>\n</div>\n"
0
+ assert_equal(expected, actual)
0
+ end
0
 end

Comments

    No one has commented yet.