rss.go (view raw)
1package main
2
3import (
4 . "github.com/gorilla/feeds"
5 "sort"
6 "time"
7 "os"
8 "path/filepath"
9)
10
11func generateRSS(posts []Post, cfg Config) {
12 now := time.Now()
13 feed := &Feed{
14 Title: cfg.Title,
15 Link: &Link{Href: cfg.SiteURL},
16 Description: cfg.Description,
17 Author: &Author{
18 Name: cfg.Author["name"],
19 Email: cfg.Author["email"],
20 },
21 Created: now,
22 }
23
24 // Sort posts by date
25 sort.Slice(posts, func(i, j int) bool {
26 return posts[j].fm.Date.Time.Before(posts[i].fm.Date.Time)
27 })
28
29 atomfile, err := os.Create(filepath.Join("build", "blog", "feed.xml"))
30 if err != nil {
31 printErr(err)
32 }
33 for _, p := range posts {
34 feed.Items = append(feed.Items, &Item{
35 Title: p.fm.Title,
36 Link: &Link{Href: cfg.RSSPrefixURL + p.fm.URL},
37 Description: string(p.fm.Body),
38 Created: p.fm.Date.Time,
39 })
40 }
41
42 err = feed.WriteAtom(atomfile)
43 if err != nil {
44 printErr(err)
45 }
46}