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 @real_tabs += tab_change
end end
# Properly formats the output of a script that was run in the Haml::Util.def_static_method(self, :format_script, [:result],
# instance_eval. :preserve_script, :in_tag, :preserve_tag, :escape_html,
def format_script(result, preserve_script, in_tag, preserve_tag, escape_html, :nuke_inner_whitespace, :interpolated, :ugly, <<RUBY)
nuke_inner_whitespace, interpolated)
# If we're interpolated, # If we're interpolated,
# then the custom tabulation is handled in #push_text. # then the custom tabulation is handled in #push_text.
# The easiest way to avoid is is to reset @tabulation. # The easiest way to avoid is is to reset @tabulation.
if !@options[:ugly] && interpolated <% if !ugly && interpolated %>
old_tabulation = @tabulation old_tabulation = @tabulation
@tabulation = 0 @tabulation = 0
end <% end %>
tabulation = @real_tabs tabulation = @real_tabs
result = result.to_s.rstrip 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) result = Haml::Helpers.preserve(result)
elsif preserve_script <% elsif preserve_script %>
result = Haml::Helpers.find_and_preserve(result, options[:preserve]) 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 @real_tabs -= 1
@tabulation = old_tabulation if !@options[:ugly] && interpolated <% if !ugly && interpolated %> @tabulation = old_tabulation <% end %>
return result return result
end <% unless ugly || preserve_tag %> end <% end %>
<% end %>
# Precompiled tabulation may be wrong # Precompiled tabulation may be wrong
if !interpolated && @tabulation > 0 && !in_tag && !@options[:ugly] <% if !interpolated && !in_tag && !ugly %>
result = tabs + result result = tabs + result if @tabulation > 0
end <% end %>
if !@options[:ugly] && (has_newline ||= result.include?("\n")) <% if !ugly %>
result = result.gsub "\n", "\n" + tabs(tabulation) if has_newline ||= result.include?("\\n")
result = result.gsub "\\n", "\\n" + tabs(tabulation)
# Add tabulation if it wasn't precompiled # Add tabulation if it wasn't precompiled
result = tabs(tabulation) + result if in_tag && !nuke_inner_whitespace <% if in_tag && !nuke_inner_whitespace %> result = tabs(tabulation) + result <% end %>
end end
<% end %>
result = "\n" + result if in_tag && !nuke_inner_whitespace <% if in_tag && !nuke_inner_whitespace %> result = "\\n" + result <% end %>
result << "\n" unless nuke_inner_whitespace <% unless nuke_inner_whitespace %> result << "\\n" <% end %>
if in_tag && !nuke_inner_whitespace <% if in_tag && !nuke_inner_whitespace %>
# We never get here if @options[:ugly] is true # We never get here if ugly is true
result << tabs(tabulation-1) result << tabs(tabulation-1)
@real_tabs -= 1 @real_tabs -= 1
end <% end %>
@tabulation = old_tabulation if !@options[:ugly] && interpolated <% if !ugly && interpolated %> @tabulation = old_tabulation <% end %>
result result
end RUBY
# Takes the various information about the opening tag for an # Takes the various information about the opening tag for an
# element, formats it, and adds it to the buffer. # 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? raise SyntaxError.new("There's no Ruby code for = to evaluate.") if text.empty?
return if options[:suppress_eval] return if options[:suppress_eval]
args = [preserve_script, in_tag, preserve_tag, args = [preserve_script, in_tag, preserve_tag, escape_html,
escape_html, nuke_inner_whitespace, !block_opened?].map { |a| a.inspect }.join(', ') nuke_inner_whitespace, !block_opened?, @options[:ugly]]
out = "_hamlout.format_script(haml_temp, #{args});" out = "_hamlout.#{static_method_name(:format_script, *args)}(haml_temp);"
# Prerender tabulation unless we're in a tag # Prerender tabulation unless we're in a tag
push_merged_text '' unless in_tag push_merged_text '' unless in_tag