-
Notifications
You must be signed in to change notification settings - Fork 2
/
rss.go
66 lines (55 loc) · 1.48 KB
/
rss.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
// SPDX-License-Identifier: MIT
package builder
import (
"html"
"time"
"github.com/caixw/blogit/internal/data"
)
const (
rssVersion = "2.0"
rssDateFormat = time.RFC822
)
type rss struct {
XMLName struct{} `xml:"rss"`
Version string `xml:"version,attr"`
Channel *rssChannel `xml:"channel"`
}
type rssChannel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate,omitempty"`
LastBuildDate string `xml:"lastBuildDate,omitempty"`
Items []*rssItem `xml:"item,omitempty"`
}
type rssItem struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate,omitempty"`
}
func (b *Builder) buildRSS(d *data.Data) error {
if d.RSS == nil {
return nil
}
r := &rss{
Version: rssVersion,
Channel: &rssChannel{
Title: d.RSS.Title,
Link: d.URL,
Description: d.Subtitle,
PubDate: d.Uptime.Format(rssDateFormat),
LastBuildDate: d.Modified.Format(rssDateFormat),
Items: make([]*rssItem, 0, len(d.RSS.Posts)),
},
}
for _, p := range d.RSS.Posts {
r.Channel.Items = append(r.Channel.Items, &rssItem{
Title: p.Title,
Link: p.Permalink,
Description: html.EscapeString(p.Summary),
PubDate: p.Created.Format(rssDateFormat),
})
}
return b.appendXMLFile(d, d.RSS.Path, d.RSS.XSLPermalink, r)
}