Make Haml::Buffer#format_string use static conditionals.

This commit is contained in:
Nathan Weizenbaum 2008-12-28 01:28:59 -08:00
parent 4f5a9b2861
commit addaa09a99
2 changed files with 34 additions and 31 deletions

View File

@ -103,58 +103,61 @@ RUBY
@real_tabs += tab_change
end
# Properly formats the output of a script that was run in the
# instance_eval.
def format_script(result, preserve_script, in_tag, preserve_tag, escape_html,
nuke_inner_whitespace, interpolated)
Haml::Util.def_static_method(self, :format_script, [:result],
:preserve_script, :in_tag, :preserve_tag, :escape_html,
:nuke_inner_whitespace, :interpolated, :ugly, <<RUBY)
# If we're interpolated,
# then the custom tabulation is handled in #push_text.
# The easiest way to avoid is is to reset @tabulation.
if !@options[:ugly] && interpolated
<% if !ugly && interpolated %>
old_tabulation = @tabulation
@tabulation = 0
end
<% end %>
tabulation = @real_tabs
result = result.to_s.rstrip
result = html_escape(result) if escape_html
<% if escape_html %> result = html_escape(result) <% end %>
if preserve_tag
<% if preserve_tag %>
result = Haml::Helpers.preserve(result)
elsif preserve_script
<% elsif preserve_script %>
result = Haml::Helpers.find_and_preserve(result, options[:preserve])
end
<% end %>
if in_tag && !nuke_inner_whitespace && (@options[:ugly] || preserve_tag || !(has_newline = result.include?("\n")))
<% if in_tag && !nuke_inner_whitespace %>
<% unless ugly || preserve_tag %> if !(has_newline = result.include?("\\n")) <% end %>
@real_tabs -= 1
@tabulation = old_tabulation if !@options[:ugly] && interpolated
<% if !ugly && interpolated %> @tabulation = old_tabulation <% end %>
return result
end
<% unless ugly || preserve_tag %> end <% end %>
<% end %>
# Precompiled tabulation may be wrong
if !interpolated && @tabulation > 0 && !in_tag && !@options[:ugly]
result = tabs + result
end
<% if !interpolated && !in_tag && !ugly %>
result = tabs + result if @tabulation > 0
<% end %>
if !@options[:ugly] && (has_newline ||= result.include?("\n"))
result = result.gsub "\n", "\n" + tabs(tabulation)
<% if !ugly %>
if has_newline ||= result.include?("\\n")
result = result.gsub "\\n", "\\n" + tabs(tabulation)
# Add tabulation if it wasn't precompiled
result = tabs(tabulation) + result if in_tag && !nuke_inner_whitespace
end
# Add tabulation if it wasn't precompiled
<% if in_tag && !nuke_inner_whitespace %> result = tabs(tabulation) + result <% end %>
end
<% end %>
result = "\n" + result if in_tag && !nuke_inner_whitespace
result << "\n" unless nuke_inner_whitespace
<% if in_tag && !nuke_inner_whitespace %> result = "\\n" + result <% end %>
<% unless nuke_inner_whitespace %> result << "\\n" <% end %>
if in_tag && !nuke_inner_whitespace
# We never get here if @options[:ugly] is true
<% if in_tag && !nuke_inner_whitespace %>
# We never get here if ugly is true
result << tabs(tabulation-1)
@real_tabs -= 1
end
@tabulation = old_tabulation if !@options[:ugly] && interpolated
<% end %>
<% if !ugly && interpolated %> @tabulation = old_tabulation <% end %>
result
end
RUBY
# Takes the various information about the opening tag for an
# element, formats it, and adds it to the buffer.

View File

@ -316,9 +316,9 @@ END
raise SyntaxError.new("There's no Ruby code for = to evaluate.") if text.empty?
return if options[:suppress_eval]
args = [preserve_script, in_tag, preserve_tag,
escape_html, nuke_inner_whitespace, !block_opened?].map { |a| a.inspect }.join(', ')
out = "_hamlout.format_script(haml_temp, #{args});"
args = [preserve_script, in_tag, preserve_tag, escape_html,
nuke_inner_whitespace, !block_opened?, @options[:ugly]]
out = "_hamlout.#{static_method_name(:format_script, *args)}(haml_temp);"
# Prerender tabulation unless we're in a tag
push_merged_text '' unless in_tag