-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.go
152 lines (128 loc) · 2.65 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package feeder
import (
"io"
"strings"
"github.com/p1ass/feeds"
)
// ToRSS generates RSS feed
func (f *Feed) ToRSS() (string, error) {
return f.convert().ToRss()
}
// ToRSSReader generates RSS feed and return io.Reader.
func (f *Feed) ToRSSReader() (io.Reader, error) {
rss, err := f.convert().ToRss()
if err != nil {
return nil, err
}
return strings.NewReader(rss), nil
}
// ToAtom generates Atom feed
func (f *Feed) ToAtom() (string, error) {
return f.convert().ToAtom()
}
// ToAtomReader generates Atom feed and return io.Reader.
func (f *Feed) ToAtomReader() (io.Reader, error) {
atom, err := f.convert().ToAtom()
if err != nil {
return nil, err
}
return strings.NewReader(atom), nil
}
// ToJSON generates JSON feed
func (f *Feed) ToJSON() (string, error) {
return f.convert().ToJSON()
}
// ToJSONReader generates JSON feed and return io.Reader.
func (f *Feed) ToJSONReader() (io.Reader, error) {
json, err := f.convert().ToJSON()
if err != nil {
return nil, err
}
return strings.NewReader(json), nil
}
func (l *Link) convert() *feeds.Link {
return &feeds.Link{
l.Href,
l.Rel,
l.Type,
l.Length,
}
}
func (a *Author) convert() *feeds.Author {
return &feeds.Author{
a.Name,
a.Email,
}
}
func (i *Image) convert() *feeds.Image {
return &feeds.Image{
i.URL,
i.Title,
i.Link,
i.Width,
i.Height,
}
}
func (e *Enclosure) convert() *feeds.Enclosure {
return &feeds.Enclosure{
e.URL,
e.Length,
e.Type,
}
}
func (i *Item) convert() *feeds.Item {
feedsItem := &feeds.Item{
Title: i.Title,
Description: i.Description,
Id: i.ID,
Content: i.Content,
}
if i.Link != nil {
feedsItem.Link = i.Link.convert()
}
if i.Updated != nil {
feedsItem.Updated = *i.Updated
}
if i.Created != nil {
feedsItem.Created = *i.Created
}
if i.Source != nil {
feedsItem.Source = i.Source.convert()
}
if i.Author != nil {
feedsItem.Author = i.Author.convert()
}
if i.Enclosure != nil {
feedsItem.Enclosure = i.Enclosure.convert()
}
return feedsItem
}
func convert(items []*Item) []*feeds.Item {
convertedItems := []*feeds.Item{}
for _, i := range items {
convertedItems = append(convertedItems, i.convert())
}
return convertedItems
}
func (f *Feed) convert() *feeds.Feed {
feed := &feeds.Feed{
Title: f.Title,
Description: f.Description,
Updated: f.Updated,
Created: f.Created,
Id: f.Id,
Subtitle: f.Subtitle,
Items: convert(f.Items),
Copyright: f.Copyright,
}
if f.Link != nil {
feed.Link = f.Link.convert()
}
if f.Author != nil {
feed.Author = f.Author.convert()
}
if f.Image != nil {
feed.Image = f.Image.convert()
}
return feed
}