aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/template/template.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2022-01-09 00:20:24 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2022-01-09 00:20:24 +0100
commitd480edb65af5d6e9d8906940cdd4093761c1b451 (patch)
tree186cf7e2aee6576d84fdb3df11e6a06bc80d6a52 /internal/feed/template/template.go
parent0d3a8c8bab2fad592e192169a83e286d0d2ab692 (diff)
downloadfeed2imap-go-d480edb65af5d6e9d8906940cdd4093761c1b451.tar.gz
feed2imap-go-d480edb65af5d6e9d8906940cdd4093761c1b451.tar.bz2
feed2imap-go-d480edb65af5d6e9d8906940cdd4093761c1b451.zip
template: Restructuring and adding tests.
Diffstat (limited to 'internal/feed/template/template.go')
-rw-r--r--internal/feed/template/template.go96
1 files changed, 23 insertions, 73 deletions
diff --git a/internal/feed/template/template.go b/internal/feed/template/template.go
index 9804190..8b3fb73 100644
--- a/internal/feed/template/template.go
+++ b/internal/feed/template/template.go
@@ -2,96 +2,46 @@ package template
import (
_ "embed"
- "fmt"
html "html/template"
"io"
- "strconv"
- "strings"
text "text/template"
-
- "github.com/Necoro/feed2imap-go/pkg/log"
)
-type Template interface {
+type template interface {
Execute(wr io.Writer, data interface{}) error
}
+type Template struct {
+ template
+ useHtml bool
+ dflt string
+}
+
//go:embed html.tpl
-var htmlTpl string
+var defaultHtmlTpl string
//go:embed text.tpl
-var textTpl string
-
-var Html = fromString("Feed", htmlTpl, true)
-var Text = fromString("Feed", textTpl, false)
-
-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
-}
+var defaultTextTpl string
-func join(sep string, parts []string) string {
- return strings.Join(parts, sep)
+var Html = Template{
+ useHtml: true,
+ dflt: defaultHtmlTpl,
}
-func lastUrlPart(url string) string {
- split := strings.Split(url, "/")
- return split[len(split)-1]
+var Text = Template{
+ useHtml: false,
+ dflt: defaultTextTpl,
}
-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++
+func (tpl *Template) loadDefault() {
+ if tpl.useHtml {
+ tpl.template = html.Must(html.New("Html").Funcs(funcMap).Parse(tpl.dflt))
+ } else {
+ tpl.template = text.Must(text.New("Text").Funcs(funcMap).Parse(tpl.dflt))
}
- 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))
- }
+func init() {
+ Html.loadDefault()
+ Text.loadDefault()
}