aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/template/template.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-05-10 22:07:17 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-05-10 22:07:17 +0200
commit5e5d848b1324cc5ea3991276f2a0750883e5aab0 (patch)
tree50135bb4613d5486225b7cf90e409e4209bf9a76 /internal/feed/template/template.go
parent26c4cd410eee27c6e5764d991d3f495635c87faf (diff)
downloadfeed2imap-go-5e5d848b1324cc5ea3991276f2a0750883e5aab0.tar.gz
feed2imap-go-5e5d848b1324cc5ea3991276f2a0750883e5aab0.tar.bz2
feed2imap-go-5e5d848b1324cc5ea3991276f2a0750883e5aab0.zip
Text part in emails
Diffstat (limited to 'internal/feed/template/template.go')
-rw-r--r--internal/feed/template/template.go32
1 files changed, 24 insertions, 8 deletions
diff --git a/internal/feed/template/template.go b/internal/feed/template/template.go
index 5a30c56..d8eb850 100644
--- a/internal/feed/template/template.go
+++ b/internal/feed/template/template.go
@@ -2,13 +2,26 @@ package template
import (
"fmt"
- "html/template"
+ 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)
@@ -53,19 +66,22 @@ func byteCount(str string) string {
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
}
-func html(s string) template.HTML {
- return template.HTML(s)
+func _html(s string) html.HTML {
+ return html.HTML(s)
}
-var funcMap = template.FuncMap{
+var funcMap = html.FuncMap{
"dict": dict,
"join": join,
"lastUrlPart": lastUrlPart,
"byteCount": byteCount,
- "html": html,
+ "html": _html,
}
-func fromString(name, templateStr string) *template.Template {
- tpl := template.New(name).Funcs(funcMap)
- return template.Must(tpl.Parse(templateStr))
+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))
+ }
}