Forum: Ruby on Rails pretty-print and cleanse RHTML?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Phlip (Guest)
on 2007-07-22 01:01
(Received via mailing list)
Thanks' y'all for the answer to an easy question - ri.

Now here's a sick one.

HTML Tidy is an excellent program and system (by Dave Raggett) that
and pretty-prints HTML. Below my sig is an assertion that uses
to scan a @response.body, in a Rails functional test, and complain about
shenanigans in the HTML. (Take out the assert_xml call if you would like
use it without my assert_xpath plugin.)

Note that web browsers forgive shenanigans, but Rails developers should
because the cleanest HTML code is easiest to test.

Suppose someone gave us fresh HTML to import as eRB (.rhtml). Such as
an obsolete PHP project. We ought to upgrade, cleanse, and pretty-print
HTML like this...

  tidy -i -asxhtml old.html > new.rhtml

That upgrades the HTML, fixes missing and broken tags, etc.

Now suppose someone forgot to do that, and they invested their new
file with lots of <%%>, containing if statements and code-generating

Has anyone invented a pretty-printer that skips over the <%%> tags?

If not, I will presently report how to tidy that code by replacing <%
and %>
with <!--% and %-->, running tidy, and switching the tags back...

  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax

  def assert_tidy(messy = @response.body, verbosity = :noisy)
    scratch_html = RAILS_ROOT + '/../scratch.html'  #  TODO  tune me!, 'w'){|f|  f.write(messy)  }
    gripes = `tidy -eq #{scratch_html} 2>&1`

    exclude, inclued = gripes.partition do |g|
      g =~ / - Info\: /                                  or
      g =~ /Warning\: missing \<\!DOCTYPE\> declaration/ or
      g =~ /proprietary attribute/                       or
      g =~ /lacks "(summary|alt)" attribute/

    puts inclued if verbosity == :noisy
    assert_xml `tidy -wrap 1001 -asxhtml #{scratch_html} 2>/dev/null`
This topic is locked and can not be replied to.