all repos — vite @ rewrite

a fast (this time, actually) and minimal static site generator

atom/feed.go (view raw)

 1package atom
 2
 3import (
 4	"encoding/xml"
 5	"time"
 6
 7	"git.icyphox.sh/vite/config"
 8	"git.icyphox.sh/vite/markdown"
 9)
10
11type AtomLink struct {
12	XMLName xml.Name `xml:"link"`
13	Href    string   `xml:"href,attr"`
14	Rel     string   `xml:"rel,attr,omitempty"`
15}
16
17type AtomSummary struct {
18	XMLName xml.Name `xml:"summary"`
19	Content string   `xml:",chardata"`
20	Type    string   `xml:"type,attr"`
21}
22
23type AtomAuthor struct {
24	XMLName xml.Name `xml:"author"`
25	Name    string   `xml:"name"`
26	Email   string   `xml:"email"`
27}
28
29type AtomEntry struct {
30	XMLName xml.Name `xml:"entry"`
31	Title   string   `xml:"title"`
32	Updated string   `xml:"updated"`
33	ID      string   `xml:"id"`
34	Link    *AtomLink
35	Summary *AtomSummary
36}
37
38type AtomFeed struct {
39	XMLName  xml.Name `xml:"feed"`
40	Xmlns    string   `xml:"xmlns,attr"`
41	Title    string   `xml:"title"`
42	Subtitle string   `xml:"subtitle"`
43	ID       string   `xml:"id"`
44	Updated  string   `xml:"updated"`
45	Link     *AtomLink
46	Author   *AtomAuthor `xml:"author"`
47	Entries  []AtomEntry
48}
49
50func NewAtomFeed(srcDir string, posts []markdown.Output) ([]byte, error) {
51	entries := []AtomEntry{}
52	config := config.Config
53	for _, p := range posts {
54		dateStr := p.Meta["date"]
55		date, err := time.Parse("2006-01-02", dateStr)
56		if err != nil {
57			return nil, err
58		}
59		rfc3339 := date.Format(time.RFC3339)
60
61		entry := AtomEntry{
62			Title:   p.Meta["title"],
63			Updated: rfc3339,
64			ID:      NewUUID().String(),
65			Link:    &AtomLink{Href: config.URL + srcDir + p.Meta["slug"]},
66			Summary: &AtomSummary{Content: string(p.HTML), Type: "html"},
67		}
68		entries = append(entries, entry)
69	}
70
71	// 2021-07-14T00:00:00Z
72	now := time.Now().Format(time.RFC3339)
73	feed := &AtomFeed{
74		Xmlns:    "http://www.w3.org/2005/Atom",
75		Title:    config.Title,
76		ID:       config.URL,
77		Subtitle: config.Desc,
78		Link:     &AtomLink{Href: config.URL},
79		Author: &AtomAuthor{
80			Name:  config.Author.Name,
81			Email: config.Author.Email,
82		},
83		Updated: now,
84		Entries: entries,
85	}
86
87	feedXML, err := xml.MarshalIndent(feed, " ", " ")
88	if err != nil {
89		return nil, err
90	}
91	// Add the <?xml...> header.
92	return []byte(xml.Header + string(feedXML)), nil
93}