forked from hnrss/hnrss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.go
66 lines (57 loc) · 1.64 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
package main
// https://validator.w3.org/feed/docs/atom.html
type Atom struct {
XMLName string `xml:"feed"`
NS string `xml:"xmlns,attr"`
ID string `xml:"id"`
Title string `xml:"title"`
Updated string `xml:"updated"`
Links []AtomLink `xml:"link"`
Entries []AtomEntry `xml:"entry"`
}
type AtomEntry struct {
Title CDATA `xml:"title"`
Links []AtomLink `xml:"link"`
Author string `xml:"author>name"`
Content *AtomContent `xml:"content,omitempty"`
Updated string `xml:"updated"`
Published string `xml:"published"`
ID string `xml:"id"`
}
type AtomContent struct {
Type string `xml:"type,attr"`
Value string `xml:",cdata"`
}
type AtomLink struct {
Reference string `xml:"href,attr"`
Relationship string `xml:"rel,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
}
func NewAtom(results *AlgoliaSearchResponse, op *OutputParams) *Atom {
atom := Atom{
NS: NSAtom,
ID: op.SelfLink,
Title: op.Title,
Updated: Timestamp("atom", UTCNow()),
Links: []AtomLink{
{op.SelfLink, "self", "application/atom+xml"},
},
}
for _, hit := range results.Hits {
entry := AtomEntry{
ID: hit.GetPermalink(),
Title: CDATA{hit.GetTitle()},
Updated: Timestamp("atom", hit.GetCreatedAt()),
Published: Timestamp("atom", hit.GetCreatedAt()),
Links: []AtomLink{
{hit.GetURL(op.LinkTo), "alternate", ""},
},
Author: hit.Author,
}
if op.Description != descriptionDisabledFlag {
entry.Content = &AtomContent{"html", hit.GetDescription()}
}
atom.Entries = append(atom.Entries, entry)
}
return &atom
}