From 5efeb73d59404d3019383b2bc5dcd84f76bddc8d 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 5366a4ef..89e8aa20 100644 --- a/rss/feed.go +++ b/rss/feed.go @@ -37,6 +37,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) @@ -61,6 +65,10 @@ type Item struct { Extensions ext.Extensions `json:"extensions,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 3d3569eb..6e9b8fb1 100644 --- a/translator.go +++ b/translator.go @@ -24,7 +24,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. @@ -323,6 +325,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) @@ -413,8 +417,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 { @@ -426,6 +430,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