Guest User

Untitled

a guest
May 15th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. # usage: ruby convert-html-to-json.rb PATH_TO_FILE
  2.  
  3. require 'rubygems'
  4. require 'hpricot'
  5. require 'activesupport'
  6.  
  7. def node_to_s(node, indent_level=0)
  8. return nil unless node.elem?
  9.  
  10. ret = ""
  11. indentation = " " * indent_level
  12. ret << "\n%s%s(" % [indentation, node.name]
  13. ret << node.attributes.to_json.gsub(',', ', ') unless node.attributes.empty?
  14. if !node.children.empty? && node.children.detect{ |child| child.elem? }
  15. ret << "," unless node.attributes.empty?
  16. ret << node.children.map { |child| node_to_s(child, indent_level + 4) }.compact.join(',')
  17. ret << "\n%s)" % indentation
  18. elsif !node.children.empty? || !node.inner_text.blank?
  19. ret << ", " unless node.attributes.empty?
  20. ret << "'%s'" % [node.inner_text]
  21. ret << ")"
  22. else
  23. ret << ")"
  24. end
  25. return ret
  26. end
  27.  
  28. file = File.read(ARGV[0])
  29. doc = Hpricot.parse(file)
  30. print node_to_s(doc.root)
Add Comment
Please, Sign In to add comment