Skip to content

Commit

Permalink
Implement --invert to remove tags matching selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgang42 authored and frioux committed Jan 24, 2022
1 parent cc075e0 commit 69c02e1
Show file tree
Hide file tree
Showing 7 changed files with 4,154 additions and 1 deletion.
4 changes: 4 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
pupPreformatted bool = false
pupPrintColor bool = false
pupEscapeHTML bool = true
pupInvertSelect bool = false
pupIndentString string = " "
pupDisplayer Displayer = TreeDisplayer{}
)
Expand Down Expand Up @@ -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
`
Expand Down Expand Up @@ -88,6 +90,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":
Expand Down
26 changes: 26 additions & 0 deletions pup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"

"golang.org/x/net/html"
)

// _=,_
Expand Down Expand Up @@ -34,4 +36,28 @@ func main() {
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
}
1 change: 1 addition & 0 deletions pup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestMain(t *testing.T) {
50: {[]string{"table.infobox tr > th:first-child json{}"}},
51: {[]string{"table.infobox tr > *:first-child json{}"}},
52: {[]string{"frew text{}"}},
53: {[]string{"li", "-v"}},
}

for i, tt := range tests {
Expand Down
7 changes: 6 additions & 1 deletion runselectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func runSelectors(w io.Writer, cmds []string, root *html.Node) error {
}
}
selectedNodes = append(selectedNodes, currNodes...)
pupDisplayer.Display(w, selectedNodes)
if pupInvertSelect {
RemoveInverseMatches(root, selectedNodes)
pupDisplayer.Display(w, []*html.Node{root})
} else {
pupDisplayer.Display(w, selectedNodes)
}

return nil
}
4,017 changes: 4,017 additions & 0 deletions testdata/053.golden

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions tests/cmds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#footer
#footer li
#footer li + a
#footer li + a attr{title}
#footer li > li
table li
table li:first-child
table li:first-of-type
table li:last-child
table li:last-of-type
table a[title="The Practice of Programming"]
table a[title="The Practice of Programming"] text{}
json{}
text{}
.after-portlet
.after
:empty
td:empty
.navbox-list li:nth-child(1)
.navbox-list li:nth-child(2)
.navbox-list li:nth-child(3)
.navbox-list li:nth-last-child(1)
.navbox-list li:nth-last-child(2)
.navbox-list li:nth-last-child(3)
.navbox-list li:nth-child(n+1)
.navbox-list li:nth-child(3n+1)
.navbox-list li:nth-last-child(n+1)
.navbox-list li:nth-last-child(3n+1)
:only-child
.navbox-list li:only-child
.summary
[class=summary]
[class="summary"]
#toc
#toc li + a
#toc li + a text{}
#toc li + a json{}
#toc li + a + span
#toc li + span
#toc li > li
li a:not([rel])
link, a
link ,a
link , a
link , a sup
link , a:parent-of(sup)
link , a:parent-of(sup) sup
li --number
li -n
li a --invert
50 changes: 50 additions & 0 deletions tests/expected_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
c00fef10d36c1166cb5ac886f9d25201b720e37e #footer
a7bb8dbfdd638bacad0aa9dc3674126d396b74e2 #footer li
da39a3ee5e6b4b0d3255bfef95601890afd80709 #footer li + a
da39a3ee5e6b4b0d3255bfef95601890afd80709 #footer li + a attr{title}
da39a3ee5e6b4b0d3255bfef95601890afd80709 #footer li > li
a92e50c09cd56970625ac3b74efbddb83b2731bb table li
505c04a42e0084cd95560c233bd3a81b2c59352d table li:first-child
505c04a42e0084cd95560c233bd3a81b2c59352d table li:first-of-type
66950e746590d7f4e9cfe3d1adef42cd0addcf1d table li:last-child
66950e746590d7f4e9cfe3d1adef42cd0addcf1d table li:last-of-type
0a37d612cd4c67a42bd147b1edc5a1128456b017 table a[title="The Practice of Programming"]
0d3918d54f868f13110262ffbb88cbb0b083057d table a[title="The Practice of Programming"] text{}
199188dc8f1522426a628e41d96264bffb8beb0f json{}
95ef88ded9dab22ee3206cca47b9c3a376274bda text{}
e4f7358fbb7bb1748a296fa2a7e815fa7de0a08b .after-portlet
da39a3ee5e6b4b0d3255bfef95601890afd80709 .after
5b3020ba03fb43f7cdbcb3924546532b6ec9bd71 :empty
3406ca0f548d66a7351af5411ce945cf67a2f849 td:empty
30fff0af0b1209f216d6e9124e7396c0adfa0758 .navbox-list li:nth-child(1)
a38e26949f047faab5ea7ba2acabff899349ce03 .navbox-list li:nth-child(2)
d954831229a76b888e85149564727776e5a2b37a .navbox-list li:nth-child(3)
d314e83b059bb876b0e5ee76aa92d54987961f9a .navbox-list li:nth-last-child(1)
1f19496e239bca61a1109dbbb8b5e0ab3e302b50 .navbox-list li:nth-last-child(2)
1ec9ebf14fc28c7d2b13e81241a6d2e1608589e8 .navbox-list li:nth-last-child(3)
52e726f0993d2660f0fb3ea85156f6fbcc1cfeee .navbox-list li:nth-child(n+1)
0b20c98650efa5df39d380fea8d5b43f3a08cb66 .navbox-list li:nth-child(3n+1)
52e726f0993d2660f0fb3ea85156f6fbcc1cfeee .navbox-list li:nth-last-child(n+1)
972973fe1e8f63e4481c8641d6169c638a528a6e .navbox-list li:nth-last-child(3n+1)
6c45ee6bca361b8a9baee50a15f575fc6ac73adc :only-child
44c99f6ad37b65dc0893cdcb1c60235d827ee73e .navbox-list li:only-child
641037814e358487d1938fc080e08f72a3846ef8 .summary
641037814e358487d1938fc080e08f72a3846ef8 [class=summary]
641037814e358487d1938fc080e08f72a3846ef8 [class="summary"]
613bf65ac4042b6ee0a7a47f08732fdbe1b5b06b #toc
da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + a
da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + a text{}
cd0d4cc32346750408f7d4f5e78ec9a6e5b79a0d #toc li + a json{}
da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + a + span
da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + span
da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li > li
87eee1189dd5296d6c010a1ad329fc53c6099d72 li a:not([rel])
055f3c98e9160beb13f72f1009ad66b6252a9bba link, a
055f3c98e9160beb13f72f1009ad66b6252a9bba link ,a
055f3c98e9160beb13f72f1009ad66b6252a9bba link , a
0d1f66765d1632c70f8608947890524e78459362 link , a sup
b6a3d6cccd305fcc3e8bf2743c443743bdaaa02b link , a:parent-of(sup)
0d1f66765d1632c70f8608947890524e78459362 link , a:parent-of(sup) sup
d200756078ef79d77fb17848c5f15b85bcea7e00 li --number
d200756078ef79d77fb17848c5f15b85bcea7e00 li -n
1fa074385374bcd613394678b5e08ed1ad4d5d9d li a --invert

0 comments on commit 69c02e1

Please sign in to comment.