awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.08k stars 454 forks source link

Indenting Nokogiri HTML #177

Open epitron opened 9 years ago

epitron commented 9 years ago

Hi there! Love this gem!

I noticed that the Nokogiri pretty printer doesn't always indent HTML as nicely as it could. The problem seems to be that Nokogiri's #to_xml method behaves slightly differently from the #to_xhtml method.

Examples:

[4] pry(main)> puts Nokogiri::HTML("<div><span><b>whee").to_xml(indent: 2);               
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div><span><b>whee</b></span></div></body></html>

vs.

[5] pry(main)> puts Nokogiri::HTML("<div><span><b>whee").to_xhtml(indent: 2);             
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <div>
      <span>
        <b>whee</b>
      </span>
    </div>
  </body>
</html>

The second one looks a lot nicer to me -- it's especially nice when the HTML document has very deep nesting.

What say you? :)

7dir commented 6 years ago

https://github.com/awesome-print/awesome_print/blob/master/lib/awesome_print/ext/nokogiri.rb