12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/googletastic/ext/xml.rb', line 12
def pretty_html(html)
return "" if html.nil?
raise "will throw segmentation error if not an Nokogiri::HTML::Document" unless xml.is_a?(Nokogiri::HTML::Document)
pretty_printer = File.join(File.dirname(__FILE__), "pretty_print.xsl")
xsl = Nokogiri::XSLT(IO.read(pretty_printer))
top_level_attributes = []
elements = html.is_a?(Nokogiri::XML::NodeSet) ? html : [html]
puts "ELEMENTS: #{elements[0].class}"
elements.each do |element|
top_level_attributes.push(element.attributes)
end
top_level_attributes.reverse!
puts "??"
children = xsl.transform(html)
puts "TOP ATTRIBUTES: #{top_level_attributes.inspect}"
children.each do |child|
attributes = top_level_attributes.pop
attributes.each do |k,v|
child[k] = v
end unless attributes.nil?
end
children.to_html.gsub(/\t/, "")
end
|