This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.go
143 lines (126 loc) · 4.26 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
package temple
import (
"errors"
"fmt"
"html"
"html/template"
"net/url"
_strings "strings"
"time"
"github.com/spf13/cast"
)
// FuncMap is a map of useful template funcs available for FuncMap use.
var FuncMap = template.FuncMap{
"dateFormat": dateFormat,
"htmlEscape": htmlEscape,
"htmlUnescape": htmlUnescape,
"safeHTML": safeHTML,
"safeURL": safeURL,
"dict": dictionary,
"querify": querify,
"split": split,
"loop": loop,
}
// loop allows you to create an arbitrary iterator within a template.
// https://stackoverflow.com/questions/28917530/golang-how-to-create-loop-function-using-html-template-package/28918301
func loop(n int) []struct{} {
return make([]struct{}, n)
}
// dateFormat formats a textual representation of a datetime string into the
// specified layout. If nil is provided as the textual datetime it will be
// replaced with time.Now.
// https://golang.org/pkg/time/#pkg-constants
func dateFormat(layout string, v interface{}) (string, error) {
var t time.Time
var err error
if v == nil {
t = time.Now()
} else {
t, err = cast.ToTimeE(v)
if err != nil {
return "", err
}
}
return t.Format(layout), nil
}
// htmlEscape returns the given string with critical reserved HTML codes
// escaped, such that `&` becomes `&` and so on. Only the `<`, `>`, `&`,
// `_`, `'`, and `"` characters are escaped.
//
// Keep in mind that, unless content is passed through `safeHTML`, output
// strings are escaped in default settings by the processor anyway.
func htmlEscape(in interface{}) (string, error) {
conv, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return html.EscapeString(conv), nil
}
// htmlUnescape returns the given string with HTML escape codes un-escaped. This
// un-escapes more codes than `htmlEscape` escapes, including `#` codes and
// pre-UTF8 escapes for accented characters. It defers completely to the native
// `html.UnescapeString` function, so it's functionally consistent with it.
// Remember to pass the output of this to `safeHTML` if fully unescaped
// characters are desired, otherwise the output will be escaped again as normal.
// https://golang.org/pkg/html/#EscapeString
func htmlUnescape(in interface{}) (string, error) {
conv, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return html.UnescapeString(conv), nil
}
// dictionary creates a map[string]interface{} from the given parameters by
// walking the parameters and treating them as key-value pairs. The number of
// parameters must be even.
func dictionary(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
// querify encodes a set of key-value pairs into a "URL encoded" query string
// that can be appended to a URL after the `?` character.
func querify(params ...interface{}) (string, error) {
qs := url.Values{}
vals, err := dictionary(params...)
if err != nil {
return "", errors.New("querify keys must be strings")
}
for name, value := range vals {
qs.Add(name, fmt.Sprintf("%v", value))
}
return qs.Encode(), nil
}
// safeHTML returns a given string as a html/template known-safe HTML document
// fragment, instructing template parsers to output its content verbatim.
// https://golang.org/pkg/html/template/#HTML
func safeHTML(a interface{}) (template.HTML, error) {
s, err := cast.ToStringE(a)
return template.HTML(s), err
}
// safeURL returns a given string as a html/template known-safe URL or URL
// substring, instructing template parsers to output its content verbatim.
// https://golang.org/pkg/html/template/#URL
func safeURL(a interface{}) (template.URL, error) {
s, err := cast.ToStringE(a)
return template.URL(s), err
}
// split slices s string into all substrings separated by sep and returns a
// slice of the substrings between those separators.
// https://golang.org/pkg/strings/#Split
func split(a interface{}, delimiter string) ([]string, error) {
aStr, err := cast.ToStringE(a)
if err != nil {
return []string{}, err
}
return _strings.Split(aStr, delimiter), nil
}