mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
100 lines
2.3 KiB
Go
100 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"text/template"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
)
|
|
|
|
// Config contains site configuration.
|
|
type Config struct {
|
|
Title string `toml:"title"` // site title
|
|
URLs []string `toml:"urls"` // site URLs
|
|
Feeds map[string]string `toml:"feeds"` // site feeds
|
|
Tasks map[string]*Task `toml:"tasks"` // site tasks
|
|
Templates *Templates `toml:"-"` // site templates
|
|
}
|
|
|
|
// Task represents a site build task.
|
|
type Task struct {
|
|
Input string `toml:"input"` // input file extension
|
|
Output string `toml:"output"` // output file extension
|
|
Template string `toml:"template"` // template file extension
|
|
PostProcess string `toml:"postprocess"` // postprocess directive
|
|
Destination string `toml:"destination"` // destination directory
|
|
postProcess Format
|
|
}
|
|
|
|
func (t Task) Format(p *Page) (string, []byte) {
|
|
if t.postProcess == nil {
|
|
return path.Join(p.Path, "index"+t.Output), []byte(p.Content)
|
|
}
|
|
return t.postProcess.Format(p)
|
|
}
|
|
|
|
// LoadConfig loads the configuration from the provided path.
|
|
func LoadConfig(path string) (*Config, error) {
|
|
c := new(Config)
|
|
c.Feeds = make(map[string]string)
|
|
c.Tasks = make(map[string]*Task)
|
|
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := toml.NewDecoder(f).Decode(c); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Add default task
|
|
// The toml library overwrites the map, so add default values after parsing
|
|
if _, ok := c.Tasks["gemini"]; !ok {
|
|
c.Tasks["gemini"] = &Task{
|
|
Input: ".gmi",
|
|
Output: ".gmi",
|
|
Template: ".gmi",
|
|
Destination: "public",
|
|
}
|
|
}
|
|
|
|
for _, task := range c.Tasks {
|
|
switch task.PostProcess {
|
|
case "":
|
|
continue
|
|
case "geminiToHTML":
|
|
task.postProcess = GeminiToHTML(c)
|
|
default:
|
|
return nil, fmt.Errorf("unrecognized postprocess directive %q", task.PostProcess)
|
|
}
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// LoadTemplates loads templates from the provided path.
|
|
func (c *Config) LoadTemplates(path string) error {
|
|
// Site contains site metadata passed to templates
|
|
type Site struct {
|
|
Title string
|
|
URLs []string
|
|
}
|
|
|
|
// Load templates
|
|
c.Templates = NewTemplates()
|
|
c.Templates.Funcs(template.FuncMap{
|
|
"site": func() Site {
|
|
return Site{
|
|
Title: c.Title,
|
|
URLs: c.URLs,
|
|
}
|
|
},
|
|
})
|
|
c.Templates.LoadDefault()
|
|
return c.Templates.Load(path)
|
|
}
|