Class: Haml::HTML

Inherits:
Object show all
Defined in:
/build/buildd/ruby-haml-3.1.4/lib/haml/html.rb,
/build/buildd/ruby-haml-3.1.4/lib/haml/html/erb.rb

Overview

Converts HTML documents into Haml templates. Depends on Hpricot for HTML parsing. If ERB conversion is being used, also depends on Erubis to parse the ERB and ruby_parser to parse the Ruby code.

Example usage:

Haml::HTML.new("<a href='http://google.com'>Blat</a>").render
  #=> "%a{:href => 'http://google.com'} Blat"

Defined Under Namespace

Classes: ERB

Constant Summary

Instance Method Summary (collapse)

Constructor Details

- (HTML) initialize(template, options = {})

A new instance of HTML

Parameters:

  • template (String, Hpricot::Node)

    The HTML template to convert

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :erb (Boolean) — default: false

    Whether or not to parse ERB’s <%= %> and <% %> into Haml’s = and -

  • :xhtml (Boolean) — default: false

    Whether or not to parse the HTML strictly as XHTML



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/html.rb', line 121

def initialize(template, options = {})
  @options = options

  if template.is_a? Hpricot::Node
    @template = template
  else
    if template.is_a? IO
      template = template.read
    end

    template = Haml::Util.check_encoding(template) {|msg, line| raise Haml::Error.new(msg, line)}

    if @options[:erb]
      require 'haml/html/erb'
      template = ERB.compile(template)
    end

    method = @options[:xhtml] ? Hpricot.method(:XML) : method(:Hpricot)
    @template = method.call(template.gsub('&', '&amp;'))
  end
end

Instance Method Details

- render Also known as: to_haml

Processes the document and returns the result as a string containing the Haml template.



145
146
147
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/html.rb', line 145

def render
  @template.to_haml(0, @options)
end