1
0
mirror of https://github.com/ericchiang/pup synced 2024-11-24 08:58:08 +00:00
This commit is contained in:
ericchiang 2014-10-11 14:01:13 -04:00
parent 571adeb841
commit 2fb2dc084d

View File

@ -78,6 +78,9 @@ func jsonify(node *html.Node) map[string]interface{} {
}
}
}
if len(children) > 0 {
vals["children"] = children
}
return vals
}
@ -86,8 +89,19 @@ func (j JSONDisplayer) Display(nodes []*html.Node) {
var err error
switch len(nodes) {
case 1:
node := nodes[0]
if node.Type != html.DocumentNode {
jsonNode := jsonify(nodes[0])
data, err = json.MarshalIndent(&jsonNode, "", indentString)
} else {
children := []*html.Node{}
child := node.FirstChild
for child != nil {
children = append(children, child)
child = child.NextSibling
}
j.Display(children)
}
default:
jsonNodes := []map[string]interface{}{}
for _, node := range nodes {
@ -98,7 +112,7 @@ func (j JSONDisplayer) Display(nodes []*html.Node) {
if err != nil {
panic("Could not jsonify nodes")
}
fmt.Printf("%s\n", data)
fmt.Printf("%s", data)
}
var (