public
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/nex3/haml.git
Get rid of the 50-char one-liner rule in Haml.
nex3 (author)
Thu Apr 24 19:14:52 -0700 2008
commit  a241cd4eb4681f473dc42a0976bc6d9146dee3c8
tree    4f87b0498983425a9e69bc56c68452d84a11a812
parent  22a22689e52fa6cb5ce67056ce5d98bb5d28a015
...
6
7
8
9
10
11
12
13
14
15
16
...
118
119
120
121
 
122
123
124
...
162
163
164
165
 
166
167
168
...
189
190
191
 
 
192
193
194
195
196
197
198
199
200
201
202
203
204
205
...
6
7
8
 
 
 
 
 
9
10
11
...
113
114
115
 
116
117
118
119
...
157
158
159
 
160
161
162
163
...
184
185
186
187
188
189
190
191
 
 
 
 
 
 
 
 
192
193
194
0
@@ -6,11 +6,6 @@ module Haml
0
   class Buffer
0
     include Haml::Helpers
0
 
0
- # Set the maximum length for a line to be considered a one-liner.
0
- # Lines <= the maximum will be rendered on one line,
0
- # i.e. <tt><p>Hello world</p></tt>
1
- ONE_LINER_LENGTH = 50
0
-
0
     # The string that holds the compiled XHTML. This is aliased as
0
     # _erbout for compatibility with ERB-specific code.
0
     attr_accessor :buffer
0
@@ -118,7 +113,7 @@ module Haml
0
 
0
       result = html_escape(result) if escape_html
0
 
0
- if close_tag && (@options[:ugly] || Buffer.one_liner?(result) || preserve_tag)
0
+ if close_tag && (@options[:ugly] || !result.include?("\n") || preserve_tag)
0
         @buffer << "#{result}</#{close_tag}>\n"
0
         @real_tabs -= 1
0
       else
0
@@ -162,7 +157,7 @@ module Haml
0
       @buffer << "#{@options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}"
0
 
0
       if content
0
- if @options[:ugly] || Buffer.one_liner?(content)
0
+ if @options[:ugly] || !content.include?("\n")
0
           @buffer << "#{content}</#{name}>\n"
0
         else
0
           @buffer << "\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}</#{name}>\n"
0
@@ -189,17 +184,11 @@ module Haml
0
       to.merge!(from)
0
     end
0
 
0
+ private
0
+
0
     # Some of these methods are exposed as public class methods
0
     # so they can be re-used in helpers.
0
 
0
- # Returns whether or not the given value is short enough to be rendered
0
- # on one line.
0
- def self.one_liner?(value)
0
- value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
0
- end
0
-
0
- private
0
-
0
     @@tab_cache = {}
0
     # Gets <tt>count</tt> tabs. Mostly for internal use.
0
     def tabs(count)
...
284
285
286
287
288
289
290
291
292
293
294
295
296
...
301
302
303
304
 
305
306
307
308
309
310
311
...
559
560
561
562
563
 
564
565
 
566
567
568
...
610
611
612
613
 
614
615
616
...
284
285
286
 
287
288
289
290
291
 
292
293
294
...
299
300
301
 
302
303
304
305
 
306
307
308
...
556
557
558
 
 
559
560
 
561
562
563
564
...
606
607
608
 
609
610
611
612
0
@@ -284,13 +284,11 @@ END
0
     def push_merged_text(text, tab_change = 0, try_one_liner = false)
0
       @merged_text << (@options[:ugly] ? text : "#{' ' * @output_tabs}#{text}")
0
       @tab_change += tab_change
0
- @try_one_liner = try_one_liner
0
     end
0
 
0
     # Concatenate <tt>text</tt> to <tt>@buffer</tt> without tabulation.
0
     def concat_merged_text(text)
0
       @merged_text << text
0
- @try_one_liner = false
0
     end
0
 
0
     def push_text(text, tab_change = 0, try_one_liner = false)
0
@@ -301,11 +299,10 @@ END
0
       return if @merged_text.empty?
0
 
0
       @precompiled << "_hamlout.push_text(#{@merged_text.dump}"
0
- @precompiled << ", #{@tab_change}" if @tab_change != 0 || @try_one_liner
0
+ @precompiled << ", #{@tab_change}" if @tab_change != 0
0
       @precompiled << ");"
0
       @merged_text = ''
0
       @tab_change = 0
0
- @try_one_liner = false
0
     end
0
 
0
     # Renders a block of text as plain text.
0
@@ -559,10 +556,9 @@ END
0
 
0
       self_closing ||= !!( !@block_opened && value.empty? && @options[:autoclose].include?(tag_name) )
0
 
0
- one_liner = Buffer.one_liner?(value) || preserve_tag
0
- if object_ref == "nil" && attributes_hash.nil? && !preserve_script && (parse || one_liner)
0
+ if object_ref == "nil" && attributes_hash.nil? && !preserve_script
0
         # This means that we can render the tag directly to text and not process it in the buffer
0
- tag_closed = !value.empty? && one_liner && !parse
0
+ tag_closed = !value.empty? && !parse
0
 
0
         open_tag = prerender_tag(tag_name, self_closing, attributes)
0
         open_tag << "#{value}</#{tag_name}>" if tag_closed
0
@@ -610,7 +606,7 @@ END
0
       open = "<!--#{conditional} "
0
 
0
       # Render it statically if possible
0
- if !content.empty? && Buffer.one_liner?(content)
0
+ unless content.empty?
0
         return push_text("#{open}#{content} #{conditional ? "<![endif]-->" : "-->"}")
0
       end
0
 
...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
 
84
85
86
...
67
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
71
72
73
74
0
@@ -67,20 +67,8 @@ END
0
     assert_equal("<p>Hello</p>", render('%p Hello').chomp)
0
   end
0
 
0
- def test_long_liner_should_not_print_on_one_line
0
- assert_equal("<div>\n #{'x' * 51}\n</div>", render("%div #{'x' * 51}").chomp)
0
- end
0
-
0
- def test_non_prerendered_one_liner
0
- assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c} One line", :locals => {:c => 'awesome'}))
0
- end
0
-
0
- def test_non_prerendered_script_one_liner
0
- assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c}= 'One line'", :locals => {:c => 'awesome'}))
0
- end
0
-
0
- def test_non_prerendered_long_script_one_liner
0
- assert_equal("<p class='awesome'>\n #{'x' * 60}\n</p>\n", render("%p{:class => c}= 'x' * 60", :locals => {:c => 'awesome'}))
0
+ def test_one_liner_with_newline_shouldnt_be_one_line
0
+ assert_equal("<p>\n foo\n bar\n</p>", render('%p= "foo\nbar"').chomp)
0
   end
0
 
0
   def test_multi_render
...
24
25
26
27
28
29
 
30
31
32
...
24
25
26
 
 
 
27
28
29
30
0
@@ -24,9 +24,7 @@
0
   </p>
0
 </div>
0
 <p>foo</p>
0
- <p>
0
- reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
0
- </p>
0
+ <p>reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong</p>
0
 <div class='woah'>
0
   <div id='funky'>
0
     <div>
...
28
29
30
31
32
33
34
35
36
...
28
29
30
 
 
 
31
32
33
0
@@ -28,9 +28,6 @@ stuff followed by whitespace
0
 </p>
0
 <!-- Short comment -->
0
 <!--
0
- This is a really long comment look how long it is it should be on a line of its own don't you think?
0
--->
0
-<!--
0
   This is a block comment
0
   cool, huh?
0
   <strong>there can even be sub-tags!</strong>
...
4
5
6
7
8
9
 
10
11
12
...
4
5
6
 
 
 
7
8
9
10
0
@@ -4,9 +4,7 @@
0
     <title>Stop. haml time</title>
0
     <div id='content'>
0
       <h1>This is a title!</h1>
0
- <p>
0
- Lorem ipsum dolor sit amet, consectetur adipisicing elit
0
- </p>
0
+ <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
0
       <p class='foo'>Cigarettes!</p>
0
       <h2>Man alive!</h2>
0
       <ul class='things'>
...
31
32
33
34
35
36
37
...
31
32
33
 
34
35
36
0
@@ -31,7 +31,6 @@
0
   \%p foo
0
   \yee\ha
0
 / Short comment
0
-/ This is a really long comment look how long it is it should be on a line of its own don't you think?
0
 /
0
   This is a block comment
0
   cool, huh?

Comments