From ddf33efab884f1dfab7941599d7ee1fb00c1ca6d Mon Sep 17 00:00:00 2001 From: Wolfgang Faust Date: Tue, 5 Sep 2017 16:53:41 -0400 Subject: [PATCH 1/3] Update tests for json{} On the assumption that this is currently correct but the tests were never updated after the formatter changed. Closes #80. --- tests/expected_output.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/expected_output.txt b/tests/expected_output.txt index 7f06b47..3b08cee 100644 --- a/tests/expected_output.txt +++ b/tests/expected_output.txt @@ -10,7 +10,7 @@ a92e50c09cd56970625ac3b74efbddb83b2731bb table li 66950e746590d7f4e9cfe3d1adef42cd0addcf1d table li:last-of-type 0a37d612cd4c67a42bd147b1edc5a1128456b017 table a[title="The Practice of Programming"] 0d3918d54f868f13110262ffbb88cbb0b083057d table a[title="The Practice of Programming"] text{} -ecb542a30fc75c71a0c6380692cbbc4266ccbce4 json{} +199188dc8f1522426a628e41d96264bffb8beb0f json{} 95ef88ded9dab22ee3206cca47b9c3a376274bda text{} e4f7358fbb7bb1748a296fa2a7e815fa7de0a08b .after-portlet da39a3ee5e6b4b0d3255bfef95601890afd80709 .after @@ -34,7 +34,7 @@ d314e83b059bb876b0e5ee76aa92d54987961f9a .navbox-list li:nth-last-child(1) 613bf65ac4042b6ee0a7a47f08732fdbe1b5b06b #toc da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + a da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + a text{} -97d170e1550eee4afc0af065b78cda302a97674c #toc li + a json{} +cd0d4cc32346750408f7d4f5e78ec9a6e5b79a0d #toc li + a json{} da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + a + span da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li + span da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li > li From 3b0097e9edad37b24ab0e46a45843736fc6db00c Mon Sep 17 00:00:00 2001 From: Wolfgang Faust Date: Tue, 5 Sep 2017 16:56:35 -0400 Subject: [PATCH 2/3] Modify tests to support multiple arguments. Also, update tests for --number to actually check that flag. Previously they were checking for a '-n' tag inside an 'li' tag, producing the empty string. I don't think this was the intended result. --- tests/cmds.txt | 4 ++-- tests/expected_output.txt | 4 ++-- tests/run.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/cmds.txt b/tests/cmds.txt index beca6c2..c699bfe 100644 --- a/tests/cmds.txt +++ b/tests/cmds.txt @@ -45,5 +45,5 @@ link , a link , a sup link , a:parent-of(sup) link , a:parent-of(sup) sup -li --number -li -n +li --number +li -n diff --git a/tests/expected_output.txt b/tests/expected_output.txt index 3b08cee..e448b66 100644 --- a/tests/expected_output.txt +++ b/tests/expected_output.txt @@ -45,5 +45,5 @@ da39a3ee5e6b4b0d3255bfef95601890afd80709 #toc li > li 0d1f66765d1632c70f8608947890524e78459362 link , a sup b6a3d6cccd305fcc3e8bf2743c443743bdaaa02b link , a:parent-of(sup) 0d1f66765d1632c70f8608947890524e78459362 link , a:parent-of(sup) sup -da39a3ee5e6b4b0d3255bfef95601890afd80709 li --number -da39a3ee5e6b4b0d3255bfef95601890afd80709 li -n +d200756078ef79d77fb17848c5f15b85bcea7e00 li --number +d200756078ef79d77fb17848c5f15b85bcea7e00 li -n diff --git a/tests/run.py b/tests/run.py index 67a13e0..6789e6b 100755 --- a/tests/run.py +++ b/tests/run.py @@ -8,7 +8,7 @@ data = open("index.html", "r").read() for line in open("cmds.txt", "r"): line = line.strip() - p = Popen(['pup', line], stdout=PIPE, stdin=PIPE, stderr=PIPE) + p = Popen(['pup']+line.split('\t'), stdout=PIPE, stdin=PIPE, stderr=PIPE) h = sha1() h.update(p.communicate(input=data)[0]) print("%s %s" % (h.hexdigest(), line)) From be5dee7868ea619b9888b15cbec79da8697dc96a Mon Sep 17 00:00:00 2001 From: Wolfgang Faust Date: Sat, 26 Aug 2017 13:54:53 -0400 Subject: [PATCH 3/3] Implement --invert to remove tags matching selector. --- parse.go | 4 ++++ pup.go | 30 +++++++++++++++++++++++++++++- tests/cmds.txt | 1 + tests/expected_output.txt | 1 + 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/parse.go b/parse.go index 57d9fd3..471ae0a 100644 --- a/parse.go +++ b/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": diff --git a/pup.go b/pup.go index aa2dee6..b3a8236 100644 --- a/pup.go +++ b/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 } diff --git a/tests/cmds.txt b/tests/cmds.txt index c699bfe..4d0ca2c 100644 --- a/tests/cmds.txt +++ b/tests/cmds.txt @@ -47,3 +47,4 @@ link , a:parent-of(sup) link , a:parent-of(sup) sup li --number li -n +li a --invert diff --git a/tests/expected_output.txt b/tests/expected_output.txt index e448b66..2da70b0 100644 --- a/tests/expected_output.txt +++ b/tests/expected_output.txt @@ -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