-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.go
160 lines (148 loc) · 4.25 KB
/
atom.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
package syndfeed
import (
"errors"
"html"
"io"
"github.com/antchfx/xmlquery"
)
// Atom is an Atom feed parser.
// https://validator.w3.org/feed/docs/rfc4287.html
type Atom struct{}
func (a *Atom) parseItemElement(elem *xmlquery.Node, ns map[string]string) *Item {
item := new(Item)
for elem := elem.FirstChild; elem != nil; elem = elem.NextSibling {
if ns, ok := ns[elem.Prefix]; ok {
item.ElementExtensions = append(item.ElementExtensions, &ElementExtension{elem.Data, elem.Prefix, elem.InnerText()})
if m := lookupModule(ns); m != nil {
m.ParseElement(elem, item)
}
}
switch elem.Data {
case "author":
item.Authors = append(item.Authors, a.parseAuthorElement(elem))
case "category":
if v := elem.SelectAttr("term"); v != "" {
item.Categories = append(item.Categories, v)
}
case "contributor":
item.Contributors = append(item.Contributors, a.parseAuthorElement(elem))
case "id":
item.Id = elem.InnerText()
case "title":
item.Title = elem.InnerText()
case "link":
item.Links = append(item.Links, a.parseLinkElement(elem))
case "published":
if t, err := parseDateString(elem.InnerText()); err == nil {
item.PublishDate = t
}
case "rights":
item.Copyright = elem.InnerText()
case "summary":
item.Summary = html.UnescapeString(elem.InnerText())
case "updated":
if t, err := parseDateString(elem.InnerText()); err == nil {
item.LastUpdatedTime = t
}
case "content":
item.Content = html.UnescapeString(elem.InnerText())
case "source":
}
}
return item
}
func (a *Atom) parseLinkElement(elem *xmlquery.Node) *Link {
return &Link{
URL: html.UnescapeString(elem.SelectAttr("href")),
Title: elem.SelectAttr("title"),
MediaType: elem.SelectAttr("type"),
RelType: elem.SelectAttr("rel"),
}
}
func (a *Atom) parseAuthorElement(elem *xmlquery.Node) *Person {
author := new(Person)
if n := elem.SelectElement("name"); n != nil {
author.Name = n.InnerText()
}
if n := elem.SelectElement("uri"); n != nil {
author.URL = n.InnerText()
}
if n := elem.SelectElement("email"); n != nil {
author.Email = n.InnerText()
}
return author
}
func (a *Atom) parse(doc *xmlquery.Node) (*Feed, error) {
root := doc.SelectElement("feed")
if root == nil {
return nil, errors.New("invalid Atom document without feed element")
}
feed := &Feed{Version: "1.0"} // default atom version is 1.0
feed.Namespace = make(map[string]string)
// xmlns:prefix = namespace
for _, attr := range root.Attr {
switch {
case attr.Name.Local == "version":
feed.Version = attr.Value
case attr.Name.Space == "xmlns":
feed.Namespace[attr.Name.Local] = attr.Value
}
}
for elem := root.FirstChild; elem != nil; elem = elem.NextSibling {
if ns, ok := feed.Namespace[elem.Prefix]; ok {
feed.ElementExtensions = append(feed.ElementExtensions, &ElementExtension{elem.Data, elem.Prefix, elem.InnerText()})
if m := lookupModule(ns); m != nil {
m.ParseElement(elem, feed)
}
continue
}
switch elem.Data {
case "author":
feed.Authors = append(feed.Authors, a.parseAuthorElement(elem))
case "category":
if v := elem.SelectAttr("term"); v != "" {
feed.Categories = append(feed.Categories, v)
}
case "contributor":
feed.Contributors = append(feed.Contributors, a.parseAuthorElement(elem))
case "generator":
feed.Generator = elem.InnerText()
case "id":
feed.Id = elem.InnerText()
case "link":
feed.Links = append(feed.Links, a.parseLinkElement(elem))
case "logo":
feed.ImageURL = elem.InnerText()
case "rights":
feed.Copyright = elem.InnerText()
case "subtitle":
feed.Description = elem.InnerText()
case "title":
feed.Title = elem.InnerText()
case "updated":
if t, err := parseDateString(elem.InnerText()); err == nil {
feed.LastUpdatedTime = t
}
case "entry":
item := a.parseItemElement(elem, feed.Namespace)
feed.Items = append(feed.Items, item)
case "icon":
case "published":
case "source":
}
}
return feed, nil
}
// Parse parses an atom feed.
func (a *Atom) Parse(r io.Reader) (*Feed, error) {
doc, err := xmlquery.Parse(r)
if err != nil {
return nil, err
}
return a.parse(doc)
}
var atom = &Atom{}
// ParseAtom parses Atom feed from the specified io.Reader r.
func ParseAtom(r io.Reader) (*Feed, error) {
return atom.Parse(r)
}