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
|
package template
import (
"fmt"
html "html/template"
"io"
"strconv"
"strings"
text "text/template"
"github.com/Necoro/feed2imap-go/pkg/log"
)
type Template interface {
Execute(wr io.Writer, data interface{}) error
}
func must(t Template, err error) Template {
if err != nil {
panic(err)
}
return t
}
func dict(v ...interface{}) map[string]interface{} {
dict := make(map[string]interface{})
lenv := len(v)
for i := 0; i < lenv; i += 2 {
key := v[i].(string)
if i+1 >= lenv {
dict[key] = ""
continue
}
dict[key] = v[i+1]
}
return dict
}
func join(sep string, parts []string) string {
return strings.Join(parts, sep)
}
func lastUrlPart(url string) string {
split := strings.Split(url, "/")
return split[len(split)-1]
}
func byteCount(str string) string {
var b uint64
if str != "" {
var err error
if b, err = strconv.ParseUint(str, 10, 64); err != nil {
log.Printf("Cannot convert '%s' to byte count: %s", str, err)
}
}
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
}
func _html(s string) html.HTML {
return html.HTML(s)
}
var funcMap = html.FuncMap{
"dict": dict,
"join": join,
"lastUrlPart": lastUrlPart,
"byteCount": byteCount,
"html": _html,
}
func fromString(name, templateStr string, useHtml bool) Template {
if useHtml {
return must(html.New(name).Funcs(funcMap).Parse(templateStr))
} else {
return must(text.New(name).Funcs(text.FuncMap(funcMap)).Parse(templateStr))
}
}
|