mirror of
https://github.com/ericchiang/pup
synced 2025-01-29 17:11:00 +00:00
parent
8d58f37763
commit
6d1318bfa4
25
display.go
25
display.go
@ -315,28 +315,11 @@ func jsonify(node *html.Node) map[string]interface{} {
|
|||||||
func (j JSONDisplayer) Display(nodes []*html.Node) {
|
func (j JSONDisplayer) Display(nodes []*html.Node) {
|
||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
switch len(nodes) {
|
jsonNodes := []map[string]interface{}{}
|
||||||
case 1:
|
for _, node := range nodes {
|
||||||
node := nodes[0]
|
jsonNodes = append(jsonNodes, jsonify(node))
|
||||||
if node.Type != html.DocumentNode {
|
|
||||||
jsonNode := jsonify(nodes[0])
|
|
||||||
data, err = json.MarshalIndent(&jsonNode, "", pupIndentString)
|
|
||||||
} 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 {
|
|
||||||
jsonNodes = append(jsonNodes, jsonify(node))
|
|
||||||
}
|
|
||||||
data, err = json.MarshalIndent(&jsonNodes, "", pupIndentString)
|
|
||||||
}
|
}
|
||||||
|
data, err = json.MarshalIndent(&jsonNodes, "", pupIndentString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Could not jsonify nodes")
|
panic("Could not jsonify nodes")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user