From dde77cd312322620be2be995386cbb04bb008caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sat, 22 Aug 2020 20:40:36 +0200 Subject: [PATCH] #151: Translation support for parsed extensions --- extensions/extensions.go | 4 ++++ rss/feed.go | 8 ++++++++ translator.go | 20 +++++++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/extensions/extensions.go b/extensions/extensions.go index 53021db8..eef62999 100644 --- a/extensions/extensions.go +++ b/extensions/extensions.go @@ -15,6 +15,10 @@ type Extension struct { Parsed interface{} `json:"parsed,omitempty"` } +type Extendable interface { + GetExtensions() Extensions +} + func parseTextExtension(name string, extensions map[string][]Extension) (value string) { if extensions == nil { return diff --git a/rss/feed.go b/rss/feed.go index 394f3d13..cdc07906 100644 --- a/rss/feed.go +++ b/rss/feed.go @@ -38,6 +38,10 @@ type Feed struct { Version string `json:"version"` } +func (f Feed) GetExtensions() ext.Extensions { + return f.Extensions +} + func (f Feed) String() string { json, _ := json.MarshalIndent(f, "", " ") return string(json) @@ -65,6 +69,10 @@ type Item struct { Custom map[string]string `json:"custom,omitempty"` } +func (i Item) GetExtensions() ext.Extensions { + return i.Extensions +} + // Image is an image that represents the feed type Image struct { URL string `json:"url,omitempty"` diff --git a/translator.go b/translator.go index bdb406fd..04c39e8c 100644 --- a/translator.go +++ b/translator.go @@ -27,7 +27,9 @@ type Translator interface { // This default implementation defines a set of // mapping rules between rss.Feed -> Feed // for each of the fields in Feed. -type DefaultRSSTranslator struct{} +type DefaultRSSTranslator struct{ + atomTranslator DefaultAtomTranslator +} // Translate converts an RSS feed into the universal // feed type. @@ -375,6 +377,8 @@ func (t *DefaultRSSTranslator) translateItemAuthor(rssItem *rss.Item) (author *P author = &Person{} author.Name = name author.Email = address + } else if authorVal, ok := t.hasAtomExtensionsForKey(rssItem, "author"); ok { + author = t.atomTranslator.translateItemAuthor(authorVal) } else if rssItem.DublinCoreExt != nil && rssItem.DublinCoreExt.Author != nil { dcAuthor := t.firstEntry(rssItem.DublinCoreExt.Author) name, address := shared.ParseNameAddress(dcAuthor) @@ -511,8 +515,8 @@ func (t *DefaultRSSTranslator) extensionsForKeys(keys []string, extensions ext.E return } -func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string, f func(ext.Extension) bool) { - atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.Extensions) +func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss ext.Extendable, tag string, f func(ext.Extension) bool) { + atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.GetExtensions()) for _, ex := range atomExtensions { if exts, ok := ex[tag]; ok { for _, e := range exts { @@ -524,6 +528,16 @@ func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string, } } +func (t *DefaultRSSTranslator) hasAtomExtensionsForKey(rss ext.Extendable, tag string) (entry *atom.Entry, ok bool) { + t.atomExtensionsWithKey(rss, tag, func(extension ext.Extension) bool { + if extension.Parsed != nil { + entry, ok = extension.Parsed.(*atom.Entry) + } + return ok + }) + return +} + func (t *DefaultRSSTranslator) firstEntry(entries []string) (value string) { if entries == nil { return