From 87935efe945c6847d2b18afaf7bfae5e80aa61e6 Mon Sep 17 00:00:00 2001 From: RenĂ© 'Necoro' Neumann Date: Tue, 21 Apr 2020 01:26:48 +0200 Subject: HTML Template part of the mail --- .editorconfig | 6 +++- internal/feed/feed.tpl | 67 ++++++++++++++++++++++++++++++++++++++++ internal/feed/mail.go | 72 ++++++++++++++++++++++++++++++++++--------- internal/feed/parse.go | 4 +-- internal/template/template.go | 57 ++++++++++++++++++++++++++++++++++ internal/yaml/yaml_test.go | 2 +- main.go | 9 ++++++ 7 files changed, 199 insertions(+), 18 deletions(-) create mode 100644 internal/feed/feed.tpl create mode 100644 internal/template/template.go diff --git a/.editorconfig b/.editorconfig index d817a59..03f964f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,4 +5,8 @@ end_of_line = lf [*.go] indent_style = tab -indent_size = 4 \ No newline at end of file +indent_size = 4 + +[*.tpl] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/internal/feed/feed.tpl b/internal/feed/feed.tpl new file mode 100644 index 0000000..650176e --- /dev/null +++ b/internal/feed/feed.tpl @@ -0,0 +1,67 @@ +{{- /*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.Content}} +
+ {{.}} +{{end}} +{{with .Item.Enclosures}} + + + + +
+ + + {{range .}} + + + + {{end}} +
Files:
+     + {{.URL | lastUrlPart}} ({{.Length | byteCount}}, {{.Type}}) +
+
+{{end}} +
+ + {{template "bottomLine" (dict "descr" "Date:" "content" .Item.Published)}} + {{template "bottomLine" (dict "descr" "Author:" "content" .Item.Author.Name)}} + {{template "bottomLine" (dict "descr" "Subject:" "content" .Item.Title)}} + {{template "bottomLine" (dict "descr" "Filed under:" "content" (join ", " .Item.Categories))}} +
\ No newline at end of file diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 05d095b..4d07c1a 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -2,19 +2,21 @@ package feed import ( "bytes" + "fmt" "io" "time" "github.com/emersion/go-message/mail" "github.com/Necoro/feed2imap-go/internal/config" + "github.com/Necoro/feed2imap-go/internal/template" ) func address(name, address string) []*mail.Address { return []*mail.Address{{Name: name, Address: address}} } -func fromAdress(feed Feed, item feeditem, cfg config.Config) []*mail.Address { +func fromAdress(feed *Feed, item feeditem, cfg config.Config) []*mail.Address { switch { case item.Item.Author != nil && item.Item.Author.Email != "": return address(item.Item.Author.Name, item.Item.Author.Email) @@ -29,9 +31,13 @@ func fromAdress(feed Feed, item feeditem, cfg config.Config) []*mail.Address { } } -func asMail(feed Feed, item feeditem, cfg config.Config) (string, error) { - var b bytes.Buffer +var htmlTemplate = template.ForFile("internal/feed/feed.tpl") + +func writeHtml(writer io.Writer, item feeditem) error { + return htmlTemplate.Execute(writer, item) +} +func writeToBuffer(b *bytes.Buffer, feed *Feed, item feeditem, cfg config.Config) error { var h mail.Header h.SetAddressList("From", fromAdress(feed, item, cfg)) h.SetAddressList("To", address(feed.Name, cfg.DefaultEmail)) @@ -55,31 +61,69 @@ func asMail(feed Feed, item feeditem, cfg config.Config) (string, error) { h.SetSubject(subject) } - mw, err := mail.CreateWriter(&b, h) + mw, err := mail.CreateWriter(b, h) if err != nil { - return "", err + return err } + defer mw.Close() - if cfg.WithPartText() { - tw, err := mw.CreateInline() - if err != nil { - return "", err - } + tw, err := mw.CreateInline() + if err != nil { + return err + } + defer tw.Close() + if false /* cfg.WithPartText() */ { var th mail.InlineHeader th.SetContentType("text/plain", map[string]string{"charset": "utf-8", "format": "flowed"}) w, err := tw.CreatePart(th) if err != nil { - return "", err + return err } + defer w.Close() + _, _ = io.WriteString(w, "Who are you?") + } + + if cfg.WithPartHtml() { + var th mail.InlineHeader + th.SetContentType("text/html", map[string]string{"charset": "utf-8"}) + + w, err := tw.CreatePart(th) + if err != nil { + return err + } + + if err = writeHtml(w, item); err != nil { + return fmt.Errorf("writing html part: %w", err) + } - _ = w.Close() - _ = tw.Close() + w.Close() } - _ = mw.Close() + return nil +} + +func asMail(feed *Feed, item feeditem, cfg config.Config) (string, error) { + var b bytes.Buffer + + if err := writeToBuffer(&b, feed, item, cfg); err != nil { + return "", err + } return b.String(), nil } + +func (feed *Feed) ToMails(cfg config.Config) ([]string, error) { + var ( + err error + mails = make([]string, len(feed.items)) + ) + for idx := range feed.items { + if mails[idx], err = asMail(feed, feed.items[idx], cfg); err != nil { + return nil, fmt.Errorf("creating mails for %s: %w", feed.Name, err) + } + } + return mails, nil +} diff --git a/internal/feed/parse.go b/internal/feed/parse.go index 00b6aff..22d16f2 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -26,8 +26,8 @@ func parseFeed(feed *Feed) error { feed.feed = parsedFeed feed.items = make([]feeditem, len(parsedFeed.Items)) - for _, item := range parsedFeed.Items { - feed.items = append(feed.items, feeditem{parsedFeed, item}) + for idx, item := range parsedFeed.Items { + feed.items[idx] = feeditem{parsedFeed, item} } return nil } diff --git a/internal/template/template.go b/internal/template/template.go new file mode 100644 index 0000000..e31ece2 --- /dev/null +++ b/internal/template/template.go @@ -0,0 +1,57 @@ +package template + +import ( + "fmt" + "html/template" + "path/filepath" + "strings" +) + +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(b int64) string { + const unit = 1024 + if b < unit { + return fmt.Sprintf("%d B", b) + } + div, exp := int64(unit), 0 + for n := b / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp]) +} + +var funcMap = template.FuncMap{ + "dict": dict, + "join": join, + "lastUrlPart": LastUrlPart, + "byteCount": byteCount, +} + +func ForFile(filename string) *template.Template { + name := filepath.Base(filename) + tpl := template.New(name).Funcs(funcMap) + return template.Must(tpl.ParseFiles(filename)) +} diff --git a/internal/yaml/yaml_test.go b/internal/yaml/yaml_test.go index a71b95b..9562ef8 100644 --- a/internal/yaml/yaml_test.go +++ b/internal/yaml/yaml_test.go @@ -116,7 +116,7 @@ func TestBuildFeeds(tst *testing.T) { } for _, tt := range tests { tst.Run(tt.name, func(tst *testing.T) { - var feeds F.Feeds = F.Feeds{} + var feeds = F.Feeds{} err := buildFeeds(tt.feeds, t(tt.target), feeds) if (err != nil) != tt.wantErr { tst.Errorf("buildFeeds() error = %v, wantErr %v", err, tt.wantErr) diff --git a/main.go b/main.go index 7e6d560..af75fb7 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,15 @@ func run() error { feed.Parse(feeds) + for _, f := range feeds { + mails, err := f.ToMails(cfg) + if err != nil { + return err + } + _ = mails + break + } + imapUrl, err := url.Parse(cfg.Target) if err != nil { return fmt.Errorf("parsing 'target': %w", err) -- cgit v1.2.3-70-g09d2