aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--.editorconfig6
-rw-r--r--internal/feed/feed.tpl67
-rw-r--r--internal/feed/mail.go72
-rw-r--r--internal/feed/parse.go4
-rw-r--r--internal/template/template.go57
-rw-r--r--internal/yaml/yaml_test.go2
-rw-r--r--main.go9
7 files changed, 199 insertions, 18 deletions
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}}
+ <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
}
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)