-
Notifications
You must be signed in to change notification settings - Fork 13
/
filters.go
298 lines (280 loc) · 8.45 KB
/
filters.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
package filters
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"math/rand"
"net/url"
"reflect"
"regexp"
"strings"
"time"
sass "github.com/bep/godartsass/v2"
blackfriday "github.com/danog/blackfriday/v2"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
"github.com/osteele/liquid/evaluator"
"github.com/osteele/liquid/expressions"
)
// AddJekyllFilters adds the Jekyll filters to the Liquid engine.
func AddJekyllFilters(e *liquid.Engine, c *config.Config) {
// array filters
e.RegisterFilter("array_to_sentence_string", arrayToSentenceStringFilter)
// TODO doc neither Liquid nor Jekyll docs this, but it appears to be present
e.RegisterFilter("filter", func(values []map[string]interface{}, key string) []interface{} {
var result []interface{}
for _, value := range values {
if _, ok := value[key]; ok {
result = append(result, value)
}
}
return result
})
e.RegisterFilter("group_by", groupByFilter)
e.RegisterFilter("group_by_exp", groupByExpFilter)
e.RegisterFilter("sample", func(array []interface{}) interface{} {
if len(array) == 0 {
return nil
}
return array[rand.Intn(len(array))]
})
// sort overrides the Liquid filter with one that takes parameters
e.RegisterFilter("sort", sortFilter)
e.RegisterFilter("where", whereFilter) // TODO test case
e.RegisterFilter("where_exp", whereExpFilter)
e.RegisterFilter("xml_escape", xml.Marshal)
e.RegisterFilter("push", func(array []interface{}, item interface{}) interface{} {
return append(array, evaluator.MustConvertItem(item, array))
})
e.RegisterFilter("pop", requireNonEmptyArray(func(array []interface{}) interface{} {
return array[0]
}))
e.RegisterFilter("shift", requireNonEmptyArray(func(array []interface{}) interface{} {
return array[len(array)-1]
}))
e.RegisterFilter("unshift", func(array []interface{}, item interface{}) interface{} {
return append([]interface{}{evaluator.MustConvertItem(item, array)}, array...)
})
// dates
e.RegisterFilter("date_to_rfc822", func(date time.Time) string {
return date.Format(time.RFC822)
// Out: Mon, 07 Nov 2008 13:07:54 -0800
})
e.RegisterFilter("date_to_string", func(date time.Time) string {
return date.Format("02 Jan 2006")
// Out: 07 Nov 2008
})
e.RegisterFilter("date_to_long_string", func(date time.Time) string {
return date.Format("02 January 2006")
// Out: 07 November 2008
})
e.RegisterFilter("date_to_xmlschema", func(date time.Time) string {
return date.Format("2006-01-02T15:04:05-07:00")
// Out: 2008-11-07T13:07:54-08:00
})
// strings
e.RegisterFilter("absolute_url", func(s string) string {
return utils.URLJoin(c.AbsoluteURL, c.BaseURL, s)
})
e.RegisterFilter("relative_url", func(s string) string {
return c.BaseURL + s
})
e.RegisterFilter("jsonify", json.Marshal)
e.RegisterFilter("markdownify", blackfriday.Run)
e.RegisterFilter("normalize_whitespace", func(s string) string {
// s = strings.Replace(s, "n", "N", -1)
wsPattern := regexp.MustCompile(`(?s:[\s\n]+)`)
return wsPattern.ReplaceAllString(s, " ")
})
e.RegisterFilter("slugify", func(s, mode string) string {
if mode == "" {
mode = "default"
}
p := map[string]string{
"raw": `\s+`,
"default": `[^[:alnum:]]+`,
"pretty": `[^[:alnum:]\._~!$&'()+,;=@]+`,
}[mode]
if p != "" {
s = regexp.MustCompile(p).ReplaceAllString(s, "-")
}
return strings.ToLower(s)
})
e.RegisterFilter("to_integer", func(n int) int { return n })
e.RegisterFilter("number_of_words", func(s string) int {
wordPattern := regexp.MustCompile(`\w+`) // TODO what's the Jekyll spec for a word?
m := wordPattern.FindAllStringIndex(s, -1)
if m == nil {
return 0
}
return len(m)
})
// string escapes
e.RegisterFilter("cgi_escape", url.QueryEscape)
e.RegisterFilter("sassify", unimplementedFilter("sassify"))
e.RegisterFilter("scssify", scssifyFilter)
e.RegisterFilter("smartify", smartifyFilter)
e.RegisterFilter("uri_escape", func(s string) string {
return regexp.MustCompile(`\?(.+?)=([^&]*)(?:\&(.+?)=([^&]*))*`).ReplaceAllStringFunc(s, func(m string) string {
pair := strings.SplitN(m, "=", 2)
return pair[0] + "=" + url.QueryEscape(pair[1])
})
})
e.RegisterFilter("xml_escape", func(s string) string {
// TODO can't handle maps
// eval https://github.com/clbanning/mxj
// adapt https://stackoverflow.com/questions/30928770/marshall-map-to-xml-in-go
buf := new(bytes.Buffer)
if err := xml.EscapeText(buf, []byte(s)); err != nil {
panic(err)
}
return buf.String()
})
}
// helpers
func requireNonEmptyArray(fn func([]interface{}) interface{}) func([]interface{}) interface{} {
return func(array []interface{}) interface{} {
if len(array) == 0 {
return nil
}
return fn(array)
}
}
func unimplementedFilter(name string) func(value interface{}) interface{} {
warned := false
return func(value interface{}) interface{} {
if !warned {
fmt.Println("warning: unimplemented filter:", name)
warned = true
}
return value
}
}
// array filters
func arrayToSentenceStringFilter(array []string, conjunction func(string) string) string {
conj := conjunction("and ")
switch len(array) {
case 1:
return array[0]
default:
rt := reflect.ValueOf(array)
ar := make([]string, rt.Len())
for i, v := range array {
ar[i] = v
if i == rt.Len()-1 {
ar[i] = conj + v
}
}
return strings.Join(ar, ", ")
}
}
func groupByExpFilter(array []map[string]interface{}, name string, expr expressions.Closure) ([]map[string]interface{}, error) {
rt := reflect.ValueOf(array)
if !(rt.Kind() != reflect.Array || rt.Kind() == reflect.Slice) {
return nil, nil
}
groups := map[interface{}][]interface{}{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i).Interface()
key, err := expr.Bind(name, item).Evaluate()
if err != nil {
return nil, err
}
if group, found := groups[key]; found {
groups[key] = append(group, item)
} else {
groups[key] = []interface{}{item}
}
}
var result []map[string]interface{}
for k, v := range groups {
result = append(result, map[string]interface{}{"name": k, "items": v})
}
return result, nil
}
func groupByFilter(array []map[string]interface{}, property string) []map[string]interface{} {
rt := reflect.ValueOf(array)
if !(rt.Kind() != reflect.Array || rt.Kind() == reflect.Slice) {
return nil
}
groups := map[interface{}][]interface{}{}
for i := 0; i < rt.Len(); i++ {
irt := rt.Index(i)
if irt.Kind() == reflect.Map && irt.Type().Key().Kind() == reflect.String {
krt := irt.MapIndex(reflect.ValueOf(property))
if krt.IsValid() && krt.CanInterface() {
key := krt.Interface()
if group, found := groups[key]; found {
groups[key] = append(group, irt.Interface())
} else {
groups[key] = []interface{}{irt.Interface()}
}
}
}
}
var result []map[string]interface{}
for k, v := range groups {
result = append(result, map[string]interface{}{"name": k, "items": v})
}
return result
}
func sortFilter(array []interface{}, key interface{}, nilFirst func(bool) bool) []interface{} {
nf := nilFirst(true)
result := make([]interface{}, len(array))
copy(result, array)
if key == nil {
evaluator.Sort(result)
} else {
// TODO error if key is not a string
evaluator.SortByProperty(result, key.(string), nf)
}
return result
}
func whereExpFilter(array []interface{}, name string, expr expressions.Closure) ([]interface{}, error) {
rt := reflect.ValueOf(array)
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil, nil
}
var result []interface{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i).Interface()
value, err := expr.Bind(name, item).Evaluate()
if err != nil {
return nil, err
}
if value != nil && value != false {
result = append(result, item)
}
}
return result, nil
}
func whereFilter(array []map[string]interface{}, key string, value interface{}) []interface{} {
rt := reflect.ValueOf(array)
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil
}
var result []interface{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i)
if item.Kind() == reflect.Map && item.Type().Key().Kind() == reflect.String {
attr := item.MapIndex(reflect.ValueOf(key))
if attr.IsValid() && fmt.Sprint(attr) == value {
result = append(result, item.Interface())
}
}
}
return result
}
// string filters
var comp, compErr = sass.Start(sass.Options{})
func scssifyFilter(s string) (string, error) {
if compErr != nil {
return "", compErr
}
res, err := comp.Execute(sass.Args{
Source: s,
})
return res.CSS, err
}