-
Notifications
You must be signed in to change notification settings - Fork 2
/
atom.go
79 lines (67 loc) · 1.88 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
// SPDX-License-Identifier: MIT
package builder
import (
"time"
"github.com/caixw/blogit/internal/data"
)
const (
atomVersion = "1.0"
atomDateFormat = time.RFC3339
atomNamespace = "http://www.w3.org/2005/Atom"
)
type atom struct {
XMLName struct{} `xml:"feed"`
XMLNS string `xml:"xmlns,attr"`
Title atomContent `xml:"title"`
Subtitle atomContent `xml:"subtitle"`
ID string `xml:"id"`
Updated string `xml:"updated"`
Links []*atomLink `xml:"link,omitempty"`
Entries []*atomEntry `xml:"entry,omitempty"`
}
type atomEntry struct {
Title atomContent `xml:"title"`
ID string `xml:"id"`
Updated string `xml:"updated"`
Links []*atomLink `xml:"link,omitempty"`
Summary *atomContent `xml:"summary,omitempty"`
}
type atomLink struct {
Href string `xml:"href,attr"`
Type string `xml:"type,attr,omitempty"`
Rel string `xml:"rel,attr,omitempty"`
Title string `xml:"title,attr,omitempty"`
}
type atomContent struct {
Type string `xml:"type,attr,omitempty"`
Content string `xml:",chardata"`
}
func (b *Builder) buildAtom(d *data.Data) error {
if d.Atom == nil {
return nil
}
a := &atom{
XMLNS: atomNamespace,
Title: atomContent{Content: d.Atom.Title},
Subtitle: atomContent{Content: d.Subtitle},
ID: d.URL,
Updated: d.Modified.Format(atomDateFormat),
Links: []*atomLink{
{Href: d.URL},
{Href: d.Atom.Permalink, Rel: "self"},
},
Entries: make([]*atomEntry, 0, len(d.Atom.Posts)),
}
for _, p := range d.Atom.Posts {
a.Entries = append(a.Entries, &atomEntry{
Title: atomContent{Content: p.Title},
ID: p.Permalink,
Updated: p.Modified.Format(atomDateFormat),
Links: []*atomLink{
{Href: p.Permalink, Type: "application/xml"},
},
Summary: &atomContent{Type: "text/html", Content: p.Summary},
})
}
return b.appendXMLFile(d, d.Atom.Path, d.Atom.XSLPermalink, a)
}