Implement support for permalinks

This commit is contained in:
adnano 2021-05-10 11:06:55 -04:00
parent e8e47f1bad
commit 66ac6015c9
4 changed files with 35 additions and 14 deletions

View file

@ -2,9 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"html/template" htemplate "html/template"
"os" "os"
"strings" "strings"
"text/template"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
@ -13,9 +14,10 @@ import (
type Config struct { type Config struct {
Title string `toml:"title"` Title string `toml:"title"`
URLs []string `toml:"urls"` URLs []string `toml:"urls"`
Feeds map[string]string `toml:"feeds"`
Tasks []*Task `toml:"tasks"` Tasks []*Task `toml:"tasks"`
Feeds map[string]string `toml:"feeds"`
Permalinks map[string]string `toml:"permalinks"` Permalinks map[string]string `toml:"permalinks"`
permalinks map[string]*template.Template
templates *Templates templates *Templates
} }
@ -44,6 +46,16 @@ func LoadConfig(path string) (*Config, error) {
return nil, err 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 // Site contains site metadata passed to templates
type Site struct { type Site struct {
Title string Title string
@ -70,11 +82,11 @@ func LoadConfig(path string) (*Config, error) {
} }
return b.String(), nil return b.String(), nil
}, },
"safeHTML": func(s string) template.HTML { return template.HTML(s) }, "safeHTML": func(s string) htemplate.HTML { return htemplate.HTML(s) },
"safeHTMLAttr": func(s string) template.HTMLAttr { return template.HTMLAttr(s) }, "safeHTMLAttr": func(s string) htemplate.HTMLAttr { return htemplate.HTMLAttr(s) },
"safeCSS": func(s string) template.CSS { return template.CSS(s) }, "safeCSS": func(s string) htemplate.CSS { return htemplate.CSS(s) },
"safeJS": func(s string) template.JS { return template.JS(s) }, "safeJS": func(s string) htemplate.JS { return htemplate.JS(s) },
"safeURL": func(s string) template.URL { return template.URL(s) }, "safeURL": func(s string) htemplate.URL { return htemplate.URL(s) },
}) })
return c, nil return c, nil

View file

@ -5,10 +5,14 @@ urls = []
# Site feeds # Site feeds
[feeds] [feeds]
# "/blog/" = "Example blog" "/" = "Example feed"
# Site permalinks
[permalinks]
"/" = '/{{.Date.Format "2006/01/02"}}{{.Path}}'
# Site tasks # Site tasks
[tasks.gemini] [[tasks]]
input_ext = ".gmi" input_ext = ".gmi"
output_ext = ".gmi" output_ext = ".gmi"
template_ext = ".gmi" template_ext = ".gmi"

13
dir.go
View file

@ -47,11 +47,11 @@ func NewDir(path string) *Dir {
} }
// read reads from a directory and indexes the files and directories within it. // read reads from a directory and indexes the files and directories within it.
func (d *Dir) read(srcDir string, task *Task) error { func (d *Dir) read(srcDir string, task *Task, cfg *Config) error {
return d._read(srcDir, "", task) 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)) entries, err := ioutil.ReadDir(pathpkg.Join(srcDir, path))
if err != nil { if err != nil {
return err return err
@ -66,7 +66,7 @@ func (d *Dir) _read(srcDir, path string, task *Task) error {
if entry.IsDir() { if entry.IsDir() {
// Gather directory data // Gather directory data
dir := NewDir(path) dir := NewDir(path)
if err := dir._read(srcDir, path, task); err != nil { if err := dir._read(srcDir, path, task, cfg); err != nil {
return err return err
} }
d.Dirs = append(d.Dirs, dir) d.Dirs = append(d.Dirs, dir)
@ -131,6 +131,11 @@ func (d *Dir) _read(srcDir, path string, task *Task) error {
path += "/" path += "/"
} }
page.Path = 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) d.Pages = append(d.Pages, page)
} }
} }

View file

@ -83,7 +83,7 @@ func run(cfg *Config) error {
func runTask(cfg *Config, task *Task) error { func runTask(cfg *Config, task *Task) error {
// Read content // Read content
dir := NewDir("") dir := NewDir("")
if err := dir.read("content", task); err != nil { if err := dir.read("content", task, cfg); err != nil {
return err return err
} }
dir.sort() dir.sort()