-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfilters.go
292 lines (243 loc) · 8.1 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
package pongo
// TODO: Add context-sensitive filters (so they know their location, e.g. for
// context-sensitive escaping within javascript <-> normal body html.)
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
type FilterFunc func(interface{}, []interface{}, *FilterChainContext) (interface{}, error)
type FilterChainContext struct {
// Store what you want along the filter chain. Every filter has access to this store.
Store map[string]interface{}
applied_filters []string
}
func (ctx *FilterChainContext) HasVisited(names ...string) bool {
for _, filter := range ctx.applied_filters {
for _, name := range names {
if filter == name {
return true
}
}
}
return false
}
func (ctx *FilterChainContext) visitFilter(name string) {
ctx.applied_filters = append(ctx.applied_filters, name)
}
var Filters = map[string]FilterFunc{
"safe": filterSafe,
"unsafe": nil, // It will not be called, just added to visited filters (applied_filters)
"lower": filterLower,
"upper": filterUpper,
"capitalize": filterCapitalize,
"default": filterDefault,
"trim": filterTrim,
"length": filterLength,
"join": filterJoin,
"striptags": filterStriptags,
"time_format": filterTimeFormat,
"floatformat": filterFloatFormat,
/* TODO:
- verbatim
- ...
*/
}
func newFilterChainContext() *FilterChainContext {
return &FilterChainContext{
applied_filters: make([]string, 0, 5),
}
}
func filterSafe(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
if ctx.HasVisited("unsafe", "safe") {
// If "unsafe" or "safe" were already applied to the value
// don't do it (again, in case of "safe")
return value, nil
}
str, is_str := value.(string)
if !is_str {
// We don't have to safe non-strings
return value, nil
}
output := strings.Replace(str, "&", "&", -1)
output = strings.Replace(output, ">", ">", -1)
output = strings.Replace(output, "<", "<", -1)
return output, nil
}
func filterLower(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
str, is_str := value.(string)
if !is_str {
return nil, errors.New(fmt.Sprintf("%v (%T) is not of type string", value, value))
}
return strings.ToLower(str), nil
}
func filterTimeFormat(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
t, is_time := value.(time.Time)
if !is_time {
return nil, errors.New(fmt.Sprintf("%v (%T) is not of type string", value, value))
}
arg := args[0]
if arg == nil {
return nil, errors.New("time_format requires you pass a format.")
}
format, is_string := arg.(string)
if !is_string {
return nil, errors.New(fmt.Sprintf("time_format's format must be a string. %v (%T) passed.", format, format))
}
return t.Format(format), nil
}
func filterUpper(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
str, is_str := value.(string)
if !is_str {
return nil, errors.New(fmt.Sprintf("%v (%T) is not of type string", value, value))
}
return strings.ToUpper(str), nil
}
func filterCapitalize(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
str, is_str := value.(string)
if !is_str {
return nil, errors.New(fmt.Sprintf("%v (%T) is not of type string", value, value))
}
return strings.Title(str), nil
}
func filterTrim(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
str, is_str := value.(string)
if !is_str {
return nil, errors.New(fmt.Sprintf("%v (%T) is not of type string", value, value))
}
return strings.TrimSpace(str), nil
}
func filterLength(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Slice, reflect.Array, reflect.String, reflect.Map:
return rv.Len(), nil
default:
return nil, errors.New(fmt.Sprintf("Cannot determine length from type %T ('%v').", value, value))
}
panic("unreachable")
}
func filterJoin(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
if len(args) != 1 {
return nil, errors.New("Please provide a separator")
}
sep, is_string := args[0].(string)
if !is_string {
return nil, errors.New(fmt.Sprintf("Separator must be of type string, not %T ('%v')", args[0], args[0]))
}
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Slice, reflect.Array:
items := make([]string, 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
items = append(items, fmt.Sprintf("%v", rv.Index(i).Interface()))
}
return strings.Join(items, sep), nil
default:
return nil, errors.New(fmt.Sprintf("Cannot join variable of type %T ('%v').", value, value))
}
panic("unreachable")
}
func filterStriptags(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
str, is_str := value.(string)
if !is_str {
return nil, errors.New(fmt.Sprintf("%v is not of type string", value))
}
if len(args) > 1 {
return nil, errors.New("Please provide a comma-seperated string with tags (or no string to remove all tags).")
}
if len(args) == 1 {
taglist, is_string := args[0].(string)
if !is_string {
return nil, errors.New(fmt.Sprintf("Taglist must be a string, not %T ('%v')", args[0], args[0]))
}
tags := strings.Split(taglist, ",")
for _, tag := range tags {
re := regexp.MustCompile(fmt.Sprintf("</?%s/?>", tag))
str = re.ReplaceAllString(str, "")
}
} else {
re := regexp.MustCompile("<[^>]*?>")
str = re.ReplaceAllString(str, "")
}
return strings.TrimSpace(str), nil
}
func filterDefault(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
// Use reflect to check against zero() of type
if len(args) != 1 {
return nil, errors.New("Default filter takes only one argument")
}
if reflect.Zero(reflect.TypeOf(value)).Interface() == value {
return args[0], nil
}
return value, nil
}
/*
Filter for formatting floats. The filter closely follows Django's implementation.
Examples:
When used without an argument, it rounds the float to 1 decimal place, but only if there's a decimal point to be displayed:
{{ 34.23234|floatformat }} displays 34.2
{{ 34.00000|floatformat }} displays 34
{{ 34.26000|floatformat }} displays 34.3
When used with an integer parameter, it rounds the float to that number of decimals. No trimming of zeros occurs.
{{ 34.23234|floatformat:3 }} displays 34.232
{{ 34.00000|floatformat:3 }} displays 34.000
{{ 34.26000|floatformat:3 }} displays 34.260
"0" rounds to the nearest integer.
{{ 34.23234|floatformat:"0" }} displays 34
{{ 34.00000|floatformat:"0" }} displays 34
{{ 39.56000|floatformat:"0" }} displays 40
A negative parameter rounds to that number of decimals, but only if necessary.
{{ 34.23234|floatformat:"-3" }} displays 34.232
{{ 34.00000|floatformat:"-3" }} displays 34
{{ 34.26000|floatformat:"-3" }} displays 34.260
*/
func filterFloatFormat(value interface{}, args []interface{}, ctx *FilterChainContext) (interface{}, error) {
// Value to format
var floatValue float64
switch val := value.(type) {
case float32:
floatValue = float64(val)
case float64:
floatValue = val
default:
return nil, errors.New("Illegal type for floatformat (only float32 and float64 are acceptable)")
}
// Default parameters
decimals, trim := 1, true
if len(args) > 1 {
return nil, errors.New("Floatformat filter takes at most one argument")
} else if len(args) == 1 {
switch val := args[0].(type) {
case int:
decimals = val
trim = false
case string:
var err error
decimals, err = strconv.Atoi(val)
if err != nil {
return nil, errors.New(fmt.Sprintf("Illegal floatformat argument: %v", val))
}
if decimals <= 0 {
decimals = -decimals
} else {
trim = false
}
default:
return nil, errors.New(fmt.Sprintf("%v (%T) is not of type int or string", val, val))
}
}
fmtFloat := strconv.FormatFloat(floatValue, 'f', decimals, 64)
// Remove zeroes if they are unnecessary
if trim {
intVal := int(floatValue)
if floatValue-float64(intVal) == 0 {
fmtFloat = strconv.Itoa(intVal)
}
}
return fmtFloat, nil
}