page: Ability to specify build tasks per page

Implements: https://todo.sr.ht/~adnano/kiln/15
This commit is contained in:
Adnan Maolood 2022-02-09 15:52:22 -05:00
parent 7b8dd422bc
commit 9263b73543
4 changed files with 59 additions and 12 deletions

View file

@ -4,6 +4,7 @@ title = "Example website"
"/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}/" "/" = "/{{ .Date.Format `2006/01/02` }}/{{ path.Base .Path }}/"
[[tasks]] [[tasks]]
name = "Gemini"
url = "gemini://example.com" url = "gemini://example.com"
input = [".gmi"] input = [".gmi"]
output = ".gmi" output = ".gmi"

View file

@ -120,6 +120,22 @@ The following keys are supported:
--- ---
``` ```
*outputs*
Optionally specifies a list of tasks that can build this page. Defaults to
all tasks.
Example:
```
---
outputs: ["Gemini", "HTTPS"]
---
---
outputs: [] # Excluded from all tasks
---
```
*params* *params*
Specifies extra parameters to be provided to templates. Specifies extra parameters to be provided to templates.
@ -211,6 +227,19 @@ Tasks can be specified in the [[tasks]] array of tables.
The following configuration options are supported per task: The following configuration options are supported per task:
*name*
An optional name for the task.
Example:
```
[[tasks]]
name = "Gemini"
[[tasks]]
name = "HTML export"
```
*input* *input*
A list of input file extensions. Files in the content directory with a A list of input file extensions. Files in the content directory with a
matching extension will be processed. matching extension will be processed.

24
page.go
View file

@ -18,10 +18,11 @@ import (
// Page represents a page. // Page represents a page.
type Page struct { type Page struct {
Title string Title string `yaml:"title"`
Date time.Time Date time.Time `yaml:"date"`
Weight int Weight int `yaml:"weight"`
Params map[string]interface{} Outputs []string `yaml:"outputs"`
Params map[string]interface{} `yaml:"params"`
Path string `yaml:"-"` Path string `yaml:"-"`
FilePath string `yaml:"-"` FilePath string `yaml:"-"`
URL string `yaml:"-"` URL string `yaml:"-"`
@ -113,6 +114,21 @@ func (p *Page) _read(fsys fs.FS, path string, task *Task, cfg *Site) error {
if len(frontmatter) != 0 { if len(frontmatter) != 0 {
if err := yaml.Unmarshal(frontmatter, page); err != nil { if err := yaml.Unmarshal(frontmatter, page); err != nil {
log.Printf("failed to parse frontmatter for %q: %v", path, err) log.Printf("failed to parse frontmatter for %q: %v", path, err)
} else if page.Outputs != nil {
// Enforce page outputs
if len(page.Outputs) == 0 || task.Name == "" {
continue
}
found := false
for _, output := range page.Outputs {
if output == task.Name {
found = true
break
}
}
if !found {
continue
}
} }
// Trim leading newlines from content // Trim leading newlines from content

View file

@ -25,6 +25,7 @@ type Site struct {
// Task represents a site build task. // Task represents a site build task.
type Task struct { type Task struct {
Name string `toml:"name"`
Input []string `toml:"input"` Input []string `toml:"input"`
OutputExt string `toml:"output"` OutputExt string `toml:"output"`
TemplateExt string `toml:"template"` TemplateExt string `toml:"template"`