/
filters.go
182 lines (153 loc) · 5 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
package template
import (
"errors"
"fmt"
"reflect"
"regexp"
"strings"
)
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,
/* 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
}
output := fmt.Sprintf("%v", value)
output = strings.Replace(output, "&", "&", -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 is not of type string", value))
}
return strings.ToLower(str), 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 is not of type string", 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 is not of type string", 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 is not of type string", 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
}