mirror of
https://github.com/ericchiang/pup
synced 2025-01-15 02:00:55 +00:00
Implement --invert to remove tags matching selector.
This commit is contained in:
parent
3b0097e9ed
commit
be5dee7868
4
parse.go
4
parse.go
@ -19,6 +19,7 @@ var (
|
||||
pupPreformatted bool = false
|
||||
pupPrintColor bool = false
|
||||
pupEscapeHTML bool = true
|
||||
pupInvertSelect bool = false
|
||||
pupIndentString string = " "
|
||||
pupDisplayer Displayer = TreeDisplayer{}
|
||||
)
|
||||
@ -56,6 +57,7 @@ Flags
|
||||
-n --number print number of elements selected
|
||||
-l --limit restrict number of levels printed
|
||||
-p --plain don't escape html
|
||||
-v --invert invert selection (remove matching elements)
|
||||
--pre preserve preformatted text
|
||||
--charset specify the charset for pup to use
|
||||
--version display version
|
||||
@ -89,6 +91,8 @@ func ProcessFlags(cmds []string) (nonFlagCmds []string, err error) {
|
||||
pupPrintColor = true
|
||||
case "-p", "--plain":
|
||||
pupEscapeHTML = false
|
||||
case "-v", "--invert":
|
||||
pupInvertSelect = true
|
||||
case "--pre":
|
||||
pupPreformatted = true
|
||||
case "-f", "--file":
|
||||
|
30
pup.go
30
pup.go
@ -76,5 +76,33 @@ func main() {
|
||||
}
|
||||
}
|
||||
selectedNodes = append(selectedNodes, currNodes...)
|
||||
pupDisplayer.Display(selectedNodes)
|
||||
if pupInvertSelect {
|
||||
RemoveInverseMatches(root, selectedNodes)
|
||||
pupDisplayer.Display([]*html.Node{root})
|
||||
} else {
|
||||
pupDisplayer.Display(selectedNodes)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -47,3 +47,4 @@ link , a:parent-of(sup)
|
||||
link , a:parent-of(sup) sup
|
||||
li --number
|
||||
li -n
|
||||
li a --invert
|
||||
|
@ -47,3 +47,4 @@ b6a3d6cccd305fcc3e8bf2743c443743bdaaa02b link , a:parent-of(sup)
|
||||
0d1f66765d1632c70f8608947890524e78459362 link , a:parent-of(sup) sup
|
||||
d200756078ef79d77fb17848c5f15b85bcea7e00 li --number
|
||||
d200756078ef79d77fb17848c5f15b85bcea7e00 li -n
|
||||
1fa074385374bcd613394678b5e08ed1ad4d5d9d li a --invert
|
||||
|
Loading…
Reference in New Issue
Block a user