From addaa09a99bd07980c9c65323135008b28b5c718 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Sun, 28 Dec 2008 01:28:59 -0800 Subject: [PATCH] Make Haml::Buffer#format_string use static conditionals. --- lib/haml/buffer.rb | 59 ++++++++++++++++++++++------------------- lib/haml/precompiler.rb | 6 ++--- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index 236dabc1..c67ba427 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -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, < 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. diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index 87a3f592..470df956 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -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