summaryrefslogtreecommitdiff
path: root/internal/feed
diff options
context:
space:
mode:
Diffstat (limited to 'internal/feed')
-rw-r--r--internal/feed/feed.tpl67
-rw-r--r--internal/feed/mail.go72
-rw-r--r--internal/feed/parse.go4
3 files changed, 127 insertions, 16 deletions
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}}
+ <tr>
+ <td align="right">
+ <span style="color: #ababab; ">{{.descr}}</span>&nbsp;&nbsp;
+ </td>
+ <td>
+ <span style="color: #ababab; ">{{.content}}</span>
+ </td>
+ </tr>
+ {{end}}
+{{end}}
+<table border="1" width="100%" cellpadding="0" cellspacing="0" borderspacing="0">
+ <tr>
+ <td>
+ <table width="100%" bgcolor="#EDEDED" cellpadding="4" cellspacing="2">
+ <tr>
+ <td align="right"><b>Feed</b></td>
+ <td width="100%">
+ {{with .Feed.Link}}<a href="{{.}}">{{end}}
+ <b>{{or .Feed.Title .Feed.Link "Unnammed feed"}}</b>
+ {{if .Feed.Link}}</a>{{end}}
+ </td>
+ </tr>
+ <tr>
+ <td align="right"><b>Item</b></td>
+ <td width="100%">
+ {{with .Item.Link}}<a href="{{.}}">{{end}}
+ <b>{{or .Item.Title .Item.Link}}</b>
+ {{if .Item.Link}}</a>{{end}}
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+</table>
+{{with .Item.Content}}
+ <br /> <!-- originally: only if content and `content !~ /\A\s*</m` -->
+ {{.}}
+{{end}}
+{{with .Item.Enclosures}}
+ <table border="1" width="100%" cellpadding="0" cellspacing="0" borderspacing="0">
+ <tr>
+ <td>
+ <table width="100%" bgcolor="#EDEDED" cellpadding="2" cellspacing="2">
+ <tr><td width="100%"><b>Files:</b></td></tr>
+ {{range .}}
+ <tr>
+ <td>
+ &nbsp;&nbsp;&nbsp;
+ <a href={{.URL}}>{{.URL | lastUrlPart}}</a> ({{.Length | byteCount}}, {{.Type}})
+ </td>
+ </tr>
+ {{end}}
+ </table>
+ </td>
+ </tr>
+ </table>
+{{end}}
+<hr width="100%"/>
+<table width="100%" cellpadding="0" cellspacing="0">
+ {{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))}}
+</table> \ 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
}