Class: Haml::HTML

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

Overview

This class contains the functionality used in the html2haml utility, namely converting HTML documents to Haml templates. It depends on Hpricot for HTML parsing (code.whytheluckystiff.net/hpricot/).

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new instance of Haml::HTML that will compile the given template, which can either be a string containing HTML or an Hpricot node, to a Haml string when render is called.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/haml/html.rb', line 16

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

Class Method Details

.haml_tag_loud(text) ⇒ Object



201
202
203
# File 'lib/haml/html.rb', line 201

def self.haml_tag_loud(text)
  "= #{text.gsub(/\n\s*/, ' ').strip}\n"
end

.haml_tag_silent(text) ⇒ Object



205
206
207
# File 'lib/haml/html.rb', line 205

def self.haml_tag_silent(text)
  text.split("\n").map { |line| "- #{line.strip}\n" }.join
end

.optionsObject

:stopdoc:



73
74
75
# File 'lib/haml/html.rb', line 73

def self.options
  @@options
end

Instance Method Details

#renderObject Also known as: to_haml

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



38
39
40
# File 'lib/haml/html.rb', line 38

def render
  @template.to_haml(0)
end