From f8a89ce8d9a3d5882a484fcaec810dc90f4a5e5d Mon Sep 17 00:00:00 2001 From: RenĂ© 'Necoro' Neumann Date: Thu, 23 Apr 2020 20:51:11 +0200 Subject: Move template to subfolder of feed --- internal/feed/mail.go | 2 +- internal/feed/template/feed.tpl.go | 76 ++++++++++++++++++++++++++++++++++++++ internal/feed/template/template.go | 68 ++++++++++++++++++++++++++++++++++ internal/template/feed.tpl.go | 76 -------------------------------------- internal/template/template.go | 68 ---------------------------------- 5 files changed, 145 insertions(+), 145 deletions(-) create mode 100644 internal/feed/template/feed.tpl.go create mode 100644 internal/feed/template/template.go delete mode 100644 internal/template/feed.tpl.go delete mode 100644 internal/template/template.go diff --git a/internal/feed/mail.go b/internal/feed/mail.go index e8b6915..ecc011f 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -9,7 +9,7 @@ import ( "github.com/emersion/go-message/mail" "github.com/Necoro/feed2imap-go/internal/config" - "github.com/Necoro/feed2imap-go/internal/template" + "github.com/Necoro/feed2imap-go/internal/feed/template" ) func address(name, address string) []*mail.Address { diff --git a/internal/feed/template/feed.tpl.go b/internal/feed/template/feed.tpl.go new file mode 100644 index 0000000..3f6470f --- /dev/null +++ b/internal/feed/template/feed.tpl.go @@ -0,0 +1,76 @@ +package template + +var Feed = fromString("Feed", feedTpl) + +//noinspection HtmlDeprecatedAttribute,HtmlUnknownTarget +const feedTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feeditem*/ -}} +{{define "bottomLine"}} + {{if .content}} + + + {{.descr}}   + + + {{.content}} + + + {{end}} +{{end}} + + + + +
+ + + + + + + + + +
Feed + {{with .Feed.Link}}{{end}} + {{or .Feed.Title .Feed.Link "Unnammed feed"}} + {{if .Feed.Link}}{{end}} +
Item + {{with .Item.Link}}{{end}} + {{or .Item.Title .Item.Link}} + {{if .Item.Link}}{{end}} +
+
+{{with .Item.Description}} +
+ {{html .}} +{{end}} +{{with .Item.Content}} +
+ {{html .}} +{{end}} +{{with .Item.Enclosures}} + + + + +
+ + + {{range .}} + + + + {{end}} +
Files:
+     + {{.URL | lastUrlPart}} ({{.Length | byteCount}}, {{.Type}}) +
+
+{{end}} +
+ + {{template "bottomLine" (dict "descr" "Date:" "content" .Item.Published)}} + {{with .Item.Author}}{{template "bottomLine" (dict "descr" "Author:" "content" .Name)}}{{end}} + {{template "bottomLine" (dict "descr" "Subject:" "content" .Item.Title)}} + {{template "bottomLine" (dict "descr" "Filed under:" "content" (join ", " .Item.Categories))}} +
` diff --git a/internal/feed/template/template.go b/internal/feed/template/template.go new file mode 100644 index 0000000..dd31f51 --- /dev/null +++ b/internal/feed/template/template.go @@ -0,0 +1,68 @@ +package template + +import ( + "fmt" + "html/template" + "strconv" + "strings" + + "github.com/Necoro/feed2imap-go/internal/log" +) + +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(str string) string { + b, err := strconv.ParseUint(str, 10, 64) + if 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++ + } + return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp]) +} + +func html(s string) template.HTML { + return template.HTML(s) +} + +var funcMap = template.FuncMap{ + "dict": dict, + "join": join, + "lastUrlPart": lastUrlPart, + "byteCount": byteCount, + "html": html, +} + +func fromString(name, templateStr string) *template.Template { + tpl := template.New(name).Funcs(funcMap) + return template.Must(tpl.Parse(templateStr)) +} diff --git a/internal/template/feed.tpl.go b/internal/template/feed.tpl.go deleted file mode 100644 index 3f6470f..0000000 --- a/internal/template/feed.tpl.go +++ /dev/null @@ -1,76 +0,0 @@ -package template - -var Feed = fromString("Feed", feedTpl) - -//noinspection HtmlDeprecatedAttribute,HtmlUnknownTarget -const feedTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feeditem*/ -}} -{{define "bottomLine"}} - {{if .content}} - - - {{.descr}}   - - - {{.content}} - - - {{end}} -{{end}} - - - - -
- - - - - - - - - -
Feed - {{with .Feed.Link}}{{end}} - {{or .Feed.Title .Feed.Link "Unnammed feed"}} - {{if .Feed.Link}}{{end}} -
Item - {{with .Item.Link}}{{end}} - {{or .Item.Title .Item.Link}} - {{if .Item.Link}}{{end}} -
-
-{{with .Item.Description}} -
- {{html .}} -{{end}} -{{with .Item.Content}} -
- {{html .}} -{{end}} -{{with .Item.Enclosures}} - - - - -
- - - {{range .}} - - - - {{end}} -
Files:
-     - {{.URL | lastUrlPart}} ({{.Length | byteCount}}, {{.Type}}) -
-
-{{end}} -
- - {{template "bottomLine" (dict "descr" "Date:" "content" .Item.Published)}} - {{with .Item.Author}}{{template "bottomLine" (dict "descr" "Author:" "content" .Name)}}{{end}} - {{template "bottomLine" (dict "descr" "Subject:" "content" .Item.Title)}} - {{template "bottomLine" (dict "descr" "Filed under:" "content" (join ", " .Item.Categories))}} -
` diff --git a/internal/template/template.go b/internal/template/template.go deleted file mode 100644 index dd31f51..0000000 --- a/internal/template/template.go +++ /dev/null @@ -1,68 +0,0 @@ -package template - -import ( - "fmt" - "html/template" - "strconv" - "strings" - - "github.com/Necoro/feed2imap-go/internal/log" -) - -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(str string) string { - b, err := strconv.ParseUint(str, 10, 64) - if 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++ - } - return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp]) -} - -func html(s string) template.HTML { - return template.HTML(s) -} - -var funcMap = template.FuncMap{ - "dict": dict, - "join": join, - "lastUrlPart": lastUrlPart, - "byteCount": byteCount, - "html": html, -} - -func fromString(name, templateStr string) *template.Template { - tpl := template.New(name).Funcs(funcMap) - return template.Must(tpl.Parse(templateStr)) -} -- cgit v1.2.3-70-g09d2