kiln/config.go
2020-11-22 15:14:50 -05:00

69 lines
1.3 KiB
Go

package main
import (
"os"
"text/template"
"git.sr.ht/~adnano/go-ini"
)
// Config contains site configuration.
type Config struct {
Title string // site title
URL string // site URL
Feeds map[string]string // site feeds
Templates *Templates // site 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 "url":
c.URL = 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
URL string
}
// Load templates
c.Templates = NewTemplates()
c.Templates.Funcs(template.FuncMap{
"site": func() Site {
return Site{
Title: c.Title,
URL: c.URL,
}
},
})
c.Templates.LoadDefault()
return c.Templates.Load(path)
}