Library to parse ERB files

Posted by Douglas Sellers on Stack Overflow See other posts from Stack Overflow or by Douglas Sellers
Published on 2010-04-06T23:14:07Z Indexed on 2010/04/09 22:53 UTC
Read the original article Hit count: 518

Filed under:
|

I am attempting to parse, not evaluate, rails ERB files in a Hpricot/Nokogiri type manner. The files I am attempting to parse contain HTML fragments intermixed with dynamic content generated using ERB (standard rails view files) I am looking for a library that will not only parse the surrounding content, much the way that Hpricot or Nokogiri will but will also treat the ERB symbols, <%, <%= etc, as though they were html/xml tags.

Ideally I would get back a DOM like structure where the <%, <%= etc symbols would be included as their own node types.

I know that it is possible to hack something together using regular expressions but I was looking for something a bit more reliable as I am developing a tool that I need to run on a very large view code base where both the html content and the erb content are important.

For example, content such as:

blah blah blah
<div>My Great Text <%= my_dynamic_expression %></div>

Would return a tree structure like:

root
 - text_node (blah blah blah)
 - element (div)
    - text_node (My Great Text )
        - erb_node (<%=)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about erb