forked from ericchiang/pup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (151 loc) · 3.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"code.google.com/p/go.net/html"
"fmt"
"github.com/ericchiang/pup/funcs"
"github.com/ericchiang/pup/selector"
"io"
"os"
"strconv"
"strings"
)
const VERSION string = "0.1.0"
var (
// Flags
attributes []string = []string{}
inputStream io.ReadCloser = os.Stdin
indentString string = " "
maxPrintLevel int = -1
printNumber bool = false
printColor bool = false
displayer funcs.Displayer = nil
)
// Print to stderr and exit
func Fatal(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
// Print help to stderr and quit
func PrintHelp() {
helpString := `Usage
pup [list of css selectors]
Version
%s
Flags
-c --color print result with color
-f --file file to read from
-h --help display this help
-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
--version display version`
Fatal(helpString, VERSION)
}
// Process command arguments and return all non-flags.
func ProcessFlags(cmds []string) []string {
var i int
var err error
defer func() {
if r := recover(); r != nil {
Fatal("Option '%s' requires an argument", cmds[i])
}
}()
nonFlagCmds := make([]string, len(cmds))
n := 0
for i = 0; i < len(cmds); i++ {
cmd := cmds[i]
switch cmd {
case "-a", "--attr":
attributes = append(attributes, cmds[i+1])
i++
case "-c", "--color":
printColor = true
case "-f", "--file":
filename := cmds[i+1]
inputStream, err = os.Open(filename)
if err != nil {
Fatal(err.Error())
}
i++
case "-h", "--help":
PrintHelp()
os.Exit(1)
case "-i", "--indent":
indentLevel, err := strconv.Atoi(cmds[i+1])
if err == nil {
indentString = strings.Repeat(" ", indentLevel)
} else {
indentString = cmds[i+1]
}
i++
case "-n", "--number":
printNumber = true
case "-l", "--limit":
maxPrintLevel, err = strconv.Atoi(cmds[i+1])
if err != nil {
Fatal("Argument for '%s' must be numeric",
cmds)
}
i++
case "--version":
Fatal(VERSION)
default:
if cmd[0] == '-' {
Fatal("Unrecognized flag '%s'", cmd)
}
nonFlagCmds[n] = cmds[i]
n++
}
}
return nonFlagCmds[:n]
}
// pup
func main() {
cmds := ProcessFlags(os.Args[1:])
root, err := html.Parse(inputStream)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(2)
}
inputStream.Close()
if len(cmds) == 0 {
PrintNode(root, 0)
os.Exit(0)
}
selectors := make([]*selector.Selector, len(cmds))
for i, cmd := range cmds {
if i+1 == len(cmds) {
d, err := funcs.NewDisplayFunc(cmd)
if err == nil {
displayer = d
selectors = selectors[0 : len(cmds)-1]
break
}
}
selectors[i], err = selector.NewSelector(cmd)
if err != nil {
Fatal("Selector parse error: %s", err)
}
}
currNodes := []*html.Node{root}
var selected []*html.Node
for _, selector := range selectors {
selected = []*html.Node{}
for _, node := range currNodes {
selected = append(selected,
selector.FindAllChildren(node)...)
}
currNodes = selected
}
if displayer != nil {
displayer.Display(currNodes)
} else if printNumber {
fmt.Println(len(currNodes))
} else {
for _, s := range currNodes {
// defined in `printing.go`
PrintNode(s, 0)
}
}
}