Created
March 23, 2014 08:42
-
-
Save dtan4/9720461 to your computer and use it in GitHub Desktop.
Convert <body> to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "nokogiri" | |
html = <<-EOS | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>hoge</title> | |
</head> | |
<body> | |
<h1>hoge</h1> | |
<div class="container"> | |
<h2>fuga</h2> | |
<ul> | |
<li>foo</li> | |
<li>bar</li> | |
</ul> | |
</div> | |
</body> | |
</html> | |
EOS | |
def get_children(element) | |
children = [] | |
element.children.select { |el| el.is_a? Nokogiri::XML::Element }.each do |el| | |
children << { name: el.name, children: get_children(el) } | |
end if element.children.length > 0 | |
children | |
end | |
body = Nokogiri::HTML.parse(html).css("body") | |
json = { name: "body", children: get_children(body) } | |
p json #=> {:name=>"body", :children=>[{:name=>"h1", :children=>[]}, {:name=>"div", :children=>[{:name=>"h2", :children=>[]}, {:name=>"ul", :children=>[{:name=>"li", :children=>[]}, {:name=>"li", :children=>[]}]}]}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment