1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-12-04 23:18:14 +01:00
kiln/templates.go
adnano 8064684827 Revert "Use text/template for Atom feed template"
This reverts commit ce0b2cff1ec980995baf47e6727375cf7dc8da53.
2021-05-10 00:56:27 -04:00

162 lines
3.7 KiB
Go

package main
import (
htemplate "html/template"
"io"
"io/fs"
"io/ioutil"
"os"
pathpkg "path"
"path/filepath"
"strings"
"text/template"
)
// Template represents a template.
type Template interface {
Execute(io.Writer, interface{}) error
}
// Templates contains site templates.
type Templates struct {
tmpls map[string]Template
funcs map[string]interface{}
}
// NewTemplates returns a new Templates with the default templates.
func NewTemplates() *Templates {
t := &Templates{
tmpls: map[string]Template{},
}
return t
}
// Funcs sets the functions available to newly created templates.
func (t *Templates) Funcs(funcs map[string]interface{}) {
t.funcs = funcs
}
// 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...)
}
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
}
// 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 {
if err != nil {
return err
}
if d.IsDir() {
name := d.Name()
if strings.HasPrefix(name, "_") && name != "_default" {
return fs.SkipDir
}
// Load atom.xml template
atom := pathpkg.Join(path, "atom.xml")
if _, err := os.Stat(atom); err == nil {
name := strings.TrimPrefix(atom, dir)
t.loadHTMLTemplate(name, atom)
}
// 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)
}
return nil
})
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// FindTemplate returns the template for the given path.
func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
tmplPath := pathpkg.Join(path, tmpl)
if t, ok := t.tmpls[tmplPath]; ok {
return t, true
}
if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok {
return t, true
}
// Failed to find template
return nil, false
}
// FindPartial returns the partial template of the given name.
func (t *Templates) FindPartial(name string) (Template, bool) {
if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok {
return t, true
}
return nil, false
}