1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Get rid of the 50-char one-liner rule in Haml.

This commit is contained in:
Nathan Weizenbaum 2008-04-24 19:14:52 -07:00
parent 22a22689e5
commit a241cd4eb4
7 changed files with 12 additions and 47 deletions

View file

@ -6,11 +6,6 @@ module Haml
class Buffer class Buffer
include Haml::Helpers include Haml::Helpers
# Set the maximum length for a line to be considered a one-liner.
# Lines <= the maximum will be rendered on one line,
# i.e. <tt><p>Hello world</p></tt>
ONE_LINER_LENGTH = 50
# The string that holds the compiled XHTML. This is aliased as # The string that holds the compiled XHTML. This is aliased as
# _erbout for compatibility with ERB-specific code. # _erbout for compatibility with ERB-specific code.
attr_accessor :buffer attr_accessor :buffer
@ -118,7 +113,7 @@ module Haml
result = html_escape(result) if escape_html result = html_escape(result) if escape_html
if close_tag && (@options[:ugly] || Buffer.one_liner?(result) || preserve_tag) if close_tag && (@options[:ugly] || !result.include?("\n") || preserve_tag)
@buffer << "#{result}</#{close_tag}>\n" @buffer << "#{result}</#{close_tag}>\n"
@real_tabs -= 1 @real_tabs -= 1
else else
@ -162,7 +157,7 @@ module Haml
@buffer << "#{@options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}" @buffer << "#{@options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}"
if content if content
if @options[:ugly] || Buffer.one_liner?(content) if @options[:ugly] || !content.include?("\n")
@buffer << "#{content}</#{name}>\n" @buffer << "#{content}</#{name}>\n"
else else
@buffer << "\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}</#{name}>\n" @buffer << "\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}</#{name}>\n"
@ -189,17 +184,11 @@ module Haml
to.merge!(from) to.merge!(from)
end end
private
# Some of these methods are exposed as public class methods # Some of these methods are exposed as public class methods
# so they can be re-used in helpers. # so they can be re-used in helpers.
# Returns whether or not the given value is short enough to be rendered
# on one line.
def self.one_liner?(value)
value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
end
private
@@tab_cache = {} @@tab_cache = {}
# Gets <tt>count</tt> tabs. Mostly for internal use. # Gets <tt>count</tt> tabs. Mostly for internal use.
def tabs(count) def tabs(count)

View file

@ -284,13 +284,11 @@ END
def push_merged_text(text, tab_change = 0, try_one_liner = false) def push_merged_text(text, tab_change = 0, try_one_liner = false)
@merged_text << (@options[:ugly] ? text : "#{' ' * @output_tabs}#{text}") @merged_text << (@options[:ugly] ? text : "#{' ' * @output_tabs}#{text}")
@tab_change += tab_change @tab_change += tab_change
@try_one_liner = try_one_liner
end end
# Concatenate <tt>text</tt> to <tt>@buffer</tt> without tabulation. # Concatenate <tt>text</tt> to <tt>@buffer</tt> without tabulation.
def concat_merged_text(text) def concat_merged_text(text)
@merged_text << text @merged_text << text
@try_one_liner = false
end end
def push_text(text, tab_change = 0, try_one_liner = false) def push_text(text, tab_change = 0, try_one_liner = false)
@ -301,11 +299,10 @@ END
return if @merged_text.empty? return if @merged_text.empty?
@precompiled << "_hamlout.push_text(#{@merged_text.dump}" @precompiled << "_hamlout.push_text(#{@merged_text.dump}"
@precompiled << ", #{@tab_change}" if @tab_change != 0 || @try_one_liner @precompiled << ", #{@tab_change}" if @tab_change != 0
@precompiled << ");" @precompiled << ");"
@merged_text = '' @merged_text = ''
@tab_change = 0 @tab_change = 0
@try_one_liner = false
end end
# Renders a block of text as plain text. # Renders a block of text as plain text.
@ -559,10 +556,9 @@ END
self_closing ||= !!( !@block_opened && value.empty? && @options[:autoclose].include?(tag_name) ) self_closing ||= !!( !@block_opened && value.empty? && @options[:autoclose].include?(tag_name) )
one_liner = Buffer.one_liner?(value) || preserve_tag if object_ref == "nil" && attributes_hash.nil? && !preserve_script
if object_ref == "nil" && attributes_hash.nil? && !preserve_script && (parse || one_liner)
# This means that we can render the tag directly to text and not process it in the buffer # This means that we can render the tag directly to text and not process it in the buffer
tag_closed = !value.empty? && one_liner && !parse tag_closed = !value.empty? && !parse
open_tag = prerender_tag(tag_name, self_closing, attributes) open_tag = prerender_tag(tag_name, self_closing, attributes)
open_tag << "#{value}</#{tag_name}>" if tag_closed open_tag << "#{value}</#{tag_name}>" if tag_closed
@ -610,7 +606,7 @@ END
open = "<!--#{conditional} " open = "<!--#{conditional} "
# Render it statically if possible # Render it statically if possible
if !content.empty? && Buffer.one_liner?(content) unless content.empty?
return push_text("#{open}#{content} #{conditional ? "<![endif]-->" : "-->"}") return push_text("#{open}#{content} #{conditional ? "<![endif]-->" : "-->"}")
end end

View file

@ -67,20 +67,8 @@ END
assert_equal("<p>Hello</p>", render('%p Hello').chomp) assert_equal("<p>Hello</p>", render('%p Hello').chomp)
end end
def test_long_liner_should_not_print_on_one_line def test_one_liner_with_newline_shouldnt_be_one_line
assert_equal("<div>\n #{'x' * 51}\n</div>", render("%div #{'x' * 51}").chomp) assert_equal("<p>\n foo\n bar\n</p>", render('%p= "foo\nbar"').chomp)
end
def test_non_prerendered_one_liner
assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c} One line", :locals => {:c => 'awesome'}))
end
def test_non_prerendered_script_one_liner
assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c}= 'One line'", :locals => {:c => 'awesome'}))
end
def test_non_prerendered_long_script_one_liner
assert_equal("<p class='awesome'>\n #{'x' * 60}\n</p>\n", render("%p{:class => c}= 'x' * 60", :locals => {:c => 'awesome'}))
end end
def test_multi_render def test_multi_render

View file

@ -24,9 +24,7 @@
</p> </p>
</div> </div>
<p>foo</p> <p>foo</p>
<p> <p>reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong</p>
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
</p>
<div class='woah'> <div class='woah'>
<div id='funky'> <div id='funky'>
<div> <div>

View file

@ -27,9 +27,6 @@ stuff followed by whitespace
yee\ha yee\ha
</p> </p>
<!-- Short comment --> <!-- Short comment -->
<!--
This is a really long comment look how long it is it should be on a line of its own don't you think?
-->
<!-- <!--
This is a block comment This is a block comment
cool, huh? cool, huh?

View file

@ -4,9 +4,7 @@
<title>Stop. haml time</title> <title>Stop. haml time</title>
<div id='content'> <div id='content'>
<h1>This is a title!</h1> <h1>This is a title!</h1>
<p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit
</p>
<p class='foo'>Cigarettes!</p> <p class='foo'>Cigarettes!</p>
<h2>Man alive!</h2> <h2>Man alive!</h2>
<ul class='things'> <ul class='things'>

View file

@ -31,7 +31,6 @@
\%p foo \%p foo
\yee\ha \yee\ha
/ Short comment / Short comment
/ This is a really long comment look how long it is it should be on a line of its own don't you think?
/ /
This is a block comment This is a block comment
cool, huh? cool, huh?