kiln/config.go

70 lines
1.3 KiB
Go
Raw Normal View History

2020-11-20 17:07:38 +00:00
package main
import (
"os"
2020-12-21 21:41:05 +00:00
"strings"
2020-11-20 17:07:38 +00:00
"text/template"
"git.sr.ht/~adnano/go-ini"
)
// Config contains site configuration.
type Config struct {
2020-11-22 20:14:50 +00:00
Title string // site title
2020-11-27 23:24:31 +00:00
URLs []string // site URLs
2020-11-22 20:14:50 +00:00
Feeds map[string]string // site feeds
Templates *Templates // site templates
2020-11-20 17:07:38 +00:00
}
// 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
2020-12-21 21:41:05 +00:00
case "urls":
c.URLs = strings.Fields(value)
2020-11-20 17:07:38 +00:00
}
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
2020-11-27 23:24:31 +00:00
URLs []string
2020-11-20 17:07:38 +00:00
}
// Load templates
c.Templates = NewTemplates()
c.Templates.Funcs(template.FuncMap{
"site": func() Site {
return Site{
Title: c.Title,
2020-11-27 23:24:31 +00:00
URLs: c.URLs,
2020-11-20 17:07:38 +00:00
}
},
})
2020-11-22 20:14:50 +00:00
c.Templates.LoadDefault()
2020-11-20 17:07:38 +00:00
return c.Templates.Load(path)
}