-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.go
99 lines (96 loc) · 2.34 KB
/
functions.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
package got
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"net/url"
"strings"
"time"
)
// Functions I've found to be required in most every web-site template engine
// Many borrowed from https://github.com/Masterminds/sprig
// DefaultFunctions for templates
var DefaultFunctions = template.FuncMap{
"title": strings.Title,
"upper": strings.ToUpper,
"lower": strings.ToLower,
"trim": strings.TrimSpace,
"urlencode": url.QueryEscape,
// Often used for tables of rows
"yesno": func(yes string, no string, value bool) string {
if value {
return yes
}
return no
},
// Display singluar or plural based on count
"plural": func(one, many string, count int) string {
if count == 1 {
return one
}
return many
},
// Current Date (Local server time)
"date": func() string {
return time.Now().Format("2006-01-02")
},
// Current Unix timestamp
"unixtimestamp": func() string {
return fmt.Sprintf("%d", time.Now().Unix())
},
// json encodes an item into a JSON string
"json": func(v interface{}) string {
output, _ := json.Marshal(v)
return string(output)
},
// Allow unsafe injection into HTML
"noescape": func(a ...interface{}) template.HTML {
return template.HTML(fmt.Sprint(a...))
},
// Allow unsafe URL injections into HTML
"noescapeurl": func(u string) template.URL {
return template.URL(u)
},
// Modern Hash
"sha256": func(input string) string {
hash := sha256.Sum256([]byte(input))
return hex.EncodeToString(hash[:])
},
// Legacy
"sha1": func(input string) string {
hash := sha1.Sum([]byte(input))
return hex.EncodeToString(hash[:])
},
// Gravatar
"md5": func(input string) string {
hash := md5.Sum([]byte(input))
return hex.EncodeToString(hash[:])
},
// Popular encodings
"base64encode": func(v string) string {
return base64.StdEncoding.EncodeToString([]byte(v))
},
"base64decode": func(v string) string {
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err.Error()
}
return string(data)
},
"base32encode": func(v string) string {
return base32.StdEncoding.EncodeToString([]byte(v))
},
"base32decode": func(v string) string {
data, err := base32.StdEncoding.DecodeString(v)
if err != nil {
return err.Error()
}
return string(data)
},
}