pull/28/head
ericchiang 10 years ago
parent 571adeb841
commit 2fb2dc084d

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

Loading…
Cancel
Save