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 Mar 5, 2023
1 parent 8cab161 commit 69a2fc6
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 @@ -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)
Expand Down Expand Up @@ -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"`
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 @@ -359,6 +361,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 @@ -464,8 +468,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 @@ -477,6 +481,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 69a2fc6

Please sign in to comment.