7stud2
(7stud --)
1
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>
···
--
Posted via http://www.ruby-forum.com/.
7stud2
(7stud --)
3
Robert Klemme wrote in post #1137701:
nokogiri pretty print at DuckDuckGo
First hit.
Thanks - for the reply.
You can see my comments below in the answer of @phrogz
to the first
hit(ruby - How do I pretty-print HTML with Nokogiri? - Stack Overflow)
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>
···
--
Posted via http://www.ruby-forum.com/\.