From d75bbcf9a7fe589f119b103d357ffa874008ab07 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sat, 2 May 2020 16:48:51 +0200 Subject: Option 'body' --- internal/feed/mail.go | 35 ++++++++++++++++++++++------------- pkg/config/body.go | 35 +++++++++++++++++++++++++++++++++++ pkg/config/config.go | 2 ++ 3 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 pkg/config/body.go diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 297aeff..4b0e38b 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -209,20 +209,29 @@ func cidNr(idx int) string { return fmt.Sprintf("cid_%d", idx) } -func (feed *Feed) buildBody(item *feeditem) { - var body string - var comment string - - if item.Item.Content != "" { - comment = "\n" - body = item.Item.Content - } else if item.Item.Description != "" { - comment = "\n" - body = item.Item.Description +func getBody(content, description string, bodyCfg config.Body) string { + switch bodyCfg { + case "default": + if content != "" { + return content + } + return description + case "description": + return description + case "content": + return content + case "both": + return description + content + default: + panic(fmt.Sprintf("Unknown value for Body: %v", bodyCfg)) } +} + +func (feed *Feed) buildBody(item *feeditem) { + body := getBody(item.Item.Content, item.Item.Description, feed.Body) if !feed.InclImages { - item.Body = comment + body + item.Body = body return } @@ -230,7 +239,7 @@ func (feed *Feed) buildBody(item *feeditem) { if err != nil { log.Debugf("Feed %s: Error while parsing html content: %s", feed.Name, err) if body != "" { - item.Body = "
" + comment + body + item.Body = "
" + body } return } @@ -271,5 +280,5 @@ func (feed *Feed) buildBody(item *feeditem) { } } - item.Body = comment + body + item.Body = body } diff --git a/pkg/config/body.go b/pkg/config/body.go new file mode 100644 index 0000000..d9957d5 --- /dev/null +++ b/pkg/config/body.go @@ -0,0 +1,35 @@ +package config + +import ( + "fmt" + + "gopkg.in/yaml.v3" + + "github.com/Necoro/feed2imap-go/pkg/util" +) + +type Body string + +var validBody = []string{"default", "both", "content", "description"} + +func (b *Body) UnmarshalYAML(node *yaml.Node) error { + var val string + if err := node.Decode(&val); err != nil { + return err + } + + if val == "" { + val = "default" + } + + if !util.StrContains(validBody, val) { + return TypeError("line %d: Invalid value for 'body': %q", node.Line, val) + } + + *b = Body(val) + return nil +} + +func TypeError(format string, v ...interface{}) *yaml.TypeError { + return &yaml.TypeError{Errors: []string{fmt.Sprintf(format, v...)}} +} diff --git a/pkg/config/config.go b/pkg/config/config.go index d1cd4c9..dfd3d0e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -44,10 +44,12 @@ type Options struct { IgnHash bool `yaml:"ignore-hash"` AlwaysNew bool `yaml:"always-new"` NoTLS bool `yaml:"tls-no-verify"` + Body Body `yaml:"body"` } // Default feed options var DefaultFeedOptions = Options{ + Body: "default", MinFreq: 1, InclImages: true, EmbedImages: false, -- cgit v1.2.3-70-g09d2