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