Class Haml::HTML::ERB
In: lib/haml/html/erb.rb
Parent: Erubis::Basic::Engine

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

Uses [Erubis](www.kuwata-lab.com/erubis)'s extensible parsing powers to parse the ERB in a reliable way, and [ruby_parser](parsetree.rubyforge.org/)'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.

Methods

Public Class methods

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

@param template [String] The ERB template @return [String] The output document @see Haml::HTML::ERB

[Source]

    # File lib/haml/html/erb.rb, line 27
27:       def self.compile(template)
28:         new(template).src
29:       end

Public Instance methods

`html2haml` doesn‘t support debugging expressions.

[Source]

    # File lib/haml/html/erb.rb, line 81
81:       def add_expr_debug(src, code)
82:         raise Haml::Error.new("html2haml doesn't support debugging expressions.")
83:       end

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.

@param src [String] The source buffer @param code [String] The Ruby expression to add to the buffer

[Source]

    # File lib/haml/html/erb.rb, line 75
75:       def add_expr_literal(src, code)
76:         src << '<haml:loud>' << h(code) << '</haml:loud>'
77:         src << '<haml:block>' if block_opener?(code)
78:       end

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

[Source]

    # File lib/haml/html/erb.rb, line 40
40:       def add_postamble(src); end

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

[Source]

    # File lib/haml/html/erb.rb, line 37
37:       def add_preamble(src); end

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`.

@param src [String] The source buffer @param code [String] The Ruby statement to add to the buffer

[Source]

    # File lib/haml/html/erb.rb, line 61
61:       def add_stmt(src, code)
62:         src << '</haml:block>' if block_closer?(code) || mid_block?(code)
63:         src << '<haml:silent>' << h(code) << '</haml:silent>' unless code.strip == "end"
64:         src << '<haml:block>' if block_opener?(code) || mid_block?(code)
65:       end

Concatenates the text onto the source buffer.

@param src [String] The source buffer @param text [String] The raw text to add to the buffer

[Source]

    # File lib/haml/html/erb.rb, line 46
46:       def add_text(src, text)
47:         src << text
48:       end

`html2haml` doesn‘t support HTML-escaped expressions.

[Source]

    # File lib/haml/html/erb.rb, line 32
32:       def escaped_expr(code)
33:         raise Haml::Error.new("html2haml doesn't support escaped expressions.")
34:       end

Private Instance methods

Checks if a string of Ruby code closes a block. This is always `end` followed optionally by some method calls.

@param code [String] Ruby code to check @return [Boolean]

[Source]

     # File lib/haml/html/erb.rb, line 123
123:       def block_closer?(code)
124:         valid_ruby?("begin\n" + code)
125:       end

Checks if a string of Ruby code opens a block. This could either be something like `foo do |a|` or a keyword that requires a matching `end` like `if`, `begin`, or `case`.

@param code [String] Ruby code to check @return [Boolean]

[Source]

     # File lib/haml/html/erb.rb, line 113
113:       def block_opener?(code)
114:         valid_ruby?(code + "\nend") ||
115:           valid_ruby?(code + "\nwhen foo\nend")
116:       end

HTML-escaped some text (in practice, always Ruby code). A utility method.

@param text [String] The text to escape @return [String] The escaped text

[Source]

    # File lib/haml/html/erb.rb, line 92
92:       def h(text)
93:         CGI.escapeHTML(text)
94:       end

Checks if a string of Ruby code comes in the middle of a block. This could be a keyword like `else`, `rescue`, or `when`, or even `end` with a method call that takes a block.

@param code [String] Ruby code to check @return [Boolean]

[Source]

     # File lib/haml/html/erb.rb, line 133
133:       def mid_block?(code)
134:         return if valid_ruby?(code)
135:         valid_ruby?("if foo\n#{code}\nend") || # else, elsif
136:           valid_ruby?("begin\n#{code}\nend") || # rescue, ensure
137:           valid_ruby?("case foo\n#{code}\nend") # when
138:       end

Returns whether the code is valid Ruby code on its own.

@param code [String] Ruby code to check @return [Boolean]

[Source]

     # File lib/haml/html/erb.rb, line 100
100:       def valid_ruby?(code)
101:         RubyParser.new.parse(code)
102:       rescue Racc::ParseError => e
103:         false
104:       end

[Validate]