forked from ericchiang/pup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pup.go
63 lines (54 loc) · 1.13 KB
/
pup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"os"
"golang.org/x/net/html"
)
// _=,_
// o_/6 /#\
// \__ |##/
// ='|--\
// / #'-.
// \#|_ _'-. /
// |/ \_( # |"
// C/ ,--___/
func main() {
// process flags and arguments
cmds, err := ParseArgs()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(2)
}
// Parse the input and get the root node
root, err := ParseHTML(pupIn, pupCharset)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(2)
}
pupIn.Close()
if err := runSelectors(os.Stdout, cmds, root); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(2)
}
}
func RemoveInverseMatches(root *html.Node, selectedNodes []*html.Node) {
var remove []*html.Node
for node := root.FirstChild; node != nil; node = node.NextSibling {
if HtmlNodeInList(node, selectedNodes) {
remove = append(remove, node)
} else {
RemoveInverseMatches(node, selectedNodes)
}
}
for _, rm := range remove {
root.RemoveChild(rm)
}
}
func HtmlNodeInList(a *html.Node, list []*html.Node) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}