2006-11-14 18:35:02 +00:00
|
|
|
require File.dirname(__FILE__) + '/helpers/action_view_mods'
|
|
|
|
|
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-11-06 03:01:04 +00:00
|
|
|
self.extend self
|
2006-11-14 18:35:02 +00:00
|
|
|
|
|
|
|
@@action_view = false
|
|
|
|
@@force_no_action_view = false
|
|
|
|
|
|
|
|
# Returns whether or not ActionView is available.
|
|
|
|
def self.action_view?
|
|
|
|
@@action_view
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets whether or not ActionView is available.
|
|
|
|
def self.action_view(value) # :nodoc:
|
|
|
|
@@action_view = value
|
|
|
|
end
|
2006-11-06 03:01:04 +00:00
|
|
|
|
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
|
|
|
# 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-11-08 03:27:37 +00:00
|
|
|
|
|
|
|
# Increments the tabulation modifier of the buffer. This is the
|
|
|
|
# number of tabs the buffer automatically adds to the lines of the
|
|
|
|
# template.
|
|
|
|
def tab_up(i = 1)
|
|
|
|
buffer.tabulation += i
|
|
|
|
end
|
|
|
|
|
|
|
|
# Decrements the tabulation modifier of the buffer. This is the
|
|
|
|
# number of tabs the buffer automatically adds to the lines of the
|
|
|
|
# template.
|
|
|
|
def tab_down(i = 1)
|
|
|
|
buffer.tabulation -= i
|
|
|
|
end
|
2006-11-20 06:11:16 +00:00
|
|
|
|
2006-11-20 07:03:31 +00:00
|
|
|
# Surrounds the given block of Haml code with the given characters,
|
|
|
|
# with no whitespace in between.
|
|
|
|
# For example,
|
|
|
|
#
|
|
|
|
# = surround '(', ')' do
|
|
|
|
# %a{:href => "food"} chicken
|
|
|
|
#
|
|
|
|
# makes
|
|
|
|
#
|
|
|
|
# (<a href='food'>chicken</a>)
|
|
|
|
#
|
|
|
|
# and
|
|
|
|
#
|
|
|
|
# = surround '*' do
|
|
|
|
# %strong angry
|
|
|
|
#
|
|
|
|
# makes
|
|
|
|
#
|
|
|
|
# *<strong>angry</strong>*
|
|
|
|
#
|
|
|
|
def surround(front, back = nil, &block)
|
|
|
|
back ||= front
|
|
|
|
output = capture_haml(&block)
|
|
|
|
|
|
|
|
"#{front}#{output.chomp}#{back}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prepends the given character to the beginning of the Haml block,
|
|
|
|
# with no whitespace between.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# = precede '*' do
|
|
|
|
# %span.small Not really
|
|
|
|
#
|
|
|
|
# compiles to
|
|
|
|
#
|
|
|
|
# *<span class='small'>Not really</span>
|
|
|
|
#
|
|
|
|
def precede(char, &block)
|
|
|
|
"#{char}#{capture_haml(&block).chomp}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Appends the given character to the end of the Haml block,
|
|
|
|
# with no whitespace between.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# click
|
|
|
|
# = succeed '.' do
|
|
|
|
# %a{:href=>"thing"} here
|
|
|
|
#
|
|
|
|
# compiles to
|
|
|
|
#
|
|
|
|
# click
|
|
|
|
# <a href='thing'>here</a>.
|
|
|
|
#
|
|
|
|
def succeed(char, &block)
|
|
|
|
"#{capture_haml(&block).chomp}#{char}\n"
|
|
|
|
end
|
|
|
|
|
2006-11-20 06:46:44 +00: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 06:11:16 +00:00
|
|
|
#
|
2006-11-20 06:46:44 +00:00
|
|
|
# .foo
|
|
|
|
# - foo = capture_haml(13) do |a|
|
|
|
|
# %p= a
|
2006-11-20 06:11:16 +00:00
|
|
|
#
|
|
|
|
# the local variable <tt>foo</tt> would be assigned to "<p>13</p>\n".
|
|
|
|
def capture_haml(*args, &block)
|
|
|
|
buffer_buffer = buffer.buffer
|
|
|
|
position = buffer_buffer.length
|
|
|
|
|
|
|
|
block.call(*args)
|
|
|
|
|
2006-11-20 06:46:44 +00:00
|
|
|
captured = buffer_buffer.slice!(position..-1)
|
|
|
|
|
|
|
|
min_tabs = nil
|
|
|
|
captured.each do |line|
|
|
|
|
tabs = line.index(/[^ ]/)
|
|
|
|
min_tabs ||= tabs
|
|
|
|
min_tabs = min_tabs > tabs ? tabs : min_tabs
|
|
|
|
end
|
|
|
|
|
|
|
|
result = captured.map do |line|
|
|
|
|
line[min_tabs..-1]
|
|
|
|
end
|
|
|
|
result.to_s
|
2006-11-20 06:11:16 +00:00
|
|
|
end
|
2006-11-08 03:27:37 +00:00
|
|
|
|
|
|
|
# Gets a reference to the current Haml::Buffer object.
|
|
|
|
def buffer # :nodoc:
|
|
|
|
@haml_stack[-1]
|
|
|
|
end
|
2006-11-14 18:35:02 +00:00
|
|
|
|
2006-11-16 01:15:01 +00:00
|
|
|
# Gives a proc the same local "_hamlout" and "_erbout" variables
|
|
|
|
# that the current template has.
|
2006-11-20 05:26:58 +00:00
|
|
|
def bind_proc(&proc) # :nodoc:
|
2006-11-16 01:15:01 +00:00
|
|
|
_hamlout = buffer
|
|
|
|
_erbout = _hamlout.buffer
|
|
|
|
proc { |*args| proc.call(*args) }
|
|
|
|
end
|
|
|
|
|
2006-11-14 18:35:02 +00:00
|
|
|
include ActionViewMods if self.const_defined? "ActionViewMods"
|
2006-08-08 21:38:50 +00:00
|
|
|
end
|
2006-07-31 14:28:44 +00:00
|
|
|
end
|