From 5e5d848b1324cc5ea3991276f2a0750883e5aab0 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sun, 10 May 2020 22:07:17 +0200 Subject: Text part in emails --- internal/feed/template/html.tpl.go | 4 ++-- internal/feed/template/template.go | 32 +++++++++++++++++++++-------- internal/feed/template/text.tpl.go | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 internal/feed/template/text.tpl.go (limited to 'internal/feed/template') diff --git a/internal/feed/template/html.tpl.go b/internal/feed/template/html.tpl.go index 4626188..be84030 100644 --- a/internal/feed/template/html.tpl.go +++ b/internal/feed/template/html.tpl.go @@ -1,9 +1,9 @@ package template -var Html = fromString("Feed", feedTpl) +var Html = fromString("Feed", htmlTpl, true) //noinspection HtmlDeprecatedAttribute,HtmlUnknownTarget -const feedTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feeditem*/ -}} +const htmlTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feeditem*/ -}} {{define "bottomLine"}} {{if .content}} 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)) + } } diff --git a/internal/feed/template/text.tpl.go b/internal/feed/template/text.tpl.go new file mode 100644 index 0000000..0c10334 --- /dev/null +++ b/internal/feed/template/text.tpl.go @@ -0,0 +1,42 @@ +package template + +var Text = fromString("Feed", textTpl, false) + +//noinspection HtmlDeprecatedAttribute,HtmlUnknownTarget +const textTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feeditem*/ -}} +{{- with .Item.Link -}} +<{{.}}> + +{{ end -}} +{{- with .TextBody -}} +{{.}} +{{ end -}} +{{- with .Item.Enclosures -}} +Files: + {{- range . -}} + {{- .URL}} ({{with .Length}}{{. | byteCount}}, {{end}}{{.Type}}) + {{- end -}} +{{- end}} +-- +Feed: {{ with .Feed.Title -}}{{.}}{{- end }} +{{ with .Feed.Link -}} + <{{.}}> +{{end -}} +Item: {{ with .Item.Title -}} + {{.}} +{{- end }} +{{ with .Item.Link -}} + <{{.}}> +{{end -}} +{{ with .Date -}} + Date: {{.}} +{{ end -}} +{{ with .Creator -}} + Author: {{.}} +{{ end -}} +{{ with (join ", " .Categories) -}} + Filed under: {{.}} +{{ end -}} +{{ with .FeedLink -}} + Feed-Link: {{.}} +{{ end -}}` -- cgit v1.2.3-70-g09d2