config: Make templates private

This commit is contained in:
adnano 2021-05-08 20:27:13 -04:00
parent 12ddad63e4
commit 13b8bb7ff3
2 changed files with 13 additions and 12 deletions

View file

@ -11,11 +11,12 @@ import (
// 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
Tasks map[string]*Task `toml:"tasks"` // site tasks
Templates *Templates `toml:"-"` // site templates
Title string
URLs []string
Feeds map[string]string
Tasks map[string]*Task
Permalinks map[string]string
templates *Templates
}
// Task represents a site build task.
@ -58,8 +59,8 @@ func (c *Config) LoadTemplates(path string) error {
}
// Load templates
c.Templates = NewTemplates()
c.Templates.Funcs(map[string]interface{}{
c.templates = NewTemplates()
c.templates.Funcs(map[string]interface{}{
"site": func() Site {
return Site{
Title: c.Title,
@ -67,7 +68,7 @@ func (c *Config) LoadTemplates(path string) error {
}
},
"partial": func(name string, data interface{}) (interface{}, error) {
t, ok := c.Templates.FindPartial(name)
t, ok := c.templates.FindPartial(name)
if !ok {
return "", fmt.Errorf("Error: partial %q not found", name)
}
@ -78,5 +79,5 @@ func (c *Config) LoadTemplates(path string) error {
return b.String(), nil
},
})
return c.Templates.Load(path)
return c.templates.Load(path)
}

6
dir.go
View file

@ -92,7 +92,7 @@ func (d *Dir) Process(cfg *Config, task *Task) error {
if task.TemplateExt != "" {
// Create index
if d.index != nil {
tmpl, ok := cfg.Templates.FindTemplate(d.Path, "index"+task.TemplateExt)
tmpl, ok := cfg.templates.FindTemplate(d.Path, "index"+task.TemplateExt)
if ok {
var b strings.Builder
if err := tmpl.Execute(&b, d); err != nil {
@ -105,7 +105,7 @@ func (d *Dir) Process(cfg *Config, task *Task) error {
// Process pages
for i := range d.Pages {
var b strings.Builder
tmpl, ok := cfg.Templates.FindTemplate(d.Path, "page"+task.TemplateExt)
tmpl, ok := cfg.templates.FindTemplate(d.Path, "page"+task.TemplateExt)
if ok {
if err := tmpl.Execute(&b, d.Pages[i]); err != nil {
return err
@ -132,7 +132,7 @@ func (d *Dir) Process(cfg *Config, task *Task) error {
Updated: time.Now(),
Entries: d.Pages,
}
tmpl, ok := cfg.Templates.FindTemplate(d.Path, "atom.xml")
tmpl, ok := cfg.templates.FindTemplate(d.Path, "atom.xml")
if ok {
if err := tmpl.Execute(&b, feed); err != nil {
return err