Skip to content

Commit

Permalink
mmcdole#151: Translation support for parsed extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Necoro committed Feb 25, 2021
1 parent 7057c57 commit fcd4f80
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions extensions/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions rss/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"`
Expand Down
20 changes: 17 additions & 3 deletions translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -332,6 +334,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)
Expand Down Expand Up @@ -430,8 +434,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 {
Expand All @@ -443,6 +447,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
Expand Down

0 comments on commit fcd4f80

Please sign in to comment.