all repos — vite @ 1c86a5361dbf3d809d45e0d0066e59a0d65f9070

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

Add a new default-template config option

Also reworks config loading.
Anirudh Oppiliappan x@icyphox.sh
Wed, 23 Feb 2022 11:40:15 +0530
commit

1c86a5361dbf3d809d45e0d0066e59a0d65f9070

parent

4c0dbd41900270f85e454f3e26c7e86fd4a7a663

5 files changed, 34 insertions(+), 34 deletions(-)

jump to
M atom/feed.goatom/feed.go

@@ -52,8 +52,7 @@

// Creates a new Atom feed. func NewAtomFeed(srcDir string, posts []markdown.Output) ([]byte, error) { entries := []AtomEntry{} - cfg := config.ConfigYaml{} - cfg.ParseConfig() + for _, p := range posts { dateStr := p.Meta["date"] date, err := time.Parse("2006-01-02", dateStr)

@@ -68,12 +67,12 @@ Updated: rfc3339,

// tag:icyphox.sh,2019-10-23:blog/some-post/ ID: fmt.Sprintf( "tag:%s,%s:%s", - cfg.URL[8:], // strip https:// + config.Config.URL[8:], // strip https:// dateStr, filepath.Join(srcDir, p.Meta["slug"]), ), // filepath.Join strips the second / in http:// - Link: &AtomLink{Href: cfg.URL + filepath.Join(srcDir, p.Meta["slug"])}, + Link: &AtomLink{Href: config.Config.URL + filepath.Join(srcDir, p.Meta["slug"])}, Summary: &AtomSummary{ Content: fmt.Sprintf("<h2>%s</h2>\n%s", p.Meta["subtitle"],

@@ -88,13 +87,13 @@ // 2021-07-14T00:00:00Z

now := time.Now().Format(time.RFC3339) feed := &AtomFeed{ Xmlns: "http://www.w3.org/2005/Atom", - Title: cfg.Title, - ID: cfg.URL, - Subtitle: cfg.Desc, - Link: &AtomLink{Href: cfg.URL}, + Title: config.Config.Title, + ID: config.Config.URL, + Subtitle: config.Config.Desc, + Link: &AtomLink{Href: config.Config.URL}, Author: &AtomAuthor{ - Name: cfg.Author.Name, - Email: cfg.Author.Email, + Name: config.Config.Author.Name, + Email: config.Config.Author.Email, }, Updated: now, Entries: entries,
M commands/build.gocommands/build.go

@@ -72,10 +72,6 @@ }

out := markdown.Output{} out.RenderMarkdown(fb) - cfg := config.ConfigYaml{} - if err := cfg.ParseConfig(); err != nil { - return err - } if err = out.RenderHTML( htmlFile, TEMPLATES,

@@ -83,7 +79,7 @@ struct {

Cfg config.ConfigYaml Meta markdown.Matter Body string - }{cfg, out.Meta, string(out.HTML)}, + }{config.Config, out.Meta, string(out.HTML)}, ); err != nil { return err }

@@ -131,11 +127,6 @@ }

out := markdown.Output{} out.RenderMarkdown(fb) - cfg := config.ConfigYaml{} - if err := cfg.ParseConfig(); err != nil { - return err - } - if err = out.RenderHTML( htmlFile, TEMPLATES,

@@ -143,7 +134,7 @@ struct {

Cfg config.ConfigYaml Meta markdown.Matter Body string - }{cfg, out.Meta, string(out.HTML)}, + }{config.Config, out.Meta, string(out.HTML)}, ); err != nil { return err }

@@ -170,17 +161,13 @@ return err

} out := markdown.Output{} out.RenderMarkdown(indexMd) - cfg := config.ConfigYaml{} - if err := cfg.ParseConfig(); err != nil { - return err - } out.RenderHTML(indexHTML, TEMPLATES, struct { Cfg config.ConfigYaml Meta markdown.Matter Body string Posts []markdown.Output - }{cfg, out.Meta, string(out.HTML), posts}) + }{config.Config, out.Meta, string(out.HTML), posts}) // Create feeds // ex: build/blog/feed.xml
M config/config.goconfig/config.go

@@ -1,15 +1,27 @@

package config import ( + "fmt" "os" "gopkg.in/yaml.v3" ) +var Config ConfigYaml + +func init() { + err := Config.parseConfig() + if err != nil { + fmt.Fprintf(os.Stderr, "error: config: %+v\n", err) + os.Exit(1) + } +} + type ConfigYaml struct { - Title string `yaml:"title"` - Desc string `yaml:"description"` - Author struct { + Title string `yaml:"title"` + Desc string `yaml:"description"` + DefaultTemplate string `yaml:"default-template"` + Author struct { Name string `yaml:"name"` Email string `yaml:"email"` } `yaml:"author"`

@@ -18,7 +30,7 @@ // Prebuild []string `yaml:"prebuild"`

// Postbuild []string `yaml:"postbuild"` } -func (c *ConfigYaml) ParseConfig() error { +func (c *ConfigYaml) parseConfig() error { cf, err := os.ReadFile("config.yaml") if err != nil { return err
M markdown/markdown.gomarkdown/markdown.go

@@ -5,6 +5,7 @@ "os"

gotmpl "text/template" "time" + "git.icyphox.sh/vite/config" "git.icyphox.sh/vite/markdown/template" bfc "github.com/Depado/bfchroma"

@@ -49,7 +50,7 @@ // in the frontmatter. data is the template struct.

func (out *Output) RenderHTML(dst, tmplDir string, data interface{}) error { metaTemplate := out.Meta["template"] if metaTemplate == "" { - metaTemplate = "text.html" + metaTemplate = config.Config.DefaultTemplate } tmpl := template.NewTmpl()
M readmereadme

@@ -26,9 +26,10 @@ The configuration is unmarshalled from a config.yaml file, into the

below struct: type ConfigYaml struct { - Title string `yaml:"title"` - Desc string `yaml:"description"` - Author struct { + Title string `yaml:"title"` + Desc string `yaml:"description"` + DefaultTemplate string `yaml:"default-template"` + Author struct { Name string `yaml:"name"` Email string `yaml:"email"` } `yaml:"author"`