module Haml::Helpers

def self.action_view?

Returns whether or not ActionView is installed on the system.
def self.action_view?
  @@action_view_defined
end

def capture_haml(*args, &block)


the local variable foo would be assigned to "

13

\n".

%p= a
- foo = capture_haml(13) do |a|
.foo

For example, after the following,
and returns it as a string.
gets rid of the excess indentation,
Captures the result of the given block of Haml code,
def capture_haml(*args, &block)
  capture_haml_with_buffer(haml_buffer.buffer, *args, &block)
end

def capture_haml_with_buffer(local_buffer, *args, &block)

is where the output of block goes.
Performs the function of capture_haml, assuming local_buffer
def capture_haml_with_buffer(local_buffer, *args, &block)
  position = local_buffer.length
  block.call *args
  captured = local_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
end

def escape_once(text)

that is already part of an escaped entity.
Escapes HTML entities in text, but without escaping an ampersand
def escape_once(text)
  text.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |s| HTML_ESCAPE[s] }
end

def find_and_preserve(input = '', tags = haml_buffer.options[:preserve], &block)

It defaults to the value of the :preserve option.
@tags@ is an array of tags to preserve.
into the HTML entities for endlines.
Uses preserve to convert any newlines inside whitespace-sensitive tags

find_and_preserve {...}
find_and_preserve(input, tags = haml_buffer.options[:preserve])
call-seq:
def find_and_preserve(input = '', tags = haml_buffer.options[:preserve], &block)
  return find_and_preserve(capture_haml(&block)) if block
  input = input.to_s
  input.gsub(/<(#{tags.map(&Regexp.method(:escape)).join('|')})([^>]*)>(.*?)(<\/\1>)/im) do
    "<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
  end
end

def haml_bind_proc(&proc)

that the current template has.
Gives a proc the same local "_hamlout" and "_erbout" variables
def haml_bind_proc(&proc)
  _hamlout = haml_buffer
  _erbout = _hamlout.buffer
  proc { |*args| proc.call(*args) }
end

def haml_buffer

Gets a reference to the current Haml::Buffer object.
def haml_buffer
  @haml_buffer
end

def haml_tag(name, *rest, &block)





more_data


data

strong!





outputs

end
end
end
puts "more_data"
haml_tag :td do
end
puts "data"
haml_tag :strong, "strong!"
haml_tag :td, {:class => 'cell'} do
haml_tag :tr do
haml_tag :table do

For example,

Currently, only :/ and :< are supported.
(:/, :<, and :>).
like those that can be put at the end of a Haml tag
flags is a list of symbol flags

the text will be properly indented.
If the block is a Haml block or outputs text using puts,
between when the opening and closing tags are output.
Can take a block that will be executed
Creates an HTML tag with the given name and optionally text and attributes.

haml_tag(name, text, *flags, attributes = {}) {...}
haml_tag(name, *flags, attributes = {}) {...}
call-seq:
def haml_tag(name, *rest, &block)
  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?(:/))
    puts "<#{name}#{attributes} />"
    return nil
  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}>"
    puts tag
    return
  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}>"
    puts tag
    return
  end
  puts tag
  tab_up
  block.call
  tab_down
  puts "</#{name}>"
  nil
end

def html_attrs(lang = 'en-US')




becomes

%html{html_attrs}

For example,
which defaults to 'en-US'.
It also takes an optional argument for the value of xml:lang and lang,
attributes of the html HTML element.
Returns a hash containing default assignments for the xmlns and xml:lang
def html_attrs(lang = 'en-US')
  {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
end

def html_escape(text)

escaped into HTML entities.
Returns a copy of text with ampersands, angle brackets and quotes
def html_escape(text)
  text.to_s.gsub(/[\"><&]/) { |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
using Haml.
as a normal ActionView rendering
as though it were in the same context
Initializes the current object

in Rails.
when using Haml helpers normally
Note: this does *not* need to be called
def init_haml_helpers
  @haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer))
  nil
end

def is_haml?

where it will always return false.
also works in other ActionView templates,
This function, unlike other Haml::Helpers functions,

Returns whether or not the current template is a Haml template.
def is_haml?
  !@haml_buffer.nil? && @haml_buffer.active?
end

def list_of(array, &block) # :yields: item

:yields: item


A book about all the stuff.


Description




  • All the stuff


    Title




  • Produces:

    %p= val
    %h3= key.humanize
    = list_of({:title => 'All the stuff', :description => 'A book about all the stuff.'}) do |key, val|

    And

  • yall

  • hello


  • Produces:

    = i[0]
    = list_of([['hello'], ['yall']]) do |i|

    For example:
    This creates a list of the results of the block.
    and putting the result into
  • elements.
    yielding each element to a Haml block
    and iterates over the object,
    Takes an Enumerable object and a block
  • def list_of(array, &block) # :yields: item
      to_return = array.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

    Note that this is automatically applied to Rails partials.

    particularly where helpers may behave differently when run from Haml.
    This is mainly useful for rendering sub-templates such as partials in a non-Haml language,

    (i.e. #is_haml? will return false).
    Runs a block of code in a non-Haml context

    non_haml { ... }
    call-seq:
    def non_haml
      was_active = @haml_buffer.active?
      @haml_buffer.active = false
      res = yield
      @haml_buffer.active = was_active
      res
    end

    def precede(char, &block)


    *Not really

    Produces:

    %span.small Not really
    = precede '*' do

    For example:
    with no whitespace between.
    Prepends the given character to the beginning of the Haml block,
    def precede(char, &block)
      "#{char}#{capture_haml(&block).chomp}\n"
    end

    def preserve(input = '', &block)

    whitespace-sensitive tags without screwing up the indentation.
    HTML entities for endlines so they'll render correctly in
    Takes any string, finds all the endlines and converts them to

    preserve {...}
    preserve(input)
    call-seq:
    def preserve(input = '', &block)
      return preserve(capture_haml(&block)) if block
      input.chomp("\n").gsub(/\n/, '&#x000A;').gsub(/\r/, '')
    end

    def puts(text = "")

    Outputs text directly to the Haml buffer, with the proper tabulation
    def puts(text = "")
      haml_buffer.buffer << ('  ' * haml_buffer.tabulation) << text.to_s << "\n"
      nil
    end

    def succeed(char, &block)


    here.
    click

    Produces:

    %a{:href=>"thing"} here
    = succeed '.' do
    click

    For example:
    with no whitespace between.
    Appends the given character to the end of the Haml block,
    def succeed(char, &block)
      "#{capture_haml(&block).chomp}#{char}\n"
    end

    def surround(front, back = nil, &block)


    *angry*

    Produces:

    %strong angry
    = surround '*' do

    and

    (chicken)

    Produces:

    %a{:href => "food"} chicken
    = surround '(', ')' do

    For example:
    with no whitespace in between.
    Surrounds the given block of Haml code with the given characters,
    def surround(front, back = nil, &block)
      back ||= front
      output = capture_haml(&block)
      "#{front}#{output.chomp}#{back}\n"
    end

    def tab_down(i = 1)

    See also tab_up.

    to the lines of the template.
    Decrements the number of tabs the buffer automatically adds
    def tab_down(i = 1)
      haml_buffer.tabulation -= i
    end

    def tab_up(i = 1)


    baz

    bar


    foo



    Produces:

    %strong baz
    - tab_down
    %p bar
    - tab_up
    %h1 foo

    For example:
    to the lines of the template.
    Increments the number of tabs the buffer automatically adds
    def tab_up(i = 1)
      haml_buffer.tabulation += i
    end