2006-09-12 04:14:21 +00:00
|
|
|
module Haml
|
2006-10-14 23:50:07 +00:00
|
|
|
# This module contains various helpful methods to make it easier to do
|
|
|
|
# various tasks. Haml::Helpers is automatically included in the context
|
|
|
|
# that a HAML template is parsed in, so all these methods are at your
|
|
|
|
# disposal from within the template.
|
2006-09-12 04:14:21 +00:00
|
|
|
module Helpers
|
2006-10-14 23:50:07 +00:00
|
|
|
# Takes any string, finds all the endlines and converts them to
|
|
|
|
# html entities for endlines so they'll render correctly in
|
|
|
|
# whitespace-sensitive tags.
|
2006-09-12 04:14:21 +00:00
|
|
|
def flatten(input)
|
|
|
|
input.gsub(/\n/, '
').gsub(/\r/, '')
|
|
|
|
end
|
2006-07-31 14:28:44 +00:00
|
|
|
|
2006-10-14 23:50:07 +00:00
|
|
|
# Isolates the whitespace-sensitive tags in the string and uses flatten
|
|
|
|
# to convert any endlines inside them into html entities.
|
2006-09-12 04:14:21 +00:00
|
|
|
def find_and_flatten(input)
|
|
|
|
input.scan(/<(textarea|code|pre)[^>]*>(.*?)<\/\1>/im).each do |thing|
|
|
|
|
input = input.gsub(thing[1], flatten(thing[1]))
|
|
|
|
end
|
|
|
|
input
|
2006-08-08 13:42:19 +00:00
|
|
|
end
|
2006-08-08 21:38:50 +00:00
|
|
|
|
2006-10-14 23:50:07 +00:00
|
|
|
# Counts the tabulation of a line. Mostly for internal use.
|
2006-09-12 04:14:21 +00:00
|
|
|
def count_soft_tabs(line)
|
|
|
|
line.index(/[^ ]/) ? [line.index(/[^ ]/)/2, line.strip] : []
|
|
|
|
end
|
2006-09-19 14:13:49 +00:00
|
|
|
|
2006-10-14 23:50:07 +00:00
|
|
|
# Takes an array and a block and iterates the array,
|
|
|
|
# yielding each element to the block and putting the
|
|
|
|
# result into <tt>li</tt> elements, creating a list
|
|
|
|
# of the results of the block. For example:
|
2006-09-19 14:13:49 +00:00
|
|
|
#
|
|
|
|
# For instance:
|
|
|
|
# list_of([['hello'], ['yall']]) { |i| i[0] }
|
|
|
|
# or
|
|
|
|
# list_of(['hello', 'yall'])
|
|
|
|
#
|
|
|
|
# Produces:
|
|
|
|
# <li>hello</li>
|
|
|
|
# <li>yall</li>
|
|
|
|
#
|
2006-10-14 23:50:07 +00:00
|
|
|
def list_of(array) # :yields: item
|
2006-09-19 14:13:49 +00:00
|
|
|
(array.collect { |i| "<li>#{yield(i)}</li>" }).join("\n")
|
|
|
|
end
|
2006-08-08 21:38:50 +00:00
|
|
|
end
|
2006-07-31 14:28:44 +00:00
|
|
|
end
|