Skip to content

Commit fcd4f80

Browse files
committed
#151: Translation support for parsed extensions
1 parent 7057c57 commit fcd4f80

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)
@@ -61,6 +65,10 @@ type Item struct {
6165
Extensions ext.Extensions `json:"extensions,omitempty"`
6266
}
6367

68+
func (i Item) GetExtensions() ext.Extensions {
69+
return i.Extensions
70+
}
71+
6472
// Image is an image that represents the feed
6573
type Image struct {
6674
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.
@@ -332,6 +334,8 @@ func (t *DefaultRSSTranslator) translateItemAuthor(rssItem *rss.Item) (author *P
332334
author = &Person{}
333335
author.Name = name
334336
author.Email = address
337+
} else if authorVal, ok := t.hasAtomExtensionsForKey(rssItem, "author"); ok {
338+
author = t.atomTranslator.translateItemAuthor(authorVal)
335339
} else if rssItem.DublinCoreExt != nil && rssItem.DublinCoreExt.Author != nil {
336340
dcAuthor := t.firstEntry(rssItem.DublinCoreExt.Author)
337341
name, address := shared.ParseNameAddress(dcAuthor)
@@ -430,8 +434,8 @@ func (t *DefaultRSSTranslator) extensionsForKeys(keys []string, extensions ext.E
430434
return
431435
}
432436

433-
func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string, f func(ext.Extension) bool) {
434-
atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.Extensions)
437+
func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss ext.Extendable, tag string, f func(ext.Extension) bool) {
438+
atomExtensions := t.extensionsForKeys([]string{"atom", "atom10", "atom03"}, rss.GetExtensions())
435439
for _, ex := range atomExtensions {
436440
if exts, ok := ex[tag]; ok {
437441
for _, e := range exts {
@@ -443,6 +447,16 @@ func (t *DefaultRSSTranslator) atomExtensionsWithKey(rss *rss.Feed, tag string,
443447
}
444448
}
445449

450+
func (t *DefaultRSSTranslator) hasAtomExtensionsForKey(rss ext.Extendable, tag string) (entry *atom.Entry, ok bool) {
451+
t.atomExtensionsWithKey(rss, tag, func(extension ext.Extension) bool {
452+
if extension.Parsed != nil {
453+
entry, ok = extension.Parsed.(*atom.Entry)
454+
}
455+
return ok
456+
})
457+
return
458+
}
459+
446460
func (t *DefaultRSSTranslator) firstEntry(entries []string) (value string) {
447461
if entries == nil {
448462
return

0 commit comments

Comments
 (0)