diff --git a/lib/haml.rb b/lib/haml.rb index 44f2ee10..4c5d53a0 100644 --- a/lib/haml.rb +++ b/lib/haml.rb @@ -367,6 +367,24 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) # #
hello
# +# ==== ~ +# +# ~ works just like =, except that it runs Haml::Helpers#find_and_preserve on its input. +# For example, +# +# ~ "Foo\nBar\nBaz" +# +# is the same as: +# +# = find_and_preserve("Foo\n
Bar\nBaz") +# +# and is compiled to: +# +# Foo +#
Bar Baz+# +# See also Whitespace Preservation, below. +# # === XHTML Helpers # # ==== No Special Character @@ -595,6 +613,7 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) # preserved blocks of text aren't indented, # and newlines are replaced with the HTML escape code for newlines, # to preserve nice-looking output. +# See also Whitespace Preservation, below. # # [erb] Parses the filtered text with ERB, like an RHTML template. # Not available if the suppress_eval option is set to true. @@ -814,6 +833,27 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) # # == Other Useful Things # +# === Whitespace Preservation +# +# Sometimes you don't want Haml to indent all your text. +# For example, tags like +pre+ and +textarea+ are whitespace-sensitive; +# indenting the text makes them render wrong. +# +# Haml deals with this by "preserving" newlines before they're put into the document -- +# converting them to the XHTML whitespace escape code, . +# Then Haml won't try to re-format the indentation. +# +# Literal +textarea+ and +pre+ tags automatically preserve their content. +# Dynamically can't be caught automatically, +# and so should be passed through Haml::Helpers#find_and_preserve or the ~ command, +# which has the same effect (see above). +# +# Blocks of literal text can be preserved using the :preserve filter (see above). +# +# After the top-level Haml template has been processed, +# all newline escapes are converted back into literal newlines +# to make the source code more readable. +# # === Helpers # # Haml offers a bunch of helpers that are useful @@ -880,6 +920,8 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) # # Defaults to ['textarea', 'pre']. # +# See also Whitespace Preservation, above. +# module Haml # Returns a hash representing the version of Haml. # The :major, :minor, and :teeny keys have their respective numbers. diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index b6c52304..8c9078e4 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -87,6 +87,13 @@ module Haml @real_tabs = 0 end + # Returns the compiled string. + # This is distinct from #buffer in that some post-processing is done. + def result + return buffer unless @options[:ugly] || toplevel? + buffer.gsub(' ', "\n") + end + # Renders +text+ with the proper tabulation. This also deals with # making a possible one-line tag one line or not. def push_text(text, tab_change = 0) diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 9a1ee9b8..fed8c623 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -171,7 +171,7 @@ END @haml_buffer = buffer.upper end - buffer.buffer + buffer.result end alias_method :to_html, :render diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index 079fede7..184f503a 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -102,7 +102,7 @@ _erbout = _hamlout.buffer END postamble = <
Foo bar baz\n", + assert_equal("
Foo\n bar\n baz\n", render('%pre= "Foo\n bar\n baz"')) assert_equal("\n", diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index 747a165f..7ffc8338 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -82,13 +82,13 @@ class HelperTest < Test::Unit::TestCase end def test_text_area - assert_equal(%(\n), + assert_equal(%(\n), render('= text_area_tag "body", "Foo\nBar\n Baz\n Boom"', :action_view)) - assert_equal(%(\n), + assert_equal(%(\n), render('= text_area :post, :body', :action_view)) - assert_equal(%(
Foo bar baz\n), + assert_equal(%(
Foo bar\n baz\n), render('= content_tag "pre", "Foo bar\n baz"', :action_view)) end @@ -141,12 +141,12 @@ class HelperTest < Test::Unit::TestCase end def test_find_and_preserve_with_block - assert_equal("
Foo Bar\nFoo\nBar\n", + assert_equal("
\n Foo\n Bar\n\nFoo\nBar\n", render("= find_and_preserve do\n %pre\n Foo\n Bar\n Foo\n Bar")) end def test_preserve_with_block - assert_equal("
Foo BarFoo Bar \n", + assert_equal("
\n Foo\n Bar\n\nFoo\nBar\n\n", render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar")) end diff --git a/test/haml/results/filters.xhtml b/test/haml/results/filters.xhtml index 01009684..75ee6ef7 100644 --- a/test/haml/results/filters.xhtml +++ b/test/haml/results/filters.xhtml @@ -41,8 +41,14 @@ This \\
-This pre is pretty deeply nested. Does interpolation work? - This one is, too. Nested, that is. +This pre is pretty deeply + nested. + Does interpolation work? + + This one is, too. +Nested, that is. + +
foo bar-
foo bar-
foo bar+
foo +bar+
foo +bar
foo bar @@ -36,7 +44,13 @@
foo bar-
foo bar-
foo bar+
foo +bar+
foo +bar
foo bar
- ___ ,o88888 ,o8888888' ,:o:o:oooo. ,8O88Pd8888" ,.::.::o:ooooOoOoO. ,oO8O8Pd888'" ,.:.::o:ooOoOoOO8O8OOo.8OOPd8O8O" , ..:.::o:ooOoOOOO8OOOOo.FdO8O8" , ..:.::o:ooOoOO8O888O8O,COCOO" , . ..:.::o:ooOoOOOO8OOOOCOCO" . ..:.::o:ooOoOoOO8O8OCCCC"o . ..:.::o:ooooOoCoCCC"o:o . ..:.::o:o:,cooooCo"oo:o: ` . . ..:.:cocoooo"'o:o:::' .` . ..::ccccoc"'o:o:o:::' :.:. ,c:cccc"':.:.:.:.:.' ..:.:"'`::::c:"'..:.:.:.:.:.' http://www.chris.com/ASCII/ ...:.'.:.::::"' . . . . .' .. . ....:."' ` . . . '' . . . ...."' .. . ."' -hrr- . It's a planet! %strong This shouldn't be bold! + ___ + ,o88888 + ,o8888888' + ,:o:o:oooo. ,8O88Pd8888" + ,.::.::o:ooooOoOoO. ,oO8O8Pd888'" + ,.:.::o:ooOoOoOO8O8OOo.8OOPd8O8O" + , ..:.::o:ooOoOOOO8OOOOo.FdO8O8" + , ..:.::o:ooOoOO8O888O8O,COCOO" + , . ..:.::o:ooOoOOOO8OOOOCOCO" + . ..:.::o:ooOoOoOO8O8OCCCC"o + . ..:.::o:ooooOoCoCCC"o:o + . ..:.::o:o:,cooooCo"oo:o: + ` . . ..:.:cocoooo"'o:o:::' + .` . ..::ccccoc"'o:o:o:::' + :.:. ,c:cccc"':.:.:.:.:.' + ..:.:"'`::::c:"'..:.:.:.:.:.' http://www.chris.com/ASCII/ + ...:.'.:.::::"' . . . . .' + .. . ....:."' ` . . . '' + . . . ...."' + .. . ."' -hrr- + . + + + It's a planet! +%strong This shouldn't be bold! +This should!
- __ ______ __ ______ .----.| |--.|__ |.----.| |--..--------.| __ | | __|| ||__ || __|| < | || __ | |____||__|__||______||____||__|__||__|__|__||______| + __ ______ __ ______ +.----.| |--.|__ |.----.| |--..--------.| __ | +| __|| ||__ || __|| < | || __ | +|____||__|__||______||____||__|__||__|__|__||______| +
-foo +foo + bar