mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 09:23:09 +00:00
105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"strings"
|
|
"text/template"
|
|
|
|
"github.com/BurntSushi/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
|
|
Static string `toml:"static"` // static file directory
|
|
Destination string `toml:"destination"` // destination directory
|
|
}
|
|
|
|
func (t Task) Format(p *Page) (string, []byte) {
|
|
path := path.Join(p.Path, "index"+t.Output)
|
|
|
|
// Run a custom command.
|
|
if t.PostProcess != "" {
|
|
split := strings.Split(t.PostProcess, " ")
|
|
cmd := exec.Command(split[0], split[1:]...)
|
|
buf := new(bytes.Buffer)
|
|
cmd.Stdin = strings.NewReader(p.Content)
|
|
cmd.Stdout = buf
|
|
if err := cmd.Run(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return path, buf.Bytes()
|
|
}
|
|
|
|
return path, []byte(p.Content)
|
|
}
|
|
|
|
// DefaultConfig returns the default configuration.
|
|
func DefaultConfig() *Config {
|
|
c := new(Config)
|
|
c.Feeds = make(map[string]string)
|
|
c.Tasks = make(map[string]*Task)
|
|
c.Tasks["gemini"] = &Task{
|
|
Input: ".gmi",
|
|
Output: ".gmi",
|
|
Template: ".gmi",
|
|
Static: "static",
|
|
Destination: "public",
|
|
}
|
|
return c
|
|
}
|
|
|
|
// LoadConfig loads the configuration from the provided path.
|
|
func LoadConfig(path string) (*Config, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
c := DefaultConfig()
|
|
if _, err := toml.DecodeReader(f, c); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
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)
|
|
}
|