Class: Html2haml::HTML::ERB
- Inherits:
-
Erubis::Basic::Engine
- Object
- Erubis::Basic::Engine
- Html2haml::HTML::ERB
- Defined in:
- lib/html2haml/html/erb.rb
Overview
A class for converting ERB code into a format that's easier for the Html2haml::HTML Nokogiri-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>
.
<%== ... %>
is converted into <haml_loud raw=raw> ... </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
-
.compile(template) ⇒ String
Compiles an ERB template into a HTML document containing
haml_*
tags.
Instance Method Summary collapse
-
#add_expr_debug(src, code) ⇒ Object
html2haml
doesn't support debugging expressions. - #add_expr_escaped(src, code) ⇒ Object
-
#add_expr_literal(src, code) ⇒ Object
Concatenates a Ruby expression that's printed to the document onto the source buffer.
-
#add_postamble(src) ⇒ Object
The ERB-to-Hamlized-HTML conversion has no postamble.
-
#add_preamble(src) ⇒ Object
The ERB-to-Hamlized-HTML conversion has no preamble.
-
#add_stmt(src, code) ⇒ Object
Concatenates a silent Ruby statement onto the source buffer.
-
#add_text(src, text) ⇒ Object
Concatenates the text onto the source buffer.
Class Method Details
.compile(template) ⇒ String
Compiles an ERB template into a HTML document containing haml_*
tags.
28 29 30 |
# File 'lib/html2haml/html/erb.rb', line 28 def self.compile(template) new(template).src end |
Instance Method Details
#add_expr_debug(src, code) ⇒ Object
html2haml
doesn't support debugging expressions.
81 82 83 |
# File 'lib/html2haml/html/erb.rb', line 81 def add_expr_debug(src, code) raise Haml::Error.new("html2haml doesn't support debugging expressions.") end |
#add_expr_escaped(src, code) ⇒ Object
76 77 78 |
# File 'lib/html2haml/html/erb.rb', line 76 def add_expr_escaped(src, code) src << "<haml_loud raw=raw>" << h(code) << "</haml_loud>" end |
#add_expr_literal(src, code) ⇒ Object
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.
71 72 73 74 |
# File 'lib/html2haml/html/erb.rb', line 71 def add_expr_literal(src, code) src << '<haml_loud>' << h(code) << '</haml_loud>' src << '<haml_block>' if block_opener?(code) end |
#add_postamble(src) ⇒ Object
The ERB-to-Hamlized-HTML conversion has no postamble.
36 |
# File 'lib/html2haml/html/erb.rb', line 36 def add_postamble(src); end |
#add_preamble(src) ⇒ Object
The ERB-to-Hamlized-HTML conversion has no preamble.
33 |
# File 'lib/html2haml/html/erb.rb', line 33 def add_preamble(src); end |
#add_stmt(src, code) ⇒ Object
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
.
57 58 59 60 61 |
# File 'lib/html2haml/html/erb.rb', line 57 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) ⇒ Object
Concatenates the text onto the source buffer.
42 43 44 |
# File 'lib/html2haml/html/erb.rb', line 42 def add_text(src, text) src << text end |