mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 09:23:09 +00:00
75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"mime"
|
|
"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
|
|
case "mediatypes":
|
|
for _, field := range strings.Fields(value) {
|
|
mime.AddExtensionType(key, field)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// 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)
|
|
}
|