2006-12-03 18:56:47 -05:00
|
|
|
require 'haml/helpers/action_view_mods'
|
2007-03-16 23:13:20 -04:00
|
|
|
require 'haml/helpers/action_view_extensions'
|
2006-11-14 13:35:02 -05:00
|
|
|
|
2006-09-12 00:14:21 -04:00
|
|
|
module Haml
|
2006-10-14 19:50:07 -04:00
|
|
|
# This module contains various helpful methods to make it easier to do
|
|
|
|
# various tasks. Haml::Helpers is automatically included in the context
|
2006-11-21 00:54:26 -05:00
|
|
|
# that a Haml template is parsed in, so all these methods are at your
|
2006-10-14 19:50:07 -04:00
|
|
|
# disposal from within the template.
|
2006-09-12 00:14:21 -04:00
|
|
|
module Helpers
|
2006-11-05 22:01:04 -05:00
|
|
|
self.extend self
|
2007-03-31 19:21:16 -04:00
|
|
|
|
|
|
|
@@action_view_defined = defined?(ActionView)
|
2006-11-14 13:35:02 -05:00
|
|
|
@@force_no_action_view = false
|
|
|
|
|
2006-11-21 00:54:26 -05:00
|
|
|
# Returns whether or not ActionView is installed on the system.
|
2006-11-14 13:35:02 -05:00
|
|
|
def self.action_view?
|
2007-03-31 19:21:16 -04:00
|
|
|
@@action_view_defined
|
2006-11-14 13:35:02 -05:00
|
|
|
end
|
2006-11-05 22:01:04 -05:00
|
|
|
|
2007-07-14 05:00:55 -04:00
|
|
|
# Note: this does *not* need to be called
|
|
|
|
# when using Haml helpers normally
|
|
|
|
# in Rails.
|
|
|
|
#
|
|
|
|
# Initializes the current object
|
|
|
|
# as though it were in the same context
|
|
|
|
# as a normal ActionView rendering
|
|
|
|
# using Haml.
|
|
|
|
# This is useful if you want to use the helpers in a context
|
|
|
|
# other than the normal setup with ActionView.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# context = Object.new
|
|
|
|
# class << context
|
|
|
|
# include Haml::Helpers
|
|
|
|
# end
|
|
|
|
# context.init_haml_helpers
|
2008-03-05 19:18:31 -05:00
|
|
|
# context.haml_tag :p, "Stuff"
|
2008-04-08 02:09:17 -04:00
|
|
|
#
|
2007-07-14 05:00:55 -04:00
|
|
|
def init_haml_helpers
|
2008-04-24 12:52:26 -04:00
|
|
|
@haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer))
|
2007-07-14 05:00:55 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2008-04-24 13:23:01 -04:00
|
|
|
# call-seq:
|
|
|
|
# non_haml { ... }
|
|
|
|
#
|
|
|
|
# Runs a block of code in a non-Haml context
|
|
|
|
# (i.e. #is_haml? will return false).
|
|
|
|
#
|
|
|
|
# This is mainly useful for rendering sub-templates such as partials in a non-Haml language,
|
|
|
|
# particularly where helpers may behave differently when run from Haml.
|
|
|
|
#
|
|
|
|
# Note that this is automatically applied to Rails partials.
|
|
|
|
def non_haml
|
2008-04-24 15:03:54 -04:00
|
|
|
was_active = @haml_buffer.active?
|
|
|
|
@haml_buffer.active = false
|
2008-11-23 04:57:24 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2008-04-24 15:03:54 -04:00
|
|
|
@haml_buffer.active = was_active
|
2008-04-24 13:23:01 -04:00
|
|
|
end
|
|
|
|
|
2008-01-04 20:19:04 -05:00
|
|
|
# call-seq:
|
2008-05-13 14:14:46 -04:00
|
|
|
# find_and_preserve(input, tags = haml_buffer.options[:preserve])
|
2008-01-04 20:19:04 -05:00
|
|
|
# find_and_preserve {...}
|
2008-04-08 02:09:17 -04:00
|
|
|
#
|
2008-05-13 14:14:46 -04:00
|
|
|
# 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)
|
2008-01-04 20:19:04 -05:00
|
|
|
return find_and_preserve(capture_haml(&block)) if block
|
|
|
|
|
2007-02-06 03:08:32 -05:00
|
|
|
input = input.to_s
|
2008-05-13 14:14:46 -04:00
|
|
|
input.gsub(/<(#{tags.map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im) do
|
2007-09-05 15:38:09 -04:00
|
|
|
"<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
|
2007-02-06 03:08:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-04 20:19:04 -05:00
|
|
|
# call-seq:
|
|
|
|
# preserve(input)
|
|
|
|
# preserve {...}
|
|
|
|
#
|
2006-10-14 19:50:07 -04:00
|
|
|
# Takes any string, finds all the endlines and converts them to
|
2006-11-21 00:54:26 -05:00
|
|
|
# HTML entities for endlines so they'll render correctly in
|
|
|
|
# whitespace-sensitive tags without screwing up the indentation.
|
2008-01-04 20:19:04 -05:00
|
|
|
def preserve(input = '', &block)
|
|
|
|
return preserve(capture_haml(&block)) if block
|
|
|
|
|
2008-05-11 01:00:19 -04:00
|
|
|
input.chomp("\n").gsub(/\n/, '
').gsub(/\r/, '')
|
2006-09-12 00:14:21 -04:00
|
|
|
end
|
2006-07-31 10:28:44 -04:00
|
|
|
|
2007-02-06 03:08:32 -05:00
|
|
|
alias_method :flatten, :preserve
|
|
|
|
|
2006-11-27 20:44:40 -05:00
|
|
|
# Takes an Enumerable object and a block
|
|
|
|
# and iterates over the object,
|
|
|
|
# yielding each element to a Haml block
|
|
|
|
# and putting the result into <tt><li></tt> elements.
|
|
|
|
# This creates a list of the results of the block.
|
|
|
|
# For example:
|
2006-09-19 10:13:49 -04:00
|
|
|
#
|
2006-11-27 20:44:40 -05:00
|
|
|
# = list_of([['hello'], ['yall']]) do |i|
|
|
|
|
# = i[0]
|
2006-09-19 10:13:49 -04:00
|
|
|
#
|
2006-11-27 20:44:40 -05:00
|
|
|
# Produces:
|
2006-11-21 00:54:26 -05:00
|
|
|
#
|
2006-09-19 10:13:49 -04:00
|
|
|
# <li>hello</li>
|
|
|
|
# <li>yall</li>
|
|
|
|
#
|
2006-11-27 20:44:40 -05:00
|
|
|
# And
|
|
|
|
#
|
|
|
|
# = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|
|
|
|
|
# %h3= key.humanize
|
|
|
|
# %p= val
|
|
|
|
#
|
|
|
|
# Produces:
|
|
|
|
#
|
|
|
|
# <li>
|
|
|
|
# <h3>Title</h3>
|
|
|
|
# <p>All the stuff</p>
|
|
|
|
# </li>
|
|
|
|
# <li>
|
|
|
|
# <h3>Description</h3>
|
|
|
|
# <p>A book about all the stuff.</p>
|
|
|
|
# </li>
|
|
|
|
#
|
|
|
|
def list_of(array, &block) # :yields: item
|
|
|
|
to_return = array.collect do |i|
|
2007-05-15 11:51:50 -04:00
|
|
|
result = capture_haml(i, &block)
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-27 20:44:40 -05:00
|
|
|
if result.count("\n") > 1
|
|
|
|
result.gsub!("\n", "\n ")
|
|
|
|
result = "\n #{result.strip}\n"
|
|
|
|
else
|
|
|
|
result.strip!
|
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-27 20:44:40 -05:00
|
|
|
"<li>#{result}</li>"
|
|
|
|
end
|
|
|
|
to_return.join("\n")
|
2006-09-19 10:13:49 -04:00
|
|
|
end
|
2006-11-07 22:27:37 -05:00
|
|
|
|
2007-03-17 07:03:36 -04:00
|
|
|
# Returns a hash containing default assignments for the xmlns and xml:lang
|
|
|
|
# attributes of the <tt>html</tt> HTML element.
|
|
|
|
# It also takes an optional argument for the value of xml:lang and lang,
|
|
|
|
# which defaults to 'en-US'.
|
|
|
|
# For example,
|
|
|
|
#
|
|
|
|
# %html{html_attrs}
|
|
|
|
#
|
|
|
|
# becomes
|
|
|
|
#
|
|
|
|
# <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-US' lang='en-US'>
|
|
|
|
#
|
|
|
|
def html_attrs(lang = 'en-US')
|
|
|
|
{:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
|
|
|
|
end
|
|
|
|
|
2006-11-21 00:54:26 -05:00
|
|
|
# Increments the number of tabs the buffer automatically adds
|
|
|
|
# to the lines of the template.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# %h1 foo
|
|
|
|
# - tab_up
|
|
|
|
# %p bar
|
|
|
|
# - tab_down
|
|
|
|
# %strong baz
|
|
|
|
#
|
|
|
|
# Produces:
|
|
|
|
#
|
|
|
|
# <h1>foo</h1>
|
|
|
|
# <p>bar</p>
|
|
|
|
# <strong>baz</strong>
|
|
|
|
#
|
2006-11-07 22:27:37 -05:00
|
|
|
def tab_up(i = 1)
|
2008-02-22 21:40:26 -05:00
|
|
|
haml_buffer.tabulation += i
|
2006-11-07 22:27:37 -05:00
|
|
|
end
|
|
|
|
|
2008-03-05 19:18:31 -05:00
|
|
|
# Decrements the number of tabs the buffer automatically adds
|
2006-11-21 00:54:26 -05:00
|
|
|
# to the lines of the template.
|
|
|
|
#
|
2008-03-05 19:18:31 -05:00
|
|
|
# See also tab_up.
|
2006-11-07 22:27:37 -05:00
|
|
|
def tab_down(i = 1)
|
2008-02-22 21:40:26 -05:00
|
|
|
haml_buffer.tabulation -= i
|
2006-11-07 22:27:37 -05:00
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-20 02:03:31 -05:00
|
|
|
# Surrounds the given block of Haml code with the given characters,
|
|
|
|
# with no whitespace in between.
|
2006-11-21 00:54:26 -05:00
|
|
|
# For example:
|
2006-11-20 02:03:31 -05:00
|
|
|
#
|
|
|
|
# = surround '(', ')' do
|
|
|
|
# %a{:href => "food"} chicken
|
|
|
|
#
|
2006-11-21 00:54:26 -05:00
|
|
|
# Produces:
|
2006-11-20 02:03:31 -05:00
|
|
|
#
|
|
|
|
# (<a href='food'>chicken</a>)
|
|
|
|
#
|
|
|
|
# and
|
|
|
|
#
|
|
|
|
# = surround '*' do
|
|
|
|
# %strong angry
|
|
|
|
#
|
2006-11-21 00:54:26 -05:00
|
|
|
# Produces:
|
2006-11-20 02:03:31 -05:00
|
|
|
#
|
|
|
|
# *<strong>angry</strong>*
|
|
|
|
#
|
|
|
|
def surround(front, back = nil, &block)
|
|
|
|
back ||= front
|
|
|
|
output = capture_haml(&block)
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-20 02:03:31 -05:00
|
|
|
"#{front}#{output.chomp}#{back}\n"
|
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-20 02:03:31 -05:00
|
|
|
# Prepends the given character to the beginning of the Haml block,
|
|
|
|
# with no whitespace between.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# = precede '*' do
|
|
|
|
# %span.small Not really
|
|
|
|
#
|
2006-11-21 00:54:26 -05:00
|
|
|
# Produces:
|
2006-11-20 02:03:31 -05:00
|
|
|
#
|
|
|
|
# *<span class='small'>Not really</span>
|
|
|
|
#
|
|
|
|
def precede(char, &block)
|
|
|
|
"#{char}#{capture_haml(&block).chomp}\n"
|
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-20 02:03:31 -05:00
|
|
|
# Appends the given character to the end of the Haml block,
|
|
|
|
# with no whitespace between.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# click
|
|
|
|
# = succeed '.' do
|
|
|
|
# %a{:href=>"thing"} here
|
|
|
|
#
|
2006-11-21 00:54:26 -05:00
|
|
|
# Produces:
|
2006-11-20 02:03:31 -05:00
|
|
|
#
|
|
|
|
# click
|
|
|
|
# <a href='thing'>here</a>.
|
|
|
|
#
|
|
|
|
def succeed(char, &block)
|
|
|
|
"#{capture_haml(&block).chomp}#{char}\n"
|
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-20 01:46:44 -05:00
|
|
|
# Captures the result of the given block of Haml code,
|
|
|
|
# gets rid of the excess indentation,
|
|
|
|
# and returns it as a string.
|
|
|
|
# For example, after the following,
|
2006-11-20 01:11:16 -05:00
|
|
|
#
|
2006-11-20 01:46:44 -05:00
|
|
|
# .foo
|
|
|
|
# - foo = capture_haml(13) do |a|
|
|
|
|
# %p= a
|
2006-11-20 01:11:16 -05:00
|
|
|
#
|
|
|
|
# the local variable <tt>foo</tt> would be assigned to "<p>13</p>\n".
|
2006-11-21 00:54:26 -05:00
|
|
|
#
|
2006-11-20 01:11:16 -05:00
|
|
|
def capture_haml(*args, &block)
|
2009-01-22 17:42:10 -05:00
|
|
|
buffer = eval('_hamlout', block.binding) rescue haml_buffer
|
2008-09-24 05:07:13 -04:00
|
|
|
with_haml_buffer(buffer) do
|
|
|
|
position = haml_buffer.buffer.length
|
|
|
|
|
|
|
|
block.call(*args)
|
|
|
|
|
2009-01-22 17:44:37 -05:00
|
|
|
captured = haml_buffer.buffer.slice!(position..-1).split(/^/)
|
2008-09-24 05:07:13 -04:00
|
|
|
|
|
|
|
min_tabs = nil
|
|
|
|
captured.each do |line|
|
2008-12-22 18:26:58 -05:00
|
|
|
tabs = line.index(/[^ ]/) || line.length
|
2008-09-24 05:07:13 -04:00
|
|
|
min_tabs ||= tabs
|
|
|
|
min_tabs = min_tabs > tabs ? tabs : min_tabs
|
|
|
|
end
|
|
|
|
|
2009-02-07 05:33:58 -05:00
|
|
|
captured.map do |line|
|
2008-09-24 05:07:13 -04:00
|
|
|
line[min_tabs..-1]
|
2009-02-07 05:33:58 -05:00
|
|
|
end.join
|
2008-09-24 05:07:13 -04:00
|
|
|
end
|
2006-11-21 01:29:39 -05:00
|
|
|
end
|
2007-03-15 22:28:03 -04:00
|
|
|
|
2008-10-27 13:33:51 -04:00
|
|
|
def puts(*args) # :nodoc:
|
|
|
|
warn <<END
|
|
|
|
DEPRECATION WARNING:
|
2008-10-31 00:41:43 -04:00
|
|
|
The Haml #puts helper is deprecated and will be removed in version 2.4.
|
2008-10-27 13:33:51 -04:00
|
|
|
Use the #haml_concat helper instead.
|
|
|
|
END
|
2008-10-28 14:26:43 -04:00
|
|
|
haml_concat *args
|
2008-10-27 13:33:51 -04:00
|
|
|
end
|
|
|
|
|
2007-03-15 22:28:03 -04:00
|
|
|
# Outputs text directly to the Haml buffer, with the proper tabulation
|
2008-10-27 13:33:51 -04:00
|
|
|
def haml_concat(text = "")
|
2008-12-22 18:26:58 -05:00
|
|
|
haml_buffer.buffer << haml_indent << text.to_s << "\n"
|
2007-03-15 22:28:03 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2008-12-22 18:26:58 -05:00
|
|
|
# Returns the string that should be used to indent the current line
|
|
|
|
def haml_indent
|
|
|
|
' ' * haml_buffer.tabulation
|
|
|
|
end
|
|
|
|
|
2007-03-15 22:28:03 -04:00
|
|
|
#
|
2007-03-15 23:02:33 -04:00
|
|
|
# call-seq:
|
2008-06-05 21:32:17 -04:00
|
|
|
# haml_tag(name, *flags, attributes = {}) {...}
|
|
|
|
# haml_tag(name, text, *flags, attributes = {}) {...}
|
2007-03-15 23:02:33 -04:00
|
|
|
#
|
|
|
|
# Creates an HTML tag with the given name and optionally text and attributes.
|
|
|
|
# Can take a block that will be executed
|
|
|
|
# between when the opening and closing tags are output.
|
2008-10-27 13:33:51 -04:00
|
|
|
# If the block is a Haml block or outputs text using haml_concat,
|
2007-03-15 23:02:33 -04:00
|
|
|
# the text will be properly indented.
|
2008-04-08 02:09:17 -04:00
|
|
|
#
|
2008-06-05 21:32:17 -04:00
|
|
|
# <tt>flags</tt> is a list of symbol flags
|
|
|
|
# like those that can be put at the end of a Haml tag
|
|
|
|
# (<tt>:/</tt>, <tt>:<</tt>, and <tt>:></tt>).
|
|
|
|
# Currently, only <tt>:/</tt> and <tt>:<</tt> are supported.
|
|
|
|
#
|
2007-03-15 23:02:33 -04:00
|
|
|
# For example,
|
|
|
|
#
|
2008-03-05 19:18:31 -05:00
|
|
|
# haml_tag :table do
|
|
|
|
# haml_tag :tr do
|
|
|
|
# haml_tag :td, {:class => 'cell'} do
|
|
|
|
# haml_tag :strong, "strong!"
|
2008-10-27 13:33:51 -04:00
|
|
|
# haml_concat "data"
|
2007-03-15 22:28:03 -04:00
|
|
|
# end
|
2008-03-05 19:18:31 -05:00
|
|
|
# haml_tag :td do
|
2008-10-27 13:33:51 -04:00
|
|
|
# haml_concat "more_data"
|
2007-03-15 22:28:03 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2007-03-15 23:02:33 -04:00
|
|
|
# outputs
|
|
|
|
#
|
|
|
|
# <table>
|
|
|
|
# <tr>
|
|
|
|
# <td class='cell'>
|
|
|
|
# <strong>
|
|
|
|
# strong!
|
|
|
|
# </strong>
|
|
|
|
# data
|
|
|
|
# </td>
|
|
|
|
# <td>
|
|
|
|
# more_data
|
|
|
|
# </td>
|
|
|
|
# </tr>
|
|
|
|
# </table>
|
|
|
|
#
|
2008-06-05 21:32:17 -04:00
|
|
|
def haml_tag(name, *rest, &block)
|
2008-04-11 12:44:15 -04:00
|
|
|
name = name.to_s
|
2008-07-03 23:10:53 -04:00
|
|
|
text = rest.shift.to_s unless [Symbol, Hash, NilClass].any? {|t| rest.first.is_a? t}
|
2008-06-05 21:32:17 -04:00
|
|
|
flags = []
|
|
|
|
flags << rest.shift while rest.first.is_a? Symbol
|
|
|
|
attributes = Haml::Precompiler.build_attributes(haml_buffer.html?,
|
|
|
|
haml_buffer.options[:attr_wrapper],
|
|
|
|
rest.shift || {})
|
2008-04-11 12:44:15 -04:00
|
|
|
|
2008-06-05 21:32:17 -04:00
|
|
|
if text.nil? && block.nil? && (haml_buffer.options[:autoclose].include?(name) || flags.include?(:/))
|
2008-10-27 13:33:51 -04:00
|
|
|
haml_concat "<#{name}#{attributes} />"
|
2007-07-08 17:39:11 -04:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2008-06-05 21:32:17 -04:00
|
|
|
if flags.include?(:/)
|
|
|
|
raise Error.new("Self-closing tags can't have content.") if text
|
|
|
|
raise Error.new("Illegal nesting: nesting within a self-closing tag is illegal.") if block
|
|
|
|
end
|
|
|
|
|
2008-06-05 21:16:27 -04:00
|
|
|
tag = "<#{name}#{attributes}>"
|
|
|
|
if block.nil?
|
|
|
|
tag << text.to_s << "</#{name}>"
|
2008-10-27 13:33:51 -04:00
|
|
|
haml_concat tag
|
2008-06-05 21:16:27 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if text
|
|
|
|
raise Error.new("Illegal nesting: content can't be both given to haml_tag :#{name} and nested within it.")
|
2007-07-08 17:39:11 -04:00
|
|
|
end
|
2008-06-05 21:16:27 -04:00
|
|
|
|
2008-06-05 21:32:17 -04:00
|
|
|
if flags.include?(:<)
|
|
|
|
tag << capture_haml(&block).strip << "</#{name}>"
|
2008-10-27 13:33:51 -04:00
|
|
|
haml_concat tag
|
2008-06-05 21:32:17 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2008-10-27 13:33:51 -04:00
|
|
|
haml_concat tag
|
2008-06-05 21:16:27 -04:00
|
|
|
tab_up
|
|
|
|
block.call
|
|
|
|
tab_down
|
2008-10-27 13:33:51 -04:00
|
|
|
haml_concat "</#{name}>"
|
2009-02-22 03:32:32 -05:00
|
|
|
<<MESSAGE
|
|
|
|
<h1><code>haml_tag</code> outputs directly to the Haml template.
|
|
|
|
Disregard its return value and use the <code>-</code> operator.</h1>
|
|
|
|
MESSAGE
|
2007-03-15 22:28:03 -04:00
|
|
|
end
|
2008-02-22 21:33:49 -05:00
|
|
|
|
2008-03-18 15:40:04 -04:00
|
|
|
# Characters that need to be escaped to HTML entities from user input
|
2008-04-30 05:47:24 -04:00
|
|
|
HTML_ESCAPE = { '&'=>'&', '<'=>'<', '>'=>'>', '"'=>'"', "'"=>''', }
|
2008-03-18 15:40:04 -04:00
|
|
|
|
2008-03-14 19:39:19 -04:00
|
|
|
# Returns a copy of <tt>text</tt> with ampersands, angle brackets and quotes
|
|
|
|
# escaped into HTML entities.
|
|
|
|
def html_escape(text)
|
2008-04-30 05:47:24 -04:00
|
|
|
text.to_s.gsub(/[\"><&]/) { |s| HTML_ESCAPE[s] }
|
2008-03-18 15:40:04 -04:00
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2008-03-18 15:40:04 -04:00
|
|
|
# Escapes HTML entities in <tt>text</tt>, but without escaping an ampersand
|
|
|
|
# that is already part of an escaped entity.
|
|
|
|
def escape_once(text)
|
2008-04-30 05:47:24 -04:00
|
|
|
text.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |s| HTML_ESCAPE[s] }
|
2008-03-14 19:39:19 -04:00
|
|
|
end
|
|
|
|
|
2008-04-24 13:23:26 -04:00
|
|
|
# Returns whether or not the current template is a Haml template.
|
|
|
|
#
|
|
|
|
# This function, unlike other Haml::Helpers functions,
|
|
|
|
# also works in other ActionView templates,
|
|
|
|
# where it will always return false.
|
|
|
|
def is_haml?
|
2008-04-24 15:03:54 -04:00
|
|
|
!@haml_buffer.nil? && @haml_buffer.active?
|
2008-04-24 13:23:26 -04:00
|
|
|
end
|
|
|
|
|
2008-11-25 00:23:58 -05:00
|
|
|
# Returns whether or not +block+ is defined directly in a Haml template.
|
2008-11-22 23:53:19 -05:00
|
|
|
def block_is_haml?(block)
|
2009-01-22 17:42:10 -05:00
|
|
|
eval('_hamlout', block.binding)
|
2008-11-25 00:23:58 -05:00
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
2008-11-22 23:53:19 -05:00
|
|
|
end
|
|
|
|
|
2006-11-21 01:29:39 -05:00
|
|
|
private
|
|
|
|
|
2008-09-24 05:07:13 -04:00
|
|
|
# call-seq:
|
|
|
|
# with_haml_buffer(buffer) {...}
|
|
|
|
#
|
|
|
|
# Runs the block with the given buffer as the currently active buffer.
|
|
|
|
def with_haml_buffer(buffer)
|
|
|
|
@haml_buffer, old_buffer = buffer, @haml_buffer
|
|
|
|
old_buffer.active, was_active = false, old_buffer.active? if old_buffer
|
|
|
|
@haml_buffer.active = true
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@haml_buffer.active = false
|
|
|
|
old_buffer.active = was_active if old_buffer
|
|
|
|
@haml_buffer = old_buffer
|
|
|
|
end
|
|
|
|
|
2006-11-21 01:29:39 -05:00
|
|
|
# Gets a reference to the current Haml::Buffer object.
|
2008-02-22 21:40:26 -05:00
|
|
|
def haml_buffer
|
2008-04-24 12:52:26 -04:00
|
|
|
@haml_buffer
|
2006-11-21 01:29:39 -05:00
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2006-11-21 01:29:39 -05:00
|
|
|
# Gives a proc the same local "_hamlout" and "_erbout" variables
|
|
|
|
# that the current template has.
|
2008-02-22 21:40:26 -05:00
|
|
|
def haml_bind_proc(&proc)
|
|
|
|
_hamlout = haml_buffer
|
2006-11-21 01:29:39 -05:00
|
|
|
_erbout = _hamlout.buffer
|
|
|
|
proc { |*args| proc.call(*args) }
|
|
|
|
end
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2007-03-16 23:13:20 -04:00
|
|
|
include ActionViewExtensions if self.const_defined? "ActionViewExtensions"
|
2006-08-08 17:38:50 -04:00
|
|
|
end
|
2006-07-31 10:28:44 -04:00
|
|
|
end
|
2008-04-29 23:06:35 -04:00
|
|
|
|
|
|
|
class Object
|
|
|
|
# Haml overrides various ActionView helpers,
|
|
|
|
# which call an #is_haml? method
|
|
|
|
# to determine whether or not the current context object
|
|
|
|
# is a proper Haml context.
|
|
|
|
# Because ActionView helpers may be included in non-ActionView::Base classes,
|
|
|
|
# it's a good idea to define is_haml? for all objects.
|
|
|
|
def is_haml?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|