1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

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
This commit is contained in:
nex3 2008-01-05 01:19:04 +00:00
parent 3e92427647
commit 2208ff91ca
2 changed files with 24 additions and 2 deletions

View file

@ -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)}</#{$1}>"
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/, '&#x000A;').gsub(/\r/, '')
end

View file

@ -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("<pre>&#x000A; Foo&#x000A; Bar&#x000A;</pre>\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("<pre>&#x000A; Foo&#x000A; Bar&#x000A;</pre>&#x000A;Foo&#x000A;Bar&#x000A;\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