site: Configure permalinks per task

This commit is contained in:
Adnan Maolood 2022-10-01 15:22:00 -04:00
parent e47e3c2790
commit 509ae0c75a
4 changed files with 46 additions and 43 deletions

View file

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

View file

@ -296,26 +296,6 @@ The following keys are supported:
| params | params
: Extra parameters made available to templates : 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
Tasks can be specified in the [[tasks]] array of tables. 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" 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
Feeds can be specified in the [[tasks.feeds]] array of tables. Multiple 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 page.Path = path
if permalink, ok := cfg.permalinks[p.Path]; ok { if permalink, ok := task.permalinks[p.Path]; ok {
var b strings.Builder var b strings.Builder
permalink.Execute(&b, page) permalink.Execute(&b, page)
page.Path = b.String() page.Path = b.String()

26
site.go
View file

@ -16,10 +16,8 @@ type Site struct {
Title string `toml:"title"` Title string `toml:"title"`
Tasks []*Task `toml:"tasks"` Tasks []*Task `toml:"tasks"`
Params map[string]interface{} `toml:"params"` Params map[string]interface{} `toml:"params"`
Permalinks map[string]string `toml:"permalinks"`
Generated time.Time `toml:"-"` Generated time.Time `toml:"-"`
Root *Page `toml:"-"` Root *Page `toml:"-"`
permalinks map[string]*template.Template
templates Templates templates Templates
} }
@ -35,9 +33,11 @@ type Task struct {
OutputDir string `toml:"output_dir"` OutputDir string `toml:"output_dir"`
URL string `toml:"url"` URL string `toml:"url"`
UglyURLs bool `toml:"ugly_urls"` UglyURLs bool `toml:"ugly_urls"`
Permalinks map[string]string `toml:"permalinks"`
Feeds []Feed `toml:"feeds"` Feeds []Feed `toml:"feeds"`
preprocess map[string][]string preprocess map[string][]string
postprocess []string postprocess []string
permalinks map[string]*template.Template
feeds map[string][]Feed feeds map[string][]Feed
} }
@ -74,17 +74,6 @@ func LoadSite(config string) (*Site, error) {
funcs := site.funcs() 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 // Load templates
templateExts := []string{} templateExts := []string{}
for _, task := range site.Tasks { for _, task := range site.Tasks {
@ -98,6 +87,17 @@ func LoadSite(config string) (*Site, error) {
} }
for _, task := range site.Tasks { 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 // Populate feeds
task.feeds = map[string][]Feed{} task.feeds = map[string][]Feed{}
for _, feed := range task.Feeds { for _, feed := range task.Feeds {