all repos — vite @ 6b7947f82db15780f91b97b0b2360577551c2d05

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

atom/feed.go (view raw)

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