forked from foolin/pagser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.go
98 lines (92 loc) · 2.73 KB
/
builtin.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
package pagser
import (
"github.com/PuerkitoBio/goquery"
)
// CallFunc write function interface
//
// # Define Global Function
//
// func MyFunc(node *goquery.Selection, args ...string) (out interface{}, err error) {
// //Todo
// return "Hello", nil
// }
//
// //Register function
// pagser.RegisterFunc("MyFunc", MyFunc)
//
// //Use function
// type PageData struct{
// Text string `pagser:"h1->MyFunc()"`
// }
//
//
// # Define Struct Function
// //Use function
// type PageData struct{
// Text string `pagser:"h1->MyFunc()"`
// }
//
// func (pd PageData) MyFunc(node *goquery.Selection, args ...string) (out interface{}, err error) {
// //Todo
// return "Hello", nil
// }
//
// # Lookup function priority order
//
// struct method -> parent method -> ... -> global
//
// # Implicit convert type
//
// Automatic type conversion, Output result string convert to int, int64, float64...
//
// CallFunc is a define function interface
type CallFunc func(node *goquery.Selection, args ...string) (out interface{}, err error)
//BuiltinFunctions instance
var builtinFun BuiltinFunctions
//BuiltinSelections instance
var builtinSel BuiltinSelections
//builtin functions
var builtinFuncs = map[string]CallFunc{
"absHref": builtinFun.AbsHref,
"attr": builtinFun.Attr,
"attrConcat": builtinFun.AttrConcat,
"attrEmpty": builtinFun.AttrEmpty,
"attrSplit": builtinFun.AttrSplit,
"eachAttr": builtinFun.EachAttr,
"eachAttrEmpty": builtinFun.EachAttrEmpty,
"eachHtml": builtinFun.EachHtml,
"eachOutHtml": builtinFun.EachOutHtml,
"eachText": builtinFun.EachText,
"eachTextEmpty": builtinFun.EachTextEmpty,
"eachTextJoin": builtinFun.EachTextJoin,
"eqAndAttr": builtinFun.EqAndAttr,
"eqAndHtml": builtinFun.EqAndHtml,
"eqAndOutHtml": builtinFun.EqAndOutHtml,
"eqAndText": builtinFun.EqAndText,
"html": builtinFun.Html,
"outerHtml": builtinFun.OutHtml,
"size": builtinFun.Size,
"text": builtinFun.Text,
"textConcat": builtinFun.TextConcat,
"textEmpty": builtinFun.TextEmpty,
"textSplit": builtinFun.TextSplit,
// selector
"child": builtinSel.Child,
"eq": builtinSel.Eq,
"first": builtinSel.First,
"last": builtinSel.Last,
"next": builtinSel.Next,
"parent": builtinSel.Parent,
"parents": builtinSel.Parents,
"parentsUntil": builtinSel.ParentsUntil,
"prev": builtinSel.Prev,
"siblings": builtinSel.Siblings,
}
// RegisterFunc register function for parse result
// pagser.RegisterFunc("MyFunc", func(node *goquery.Selection, args ...string) (out interface{}, err error) {
// //Todo
// return "Hello", nil
// })
func (p *Pagser) RegisterFunc(name string, fn CallFunc) {
p.mapFuncs.Store(name, fn)
}