From 2208ff91ca53a9326fda48c33fc0bfc7d1970eba Mon Sep 17 00:00:00 2001 From: nex3 Date: Sat, 5 Jan 2008 01:19:04 +0000 Subject: [PATCH] Adding an optional block parameter to preserve and find_and_preserve. git-svn-id: svn://hamptoncatlin.com/haml/trunk@708 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/helpers.rb | 16 ++++++++++++++-- test/haml/helper_test.rb | 10 ++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 416f9914..927f9920 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -42,19 +42,31 @@ module Haml nil end + # call-seq: + # find_and_preserve(input) + # find_and_preserve {...} + # # Isolates the whitespace-sensitive tags in the string and uses preserve # to convert any endlines inside them into HTML entities for endlines. - def find_and_preserve(input) + def find_and_preserve(input = '', &block) + return find_and_preserve(capture_haml(&block)) if block + input = input.to_s input.gsub(/<(textarea|code|pre)([^>]*)>(.*?)(<\/\1>)/im) do "<#{$1}#{$2}>#{preserve($3)}" end end + # call-seq: + # preserve(input) + # preserve {...} + # # Takes any string, finds all the endlines and converts them to # HTML entities for endlines so they'll render correctly in # whitespace-sensitive tags without screwing up the indentation. - def preserve(input) + def preserve(input = '', &block) + return preserve(capture_haml(&block)) if block + input.gsub(/\n/, ' ').gsub(/\r/, '') end diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index d8c0ec48..7e3f9aa1 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -122,6 +122,16 @@ class HelperTest < Test::Unit::TestCase assert_equal("1\n\n2\n\n3\n\n", render("- trc([1, 2, 3]) do |i|\n = i.inspect")) end + def test_find_and_preserve_with_block + assert_equal("
  Foo
  Bar
\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
  Bar
Foo Bar \n", + render("= preserve do\n %pre\n Foo\n Bar\n Foo\n Bar")) + end + def test_init_haml_helpers context = Object.new class << context