1
0
Fork 0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-05-21 06:56:20 +02:00
kiln/config.go
2020-12-21 16:41:05 -05:00

70 lines
1.3 KiB
Go

package main
import (
"os"
"strings"
"text/template"
"git.sr.ht/~adnano/go-ini"
)
// Config contains site configuration.
type Config struct {
Title string // site title
URLs []string // site URLs
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 "urls":
c.URLs = strings.Fields(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
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)
}