How do I pretty-print HTML with Nokogiri?

Viewed 30332

I wrote a web crawler in Ruby and I'm using Nokogiri::HTML to parse the page. I need to print the page out and while messing around in IRB I noticed a pretty_print method. However it takes a parameter and I can't figure out what it wants.

My crawler is caching the HTML of the webpages and writing it to files on my local machine. I would like to "pretty print" the HTML so that it looks nice and properly formatted when I do so.

8 Answers

By "pretty printing" of HTML page I presume you meant that you want to reformat the HTML structure with proper indentation. Nokogiri doesn't support this; the pretty_print method is for the "pp" library and the output is useful for debugging only.

There are several projects that understand HTML well enough to be able to reformat it without destroying whitespace that is actually significant (the famous one is HTML Tidy), but by Googling I've found this post titled "Pretty printing XHTML with Nokogiri and XSLT".

It comes down to this:

xsl = Nokogiri::XSLT(File.open("pretty_print.xsl"))
html = Nokogiri(File.open("source.html"))
puts xsl.apply_to(html).to_s

It requires you, of course, to download the linked XSL file to your filesystem. I've tried it very quickly on my machine and it works like a charm.

I know I am extremely late to answer this question, but still, I'll leave the answer. I tried all the above steps and it does work to an extent.

Nokogiri does format the HTML but does not care about the closing or the opening tag, hence pretty format is out of the picture.

I found a gem called htmlbeautifier that works like a charm. I hope other people who are still searching for the answer will find this valuable.

Simpler and works well

puts Nokogiri::HTML(File.read('terms.fr.html')).to_xhtml

why don't you try the pp method?

require 'pp'
pp some_var
Related