Skip to content

Commit 8e1edb8

Browse files
committed
#151: Translation support for parsed extensions
1 parent beb9f83 commit 8e1edb8

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
@@ -37,6 +37,10 @@ type Feed struct {
3737
Version string `json:"version"`
3838
}
3939

40+
func (f Feed) GetExtensions() ext.Extensions {
41+
return f.Extensions
42+
}
43+
4044
func (f Feed) String() string {
4145
json, _ := json.MarshalIndent(f, "", " ")
4246
return string(json)
@@ -62,6 +66,10 @@ type Item struct {
6266
Custom map[string]string `json:"custom,omitempty"`
6367
}
6468

69+
func (i Item) GetExtensions() ext.Extensions {
70+
return i.Extensions
71+
}
72+
6573
// Image is an image that represents the feed
6674
type Image struct {
6775
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.
@@ -361,6 +363,8 @@ func (t *DefaultRSSTranslator) translateItemAuthor(rssItem *rss.Item) (author *P
361363
author = &Person{}
362364
author.Name = name
363365
author.Email = address
366+
} else if authorVal, ok := t.hasAtomExtensionsForKey(rssItem, "author"); ok {
367+
author = t.atomTranslator.translateItemAuthor(authorVal)
364368
} else if rssItem.DublinCoreExt != nil && rssItem.DublinCoreExt.Author != nil {
365369
dcAuthor := t.firstEntry(rssItem.DublinCoreExt.Author)
366370
name, address := shared.ParseNameAddress(dcAuthor)
@@ -459,8 +463,8 @@ func (t *DefaultRSSTranslator) extensionsForKeys(keys []string, extensions ext.E
459463
return
460464
}
461465

462-
func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string, f func(ext.Extension) bool) {
463-
atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.Extensions)
466+
func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss ext.Extendable, tag string, f func(ext.Extension) bool) {
467+
atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.GetExtensions())
464468
for _, ex := range atomExtensions {
465469
if exts, ok := ex[tag]; ok {
466470
for _, e := range exts {
@@ -472,6 +476,16 @@ func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string,
472476
}
473477
}
474478

479+
func (t *DefaultRSSTranslator) hasAtomExtensionsForKey(rss ext.Extendable, tag string) (entry *atom.Entry, ok bool) {
480+
t.atomExtensionsWithKey(rss, tag, func(extension ext.Extension) bool {
481+
if extension.Parsed != nil {
482+
entry, ok = extension.Parsed.(*atom.Entry)
483+
}
484+
return ok
485+
})
486+
return
487+
}
488+
475489
func (t *DefaultRSSTranslator) firstEntry(entries []string) (value string) {
476490
if entries == nil {
477491
return

0 commit comments

Comments
 (0)