kiln/config.go
2021-03-20 16:29:06 -04:00

55 lines
1.1 KiB
Go

package main
import (
"os"
"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
Templates *Templates `toml:"-"` // site templates
}
// LoadConfig loads the configuration from the provided path.
func LoadConfig(path string) (*Config, error) {
c := new(Config)
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
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)
}