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:
parent
3e92427647
commit
2208ff91ca
2 changed files with 24 additions and 2 deletions
|
@ -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/, '
').gsub(/\r/, '')
|
||||
end
|
||||
|
||||
|
|
|
@ -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>
 Foo
 Bar
</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>
 Foo
 Bar
</pre>
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
|
||||
|
|
Loading…
Add table
Reference in a new issue