Class: Haml::HTML

Inherits:
Object show all
Defined in:
lib/haml/html.rb

Overview

Converts HTML documents into Haml templates. Depends on [Hpricot](code.whytheluckystiff.net/hpricot/) for HTML parsing.

Example usage:

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

Constant Summary collapse

TEXT_REGEXP =
/^(\s*).*$/

Instance Method Summary collapse

Constructor Details

#initialize(template, options = {}) ⇒ HTML

Returns 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):

  • :rhtml (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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/haml/html.rb', line 22

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

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

    if @options[:rhtml]
      match_to_html(template, /<%=(.*?)-?%>/m, 'loud')
      match_to_html(template, /<%-?(.*?)-?%>/m,  'silent')
    end

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

Instance Method Details

#renderObject Also known as: to_haml

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



44
45
46
# File 'lib/haml/html.rb', line 44

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