From 81e35cb1eb10ed8dbba19cd4eba05533ec644e22 Mon Sep 17 00:00:00 2001 From: adnano Date: Sat, 1 Oct 2022 15:22:00 -0400 Subject: [PATCH] site: Configure permalinks per task --- config.toml | 6 +++--- docs/kiln.1.scd | 43 +++++++++++++++++++++++-------------------- page.go | 2 +- site.go | 38 +++++++++++++++++++------------------- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/config.toml b/config.toml index 3dc2bdc..4e99c62 100644 --- a/config.toml +++ b/config.toml @@ -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" diff --git a/docs/kiln.1.scd b/docs/kiln.1.scd index 3af2efd..42c0658 100644 --- a/docs/kiln.1.scd +++ b/docs/kiln.1.scd @@ -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 diff --git a/page.go b/page.go index 93bfd16..b62fddc 100644 --- a/page.go +++ b/page.go @@ -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() diff --git a/site.go b/site.go index 9524d30..8e1de12 100644 --- a/site.go +++ b/site.go @@ -13,14 +13,12 @@ import ( // Site represents a site. 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 + Title string `toml:"title"` + Tasks []*Task `toml:"tasks"` + Params map[string]interface{} `toml:"params"` + Generated time.Time `toml:"-"` + Root *Page `toml:"-"` + templates Templates } // Task represents a site build task. @@ -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 {