From e0a857eafebd92d5e94d156997d5d9422a50933f Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Wed, 6 May 2020 22:56:38 +0200 Subject: Improve template --- internal/feed/item.go | 16 ++++++++++++---- internal/feed/mail.go | 26 +++++++++++++------------- internal/feed/template/feed.tpl.go | 6 +++--- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/internal/feed/item.go b/internal/feed/item.go index b8fb7f7..bbf7668 100644 --- a/internal/feed/item.go +++ b/internal/feed/item.go @@ -17,8 +17,8 @@ type feedImage struct { } type item struct { - *gofeed.Feed *gofeed.Item + Feed *gofeed.Feed feed *Feed Body string updateOnly bool @@ -28,14 +28,22 @@ type item struct { } // Creator returns the name of the creating author. -// MUST NOT have `*item` has the receiver, because the template breaks then. func (item *item) Creator() string { - if item.Item.Author != nil { - return item.Item.Author.Name + if item.Author != nil { + return item.Author.Name } return "" } +func (item *item) FeedLink() string { + if item.Feed.Link != "" { + // the one in the feed itself + return item.Feed.FeedLink + } + // the one in the config + return item.feed.Url +} + func (item *item) addReason(reason string) { if !util.StrContains(item.reasons, reason) { item.reasons = append(item.reasons, reason) diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 0360e10..190e1f2 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -31,10 +31,10 @@ func address(name, address string) []*mail.Address { func (item *item) fromAddress() []*mail.Address { switch { - case item.Item.Author != nil && item.Item.Author.Email != "": - return address(item.Item.Author.Name, item.Item.Author.Email) - case item.Item.Author != nil && item.Item.Author.Name != "": - return address(item.Item.Author.Name, item.defaultEmail()) + case item.Author != nil && item.Author.Email != "": + return address(item.Author.Name, item.Author.Email) + case item.Author != nil && item.Author.Name != "": + return address(item.Author.Name, item.defaultEmail()) case item.Feed.Author != nil && item.Feed.Author.Email != "": return address(item.Feed.Author.Name, item.Feed.Author.Email) case item.Feed.Author != nil && item.Feed.Author.Name != "": @@ -63,7 +63,7 @@ func (item *item) buildHeader() message.Header { h.Set("Message-Id", item.messageId()) { // date - date := item.Item.PublishedParsed + date := item.PublishedParsed if date == nil { now := time.Now() date = &now @@ -71,12 +71,12 @@ func (item *item) buildHeader() message.Header { h.SetDate(*date) } { // subject - subject := item.Item.Title + subject := item.Title if subject == "" { - subject = item.Item.Published + subject = item.Published } if subject == "" { - subject = item.Item.Link + subject = item.Link } h.SetSubject(subject) } @@ -244,7 +244,7 @@ func (item *item) buildBody() { panic(fmt.Sprintf("URL '%s' of feed '%s' is not a valid URL. How have we ended up here?", feed.Url, feed.Name)) } - body := getBody(item.Item.Content, item.Item.Description, feed.Body) + body := getBody(item.Content, item.Description, feed.Body) if !feed.InclImages { item.Body = body @@ -253,7 +253,7 @@ func (item *item) buildBody() { doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)) if err != nil { - log.Errorf("Feed %s: Item %s: Error while parsing html content: %s", feed.Name, item.Item.Link, err) + log.Errorf("Feed %s: Item %s: Error while parsing html content: %s", feed.Name, item.Link, err) if body != "" { item.Body = "
" + body } @@ -273,7 +273,7 @@ func (item *item) buildBody() { srcUrl, err := url.Parse(src) if err != nil { log.Errorf("Feed %s: Item %s: Error parsing URL '%s' embedded in item: %s", - feed.Name, item.Item.Link, src, err) + feed.Name, item.Link, src, err) return } imgUrl := feedUrl.ResolveReference(srcUrl) @@ -281,7 +281,7 @@ func (item *item) buildBody() { img, mime, err := getImage(imgUrl.String(), feed.Global.Timeout, feed.NoTLS) if err != nil { log.Errorf("Feed %s: Item %s: Error fetching image: %s", - feed.Name, item.Item.Link, err) + feed.Name, item.Link, err) return } if img == nil { @@ -304,7 +304,7 @@ func (item *item) buildBody() { if err != nil { item.clearImages() log.Errorf("Feed %s: Item %s: Error during rendering HTML: %s", - feed.Name, item.Item.Link, err) + feed.Name, item.Link, err) } else { body = html } diff --git a/internal/feed/template/feed.tpl.go b/internal/feed/template/feed.tpl.go index 0e09180..2f51bbe 100644 --- a/internal/feed/template/feed.tpl.go +++ b/internal/feed/template/feed.tpl.go @@ -64,10 +64,10 @@ const feedTpl = `{{- /*gotype:github.com/Necoro/feed2imap-go/internal/feed.feedi {{end}}
- {{template "bottomLine" (dict "descr" "Date:" "content" .Item.Published)}} + {{template "bottomLine" (dict "descr" "Date:" "content" .Published)}} {{template "bottomLine" (dict "descr" "Author:" "content" .Creator)}} - {{template "bottomLine" (dict "descr" "Filed under:" "content" (join ", " .Item.Categories))}} - {{with .Feed.FeedLink}} + {{template "bottomLine" (dict "descr" "Filed under:" "content" (join ", " .Categories))}} + {{with .FeedLink}} {{template "bottomLine" (dict "descr" "Feed-Link:" "content" (print "" . "" | html))}} {{end}}
` -- cgit v1.2.3-54-g00ecf