kiln/templates.go

162 lines
3.7 KiB
Go
Raw Normal View History

2020-09-29 20:42:27 +00:00
package main
2020-11-20 17:07:38 +00:00
import (
2021-05-01 18:11:41 +00:00
htemplate "html/template"
"io"
2021-05-09 23:28:09 +00:00
"io/fs"
2020-11-20 17:07:38 +00:00
"io/ioutil"
"os"
2020-11-22 20:51:07 +00:00
pathpkg "path"
2020-11-20 17:07:38 +00:00
"path/filepath"
"strings"
"text/template"
)
2021-05-01 18:11:41 +00:00
// Template represents a template.
type Template interface {
Execute(io.Writer, interface{}) error
}
2020-11-20 17:07:38 +00:00
// Templates contains site templates.
type Templates struct {
2021-05-01 18:11:41 +00:00
tmpls map[string]Template
funcs map[string]interface{}
2020-11-20 17:07:38 +00:00
}
// NewTemplates returns a new Templates with the default templates.
func NewTemplates() *Templates {
t := &Templates{
2021-05-01 18:11:41 +00:00
tmpls: map[string]Template{},
2020-11-20 17:07:38 +00:00
}
return t
}
// Funcs sets the functions available to newly created templates.
2021-05-01 18:11:41 +00:00
func (t *Templates) Funcs(funcs map[string]interface{}) {
2020-11-20 17:07:38 +00:00
t.funcs = funcs
}
2021-05-09 23:28:09 +00:00
// LoadTemplate loads a template from the provided filenames.
func (t *Templates) LoadTemplate(name string, filenames ...string) error {
if pathpkg.Ext(name) == ".html" {
return t.loadHTMLTemplate(name, filenames...)
}
return t.loadTextTemplate(name, filenames...)
2020-11-20 17:07:38 +00:00
}
2021-05-09 23:28:09 +00:00
func (t *Templates) loadTextTemplate(name string, filenames ...string) error {
tmpl := template.New(name).Funcs(t.funcs)
for i := range filenames {
b, err := ioutil.ReadFile(filenames[i])
if err != nil {
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
}
t.tmpls[name] = tmpl
return nil
}
func (t *Templates) loadHTMLTemplate(name string, filenames ...string) error {
tmpl := htemplate.New(name).Funcs(t.funcs)
for i := range filenames {
b, err := ioutil.ReadFile(filenames[i])
if err != nil {
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
}
}
t.tmpls[name] = tmpl
return nil
2021-05-01 18:11:41 +00:00
}
2021-05-09 23:28:09 +00:00
// Load loads templates from the provided directory.
func (t *Templates) Load(dir string, exts []string) error {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
2020-11-20 17:07:38 +00:00
if err != nil {
return err
}
2021-05-09 23:28:09 +00:00
if d.IsDir() {
name := d.Name()
if strings.HasPrefix(name, "_") && name != "_default" {
return fs.SkipDir
2020-11-20 17:07:38 +00:00
}
2021-05-09 23:28:09 +00:00
// Load atom.xml template
atom := pathpkg.Join(path, "atom.xml")
if _, err := os.Stat(atom); err == nil {
name := strings.TrimPrefix(atom, dir)
t.LoadTemplate(name, atom)
2021-05-01 18:11:41 +00:00
}
2021-05-09 23:28:09 +00:00
// Load page templates
for _, ext := range exts {
for _, name := range []string{"index" + ext, "page" + ext} {
filename := pathpkg.Join(path, name)
if _, err := os.Stat(filename); err != nil {
// Template does not exist
continue
}
filenames := []string{filename}
base := pathpkg.Join(path, "base"+ext)
if _, err := os.Stat(base); err == nil {
filenames = append(filenames, base)
}
name := strings.TrimPrefix(filename, dir)
if err := t.LoadTemplate(name, filenames...); err != nil {
return err
}
}
}
}
return nil
})
if err != nil && !os.IsNotExist(err) {
return err
}
// Load partial templates
partials := pathpkg.Join(dir, "_partials")
err = filepath.WalkDir(partials, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Type().IsRegular() {
name := strings.TrimPrefix(path, dir)
t.LoadTemplate(name, path)
2020-11-20 17:07:38 +00:00
}
return nil
})
2021-05-09 23:28:09 +00:00
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
2020-11-20 17:07:38 +00:00
}
2020-09-29 20:42:27 +00:00
2021-04-21 18:39:13 +00:00
// FindTemplate returns the template for the given path.
2021-05-01 18:11:41 +00:00
func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
2020-11-22 20:51:07 +00:00
tmplPath := pathpkg.Join(path, tmpl)
if t, ok := t.tmpls[tmplPath]; ok {
2021-04-21 18:39:13 +00:00
return t, true
2020-11-20 17:07:38 +00:00
}
if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok {
2021-04-21 18:39:13 +00:00
return t, true
}
2021-04-21 18:39:13 +00:00
// Failed to find template
return nil, false
2020-11-20 17:07:38 +00:00
}
// FindPartial returns the partial template of the given name.
2021-05-01 18:11:41 +00:00
func (t *Templates) FindPartial(name string) (Template, bool) {
if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok {
return t, true
}
return nil, false
}