-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.go
187 lines (174 loc) · 5.51 KB
/
feed.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package atomfeed // import "github.com/denisbrodbeck/atomfeed"
import (
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"strings"
"time"
)
// Encode writes the XML encoding of Feed to the stream.
func (f *Feed) Encode(w io.Writer) error {
enc := xml.NewEncoder(w)
enc.Indent("", " ")
w.Write([]byte(xml.Header))
return enc.Encode(f)
}
// NewFeed creates a basic atom:feed element suitable for e.g. a blog.
func NewFeed(id ID, author *Person, title, subtitle, baseURL, feedURL string, updated time.Time, entries []Entry) Feed {
generator := &Generator{
URI: "https://github.com/denisbrodbeck/atomfeed",
Version: "1.0",
Value: "atomfeed package",
}
return Feed{
Namespace: "http://www.w3.org/2005/Atom",
ID: id,
Title: &TextConstruct{Value: title},
Subtitle: &TextConstruct{Value: subtitle},
Author: author,
Links: []Link{
{
Rel: "alternate",
Type: "text/html",
Href: baseURL, // https://example.com/
},
{
Rel: "self",
Type: "application/atom+xml",
Href: feedURL, // https://example.com/feed.atom
},
},
Updated: NewDate(updated),
Entries: entries,
Generator: generator,
}
}
// NewID creates an atom:id element.
// The given id parameter is taken as a raw value for ID.
func NewID(id string) ID {
return ID{Value: id}
}
// NewFeedID creates a stable ID for an atom:feed element.
// The resulting ID follows the 'tag' URI scheme as defined in RFC 4151.
// More specifically the function creates valid atom IDs by feed creation time and a custom specifier.
//
// Further info:
// https://github.com/denisbrodbeck/atomfeed/blob/master/README.md#id
// http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
// https://tools.ietf.org/html/rfc4151
func NewFeedID(authorityName string, creationTime time.Time, specific string) ID {
tag := fmt.Sprintf("tag:%s,%s:%s", authorityName, creationTime.Format("2006-01-02"), specific)
return ID{Value: tag}
}
// NewEntryID creates a stable ID for an atom:entry element.
// The resulting ID follows the 'tag' URI scheme as defined in RFC 4151.
// More specifically the function creates valid atom IDs by article creation time.
//
// Further info:
// https://github.com/denisbrodbeck/atomfeed/blob/master/README.md#id
// http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
// https://tools.ietf.org/html/rfc4151
func NewEntryID(feedID ID, entryCreationTime time.Time) ID {
tag := fmt.Sprintf("%s.post-%s", feedID.Value, entryCreationTime.Format("20060102150405"))
return ID{Value: tag}
}
// NewContent creates the correct atom:content element depending on type attribute.
//
// https://tools.ietf.org/html/rfc4287#section-4.1.3.3
func NewContent(contentType, source string, value []byte) *Content {
if source == "" && (value == nil || len(value) == 0) {
return nil
}
switch {
case contentType == "xhtml",
contentType == "text/xml", // https://tools.ietf.org/html/rfc3023#section-3
contentType == "application/xml",
contentType == "text/xml-external-parsed-entity",
contentType == "application/xml-external-parsed-entity",
contentType == "application/xml-dtd",
strings.HasSuffix(strings.ToLower(contentType), "+xml"),
strings.HasSuffix(strings.ToLower(contentType), "/xml"):
return &Content{Type: contentType, Source: source, ValueXML: string(value)}
case contentType == "",
contentType == "text",
contentType == "html",
strings.HasPrefix(strings.ToLower(contentType), "text/"):
return &Content{Type: contentType, Source: source, Value: string(value)}
}
// all other types MUST be base64 encoded
return &Content{Type: contentType, Source: source, Value: base64.StdEncoding.EncodeToString(value), base64Encoded: true}
}
// NewDate returns an atom:date element with valid RFC3339 time data.
func NewDate(t time.Time) *Date {
if t.IsZero() {
return nil
}
return &Date{Value: t.Format(time.RFC3339)}
}
// NewPerson returns an atom:person element.
func NewPerson(name, email, uri string) *Person {
return &Person{Name: name, Email: email, URI: uri}
}
// NewCategory returns an atom:category element.
func NewCategory(category string) *Category {
return &Category{Term: category}
}
// NewEntry creates a basic atom:entry suitable for e.g. a blog.
func NewEntry(id ID, title, permalink string, author *Person, updated, published time.Time, categories []string, summary, content []byte) Entry {
return Entry{
ID: id,
Title: &TextConstruct{Value: title},
Links: []Link{
{
Rel: "alternate",
Type: "text/html",
Href: permalink,
},
},
Published: NewDate(published),
Updated: NewDate(updated),
Author: author,
Categories: termsToCategories(categories),
Summary: NewContent("html", "", summary),
Content: NewContent("html", "", content),
}
}
func termsToCategories(categories []string) []Category {
cat := []Category{}
for _, c := range categories {
cat = append(cat, *NewCategory(c))
}
return cat
}
func (e *Entry) String() string {
title := ""
if e.Title != nil {
title = e.Title.Value
}
updated := ""
if e.Updated != nil {
updated = e.Updated.Value
}
published := ""
if e.Published != nil {
published = e.Published.Value
}
author := ""
if e.Author != nil {
author = e.Author.Name
}
categories := []string{}
for _, c := range e.Categories {
categories = append(categories, c.Term)
}
return fmt.Sprintf(
"ID: %q, Title: %q, Updated: %q, Published: %q, Author: %q, Categories: %q",
e.ID.Value,
title,
updated,
published,
author,
strings.Join(categories, ","),
)
}