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

Make #find_and_preserve preserve the tags passed in the :preserve option.

This commit is contained in:
Nathan Weizenbaum 2008-05-13 11:14:46 -07:00
parent 7fcd7b739f
commit 535c40c47d
2 changed files with 8 additions and 6 deletions

View file

@ -107,7 +107,7 @@ module Haml
if preserve_tag
result = Haml::Helpers.preserve(result)
elsif preserve_script
result = Haml::Helpers.find_and_preserve(result)
result = Haml::Helpers.find_and_preserve(result, options[:preserve])
end
result = result.to_s.rstrip

View file

@ -60,16 +60,18 @@ module Haml
end
# call-seq:
# find_and_preserve(input)
# find_and_preserve(input, tags = haml_buffer.options[:preserve])
# 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 = '', &block)
# Uses preserve to convert any newlines inside whitespace-sensitive tags
# into the HTML entities for endlines.
# @tags@ is an array of tags to preserve.
# It defaults to the value of the <tt>:preserve</tt> option.
def find_and_preserve(input = '', tags = haml_buffer.options[:preserve], &block)
return find_and_preserve(capture_haml(&block)) if block
input = input.to_s
input.gsub(/<(textarea|code|pre)([^>]*)>(.*?)(<\/\1>)/im) do
input.gsub(/<(#{tags.map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im) do
"<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
end
end