-
Notifications
You must be signed in to change notification settings - Fork 5
/
templates.go
204 lines (193 loc) · 5.07 KB
/
templates.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
package common
import (
"crypto/rand"
"encoding/json"
"fmt"
"html/template"
"net/url"
"strconv"
"strings"
"time"
"github.com/andrewarrow/feedback/util"
)
const HUMAN_SMALL = "01/02/2006"
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
)
func TemplateFunctions() template.FuncMap {
fm := template.FuncMap{
"uuid": func() string {
return PseudoUuid()
},
"params": func(s template.HTML, matchKey, matchValue string) template.HTML {
buffer := []string{}
kvs, _ := url.ParseQuery(string(s))
for k, v := range kvs {
if k == matchKey {
buffer = append(buffer, k+"="+matchValue)
} else {
buffer = append(buffer, k+"="+v[0])
}
}
result := strings.Join(buffer, "&")
return template.HTML(result)
},
"humanSize": func(bytes int64) string {
size := float64(bytes)
result := ""
if size < KB {
result = fmt.Sprintf("%d bytes", bytes)
} else if size < MB {
result = fmt.Sprintf("%.2f KB", size/KB)
} else if size < GB {
result = fmt.Sprintf("%.2f MB", size/MB)
} else {
result = fmt.Sprintf("%.2f GB", size/GB)
}
return result
},
"short": func(a any) string {
tInt, ok := a.(int64)
if ok {
t := time.Unix(tInt, 0)
return t.Format(HUMAN_SMALL)
}
return ""
},
"add": func(i, j int) int { return i + j },
"mul": func(i, j int64) int64 { return i * j },
"optionList": func(s string, val any) template.HTML {
valString := ""
if val != nil {
valString = val.(string)
}
tokens := strings.Split(s[2:len(s)-2], "|")
valTokens := strings.Split(valString, ",")
valMap := map[string]bool{}
for _, item := range valTokens {
valMap[item] = true
}
buffer := []string{}
for _, token := range tokens {
option := fmt.Sprintf("<option>%s</option>", token)
if valMap[token] {
option = fmt.Sprintf("<option selected=\"true\">%s</option>", token)
}
buffer = append(buffer, option)
}
return template.HTML(strings.Join(buffer, "\n"))
},
"indent": func(thing any) string {
var other any
asBytes, ok := thing.([]byte)
if ok {
json.Unmarshal(asBytes, &other)
asBytes, _ := json.MarshalIndent(other, "", " ")
return string(asBytes)
}
asString, ok := thing.(string)
if ok {
json.Unmarshal([]byte(asString), &other)
asBytes, _ := json.MarshalIndent(other, "", " ")
return string(asBytes)
}
asBytes, _ = json.MarshalIndent(thing, "", " ")
return string(asBytes)
},
"guidOptionList": func(list []map[string]any, selectedGuid string) template.HTML {
buffer := []string{}
for _, item := range list {
selected := ""
guid := item["guid"].(string)
if guid == selectedGuid {
selected = `selected="true"`
}
option := fmt.Sprintf(`<option %s value="%s">%s</option>`,
selected, item["guid"],
item["name"])
buffer = append(buffer, option)
}
return template.HTML(strings.Join(buffer, "\n"))
},
"textfield": func(name string, val any) template.HTML {
value := ""
if val != nil {
value = fmt.Sprintf("%v", val)
}
s := fmt.Sprintf(`<input id="textfield-%s" class="nice-i border" name="%s" type="text" value="%s"/>`, name, name, value)
return template.HTML(s)
},
"breakComma": func(s string) []string { return strings.Split(s, ",") },
"price": func(s any) template.HTML {
amount := fmt.Sprintf("%v", s)
price, _ := strconv.Atoi(amount)
if price == 0 {
sp := "$0.00"
return template.HTML(sp)
}
if len(amount) < 3 {
sp := fmt.Sprintf("$00.%s", amount)
return template.HTML(sp)
}
dollars := amount[0 : len(amount)-2]
dollarsInt, _ := strconv.ParseInt(dollars, 10, 64)
dollars = util.IntComma(dollarsInt)
cents := amount[len(amount)-2:]
sp := fmt.Sprintf("$%s.%s", dollars, cents)
if price < 0 {
sp = "<span class='text-red-500'>" + sp + "</span>"
}
return template.HTML(sp)
},
"hasImage": func(thing any) bool {
s, ok := thing.(string)
if !ok {
return false
}
if strings.HasSuffix(s, ".png") {
return true
}
if strings.HasSuffix(s, ".jpg") || strings.HasSuffix(s, ".jpeg") {
return true
}
return false
},
"jq": func(thing any) template.HTML {
jsonData, _ := json.MarshalIndent(thing, "", " ")
asString := string(jsonData)
return template.HTML(strings.ReplaceAll(asString, "\n", "<br/>"))
},
"intCommaZero": func(a int64) string {
return util.IntComma(a)
},
"intComma": func(a int64) string {
if a == 0 {
return "n/a"
}
return util.IntComma(a)
},
"simplePrice": func(s any) string {
amount := fmt.Sprintf("%v", s)
price, _ := strconv.Atoi(amount)
if price == 0 {
return "0.00"
}
if len(amount) < 3 {
return fmt.Sprintf("00.%s", amount)
}
dollars := amount[0 : len(amount)-2]
//dollarsInt, _ := strconv.ParseInt(dollars, 10, 64)
//dollars = util.IntComma(dollarsInt)
cents := amount[len(amount)-2:]
return fmt.Sprintf("%s.%s", dollars, cents)
},
}
return fm
}
func PseudoUuid() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}