forked from wspl/creeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fun.go
141 lines (126 loc) · 3.09 KB
/
fun.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
package creeper
import (
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/moxar/arithmetic"
)
var(
rx_funName = regexp.MustCompile(`^[a-z$][a-zA-Z]{0,15}`)
)
type Fun struct {
Raw string
Node *Node
Name string
Params []string
Selection *goquery.Selection
Result string
PrevFun *Fun
NextFun *Fun
}
func (f *Fun) Append(s string) (*Fun, *Fun) {
f.NextFun = ParseFun(f.Node, s)
f.NextFun.PrevFun = f
return f, f.NextFun
}
func PowerfulFind(s *goquery.Selection, q string) *goquery.Selection {
rx_selectPseudoEq := regexp.MustCompile(`:eq\(\d+\)`)
if rx_selectPseudoEq.MatchString(q) {
rs := rx_selectPseudoEq.FindAllStringIndex(q, -1)
sel := s
for _, r := range rs {
iStr := q[r[0]+4:r[1]-1]
i64, _ := strconv.ParseInt(iStr, 10, 32)
i := int(i64)
sq := q[:r[0]]
q = strings.TrimSpace(q[r[1]:])
sel = sel.Find(sq).Eq(i)
}
if len(q) > 0 {
sel = sel.Find(q)
}
return sel
} else {
return s.Find(q)
}
}
func (f *Fun) InitSelector() {
if f.Node.IsArray || f.Node.IndentLen == 0 || f.Node.Page != nil {
r := strings.NewReader(f.Node.Page.Body())
doc, _ := goquery.NewDocumentFromReader(r)
bud := PowerfulFind(doc.Selection, f.Params[0])
if len(bud.Nodes) > f.Node.Index {
f.Selection = PowerfulFind(doc.Selection, f.Params[0]).Eq(f.Node.Index)
} else {
f.Node.Page.Inc()
f.Node.Reset()
f.InitSelector()
return
}
} else {
f.Node.ParentNode.Fun.Invoke()
f.Selection = PowerfulFind(f.Node.ParentNode.Fun.Selection, f.Params[0]).Eq(f.Node.Index)
}
}
func (f *Fun) Invoke() string {
switch f.Name {
case "$": f.InitSelector()
case "attr": f.Result, _ = f.PrevFun.Selection.Attr(f.Params[0])
case "text": f.Result = f.PrevFun.Selection.Text()
case "html": f.Result, _ = f.PrevFun.Selection.Html()
case "outerHTML": f.Result, _ = goquery.OuterHtml(f.PrevFun.Selection)
case "style": f.Result, _ = f.PrevFun.Selection.Attr("style")
case "href": f.Result, _ = f.PrevFun.Selection.Attr("href")
case "src": f.Result, _ = f.PrevFun.Selection.Attr("src")
case "calc":
v, _ := arithmetic.Parse(f.PrevFun.Result)
n, _ := arithmetic.ToFloat(v)
prec := 2
if len(f.Params) > 0 {
i64, _ := strconv.ParseInt(f.Params[0], 10, 32)
prec = int(i64)
}
f.Result = strconv.FormatFloat(n, 'g', prec, 64)
case "expand":
rx := regexp.MustCompile(f.Params[0])
src := f.PrevFun.Result
dst := []byte{}
m := rx.FindStringSubmatchIndex(src)
s := rx.ExpandString(dst, f.Params[1], src, m)
f.Result = string(s)
case "match":
rx := regexp.MustCompile(f.Params[0])
rs := rx.FindAllStringSubmatch(f.PrevFun.Result, -1)
if len(rs) > 0 && len(rs[0]) > 1 {
f.Result = rs[0][1]
}
}
if f.NextFun != nil {
return f.NextFun.Invoke()
} else {
return f.Result
}
}
func ParseFun(n *Node, s string) *Fun {
fun := new(Fun)
fun.Node = n
fun.Raw = s
sa := rx_funName.FindAllString(s, -1)
fun.Name = sa[0]
ls := s[len(sa[0]):]
ps := []string{}
p, pl := parseParams(ls)
for k := range p {
ps = append(ps, k)
}
if len(ps) > 0 {
fun.Params = ps
}
ls = ls[pl+1:]
if len(ls) > 0 {
ls = ls[1:]
fun.Append(ls)
}
return fun
}