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('!!defined?(_hamlout)', block.binding) 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('if defined? _hamlout then _hamlout else nil end', block.binding) || haml_buffer with_haml_buffer(buffer) do position = haml_buffer.buffer.length haml_buffer.capture_position = position value = block.call(*args) captured = haml_buffer.buffer.slice!(position..-1) if captured == '' and value != haml_buffer.buffer captured = (value.is_a?(String) ? value : nil) end return nil if captured.nil? return (haml_buffer.options[:ugly] ? captured : prettify(captured)) 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) text = text.to_s text.gsub(HTML_ESCAPE_ONCE_REGEX, HTML_ESCAPE) end
def escape_once(text)
def escape_once(text) text = text.to_s text.gsub(HTML_ESCAPE_ONCE_REGEX){|s| HTML_ESCAPE[s]} 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 re = /<(#{tags.map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im input.to_s.gsub(re) do |s| s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible "<#{$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 #double assignment is to avoid warnings _erbout = _erbout = _hamlout.buffer proc { |*args| proc.call(*args) } end
def haml_buffer
-
(Haml::Buffer)
-
def haml_buffer @haml_buffer if defined? @haml_buffer end
def haml_concat(text = "")
-
text
(#to_s
) -- The text to output
def haml_concat(text = "") unless haml_buffer.options[:ugly] || haml_indent == 0 haml_buffer.buffer << haml_indent << text.to_s.gsub("\n", "\n" + haml_indent) << "\n" else haml_buffer.buffer << text.to_s << "\n" end 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)
-
flags
(Array
) -- Haml end-of-tag flags -
text
(#to_s
) -- The text within the tag -
name
(#to_s
) -- The name of the tag
Overloads:
-
haml_tag(name, text, *flags, attributes = {})
-
haml_tag(name, *rest, attributes = {})
Other tags:
- Yield: - The block of Haml code within the tag
def haml_tag(name, *rest, &block) ret = ErrorReturn.new("haml_tag") 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 attrs = (rest.shift || {}) attrs.keys.each {|key| attrs[key.to_s] = attrs.delete(key)} unless attrs.empty? name, attrs = merge_name_and_attributes(name.to_s, attrs) attributes = Haml::Compiler.build_attributes(haml_buffer.html?, haml_buffer.options[:attr_wrapper], haml_buffer.options[:escape_attrs], haml_buffer.options[:hyphenate_data_attrs], attrs) 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(Error.message(:self_closing_content)) if text raise Error.new(Error.message(:illegal_nesting_self_closing)) if block end tag = "<#{name}#{attributes}>" if block.nil? text = text.to_s if text.include?("\n") haml_concat tag tab_up haml_concat text tab_down haml_concat "</#{name}>" else tag << text << "</#{name}>" haml_concat tag end return ret end if text raise Error.new(Error.message(:illegal_nesting_line, name)) 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 = text.to_s text.gsub(HTML_ESCAPE_REGEX, HTML_ESCAPE) end
def html_escape(text)
def html_escape(text) text = text.to_s text.gsub(HTML_ESCAPE_REGEX) {|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, Options.new.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, opts={}, &block)
- Yieldparam: item - An element of `enum`
Other tags:
- Yield: - A block which contains Haml code that goes within list items
Parameters:
-
opts
(Enumerable<#to_s,#to_s>
) -- Each key/value pair will become an attribute pair for each list item element. -
enum
(Enumerable
) -- The list of objects to iterate over
def list_of(enum, opts={}, &block) opts_attributes = opts.empty? ? "" : " ".<<(opts.map{|k,v| "#{k}='#{v}'" }.join(" ")) to_return = enum.collect do |i| result = capture_haml(i, &block) if result.count("\n") > 1 result = result.gsub("\n", "\n ") result = "\n #{result.strip}\n" else result = result.strip end %Q!<li#{opts_attributes}>#{result}</li>! end to_return.join("\n") end
def merge_name_and_attributes(name, attributes_hash = {})
Parses the tag name used for \{#haml\_tag}
def merge_name_and_attributes(name, attributes_hash = {}) # skip merging if no ids or classes found in name return name, attributes_hash unless name =~ /^(.+?)?([\.#].*)$/ return $1 || "div", Buffer.merge_attrs( Haml::Parser.parse_class_and_id($2), attributes_hash) 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 prettify(text)
def prettify(text) text = text.split(/^/) text.delete('') min_tabs = nil text.each do |line| tabs = line.index(/[^ ]/) || line.length min_tabs ||= tabs min_tabs = min_tabs > tabs ? tabs : min_tabs end text.map do |line| line.slice(min_tabs, line.length) end.join 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, old_was_active = false, old_buffer.active? if old_buffer @haml_buffer.active, was_active = true, @haml_buffer.active? yield ensure @haml_buffer.active = was_active old_buffer.active = old_was_active if old_buffer @haml_buffer = old_buffer end
def with_tabs(i)
- Yield: - A block in which the indentation will be `i` spaces
Parameters:
-
i
(Fixnum
) -- The number of tabs to use
def with_tabs(i) old_tabs = haml_buffer.tabulation haml_buffer.tabulation = i yield ensure haml_buffer.tabulation = old_tabs end