From 66ac6015c9b140bb4d28426f3e2d475c1647a4fd Mon Sep 17 00:00:00 2001 From: adnano Date: Mon, 10 May 2021 11:06:55 -0400 Subject: [PATCH] Implement support for permalinks --- config.go | 26 +++++++++++++++++++------- config.toml | 8 ++++++-- dir.go | 13 +++++++++---- main.go | 2 +- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/config.go b/config.go index f691776..b9607b6 100644 --- a/config.go +++ b/config.go @@ -2,9 +2,10 @@ package main import ( "fmt" - "html/template" + htemplate "html/template" "os" "strings" + "text/template" "github.com/BurntSushi/toml" ) @@ -13,9 +14,10 @@ import ( type Config struct { Title string `toml:"title"` URLs []string `toml:"urls"` - Feeds map[string]string `toml:"feeds"` Tasks []*Task `toml:"tasks"` + Feeds map[string]string `toml:"feeds"` Permalinks map[string]string `toml:"permalinks"` + permalinks map[string]*template.Template templates *Templates } @@ -44,6 +46,16 @@ func LoadConfig(path string) (*Config, error) { return nil, err } + // Parse permalinks + c.permalinks = map[string]*template.Template{} + for s := range c.Permalinks { + t, err := template.New("permalink " + s).Parse(c.Permalinks[s]) + if err != nil { + return nil, err + } + c.permalinks[s] = t + } + // Site contains site metadata passed to templates type Site struct { Title string @@ -70,11 +82,11 @@ func LoadConfig(path string) (*Config, error) { } return b.String(), nil }, - "safeHTML": func(s string) template.HTML { return template.HTML(s) }, - "safeHTMLAttr": func(s string) template.HTMLAttr { return template.HTMLAttr(s) }, - "safeCSS": func(s string) template.CSS { return template.CSS(s) }, - "safeJS": func(s string) template.JS { return template.JS(s) }, - "safeURL": func(s string) template.URL { return template.URL(s) }, + "safeHTML": func(s string) htemplate.HTML { return htemplate.HTML(s) }, + "safeHTMLAttr": func(s string) htemplate.HTMLAttr { return htemplate.HTMLAttr(s) }, + "safeCSS": func(s string) htemplate.CSS { return htemplate.CSS(s) }, + "safeJS": func(s string) htemplate.JS { return htemplate.JS(s) }, + "safeURL": func(s string) htemplate.URL { return htemplate.URL(s) }, }) return c, nil diff --git a/config.toml b/config.toml index 9c9e96c..13e468c 100644 --- a/config.toml +++ b/config.toml @@ -5,10 +5,14 @@ urls = [] # Site feeds [feeds] -# "/blog/" = "Example blog" +"/" = "Example feed" + +# Site permalinks +[permalinks] +"/" = '/{{.Date.Format "2006/01/02"}}{{.Path}}' # Site tasks -[tasks.gemini] +[[tasks]] input_ext = ".gmi" output_ext = ".gmi" template_ext = ".gmi" diff --git a/dir.go b/dir.go index 4537f84..16bc2e8 100644 --- a/dir.go +++ b/dir.go @@ -47,11 +47,11 @@ func NewDir(path string) *Dir { } // read reads from a directory and indexes the files and directories within it. -func (d *Dir) read(srcDir string, task *Task) error { - return d._read(srcDir, "", task) +func (d *Dir) read(srcDir string, task *Task, cfg *Config) error { + return d._read(srcDir, "", task, cfg) } -func (d *Dir) _read(srcDir, path string, task *Task) error { +func (d *Dir) _read(srcDir, path string, task *Task, cfg *Config) error { entries, err := ioutil.ReadDir(pathpkg.Join(srcDir, path)) if err != nil { return err @@ -66,7 +66,7 @@ func (d *Dir) _read(srcDir, path string, task *Task) error { if entry.IsDir() { // Gather directory data dir := NewDir(path) - if err := dir._read(srcDir, path, task); err != nil { + if err := dir._read(srcDir, path, task, cfg); err != nil { return err } d.Dirs = append(d.Dirs, dir) @@ -131,6 +131,11 @@ func (d *Dir) _read(srcDir, path string, task *Task) error { path += "/" } page.Path = path + if permalink, ok := cfg.permalinks[d.Path]; ok { + var b strings.Builder + permalink.Execute(&b, page) + page.Path = b.String() + } d.Pages = append(d.Pages, page) } } diff --git a/main.go b/main.go index cd1f5a6..3568918 100644 --- a/main.go +++ b/main.go @@ -83,7 +83,7 @@ func run(cfg *Config) error { func runTask(cfg *Config, task *Task) error { // Read content dir := NewDir("") - if err := dir.read("content", task); err != nil { + if err := dir.read("content", task, cfg); err != nil { return err } dir.sort()