1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 10:09:18 +01:00
kiln/config.go

75 lines
1.4 KiB
Go
Raw Normal View History

2020-11-20 18:07:38 +01:00
package main
import (
2021-02-28 03:45:37 +01:00
"mime"
2020-11-20 18:07:38 +01:00
"os"
2020-12-21 22:41:05 +01:00
"strings"
2020-11-20 18:07:38 +01:00
"text/template"
"git.sr.ht/~adnano/go-ini"
)
// Config contains site configuration.
type Config struct {
2020-11-22 21:14:50 +01:00
Title string // site title
2020-11-28 00:24:31 +01:00
URLs []string // site URLs
2020-11-22 21:14:50 +01:00
Feeds map[string]string // site feeds
Templates *Templates // site templates
2020-11-20 18:07:38 +01: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
2021-03-20 04:37:22 +01:00
case "url":
2020-12-21 22:41:05 +01:00
c.URLs = strings.Fields(value)
2020-11-20 18:07:38 +01:00
}
case "feeds":
c.Feeds[key] = value
2021-02-28 03:45:37 +01:00
case "mediatypes":
for _, field := range strings.Fields(value) {
mime.AddExtensionType(key, field)
}
2020-11-20 18:07:38 +01:00
}
})
}
// 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-28 00:24:31 +01:00
URLs []string
2020-11-20 18:07:38 +01:00
}
// Load templates
c.Templates = NewTemplates()
c.Templates.Funcs(template.FuncMap{
"site": func() Site {
return Site{
Title: c.Title,
2020-11-28 00:24:31 +01:00
URLs: c.URLs,
2020-11-20 18:07:38 +01:00
}
},
})
2020-11-22 21:14:50 +01:00
c.Templates.LoadDefault()
2020-11-20 18:07:38 +01:00
return c.Templates.Load(path)
}