-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
183 lines (168 loc) · 4.21 KB
/
funcs.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
package sq
import (
"errors"
"fmt"
"net/url"
pathpkg "path"
"reflect"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
douceur "github.com/aymerick/douceur/parser"
"github.com/emptyinterface/ago"
otto "github.com/robertkrimen/otto/parser"
)
type (
ParseFunc func(s, arg string) (string, error)
LoadFunc func(sel *goquery.Selection, s, arg string) (interface{}, error)
TypeLoader struct {
isType func(t reflect.Type) bool
load func(sel *goquery.Selection, s string) (interface{}, error)
}
parser struct {
f ParseFunc
args string
}
loader struct {
f LoadFunc
args string
}
)
var (
ErrNoRegexpMatch = errors.New("regexp did not match the content")
)
var (
parseFuncs = map[string]ParseFunc{
"regexp": func(s, pattern string) (string, error) {
r, err := regexp.Compile(pattern)
if err != nil {
return "", err
}
matches := r.FindStringSubmatch(s)
if len(matches) == 1 {
return matches[0], nil
}
if len(matches) > 1 {
if len(matches[1]) == 0 {
return "", ErrNoRegexpMatch
}
return matches[1], nil
}
return "", ErrNoRegexpMatch
},
"strip": func(s, pattern string) (string, error) {
r, err := regexp.Compile(pattern)
if err != nil {
return "", err
}
return r.ReplaceAllString(s, ""), nil
},
"path.prepend": func(s, token string) (string, error) {
if strings.HasPrefix(s, token) {
return s, nil
}
fmt.Println(pathpkg.Join(token, s))
return pathpkg.Join(token, s), nil
},
"path.append": func(s, token string) (string, error) {
if strings.HasSuffix(s, token) {
return s, nil
}
fmt.Println(pathpkg.Join(s, token))
return pathpkg.Join(s, token), nil
},
"prepend": func(s, token string) (string, error) {
if strings.HasPrefix(s, token) {
return s, nil
}
return token + s, nil
},
"append": func(s, token string) (string, error) {
if strings.HasSuffix(s, token) {
return s, nil
}
return s + token, nil
},
}
loadFuncs = map[string]LoadFunc{
"time": func(_ *goquery.Selection, s, layout string) (interface{}, error) {
return time.Parse(strings.TrimSpace(layout), strings.TrimSpace(s))
},
"ago": func(_ *goquery.Selection, s, _ string) (interface{}, error) {
return ago.Parse(strings.TrimSpace(s))
},
}
typeLoaders = map[string]TypeLoader{
"url": {
isType: func(t reflect.Type) bool {
return t.PkgPath() == "net/url" && t.Name() == "URL"
},
load: func(_ *goquery.Selection, s string) (interface{}, error) {
return url.Parse(s)
},
},
"goquery": {
isType: func(t reflect.Type) bool {
return strings.HasSuffix(t.PkgPath(), "/goquery") && t.Name() == "Selection"
},
load: func(sel *goquery.Selection, _ string) (interface{}, error) {
return sel.Clone(), nil
},
},
"html": {
isType: func(t reflect.Type) bool {
return t.PkgPath() == "golang.org/x/net/html" && t.Name() == "Node"
},
load: func(sel *goquery.Selection, _ string) (interface{}, error) {
return sel.Clone().Nodes[0], nil
},
},
"otto": {
isType: func(t reflect.Type) bool {
return strings.HasSuffix(t.PkgPath(), "otto/ast") && t.Name() == "Program"
},
load: func(sel *goquery.Selection, text string) (interface{}, error) {
if text == "" {
text = sel.Text()
}
return otto.ParseFile(nil, "", text, 0)
},
},
"css": {
isType: func(t reflect.Type) bool {
return strings.HasSuffix(t.PkgPath(), "douceur/css") && t.Name() == "Stylesheet"
},
load: func(sel *goquery.Selection, text string) (interface{}, error) {
if text == "" {
text = sel.Text()
}
return douceur.Parse(text)
},
},
}
)
func RegisterParseFunc(name string, f ParseFunc) {
parseFuncs[name] = f
}
func RegisterLoadFunc(name string, f LoadFunc) {
loadFuncs[name] = f
}
func RegisterTypeLoader(name string, isType func(t reflect.Type) bool, load func(sel *goquery.Selection, text string) (interface{}, error)) {
typeLoaders[name] = TypeLoader{
isType: isType,
load: load,
}
}
func (p parser) parse(s string) (string, error) {
if p.f != nil {
return p.f(s, p.args)
}
return s, nil
}
func (l *loader) load(sel *goquery.Selection, s string) (interface{}, error) {
if l.f != nil {
return l.f(sel, s, l.args)
}
return s, nil
}