Class: Haml::HTML::ERB

Inherits:
Erubis::Basic::Engine
  • Object
show all
Defined in:
lib/haml/html/erb.rb

Overview

A class for converting ERB code into a format that's easier for the Haml::HTML Hpricot-based parser to understand.

Uses Erubis's extensible parsing powers to parse the ERB in a reliable way, and ruby_parser's Ruby knowledge to figure out whether a given chunk of Ruby code starts a block or not.

The ERB tags are converted to HTML tags in the following way. <% ... %> is converted into <haml:silent> ... </haml:silent>. <%= ... %> is converted into <haml:loud> ... </haml:loud>. Finally, if either of these opens a Ruby block, <haml:block> ... </haml:block> will wrap the entire contents of the block - that is, everything that should be indented beneath the previous silent or loud tag.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(template) ⇒ String

Compiles an ERB template into a HTML document containing haml: tags.

Parameters:

  • template (String)

    The ERB template

Returns:

  • (String)

    The output document

See Also:



27
28
29
# File 'lib/haml/html/erb.rb', line 27

def self.compile(template)
  new(template).src
end

Instance Method Details

#add_expr_debug(src, code)

html2haml doesn't support debugging expressions.

Raises:



81
82
83
# File 'lib/haml/html/erb.rb', line 81

def add_expr_debug(src, code)
  raise Haml::Error.new("html2haml doesn't support debugging expressions.")
end

#add_expr_literal(src, code)

Concatenates a Ruby expression that's printed to the document onto the source buffer. This uses the <haml:silent> tag, and may open a Ruby block with the <haml:block> tag. An expression never closes a block.

Parameters:

  • src (String)

    The source buffer

  • code (String)

    The Ruby expression to add to the buffer



75
76
77
78
# File 'lib/haml/html/erb.rb', line 75

def add_expr_literal(src, code)
  src << '<haml:loud>' << h(code) << '</haml:loud>'
  src << '<haml:block>' if block_opener?(code)
end

#add_postamble(src)

The ERB-to-Hamlized-HTML conversion has no postamble.



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

def add_postamble(src); end

#add_preamble(src)

The ERB-to-Hamlized-HTML conversion has no preamble.



37
# File 'lib/haml/html/erb.rb', line 37

def add_preamble(src); end

#add_stmt(src, code)

Concatenates a silent Ruby statement onto the source buffer. This uses the <haml:silent> tag, and may close and/or open a Ruby block with the <haml:block> tag.

In particular, a block is closed if this statement is some form of end, opened if it's a block opener like do, if, or begin, and both closed and opened if it's a mid-block keyword like else or when.

Parameters:

  • src (String)

    The source buffer

  • code (String)

    The Ruby statement to add to the buffer



61
62
63
64
65
# File 'lib/haml/html/erb.rb', line 61

def add_stmt(src, code)
  src << '</haml:block>' if block_closer?(code) || mid_block?(code)
  src << '<haml:silent>' << h(code) << '</haml:silent>' unless code.strip == "end"
  src << '<haml:block>' if block_opener?(code) || mid_block?(code)
end

#add_text(src, text)

Concatenates the text onto the source buffer.

Parameters:

  • src (String)

    The source buffer

  • text (String)

    The raw text to add to the buffer



46
47
48
# File 'lib/haml/html/erb.rb', line 46

def add_text(src, text)
  src << text
end

#escaped_expr(code)

html2haml doesn't support HTML-escaped expressions.

Raises:



32
33
34
# File 'lib/haml/html/erb.rb', line 32

def escaped_expr(code)
  raise Haml::Error.new("html2haml doesn't support escaped expressions.")
end