2014-11-06 03:31:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2016-07-23 04:42:23 +00:00
|
|
|
"golang.org/x/net/html"
|
2014-11-06 03:31:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// _=,_
|
|
|
|
// o_/6 /#\
|
|
|
|
// \__ |##/
|
|
|
|
// ='|--\
|
|
|
|
// / #'-.
|
|
|
|
// \#|_ _'-. /
|
|
|
|
// |/ \_( # |"
|
|
|
|
// C/ ,--___/
|
|
|
|
|
2016-07-23 04:45:46 +00:00
|
|
|
var VERSION string = "0.4.0"
|
2014-11-06 03:31:28 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
// process flags and arguments
|
2014-11-22 15:47:33 +00:00
|
|
|
cmds, err := ParseArgs()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-11-06 03:31:28 +00:00
|
|
|
|
|
|
|
// Parse the input and get the root node
|
2014-12-14 04:52:41 +00:00
|
|
|
root, err := ParseHTML(pupIn, pupCharset)
|
2014-11-06 03:31:28 +00:00
|
|
|
if err != nil {
|
2014-12-14 04:52:41 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
2014-11-06 03:31:28 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
2014-12-14 04:52:41 +00:00
|
|
|
pupIn.Close()
|
2014-11-06 03:31:28 +00:00
|
|
|
|
|
|
|
// Parse the selectors
|
|
|
|
selectorFuncs := []SelectorFunc{}
|
|
|
|
funcGenerator := Select
|
|
|
|
var cmd string
|
|
|
|
for len(cmds) > 0 {
|
|
|
|
cmd, cmds = cmds[0], cmds[1:]
|
|
|
|
if len(cmds) == 0 {
|
|
|
|
if err := ParseDisplayer(cmd); err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch cmd {
|
2014-11-22 15:56:35 +00:00
|
|
|
case "*": // select all
|
2014-11-06 03:31:28 +00:00
|
|
|
continue
|
|
|
|
case ">":
|
2014-12-07 02:08:03 +00:00
|
|
|
funcGenerator = SelectFromChildren
|
|
|
|
case "+":
|
2014-11-06 03:31:28 +00:00
|
|
|
funcGenerator = SelectNextSibling
|
2014-11-22 15:56:35 +00:00
|
|
|
case ",": // nil will signify a comma
|
|
|
|
selectorFuncs = append(selectorFuncs, nil)
|
2014-11-06 03:31:28 +00:00
|
|
|
default:
|
|
|
|
selector, err := ParseSelector(cmd)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Selector parsing error: %s\n", err.Error())
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
selectorFuncs = append(selectorFuncs, funcGenerator(selector))
|
|
|
|
funcGenerator = Select
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-22 15:56:35 +00:00
|
|
|
selectedNodes := []*html.Node{}
|
2014-11-06 03:31:28 +00:00
|
|
|
currNodes := []*html.Node{root}
|
|
|
|
for _, selectorFunc := range selectorFuncs {
|
2014-11-22 15:56:35 +00:00
|
|
|
if selectorFunc == nil { // hit a comma
|
|
|
|
selectedNodes = append(selectedNodes, currNodes...)
|
|
|
|
currNodes = []*html.Node{root}
|
|
|
|
} else {
|
|
|
|
currNodes = selectorFunc(currNodes)
|
|
|
|
}
|
2014-11-06 03:31:28 +00:00
|
|
|
}
|
2014-11-22 15:56:35 +00:00
|
|
|
selectedNodes = append(selectedNodes, currNodes...)
|
|
|
|
pupDisplayer.Display(selectedNodes)
|
2014-11-06 03:31:28 +00:00
|
|
|
}
|