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 (
"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

View file

@ -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"

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.
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)
}
}

View file

@ -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()