aboutsummaryrefslogtreecommitdiff
path: root/internal/template/template.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-21 01:26:48 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-21 01:26:48 +0200
commit87935efe945c6847d2b18afaf7bfae5e80aa61e6 (patch)
tree748e678472b937d19c4985b059d8db590890fed0 /internal/template/template.go
parentafe2c4919dc2ded2f3b7d796b914dd66fbe78e64 (diff)
downloadfeed2imap-go-87935efe945c6847d2b18afaf7bfae5e80aa61e6.tar.gz
feed2imap-go-87935efe945c6847d2b18afaf7bfae5e80aa61e6.tar.bz2
feed2imap-go-87935efe945c6847d2b18afaf7bfae5e80aa61e6.zip
HTML Template part of the mail
Diffstat (limited to '')
-rw-r--r--internal/template/template.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/internal/template/template.go b/internal/template/template.go
new file mode 100644
index 0000000..e31ece2
--- /dev/null
+++ b/internal/template/template.go
@@ -0,0 +1,57 @@
+package template
+
+import (
+ "fmt"
+ "html/template"
+ "path/filepath"
+ "strings"
+)
+
+func dict(v ...string) map[string]string {
+ dict := map[string]string{}
+ lenv := len(v)
+ for i := 0; i < lenv; i += 2 {
+ key := v[i]
+ 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(b int64) string {
+ const unit = 1024
+ if b < unit {
+ return fmt.Sprintf("%d B", b)
+ }
+ div, exp := int64(unit), 0
+ for n := b / unit; n >= unit; n /= unit {
+ div *= unit
+ exp++
+ }
+ return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
+}
+
+var funcMap = template.FuncMap{
+ "dict": dict,
+ "join": join,
+ "lastUrlPart": LastUrlPart,
+ "byteCount": byteCount,
+}
+
+func ForFile(filename string) *template.Template {
+ name := filepath.Base(filename)
+ tpl := template.New(name).Funcs(funcMap)
+ return template.Must(tpl.ParseFiles(filename))
+}