mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"mime"
|
|
"os"
|
|
"text/template"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
)
|
|
|
|
// Config contains site configuration.
|
|
type Config struct {
|
|
Title string `toml:"title"` // site title
|
|
URLs []string `toml:"urls"` // site URLs
|
|
Feeds map[string]string `toml:"feeds"` // site feeds
|
|
Mediatypes map[string][]string `toml:"mediatypes"` // mediatypes
|
|
Templates *Templates `toml:"-"` // site templates
|
|
}
|
|
|
|
// LoadConfig loads the configuration from the provided path.
|
|
func LoadConfig(path string) (*Config, error) {
|
|
c := new(Config)
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := toml.NewDecoder(f).Decode(c); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Register media types
|
|
for typ, exts := range c.Mediatypes {
|
|
for _, ext := range exts {
|
|
mime.AddExtensionType(typ, ext)
|
|
}
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// 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)
|
|
}
|