module Haml::Helpers
def self.action_view?
-
(Boolean)
- Whether or not ActionView is loaded
def self.action_view? @@action_view_defined end
def block_is_haml?(block)
-
(Boolean)
- Whether or not `block` is defined directly in a Haml template
Parameters:
-
block
(Proc
) -- A Ruby block
def block_is_haml?(block) eval('_hamlout', block.binding) true rescue false end
def capture_haml(*args, &block)
- Yieldparam: args - `args`
Other tags:
- Yield: - A block of Haml code that will be converted to a string
Parameters:
-
args
(Array
) -- Arguments to pass into the block
def capture_haml(*args, &block) buffer = eval('_hamlout', block.binding) rescue haml_buffer with_haml_buffer(buffer) do position = haml_buffer.buffer.length haml_buffer.capture_position = position block.call(*args) captured = haml_buffer.buffer.slice!(position..-1).split(/^/) min_tabs = nil captured.each do |line| tabs = line.index(/[^ ]/) || line.length min_tabs ||= tabs min_tabs = min_tabs > tabs ? tabs : min_tabs end captured.map do |line| line[min_tabs..-1] end.join end ensure haml_buffer.capture_position = nil end
def escape_once(text)
-
(String)
- The sanitized string
Parameters:
-
text
(String
) -- The string to sanitize
def escape_once(text) Haml::Util.silence_warnings do text.to_s.gsub(/[\"><]|&(?!(?:[a-zA-Z]+|(#\d+));)/n) {|s| HTML_ESCAPE[s]} end end
def find_and_preserve(input = nil, tags = haml_buffer.options[:preserve], &block)
- Yield: - The block within which to escape newlines
Overloads:
-
find_and_preserve(tags = haml_buffer.options[:preserve])
-
find_and_preserve(input, tags = haml_buffer.options[:preserve])
Parameters:
-
input
(String
) -- The string within which to escape newlines -
tags
(Array
) -- Tags that should have newlines escaped
def find_and_preserve(input = nil, tags = haml_buffer.options[:preserve], &block) return find_and_preserve(capture_haml(&block), input || tags) if block input.to_s.gsub(/<(#{tags.map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im) do "<#{$1}#{$2}>#{preserve($3)}</#{$1}>" end end
def haml_bind_proc(&proc)
-
(Proc)
- A new proc with the new variables bound
Parameters:
-
proc
(#call
) -- The proc to bind
def haml_bind_proc(&proc) _hamlout = haml_buffer _erbout = _hamlout.buffer proc { |*args| proc.call(*args) } end
def haml_buffer
-
(Haml::Buffer)
-
def haml_buffer @haml_buffer end
def haml_concat(text = "")
-
text
(#to_s
) -- The text to output
def haml_concat(text = "") haml_buffer.buffer << haml_indent << text.to_s << "\n" ErrorReturn.new("haml_concat") end
def haml_indent
-
(String)
- The indentation string for the current line
def haml_indent ' ' * haml_buffer.tabulation end
def haml_tag(name, *rest, &block)
-
text
(#to_s
) -- The text within the tag -
flags
(Array
) -- Haml end-of-tag flags -
name
(#to_s
) -- The name of the tag
Overloads:
-
haml_tag(name, text, *flags, attributes = {})
-
haml_tag(name, *flags, attributes = {})
Other tags:
- Yield: - The block of Haml code within the tag
def haml_tag(name, *rest, &block) ret = ErrorReturn.new("haml_tag") name = name.to_s text = rest.shift.to_s unless [Symbol, Hash, NilClass].any? {|t| rest.first.is_a? t} 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 || {}) if text.nil? && block.nil? && (haml_buffer.options[:autoclose].include?(name) || flags.include?(:/)) haml_concat "<#{name}#{attributes} />" return ret end 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 tag = "<#{name}#{attributes}>" if block.nil? tag << text.to_s << "</#{name}>" haml_concat tag return ret end if text raise Error.new("Illegal nesting: content can't be both given to haml_tag :#{name} and nested within it.") end if flags.include?(:<) tag << capture_haml(&block).strip << "</#{name}>" haml_concat tag return ret end haml_concat tag tab_up block.call tab_down haml_concat "</#{name}>" ret end
def html_attrs(lang = 'en-US')
-
({#to_s => String})
- The attribute hash
Parameters:
-
lang
(String
) -- The value of `xml:lang` and `lang`
def html_attrs(lang = 'en-US') {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang} end
def html_escape(text)
-
(String)
- The sanitized string
Parameters:
-
text
(String
) -- The string to sanitize
def html_escape(text) text.to_s.gsub(/[\"><&]/n) {|s| HTML_ESCAPE[s]} end
def init_haml_helpers
context.haml_tag :p, "Stuff"
context.init_haml_helpers
end
include Haml::Helpers
class << context
context = Object.new
For example:
other than the normal setup with ActionView.
This is useful if you want to use the helpers in a context
as a normal ActionView instance using Haml.
Initializes the current object as though it were in the same context
normally in Rails.
Note: this does **not** need to be called when using Haml helpers
def init_haml_helpers @haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer)) nil end
def is_haml?
-
(Boolean)
- Whether or not the current template is a Haml template
def is_haml? !@haml_buffer.nil? && @haml_buffer.active? end
def list_of(enum, &block)
- Yieldparam: item - An element of `enum`
Other tags:
- Yield: - A block which contains Haml code that goes within list items
Parameters:
-
enum
(Enumerable
) -- The list of objects to iterate over
def list_of(enum, &block) to_return = enum.collect do |i| result = capture_haml(i, &block) if result.count("\n") > 1 result.gsub!("\n", "\n ") result = "\n #{result.strip}\n" else result.strip! end "<li>#{result}</li>" end to_return.join("\n") end
def non_haml
- Yield: - A block which won't register as Haml
def non_haml was_active = @haml_buffer.active? @haml_buffer.active = false yield ensure @haml_buffer.active = was_active end
def precede(str, &block)
- Yield: - A block of Haml to prepend to
Parameters:
-
str
(String
) -- The string to add before the Haml
def precede(str, &block) "#{str}#{capture_haml(&block).chomp}\n" end
def preserve(input = nil, &block)
- Yield: - The block within which to escape newlines
Overloads:
-
perserve
-
perserve(input)
Parameters:
-
input
(String
) -- The string within which to escape all newlines
def preserve(input = nil, &block) return preserve(capture_haml(&block)) if block input.to_s.chomp("\n").gsub(/\n/, '
').gsub(/\r/, '') end
def puts(*args)
- See: #haml_concat -
Deprecated:
- This will be removed in version 3.0.
def puts(*args) warn <<END ECATION WARNING: Haml #puts helper is deprecated and will be removed in version 3.0. the #haml_concat helper instead. haml_concat(*args) end
def succeed(str, &block)
- Yield: - A block of Haml to append to
Parameters:
-
str
(String
) -- The string to add after the Haml
def succeed(str, &block) "#{capture_haml(&block).chomp}#{str}\n" end
def surround(front, back = front, &block)
- Yield: - A block of Haml to surround
Parameters:
-
back
(String
) -- The string to add after the Haml -
front
(String
) -- The string to add before the Haml
def surround(front, back = front, &block) output = capture_haml(&block) "#{front}#{output.chomp}#{back}\n" end
def tab_down(i = 1)
- See: #tab_up -
Parameters:
-
i
(Fixnum
) -- The number of tabs by which to decrease the indentation
def tab_down(i = 1) haml_buffer.tabulation -= i end
def tab_up(i = 1)
- See: #tab_down -
Parameters:
-
i
(Fixnum
) -- The number of tabs by which to increase the indentation
def tab_up(i = 1) haml_buffer.tabulation += i end
def with_haml_buffer(buffer)
- Yield: - A block in which the given buffer should be used
Parameters:
-
buffer
(Haml::Buffer
) -- The Haml buffer to use temporarily
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