-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
218 lines (204 loc) · 6.07 KB
/
parse.go
File metadata and controls
218 lines (204 loc) · 6.07 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
charsetPkg "golang.org/x/net/html/charset"
"golang.org/x/text/transform"
)
// Flag variables
var (
flagColor = flag.Bool("c", false, "print result with color")
flagFile = flag.String("f", "", "file to read from")
flagURL = flag.String("u", "", "URL to fetch HTML from")
flagIndent = flag.String("i", "", "number of spaces to use for indent or character")
flagNumber = flag.Bool("n", false, "print number of elements selected")
flagLimit = flag.Int("l", -1, "restrict number of levels printed")
flagPlain = flag.Bool("p", false, "don't escape html")
flagRaw = flag.Bool("r", false, "raw output")
flagPre = flag.Bool("pre", false, "preserve preformatted text")
flagCharset = flag.String("charset", "", "specify the charset for monk to use")
flagJSON = flag.Bool("json", false, "output in JSON format")
flagText = flag.Bool("text", false, "output as plain text")
flagAttr = flag.String("attr", "", "output the value of the specified attribute")
flagV = flag.Bool("v", false, "display version")
flagVersion = flag.Bool("version", false, "display version")
)
func init() {
flag.Usage = func() {
ShowHelp(os.Stdout)
os.Exit(0)
}
}
// ParseDocument parses HTML from the given reader, returning a goquery Document.
// contentType is the HTTP Content-Type header value (may be empty).
func ParseDocument(r io.Reader, charset string, contentType string) (*goquery.Document, error) {
var err error
if charset == "" {
// attempt to guess the charset of the HTML document.
// charsetPkg inspects meta tags/headers and returns a reader
// that decodes the input to UTF-8, which is what goquery expects.
r, err = charsetPkg.NewReader(r, contentType)
if err != nil {
return nil, err
}
} else {
// let the user specify the charset
e, name := charsetPkg.Lookup(charset)
if name == "" {
return nil, fmt.Errorf("'%s' is not a valid charset", charset)
}
r = transform.NewReader(r, e.NewDecoder())
}
return goquery.NewDocumentFromReader(r)
}
// ShowHelp displays the usage information.
func ShowHelp(w io.Writer) {
helpString := `Usage
monk [flags] [selectors]
Version
%s
Flags
-c --color print result with color
-f --file file to read from
-u --url URL to fetch HTML from
-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
-r --raw raw output
--pre preserve preformatted text
--charset specify the charset for monk to use
--json output in JSON format
--text output as plain text
--attr <name> output the value of the specified attribute
-v --version display version
`
fmt.Fprintf(w, helpString, version)
}
// ParseArgs parses command-line flags and arguments, setting global variables and returning selectors.
func ParseArgs() ([]string, error) {
flag.Parse()
// Handle version
if *flagV || *flagVersion {
fmt.Println(version)
os.Exit(0)
}
// If no arguments, show help
if len(flag.Args()) == 0 {
ShowHelp(os.Stdout)
os.Exit(0)
}
// Set config variables from flags
if *flagFile != "" && *flagURL != "" {
return []string{}, fmt.Errorf("-f and -u are mutually exclusive")
}
if *flagFile != "" {
var err error
config.Input, err = os.Open(*flagFile)
if err != nil {
return []string{}, fmt.Errorf("error opening file: %w", err)
}
}
if *flagURL != "" {
resp, err := http.Get(*flagURL) // #nosec G107 -- URL is user-supplied input, intended
if err != nil {
return []string{}, fmt.Errorf("error fetching URL: %w", err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return []string{}, fmt.Errorf("HTTP %d fetching %s", resp.StatusCode, *flagURL)
}
config.Input = resp.Body
config.ContentType = resp.Header.Get("Content-Type")
}
config.Charset = *flagCharset
config.MaxPrintLevel = *flagLimit
config.Preformatted = *flagPre
config.PrintColor = *flagColor
config.EscapeHTML = !*flagPlain
config.RawOutput = *flagRaw
if *flagIndent != "" {
if indentLevel, err := strconv.Atoi(*flagIndent); err == nil {
config.IndentString = strings.Repeat(" ", indentLevel)
} else {
config.IndentString = *flagIndent
}
}
// Set displayer
displayerCount := 0
if *flagText {
config.Displayer = TextDisplayer{}
displayerCount++
}
if *flagJSON {
config.Displayer = JSONDisplayer{}
displayerCount++
}
if *flagAttr != "" {
config.Displayer = AttrDisplayer{Attr: *flagAttr}
displayerCount++
}
if *flagNumber {
config.Displayer = CountDisplayer{}
displayerCount++
}
if displayerCount > 1 {
return []string{}, fmt.Errorf("only one display option can be specified")
}
// Get selectors from positional args
selectors := strings.Join(flag.Args(), " ")
return ParseCommands(selectors)
}
// ParseCommands splits a command string into individual commands, handling quotes and commas.
func ParseCommands(cmdString string) ([]string, error) {
var cmds []string
last, next, max := 0, 0, len(cmdString)
for {
// if we're at the end of the string, return
if next == max {
if next > last {
cmds = append(cmds, cmdString[last:next])
}
return cmds, nil
}
// evaluate a rune
c := cmdString[next]
switch c {
case ' ':
if next > last {
cmds = append(cmds, cmdString[last:next])
}
last = next + 1
case ',':
if next > last {
cmds = append(cmds, cmdString[last:next])
}
cmds = append(cmds, ",")
last = next + 1
case '\'', '"':
// for quotes, consume runes until the quote has ended
quoteChar := c
for {
next++
if next == max {
return []string{}, fmt.Errorf("unmatched open quote (%c)", quoteChar)
}
if cmdString[next] == '\\' {
next++
if next == max {
return []string{}, fmt.Errorf("unmatched open quote (%c)", quoteChar)
}
} else if cmdString[next] == quoteChar {
break
}
}
}
next++
}
}