forked from foolin/pagser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_functions.go
426 lines (400 loc) · 13.4 KB
/
builtin_functions.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package pagser
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/spf13/cast"
"net/url"
"strconv"
"strings"
)
// BuiltinFunctions builtin functions are registered with a lowercase initial, eg: Text -> text()
type BuiltinFunctions struct {
}
// AbsHref absHref(baseUrl) get element attribute name `href`, and convert to absolute url, return *URL.
// `baseUrl` is the base url like `https://example.com/`.
// //<a href="/foolin/pagser">Pagser</a>
// struct {
// Example string `pagser:".selector->absHref('https://github.com/')"`
// }
func (builtin BuiltinFunctions) AbsHref(selection *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("args must has baseUrl")
}
baseUrl, err := url.Parse(args[0])
if err != nil {
return "", fmt.Errorf("invalid base url: %v error: %v", baseUrl, err)
}
hrefUrl, err := url.Parse(selection.AttrOr("href", ""))
if err != nil {
return "", err
}
return baseUrl.ResolveReference(hrefUrl), nil
}
// Attr attr(name, defaultValue='') get element attribute value, return string.
// outerHtml() get element outer html, return string.
// //<a href="https://github.com/foolin/pagser">Pagser</a>
// struct {
// Example string `pagser:".selector->attr(href)"`
// }
func (builtin BuiltinFunctions) Attr(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("attr(name) must has name")
}
name := args[0]
defaultValue := ""
if len(args) > 1 {
defaultValue = args[1]
}
val := node.AttrOr(name, defaultValue)
return val, nil
}
// AttrConcat attrConcat(name, text1, $value, [ text2, ... text_n ])
// `name` get element attribute value by name,
// `text1, text2, ... text_n` The strings that you wish to join together,
// `$value` is placeholder for get element text
// return string.
// struct {
// Example string `pagser:".selector->attrConcat('Result:', '<', $value, '>')"`
// }
func (builtin BuiltinFunctions) AttrConcat(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 3 {
return "", fmt.Errorf("attrConcat(name, text1, $value, [ text2, ... text_n ]) must be more than two arguments")
}
name := args[0]
value := strings.TrimSpace(node.AttrOr(name, ""))
builder := strings.Builder{}
for i, v := range args {
if i == 0 {
continue
}
if v == "$value" {
builder.WriteString(value)
} else {
builder.WriteString(v)
}
}
return builder.String(), nil
}
// AttrEmpty attrEmpty(name, defaultValue) get element attribute value, return string.
// //<a href="https://github.com/foolin/pagser">Pagser</a>
// struct {
// Example string `pagser:".selector->AttrEmpty(href, '#')"`
// }
func (builtin BuiltinFunctions) AttrEmpty(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 2 {
return "", fmt.Errorf("attr(name, defaultValue) must has name and default value")
}
name := args[0]
defaultValue := args[1]
value := strings.TrimSpace(node.AttrOr(name, defaultValue))
if value == "" {
value = defaultValue
}
return value, nil
}
// AttrSplit attrSplit(name, sep=',', trim='true') get attribute value and split by separator to array string, return []string.
// struct {
// Examples []string `pagser:".selector->attrSplit('keywords', ',')"`
// }
func (builtin BuiltinFunctions) AttrSplit(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("attr(name) must has `name`")
}
name := args[0]
sep := ","
trim := true
if len(args) > 1 {
sep = args[1]
}
if len(args) > 2 {
var err error
trim, err = cast.ToBoolE(args[2])
if err != nil {
return nil, fmt.Errorf("`trim` must bool type value: true/false")
}
}
list := strings.Split(node.AttrOr(name, ""), sep)
if trim {
for i, v := range list {
list[i] = strings.TrimSpace(v)
}
}
return list, nil
}
// EachAttr eachAttr(name) get each element attribute value, return []string.
// //<a href="https://github.com/foolin/pagser">Pagser</a>
// struct {
// Examples []string `pagser:".selector->eachAttr(href)"`
// }
func (builtin BuiltinFunctions) EachAttr(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("attr(name) must has name")
}
name := args[0]
list := make([]string, 0)
node.Each(func(i int, selection *goquery.Selection) {
list = append(list, strings.TrimSpace(selection.AttrOr(name, "")))
})
return list, nil
}
// EachAttrEmpty eachAttrEmpty(name, defaultValue) get each element attribute value, return []string.
// //<a href="https://github.com/foolin/pagser">Pagser</a>
// struct {
// Examples []string `pagser:".selector->eachAttrEmpty(href, '#')"`
// }
func (builtin BuiltinFunctions) EachAttrEmpty(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 2 {
return "", fmt.Errorf("eachAttrEmpty(name) must has name")
}
name := args[0]
defaultValue := args[1]
list := make([]string, 0)
node.Each(func(i int, selection *goquery.Selection) {
value := strings.TrimSpace(selection.AttrOr(name, ""))
if value == "" {
value = defaultValue
}
list = append(list, value)
})
return list, nil
}
// EachHtml eachHtml() get each element inner html, return []string.
// eachTextEmpty(defaultValue) get each element text, return []string.
// struct {
// Examples []string `pagser:".selector->eachHtml()"`
// }
func (builtin BuiltinFunctions) EachHtml(node *goquery.Selection, args ...string) (out interface{}, err error) {
list := make([]string, 0)
node.EachWithBreak(func(i int, selection *goquery.Selection) bool {
var html string
html, err = node.Html()
if err != nil {
return false
}
list = append(list, html)
return true
})
if err != nil {
return nil, err
}
return list, nil
}
// EachOutHtml eachOutHtml() get each element outer html, return []string.
// struct {
// Examples []string `pagser:".selector->eachOutHtml()"`
// }
func (builtin BuiltinFunctions) EachOutHtml(node *goquery.Selection, args ...string) (out interface{}, err error) {
list := make([]string, 0)
node.EachWithBreak(func(i int, selection *goquery.Selection) bool {
var html string
html, err = goquery.OuterHtml(node)
if err != nil {
return false
}
list = append(list, html)
return true
})
if err != nil {
return nil, err
}
return list, nil
}
// EachText eachText() get each element text, return []string.
// struct {
// Examples []string `pagser:".selector->eachText('')"`
// }
func (builtin BuiltinFunctions) EachText(node *goquery.Selection, args ...string) (out interface{}, err error) {
list := make([]string, 0)
node.Each(func(i int, selection *goquery.Selection) {
list = append(list, strings.TrimSpace(selection.Text()))
})
return list, nil
}
// EachTextEmpty eachTextEmpty(defaultValue) get each element text, return []string.
// struct {
// Examples []string `pagser:".selector->eachTextEmpty('')"`
// }
func (builtin BuiltinFunctions) EachTextEmpty(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("eachTextEmpty(defaultValue) must has defaultValue")
}
defaultValue := args[0]
list := make([]string, 0)
node.Each(func(i int, selection *goquery.Selection) {
value := strings.TrimSpace(selection.Text())
if value == "" {
value = defaultValue
}
list = append(list, value)
})
return list, nil
}
// EachTextJoin eachTextJoin(sep) get each element text and join to string, return string.
// struct {
// Example string `pagser:".selector->eachTextJoin(',')"`
// }
func (builtin BuiltinFunctions) EachTextJoin(node *goquery.Selection, args ...string) (out interface{}, err error) {
sep := ","
if len(args) > 0 {
sep = args[0]
}
list := make([]string, 0)
node.Each(func(i int, selection *goquery.Selection) {
list = append(list, strings.TrimSpace(selection.Text()))
})
return strings.Join(list, sep), nil
}
// EqAndAttr eqAndAttr(index, name) reduces the set of matched elements to the one at the specified index, and attr() return string.
// struct {
// Example string `pagser:".selector->eqAndAttr(0, href)"`
// }
func (builtin BuiltinFunctions) EqAndAttr(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 2 {
return "", fmt.Errorf("eqAndAttr(index) must has index and attr name")
}
indexValue := strings.TrimSpace(args[0])
idx, err := strconv.Atoi(indexValue)
if err != nil {
return "", fmt.Errorf("index=`" + indexValue + "` is not number: " + err.Error())
}
name := strings.TrimSpace(args[1])
return node.Eq(idx).AttrOr(name, ""), nil
}
// EqAndHtml eqAndHtml(index) reduces the set of matched elements to the one at the specified index, and html() return string.
// struct {
// Example string `pagser:".selector->eqAndHtml(0)"`
// }
func (builtin BuiltinFunctions) EqAndHtml(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("eqAndHtml(index) must has index")
}
indexValue := strings.TrimSpace(args[0])
idx, err := strconv.Atoi(indexValue)
if err != nil {
return "", fmt.Errorf("index=`" + indexValue + "` is not number: " + err.Error())
}
return node.Eq(idx).Html()
}
// EqAndOutHtml eqAndOutHtml(index) reduces the set of matched elements to the one at the specified index, and outHtml() return string.
// struct {
// Example string `pagser:".selector->eqAndOutHtml(0)"`
// }
func (builtin BuiltinFunctions) EqAndOutHtml(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("eqAndOutHtml(index) must has index")
}
indexValue := strings.TrimSpace(args[0])
idx, err := strconv.Atoi(indexValue)
if err != nil {
return "", fmt.Errorf("index=`" + indexValue + "` is not number: " + err.Error())
}
return goquery.OuterHtml(node.Eq(idx))
}
// EqAndText eqAndText(index) reduces the set of matched elements to the one at the specified index, return string.
// struct {
// Example string `pagser:".selector->eqAndText(0)"`
// }
func (builtin BuiltinFunctions) EqAndText(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("eqAndText(index) must has index")
}
indexValue := strings.TrimSpace(args[0])
idx, err := strconv.Atoi(indexValue)
if err != nil {
return "", fmt.Errorf("index=`" + indexValue + "` is not number: " + err.Error())
}
return strings.TrimSpace(node.Eq(idx).Text()), nil
}
// Html html() get element inner html, return string.
// struct {
// Example string `pagser:".selector->html()"`
// }
func (builtin BuiltinFunctions) Html(node *goquery.Selection, args ...string) (out interface{}, err error) {
return node.Html()
}
// OutHtml outerHtml() get element outer html, return string.
// struct {
// Example string `pagser:".selector->outerHtml()"`
// }
func (builtin BuiltinFunctions) OutHtml(node *goquery.Selection, args ...string) (out interface{}, err error) {
html, err := goquery.OuterHtml(node)
if err != nil {
return "", err
}
return html, nil
}
// Size size() returns the number of elements in the Selection object, return int.
// struct {
// Size int `pagser:".selector->size()"`
// }
func (builtin BuiltinFunctions) Size(node *goquery.Selection, args ...string) (out interface{}, err error) {
return node.Size(), nil
}
// Text text() get element text, return string, this is default function, if not define function in struct tag.
// struct {
// Example string `pagser:".selector->text()"`
// }
func (builtin BuiltinFunctions) Text(node *goquery.Selection, args ...string) (out interface{}, err error) {
return strings.TrimSpace(node.Text()), nil
}
// TextConcat textConcat(text1, $value, [ text2, ... text_n ])
// The `text1, text2, ... text_n` strings that you wish to join together,
// `$value` is placeholder for get element text, return string.
// struct {
// Example string `pagser:".selector->textConcat('Result:', '<', $value, '>')"`
// }
func (builtin BuiltinFunctions) TextConcat(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 2 {
return "", fmt.Errorf("textConcat(text1, $value, [ text2, ... text_n ]) must be more than two arguments")
}
value := strings.TrimSpace(node.Text())
builder := strings.Builder{}
for _, v := range args {
if v == "$value" {
builder.WriteString(value)
} else {
builder.WriteString(v)
}
}
return builder.String(), nil
}
// TextEmpty textEmpty(defaultValue) get element text, if empty will return defaultValue, return string.
// struct {
// Example string `pagser:".selector->textEmpty('')"`
// }
func (builtin BuiltinFunctions) TextEmpty(node *goquery.Selection, args ...string) (out interface{}, err error) {
if len(args) < 1 {
return "", fmt.Errorf("textEmpty(defaultValue) must has defaultValue")
}
defaultValue := args[0]
value := strings.TrimSpace(node.Text())
if value == "" {
value = defaultValue
}
return value, nil
}
// TextSplit textSplit(sep=',', trim='true') get element text and split by separator to array string, return []string.
// struct {
// Examples []string `pagser:".selector->textSplit('|')"`
// }
func (builtin BuiltinFunctions) TextSplit(node *goquery.Selection, args ...string) (out interface{}, err error) {
sep := ","
trim := true
if len(args) > 0 {
sep = args[0]
}
if len(args) > 1 {
var err error
trim, err = cast.ToBoolE(args[1])
if err != nil {
return nil, fmt.Errorf("`trim` must bool type value: true/false")
}
}
list := strings.Split(node.Text(), sep)
if trim {
for i, v := range list {
list[i] = strings.TrimSpace(v)
}
}
return list, nil
}