Forum: Ruby Nokogiri::XML::NodeSet#to_html is not doing pretty printing

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-22 21:43
Can the below output be pretty-print ?

require 'nokogiri'

doc =  Nokogiri::HTML::DocumentFragment.parse(<<-html)
<p>First</p>
<p>Second</p>
<p>Third</p>
html

nodeset = doc.css("p")
new_node = Nokogiri::XML::Node.new('span',doc)
new_node << nodeset[1..-1]
nodeset.first.after(new_node)
puts doc.to_html
# >> <p>First</p><span><p>Second</p>
# >> <p>Third</p></span>
# >>

expected :

# >> <p>First</p>
# >>    <span>
# >>        <p>Second</p>
# >>        <p>Third</p>
# >     </span>
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-02-23 13:40
(Received via mailing list)
On Sat, Feb 22, 2014 at 9:43 PM, Arup Rakshit <lists@ruby-forum.com>
wrote:
> nodeset = doc.css("p")
> # >> <p>First</p>
> # >>    <span>
> # >>        <p>Second</p>
> # >>        <p>Third</p>
> # >     </span>

https://duckduckgo.com/?q=nokogiri%20pretty%20prin...

First hit.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-23 14:15
Robert Klemme wrote in post #1137701:

> https://duckduckgo.com/?q=nokogiri%20pretty%20prin...
>
> First hit.

Thanks - for the reply.

You can see my comments below in the answer of @phrogz
to the first
hit(http://stackoverflow.com/questions/1898829/how-do-...)

But it not helped me.

require 'nokogiri'

doc =  Nokogiri.HTML(<<-html,&:noblanks)
<p>First</p>
<p>Second</p>
<p>Third</p>
html


nodeset = doc.css("p")
new_node = Nokogiri::XML::Node.new('span',doc)
new_node << nodeset[1..-1]
nodeset.first.after(new_node)
puts doc.to_html( indent:3, indent_text:"." )
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body>
# >> <p>First</p>
# >> <span><p>Second</p>
# >> <p>Third</p></span>
# >>
# >> </body></html>
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.