Skip to content

Commit 69a2fc6

Browse files
committed
#151: Translation support for parsed extensions
1 parent 8cab161 commit 69a2fc6

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

extensions/extensions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ type Extension struct {
1515
Parsed interface{} `json:"parsed,omitempty"`
1616
}
1717

18+
type Extendable interface {
19+
GetExtensions() Extensions
20+
}
21+
1822
func parseTextExtension(name string, extensions map[string][]Extension) (value string) {
1923
if extensions == nil {
2024
return

rss/feed.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type Feed struct {
3838
Version string `json:"version"`
3939
}
4040

41+
func (f Feed) GetExtensions() ext.Extensions {
42+
return f.Extensions
43+
}
44+
4145
func (f Feed) String() string {
4246
json, _ := json.MarshalIndent(f, "", " ")
4347
return string(json)
@@ -65,6 +69,10 @@ type Item struct {
6569
Custom map[string]string `json:"custom,omitempty"`
6670
}
6771

72+
func (i Item) GetExtensions() ext.Extensions {
73+
return i.Extensions
74+
}
75+
6876
// Image is an image that represents the feed
6977
type Image struct {
7078
URL string `json:"url,omitempty"`

translator.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ type Translator interface {
2424
// This default implementation defines a set of
2525
// mapping rules between rss.Feed -> Feed
2626
// for each of the fields in Feed.
27-
type DefaultRSSTranslator struct{}
27+
type DefaultRSSTranslator struct{
28+
atomTranslator DefaultAtomTranslator
29+
}
2830

2931
// Translate converts an RSS feed into the universal
3032
// feed type.
@@ -359,6 +361,8 @@ func (t *DefaultRSSTranslator) translateItemAuthor(rssItem *rss.Item) (author *P
359361
author = &Person{}
360362
author.Name = name
361363
author.Email = address
364+
} else if authorVal, ok := t.hasAtomExtensionsForKey(rssItem, "author"); ok {
365+
author = t.atomTranslator.translateItemAuthor(authorVal)
362366
} else if rssItem.DublinCoreExt != nil && rssItem.DublinCoreExt.Author != nil {
363367
dcAuthor := t.firstEntry(rssItem.DublinCoreExt.Author)
364368
name, address := shared.ParseNameAddress(dcAuthor)
@@ -464,8 +468,8 @@ func (t *DefaultRSSTranslator) extensionsForKeys(keys []string, extensions ext.E
464468
return
465469
}
466470

467-
func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string, f func(ext.Extension) bool) {
468-
atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.Extensions)
471+
func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss ext.Extendable, tag string, f func(ext.Extension) bool) {
472+
atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.GetExtensions())
469473
for _, ex := range atomExtensions {
470474
if exts, ok := ex[tag]; ok {
471475
for _, e := range exts {
@@ -477,6 +481,16 @@ func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string,
477481
}
478482
}
479483

484+
func (t *DefaultRSSTranslator) hasAtomExtensionsForKey(rss ext.Extendable, tag string) (entry *atom.Entry, ok bool) {
485+
t.atomExtensionsWithKey(rss, tag, func(extension ext.Extension) bool {
486+
if extension.Parsed != nil {
487+
entry, ok = extension.Parsed.(*atom.Entry)
488+
}
489+
return ok
490+
})
491+
return
492+
}
493+
480494
func (t *DefaultRSSTranslator) firstEntry(entries []string) (value string) {
481495
if entries == nil {
482496
return

0 commit comments

Comments
 (0)