-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.go
More file actions
84 lines (82 loc) · 1.75 KB
/
Copy pathtemplates.go
File metadata and controls
84 lines (82 loc) · 1.75 KB
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
package main
import (
"fmt"
"html/template"
"strings"
)
var templates = template.Must(template.New("").Funcs(template.FuncMap{
"split": strings.Split,
"join": func(base, part string) string {
if base == "" {
return part
}
return base + "/" + part
},
"parent": func(path string) string {
if !strings.Contains(path, "/") {
return ""
}
parts := strings.Split(path, "/")
return strings.Join(parts[:len(parts)-1], "/")
},
"add": func(a, b int) int {
return a + b
},
"max": func(a, b int) int {
if a > b {
return a
}
return b
},
"truncate": func(s string, l int) string {
s = strings.Split(s, "\n")[0]
s = strings.TrimSpace(s)
r := []rune(s)
if len(r) <= l {
return s
}
return string(r[:l]) + "..."
},
"float64": func(i int) float64 {
return float64(i)
},
"int": func(f float64) int {
return int(f)
},
"multiply": func(a, b float64) float64 {
return a * b
},
"divide": func(a, b float64) float64 {
if b == 0 {
return 0
}
return a / b
},
"hasPrefix": strings.HasPrefix,
"dict": func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, fmt.Errorf("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, fmt.Errorf("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
},
"humanize": func(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(size)/float64(div), "KMGTPE"[exp])
},
}).ParseFS(viewsFS, "views/*.html"))