kiln/config.go

63 lines
1.1 KiB
Go
Raw Normal View History

2020-11-20 17:07:38 +00:00
package main
import (
"os"
"text/template"
"git.sr.ht/~adnano/go-ini"
)
// Config contains site configuration.
type Config struct {
Title string
Feeds map[string]string
Templates *Templates
}
// NewConfig returns a new configuration.
func NewConfig() *Config {
return &Config{
Feeds: map[string]string{},
}
}
// Load loads the configuration from the provided path.
func (c *Config) Load(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
// Load config
return ini.Parse(f, func(section, key, value string) {
switch section {
case "":
switch key {
case "title":
c.Title = value
}
case "feeds":
c.Feeds[key] = value
}
})
}
// 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
}
// Load templates
c.Templates = NewTemplates()
c.Templates.Funcs(template.FuncMap{
"site": func() Site {
return Site{
Title: c.Title,
}
},
})
return c.Templates.Load(path)
}