-
Notifications
You must be signed in to change notification settings - Fork 58
/
standard_filters.go
271 lines (255 loc) · 7.05 KB
/
standard_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
// Package filters is an internal package that defines the standard Liquid filters.
package filters
import (
"encoding/json"
"fmt"
"html"
"math"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/osteele/liquid/values"
"github.com/osteele/tuesday"
)
// A FilterDictionary holds filters.
type FilterDictionary interface {
AddFilter(string, interface{})
}
// AddStandardFilters defines the standard Liquid filters.
func AddStandardFilters(fd FilterDictionary) { // nolint: gocyclo
// value filters
fd.AddFilter("default", func(value, defaultValue interface{}) interface{} {
if value == nil || value == false || values.IsEmpty(value) {
value = defaultValue
}
return value
})
// array filters
fd.AddFilter("compact", func(a []interface{}) (result []interface{}) {
for _, item := range a {
if item != nil {
result = append(result, item)
}
}
return
})
fd.AddFilter("join", joinFilter)
fd.AddFilter("map", func(a []map[string]interface{}, key string) (result []interface{}) {
for _, obj := range a {
result = append(result, obj[key])
}
return result
})
fd.AddFilter("reverse", reverseFilter)
fd.AddFilter("sort", sortFilter)
// https://shopify.github.io/liquid/ does not demonstrate first and last as filters,
// but https://help.shopify.com/themes/liquid/filters/array-filters does
fd.AddFilter("first", func(a []interface{}) interface{} {
if len(a) == 0 {
return nil
}
return a[0]
})
fd.AddFilter("last", func(a []interface{}) interface{} {
if len(a) == 0 {
return nil
}
return a[len(a)-1]
})
fd.AddFilter("uniq", uniqFilter)
// date filters
fd.AddFilter("date", func(t time.Time, format func(string) string) (string, error) {
f := format("%a, %b %d, %y")
return tuesday.Strftime(f, t)
})
// number filters
fd.AddFilter("abs", math.Abs)
fd.AddFilter("ceil", math.Ceil)
fd.AddFilter("floor", math.Floor)
fd.AddFilter("modulo", math.Mod)
fd.AddFilter("minus", func(a, b float64) float64 {
return a - b
})
fd.AddFilter("plus", func(a, b float64) float64 {
return a + b
})
fd.AddFilter("times", func(a, b float64) float64 {
return a * b
})
fd.AddFilter("divided_by", func(a float64, b interface{}) interface{} {
switch q := b.(type) {
case int, int16, int32, int64:
return int(a) / q.(int)
case float32, float64:
return a / b.(float64)
default:
return nil
}
})
fd.AddFilter("round", func(n float64, places func(int) int) float64 {
pl := places(0)
exp := math.Pow10(pl)
return math.Floor(n*exp+0.5) / exp
})
// sequence filters
fd.AddFilter("size", values.Length)
// string filters
fd.AddFilter("append", func(s, suffix string) string {
return s + suffix
})
fd.AddFilter("capitalize", func(s, suffix string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
})
fd.AddFilter("downcase", func(s, suffix string) string {
return strings.ToLower(s)
})
fd.AddFilter("escape", html.EscapeString)
fd.AddFilter("escape_once", func(s, suffix string) string {
return html.EscapeString(html.UnescapeString(s))
})
fd.AddFilter("newline_to_br", func(s string) string {
return strings.Replace(s, "\n", "<br />", -1)
})
fd.AddFilter("prepend", func(s, prefix string) string {
return prefix + s
})
fd.AddFilter("remove", func(s, old string) string {
return strings.Replace(s, old, "", -1)
})
fd.AddFilter("remove_first", func(s, old string) string {
return strings.Replace(s, old, "", 1)
})
fd.AddFilter("replace", func(s, old, new string) string {
return strings.Replace(s, old, new, -1)
})
fd.AddFilter("replace_first", func(s, old, new string) string {
return strings.Replace(s, old, new, 1)
})
fd.AddFilter("sort_natural", sortNaturalFilter)
fd.AddFilter("slice", func(s string, start int, length func(int) int) string {
// runes aren't bytes; don't use slice
n := length(1)
if start < 0 {
start = utf8.RuneCountInString(s) + start
}
p := regexp.MustCompile(fmt.Sprintf(`^.{%d}(.{0,%d}).*$`, start, n))
return p.ReplaceAllString(s, "$1")
})
fd.AddFilter("split", splitFilter)
fd.AddFilter("strip_html", func(s string) string {
// TODO this probably isn't sufficient
return regexp.MustCompile(`<.*?>`).ReplaceAllString(s, "")
})
fd.AddFilter("strip_newlines", func(s string) string {
return strings.Replace(s, "\n", "", -1)
})
fd.AddFilter("strip", strings.TrimSpace)
fd.AddFilter("lstrip", func(s string) string {
return strings.TrimLeftFunc(s, unicode.IsSpace)
})
fd.AddFilter("rstrip", func(s string) string {
return strings.TrimRightFunc(s, unicode.IsSpace)
})
fd.AddFilter("truncate", func(s string, length func(int) int, ellipsis func(string) string) string {
n := length(50)
el := ellipsis("...")
// runes aren't bytes; don't use slice
re := regexp.MustCompile(fmt.Sprintf(`^(.{%d})..{%d,}`, n-len(el), len(el)))
return re.ReplaceAllString(s, `$1`+el)
})
fd.AddFilter("truncatewords", func(s string, length func(int) int, ellipsis func(string) string) string {
el := ellipsis("...")
n := length(15)
re := regexp.MustCompile(fmt.Sprintf(`^(?:\s*\S+){%d}`, n))
m := re.FindString(s)
if m == "" {
return s
}
return m + el
})
fd.AddFilter("upcase", func(s, suffix string) string {
return strings.ToUpper(s)
})
fd.AddFilter("url_encode", url.QueryEscape)
fd.AddFilter("url_decode", url.QueryUnescape)
// debugging filters
// inspect is from Jekyll
fd.AddFilter("inspect", func(value interface{}) string {
s, err := json.Marshal(value)
if err != nil {
return fmt.Sprintf("%#v", value)
}
return string(s)
})
fd.AddFilter("type", func(value interface{}) string {
return fmt.Sprintf("%T", value)
})
}
func joinFilter(a []interface{}, sep func(string) string) interface{} {
ss := make([]string, 0, len(a))
s := sep(" ")
for _, v := range a {
if v != nil {
ss = append(ss, fmt.Sprint(v))
}
}
return strings.Join(ss, s)
}
func reverseFilter(a []interface{}) interface{} {
result := make([]interface{}, len(a))
for i, x := range a {
result[len(result)-1-i] = x
}
return result
}
var wsre = regexp.MustCompile(`[[:space:]]+`)
func splitFilter(s, sep string) interface{} {
result := strings.Split(s, sep)
if sep == " " {
// Special case for Ruby, therefore Liquid
result = wsre.Split(s, -1)
}
// This matches Ruby / Liquid / Jekyll's observed behavior.
for len(result) > 0 && result[len(result)-1] == "" {
result = result[:len(result)-1]
}
return result
}
func uniqFilter(a []interface{}) (result []interface{}) {
seenMap := map[interface{}]bool{}
seen := func(item interface{}) bool {
if k := reflect.TypeOf(item).Kind(); k < reflect.Array || k == reflect.Ptr || k == reflect.UnsafePointer {
if seenMap[item] {
return true
}
seenMap[item] = true
return false
}
// the O(n^2) case:
for _, other := range result {
if eqItems(item, other) {
return true
}
}
return false
}
for _, item := range a {
if !seen(item) {
result = append(result, item)
}
}
return
}
func eqItems(a, b interface{}) bool {
if reflect.TypeOf(a).Comparable() && reflect.TypeOf(b).Comparable() {
return a == b
}
return reflect.DeepEqual(a, b)
}