aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/template
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--internal/feed/template/html.tpl.go4
-rw-r--r--internal/feed/template/template.go32
-rw-r--r--internal/feed/template/text.tpl.go42
3 files changed, 68 insertions, 10 deletions
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}}
<tr>
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 -}}`