all repos — vite @ b64e5bd202d9b87397b0f4fd530110774ec5a324

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

RSS feeds!
Anirudh Oppiliappan x@icyphox.sh
Wed, 04 Nov 2020 11:13:15 +0530
commit

b64e5bd202d9b87397b0f4fd530110774ec5a324

parent

262d705c146993384e3c6dd00d62357b9bff15f6

6 files changed, 98 insertions(+), 10 deletions(-)

jump to
M build.gobuild.go

@@ -13,6 +13,12 @@ )

var cfg = parseConfig() +type Post struct { + fm Matter +} + +var posts []Post + func execute(cmds []string) { for _, cmd := range cmds { out, err := exec.Command(cmd).Output()

@@ -58,6 +64,10 @@

os.MkdirAll(buildPath, 0755) fm.Body = string(bodyHtml) + + if strings.Contains(relPath, "blog/") { + posts = append(posts, Post{fm}) + } // combine config and matter structs combined := struct {

@@ -120,6 +130,8 @@ if err != nil {

printErr(err) } printMsg("site build complete") + printMsg("generating feeds...") + generateRSS(posts, cfg) printMsg("executing post-build actions...") execute(cfg.Postbuild) }
M config.goconfig.go

@@ -6,11 +6,15 @@ "io/ioutil"

) type Config struct { - Title string - Header string - Footer string - Prebuild []string - Postbuild []string + Title string `yaml:"title"` + Header string `yaml:"header"` + SiteURL string `yaml:"siteurl"` + Description string `yaml:"description"` + Author map[string]string `yaml:"author"` + Footer string `yaml:"footer"` + Prebuild []string `yaml:"prebuild"` + Postbuild []string `yaml:"postbuild"` + RSSPrefixURL string `yaml:"rssprefixurl"` } func parseConfig() Config {
M frontmatter.gofrontmatter.go

@@ -3,14 +3,31 @@

import ( "bytes" "github.com/adrg/frontmatter" + "time" ) +type Date8601 struct { + time.Time +} + +func (d *Date8601) UnmarshalYAML(unmarshal func(interface{}) error) error { + var date string + + err := unmarshal(&date) + if err != nil { + return err + } + + d.Time, err = time.Parse("2006-01-02", date) + return err +} + type Matter struct { - Template string `yaml:"template"` - URL string `yaml:"url"` - Title string `yaml:"title"` - Subtitle string `yaml:"subtitle"` - Date string `yaml:"date"` + Template string `yaml:"template"` + URL string `yaml:"url"` + Title string `yaml:"title"` + Subtitle string `yaml:"subtitle"` + Date Date8601 `yaml:"date"` Body string }
M go.modgo.mod

@@ -6,6 +6,8 @@ require (

github.com/Depado/bfchroma v1.3.0 github.com/adrg/frontmatter v0.1.0 github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e + github.com/gorilla/feeds v1.1.1 + github.com/kr/pretty v0.2.1 // indirect github.com/russross/blackfriday/v2 v2.0.1 gopkg.in/yaml.v2 v2.3.0 )
M go.sumgo.sum

@@ -23,6 +23,13 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= +github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY= +github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
A rss.go

@@ -0,0 +1,46 @@

+package main + +import ( + . "github.com/gorilla/feeds" + "sort" + "time" + "os" + "path/filepath" +) + +func generateRSS(posts []Post, cfg Config) { + now := time.Now() + feed := &Feed{ + Title: cfg.Title, + Link: &Link{Href: cfg.SiteURL}, + Description: cfg.Description, + Author: &Author{ + Name: cfg.Author["name"], + Email: cfg.Author["email"], + }, + Created: now, + } + + // Sort posts by date + sort.Slice(posts, func(i, j int) bool { + return posts[j].fm.Date.Time.Before(posts[i].fm.Date.Time) + }) + + atomfile, err := os.Create(filepath.Join("build", "blog", "feed.xml")) + if err != nil { + printErr(err) + } + for _, p := range posts { + feed.Items = append(feed.Items, &Item{ + Title: p.fm.Title, + Link: &Link{Href: cfg.RSSPrefixURL + p.fm.URL}, + Description: string(p.fm.Body), + Created: p.fm.Date.Time, + }) + } + + err = feed.WriteAtom(atomfile) + if err != nil { + printErr(err) + } +}