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 cleans
and pretty-prints HTML. Below my sig is an assertion that uses assert_tidy
to scan a @response.body, in a Rails functional test, and complain about any
shenanigans in the HTML. (Take out the assert_xml call if you would like to
use it without my assert_xpath plugin.)
Note that web browsers forgive shenanigans, but Rails developers should not,
because the cleanest HTML code is easiest to test.
Suppose someone gave us fresh HTML to import as eRB (.rhtml). Such as from
an obsolete PHP project. We ought to upgrade, cleanse, and pretty-print that
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 .rhtml
file with lots of <%%>, containing if statements and code-generating
things.
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...
--
Phlip
http://www.oreilly.com/catalog/9780596510657/
"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!
File.open(scratch_html, ''w''){|f| f.write(messy) }
gripes = `tidy -eq #{scratch_html} 2>&1`
gripes.split("\n")
exclude, inclued = gripes.partition do |g|
g =~ / - Info\: / or
g =~ /Warning\: missing \<\!DOCTYPE\> declaration/ or
g =~ /proprietary attribute/ or
g =~ /lacks "(summary|alt)" attribute/
end
puts inclued if verbosity == :noisy
assert_xml `tidy -wrap 1001 -asxhtml #{scratch_html} 2>/dev/null`
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> Suppose someone gave us fresh HTML to import as eRB (.rhtml). Such as from > an obsolete PHP project. We ought to upgrade, cleanse, and pretty-print > that HTML like this... > > tidy -i -asxhtml old.html > new.rhtmlBelow my sig is a program to temporarily replace <% and %> with <!--% and %--> and run Tidy. Save it as ''tidyErb.rb'', and use this usage line: usage: ruby tidyErb.rb <filename.rhtml> >output.rhtml ''filename.rhtml'' and ''output.rhtml'' may not be the same file. The program wastes a file called ''scratch.html'', with no attempt to avoid any source files with the same name... As a convenience, the program reports diagnostics to STDERR. Obey them (per assert_tidy), to improve your programs! Note that Tidy treats <!-- --> as flow-tags not block-tags. (My verbiage. <em> is a flow-tag, and <div> is the cannonical block-tag. Tidy line-wraps the former.) Searching for <% and moving them to their correct indentation (such as for <% end %>) is a small price to pay for clean HTML! Oh, also, review my gsubs to see if they match what Tidy did to your RHTML''s comments, and <%%> nested inside attributes. If I return to this project, I will just upgrade Tidy... -- Phlip http://www.oreilly.com/catalog/9780596510657/ "Test Driven Ajax (on Rails)" assert_xpath, assert_javascript, & assert_ajax if ARGV.size != 1 or !File.exist?(filename = ARGV.first) puts ''usage: ruby tidyErb.rb <filename.rhtml> >output.rhtml'' exit end rhtml = File.read(filename) escaped = rhtml.gsub(''<%'', ''<!--%'').gsub(''%>'', ''%-->'') File.open(''scratch.html'', ''w''){|f| f.write(escaped) } system(''tidy -i -asxhtml -m scratch.html'') html = File.read(''scratch.html'') File.unlink(''scratch.html'') html.gsub!(''<!--'', ''<!--'') # undo Tidy''s CGI-nanny escapes html.gsub!(''-->'', ''-->'') html.gsub!(''%3C!--%'', ''<!--%'') html.gsub!(''%20'', '' '') # TODO nest this gsub? html.gsub!(''%--%3E'', ''%-->'') rhtml = html.gsub(''%-->'', ''%>'').gsub(''<!--%'', ''<%'') puts rhtml --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---