mirror of
https://github.com/ericchiang/pup
synced 2025-02-19 02:22:01 +00:00
commit
6ca590964c
61
display.go
61
display.go
@ -6,10 +6,10 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/net/html/atom"
|
||||
"github.com/fatih/color"
|
||||
"github.com/mattn/go-colorable"
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/net/html/atom"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -70,7 +70,13 @@ func (t TreeDisplayer) Display(nodes []*html.Node) {
|
||||
func (t TreeDisplayer) printNode(n *html.Node, level int) {
|
||||
switch n.Type {
|
||||
case html.TextNode:
|
||||
s := html.EscapeString(n.Data)
|
||||
s := n.Data
|
||||
if pupEscapeHTML {
|
||||
// don't escape javascript
|
||||
if n.Parent == nil || n.Parent.DataAtom != atom.Script {
|
||||
s = html.EscapeString(s)
|
||||
}
|
||||
}
|
||||
s = strings.TrimSpace(s)
|
||||
if s != "" {
|
||||
t.printIndent(level)
|
||||
@ -85,7 +91,10 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
|
||||
fmt.Printf("<%s", n.Data)
|
||||
}
|
||||
for _, a := range n.Attr {
|
||||
val := html.EscapeString(a.Val)
|
||||
val := a.Val
|
||||
if pupEscapeHTML {
|
||||
val = html.EscapeString(val)
|
||||
}
|
||||
if pupPrintColor {
|
||||
fmt.Print(" ")
|
||||
attrKeyColor.Printf("%s", a.Key)
|
||||
@ -113,10 +122,14 @@ func (t TreeDisplayer) printNode(n *html.Node, level int) {
|
||||
}
|
||||
case html.CommentNode:
|
||||
t.printIndent(level)
|
||||
data := n.Data
|
||||
if pupEscapeHTML {
|
||||
data = html.EscapeString(data)
|
||||
}
|
||||
if pupPrintColor {
|
||||
commentColor.Printf("<!--%s-->\n", n.Data)
|
||||
commentColor.Printf("<!--%s-->\n", data)
|
||||
} else {
|
||||
fmt.Printf("<!--%s-->\n", n.Data)
|
||||
fmt.Printf("<!--%s-->\n", data)
|
||||
}
|
||||
t.printChildren(n, level)
|
||||
case html.DoctypeNode, html.DocumentNode:
|
||||
@ -151,7 +164,14 @@ type TextDisplayer struct{}
|
||||
func (t TextDisplayer) Display(nodes []*html.Node) {
|
||||
for _, node := range nodes {
|
||||
if node.Type == html.TextNode {
|
||||
fmt.Println(node.Data)
|
||||
data := node.Data
|
||||
if pupEscapeHTML {
|
||||
// don't escape javascript
|
||||
if node.Parent == nil || node.Parent.DataAtom != atom.Script {
|
||||
data = html.EscapeString(data)
|
||||
}
|
||||
}
|
||||
fmt.Println(data)
|
||||
}
|
||||
children := []*html.Node{}
|
||||
child := node.FirstChild
|
||||
@ -173,7 +193,10 @@ func (a AttrDisplayer) Display(nodes []*html.Node) {
|
||||
attributes := node.Attr
|
||||
for _, attr := range attributes {
|
||||
if attr.Key == a.Attr {
|
||||
val := html.EscapeString(attr.Val)
|
||||
val := attr.Val
|
||||
if pupEscapeHTML {
|
||||
val = html.EscapeString(val)
|
||||
}
|
||||
fmt.Printf("%s\n", val)
|
||||
}
|
||||
}
|
||||
@ -188,7 +211,11 @@ func jsonify(node *html.Node) map[string]interface{} {
|
||||
vals := map[string]interface{}{}
|
||||
if len(node.Attr) > 0 {
|
||||
for _, attr := range node.Attr {
|
||||
vals[attr.Key] = html.EscapeString(attr.Val)
|
||||
if pupEscapeHTML {
|
||||
vals[attr.Key] = html.EscapeString(attr.Val)
|
||||
} else {
|
||||
vals[attr.Key] = attr.Val
|
||||
}
|
||||
}
|
||||
}
|
||||
vals["tag"] = node.DataAtom.String()
|
||||
@ -200,6 +227,12 @@ func jsonify(node *html.Node) map[string]interface{} {
|
||||
case html.TextNode:
|
||||
text := strings.TrimSpace(child.Data)
|
||||
if text != "" {
|
||||
if pupEscapeHTML {
|
||||
// don't escape javascript
|
||||
if node.DataAtom != atom.Script {
|
||||
text = html.EscapeString(text)
|
||||
}
|
||||
}
|
||||
// if there is already text we'll append it
|
||||
currText, ok := vals["text"]
|
||||
if ok {
|
||||
@ -209,6 +242,9 @@ func jsonify(node *html.Node) map[string]interface{} {
|
||||
}
|
||||
case html.CommentNode:
|
||||
comment := strings.TrimSpace(child.Data)
|
||||
if pupEscapeHTML {
|
||||
comment = html.EscapeString(comment)
|
||||
}
|
||||
currComment, ok := vals["comment"]
|
||||
if ok {
|
||||
comment = fmt.Sprintf("%s %s", currComment, comment)
|
||||
@ -252,3 +288,10 @@ func (j JSONDisplayer) Display(nodes []*html.Node) {
|
||||
}
|
||||
fmt.Printf("%s", data)
|
||||
}
|
||||
|
||||
// Print the number of features returned
|
||||
type NumDisplayer struct{}
|
||||
|
||||
func (d NumDisplayer) Display(nodes []*html.Node) {
|
||||
fmt.Println(len(nodes))
|
||||
}
|
||||
|
6
parse.go
6
parse.go
@ -17,6 +17,7 @@ var (
|
||||
pupCharset string = ""
|
||||
pupMaxPrintLevel int = -1
|
||||
pupPrintColor bool = false
|
||||
pupEscapeHTML bool = true
|
||||
pupIndentString string = " "
|
||||
pupDisplayer Displayer = TreeDisplayer{}
|
||||
)
|
||||
@ -53,6 +54,7 @@ Flags
|
||||
-i --indent number of spaces to use for indent or character
|
||||
-n --number print number of elements selected
|
||||
-l --limit restrict number of levels printed
|
||||
-p --plain don't escape html
|
||||
--charset specify the charset for pup to use
|
||||
--version display version
|
||||
`
|
||||
@ -83,6 +85,8 @@ func ProcessFlags(cmds []string) (nonFlagCmds []string, err error) {
|
||||
switch cmd {
|
||||
case "-c", "--color":
|
||||
pupPrintColor = true
|
||||
case "-p", "--plain":
|
||||
pupEscapeHTML = false
|
||||
case "-f", "--file":
|
||||
filename := cmds[i+1]
|
||||
pupIn, err = os.Open(filename)
|
||||
@ -113,6 +117,8 @@ func ProcessFlags(cmds []string) (nonFlagCmds []string, err error) {
|
||||
case "--version":
|
||||
fmt.Println(VERSION)
|
||||
os.Exit(0)
|
||||
case "-n", "--number":
|
||||
pupDisplayer = NumDisplayer{}
|
||||
default:
|
||||
if cmd[0] == '-' {
|
||||
return []string{}, fmt.Errorf("Unrecognized flag '%s'", cmd)
|
||||
|
2
pup.go
2
pup.go
@ -16,7 +16,7 @@ import (
|
||||
// |/ \_( # |"
|
||||
// C/ ,--___/
|
||||
|
||||
var VERSION string = "0.3.7"
|
||||
var VERSION string = "0.3.8"
|
||||
|
||||
func main() {
|
||||
// process flags and arguments
|
||||
|
@ -45,3 +45,5 @@ link , a
|
||||
link , a sup
|
||||
link , a:parent-of(sup)
|
||||
link , a:parent-of(sup) sup
|
||||
li --number
|
||||
li -n
|
||||
|
@ -10,8 +10,8 @@ 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{}
|
||||
48a00be8203baea11bf9672bf10b183cca98c3b2 json{}
|
||||
74fa5ddf9687041ece8aed54913cc2d1a6e7101c text{}
|
||||
ecb542a30fc75c71a0c6380692cbbc4266ccbce4 json{}
|
||||
95ef88ded9dab22ee3206cca47b9c3a376274bda text{}
|
||||
e4f7358fbb7bb1748a296fa2a7e815fa7de0a08b .after-portlet
|
||||
da39a3ee5e6b4b0d3255bfef95601890afd80709 .after
|
||||
5b3020ba03fb43f7cdbcb3924546532b6ec9bd71 :empty
|
||||
@ -26,7 +26,7 @@ d314e83b059bb876b0e5ee76aa92d54987961f9a .navbox-list li:nth-last-child(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)
|
||||
a55bb21d37fbbd3f8f1b551126795fbc733451fe :only-child
|
||||
6c45ee6bca361b8a9baee50a15f575fc6ac73adc :only-child
|
||||
44c99f6ad37b65dc0893cdcb1c60235d827ee73e .navbox-list li:only-child
|
||||
641037814e358487d1938fc080e08f72a3846ef8 .summary
|
||||
641037814e358487d1938fc080e08f72a3846ef8 [class=summary]
|
||||
@ -45,3 +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
|
||||
|
Loading…
Reference in New Issue
Block a user