site: Configure permalinks per task

This commit is contained in:
adnano 2022-10-01 15:22:00 -04:00
parent 085ef5aded
commit 81e35cb1eb
4 changed files with 46 additions and 43 deletions

View file

@ -1,8 +1,5 @@
title = "Example website"
[permalinks]
"/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}/"
[[tasks]]
name = "Gemini"
url = "gemini://example.com"
@ -12,6 +9,9 @@ template = ".gmi"
static_dir = "static"
output_dir = "public"
[tasks.permalinks]
"/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}/"
[[tasks.feeds]]
input_dir = "."
title = "Example feed"

View file

@ -296,26 +296,6 @@ The following keys are supported:
| params
: Extra parameters made available to templates
## PERMALINKS
Permalinks can be used to rewrite page paths. Permalinks are specified in the
\[permalinks] table of the configuration file. Keys denote a path to a directory,
and values use the Go templating language to rewrite the final path of pages in
that directory. The templates have the same data that page templates have
available to them (see *PAGE VARIABLES*).
The following configuration will rewrite the paths of pages in the content/blog
directory to /YYYY/MM/DD/slug. For example, the file
content/blog/2021-05-12-hello-world.gmi will have a path of
/2021/05/12/hello-world/.
```
[permalinks]
"/blog/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}"
```
For more information on templates, see *TEMPLATES*.
## TASKS
Tasks can be specified in the [[tasks]] array of tables.
@ -485,6 +465,29 @@ HTML, and the *gmnitohtml*(1) command to convert Gemini text to HTML.
output_dir = "public"
```
## PERMALINKS
Permalinks can be used to rewrite page paths. Permalinks are specified in the
\[tasks.permalinks] table of the configuration file. Keys denote a path to a
directory, and values use the Go templating language to rewrite the final path
of pages in that directory. The templates have the same data that page templates
have available to them (see *PAGE VARIABLES*).
The following configuration will rewrite the paths of pages in the content/blog
directory to /YYYY/MM/DD/slug. For example, the file
content/blog/2021-05-12-hello-world.gmi will have a path of
/2021/05/12/hello-world/.
```
[[tasks]]
# ...
[tasks.permalinks]
"/blog/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}"
```
For more information on templates, see *TEMPLATES*.
## FEEDS
Feeds can be specified in the [[tasks.feeds]] array of tables. Multiple feeds

View file

@ -159,7 +159,7 @@ func (p *Page) _read(fsys fs.FS, path string, task *Task, cfg *Site) error {
}
}
page.Path = path
if permalink, ok := cfg.permalinks[p.Path]; ok {
if permalink, ok := task.permalinks[p.Path]; ok {
var b strings.Builder
permalink.Execute(&b, page)
page.Path = b.String()

26
site.go
View file

@ -16,10 +16,8 @@ type Site struct {
Title string `toml:"title"`
Tasks []*Task `toml:"tasks"`
Params map[string]interface{} `toml:"params"`
Permalinks map[string]string `toml:"permalinks"`
Generated time.Time `toml:"-"`
Root *Page `toml:"-"`
permalinks map[string]*template.Template
templates Templates
}
@ -35,9 +33,11 @@ type Task struct {
OutputDir string `toml:"output_dir"`
URL string `toml:"url"`
UglyURLs bool `toml:"ugly_urls"`
Permalinks map[string]string `toml:"permalinks"`
Feeds []Feed `toml:"feeds"`
preprocess map[string][]string
postprocess []string
permalinks map[string]*template.Template
feeds map[string][]Feed
}
@ -74,17 +74,6 @@ func LoadSite(config string) (*Site, error) {
funcs := site.funcs()
// Parse permalinks
site.permalinks = map[string]*template.Template{}
for path := range site.Permalinks {
t := template.New(fmt.Sprintf("permalink %q", path)).Funcs(funcs)
_, err := t.Parse(site.Permalinks[path])
if err != nil {
return nil, err
}
site.permalinks[path] = t
}
// Load templates
templateExts := []string{}
for _, task := range site.Tasks {
@ -98,6 +87,17 @@ func LoadSite(config string) (*Site, error) {
}
for _, task := range site.Tasks {
// Parse permalinks
task.permalinks = map[string]*template.Template{}
for path := range task.Permalinks {
t := template.New(fmt.Sprintf("permalink %q", path)).Funcs(funcs)
_, err := t.Parse(task.Permalinks[path])
if err != nil {
return nil, err
}
task.permalinks[path] = t
}
// Populate feeds
task.feeds = map[string][]Feed{}
for _, feed := range task.Feeds {