1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 10:09:18 +01:00
kiln/templates.go

203 lines
4.6 KiB
Go
Raw Normal View History

2020-09-29 22:42:27 +02:00
package main
2020-11-20 18:07:38 +01:00
import (
"fmt"
2021-05-01 20:11:41 +02:00
htemplate "html/template"
"io"
2021-05-10 01:28:09 +02:00
"io/fs"
2020-11-20 18:07:38 +01:00
"os"
2020-11-22 21:51:07 +01:00
pathpkg "path"
2020-11-20 18:07:38 +01:00
"strings"
"text/template"
)
2021-05-01 20:11:41 +02:00
// Template represents a template.
type Template interface {
Clone() (Template, error)
AddTemplates(Template) error
2021-05-01 20:11:41 +02:00
Execute(io.Writer, interface{}) error
}
type textTemplate struct {
tmpl *template.Template
}
func (t textTemplate) Clone() (Template, error) {
clone, err := t.tmpl.Clone()
return textTemplate{clone}, err
}
func (t textTemplate) AddTemplates(other Template) error {
otherTmpl := other.(textTemplate).tmpl
for _, def := range otherTmpl.Templates() {
if def.Name() == otherTmpl.Name() {
continue
}
_, err := t.tmpl.AddParseTree(def.Name(), def.Tree)
if err != nil {
return err
}
}
return nil
}
func (t textTemplate) Execute(w io.Writer, data interface{}) error {
return t.tmpl.Execute(w, data)
}
type htmlTemplate struct {
tmpl *htemplate.Template
}
func (t htmlTemplate) Clone() (Template, error) {
clone, err := t.tmpl.Clone()
return htmlTemplate{clone}, err
}
func (t htmlTemplate) AddTemplates(other Template) error {
otherTmpl := other.(htmlTemplate).tmpl
for _, def := range otherTmpl.Templates() {
if def.Name() == otherTmpl.Name() {
continue
}
_, err := t.tmpl.AddParseTree(def.Name(), def.Tree)
if err != nil {
return err
}
}
return nil
}
func (t htmlTemplate) Execute(w io.Writer, data interface{}) error {
return t.tmpl.Execute(w, data)
2021-05-01 20:11:41 +02:00
}
2020-11-20 18:07:38 +01:00
// Templates contains site templates.
type Templates struct {
2021-05-01 20:11:41 +02:00
tmpls map[string]Template
funcs map[string]interface{}
2020-11-20 18:07:38 +01:00
}
// Funcs sets the functions available to newly created templates.
2021-05-01 20:11:41 +02:00
func (t *Templates) Funcs(funcs map[string]interface{}) {
2020-11-20 18:07:38 +01:00
t.funcs = funcs
}
2021-05-10 01:28:09 +02:00
// LoadTemplate loads a template from the provided filenames.
2022-02-09 19:18:11 +01:00
func (t *Templates) LoadTemplate(fsys fs.FS, path string) error {
if t.tmpls == nil {
t.tmpls = map[string]Template{}
}
2022-02-09 19:18:11 +01:00
if ext := pathpkg.Ext(path); ext == ".html" || ext == ".xml" {
return t.loadHTMLTemplate(fsys, path)
2021-05-10 01:28:09 +02:00
}
2022-02-09 19:18:11 +01:00
return t.loadTextTemplate(fsys, path)
2020-11-20 18:07:38 +01:00
}
2022-02-09 19:18:11 +01:00
func (t *Templates) loadTextTemplate(fsys fs.FS, path string) error {
tmpl := template.New(path).Funcs(t.funcs)
b, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
2021-05-10 01:28:09 +02:00
}
2022-02-09 19:18:11 +01:00
t.tmpls[path] = textTemplate{tmpl}
2021-05-10 01:28:09 +02:00
return nil
}
2022-02-09 19:18:11 +01:00
func (t *Templates) loadHTMLTemplate(fsys fs.FS, path string) error {
tmpl := htemplate.New(path).Funcs(t.funcs)
b, err := fs.ReadFile(fsys, path)
if err != nil {
return err
}
if _, err := tmpl.Parse(string(b)); err != nil {
return err
2021-05-10 01:28:09 +02:00
}
2022-02-09 19:18:11 +01:00
t.tmpls[path] = htmlTemplate{tmpl}
2021-05-10 01:28:09 +02:00
return nil
2021-05-01 20:11:41 +02:00
}
2021-05-10 01:28:09 +02:00
// Load loads templates from the provided directory.
func (t *Templates) Load(dir string, exts []string) error {
2022-02-09 19:18:11 +01:00
fsys := os.DirFS(dir)
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
2020-11-20 18:07:38 +01:00
if err != nil {
return err
}
2022-09-25 15:43:17 +02:00
if d.Type().IsRegular() || d.Type()&fs.ModeSymlink != 0 {
2022-02-09 19:18:11 +01:00
if err := t.LoadTemplate(fsys, path); err != nil {
return err
}
2021-05-10 01:28:09 +02:00
}
return nil
})
if err != nil && !os.IsNotExist(err) {
return err
}
// Add base templates
var extsMap = map[string]struct{}{}
for _, ext := range exts {
extsMap[ext] = struct{}{}
}
for path := range t.tmpls {
ext := pathpkg.Ext(path)
if _, ok := extsMap[ext]; !ok {
continue
2021-05-10 01:28:09 +02:00
}
basePath := pathpkg.Join(pathpkg.Dir(path), "base"+ext)
if path == basePath {
continue
}
if base, ok := t.tmpls[basePath]; ok {
tmpl, err := base.Clone()
if err != nil {
return err
}
// Load customized template definitions
if err := tmpl.AddTemplates(t.tmpls[path]); err != nil {
return err
}
t.tmpls[path] = tmpl
2020-11-20 18:07:38 +01:00
}
2021-05-10 01:28:09 +02:00
}
return nil
2020-11-20 18:07:38 +01:00
}
2020-09-29 22:42:27 +02:00
2021-04-21 20:39:13 +02:00
// FindTemplate returns the template for the given path.
2021-05-01 20:11:41 +02:00
func (t *Templates) FindTemplate(path string, tmpl string) (Template, bool) {
2020-11-22 21:51:07 +01:00
tmplPath := pathpkg.Join(path, tmpl)
if t, ok := t.tmpls[tmplPath]; ok {
2021-04-21 20:39:13 +02:00
return t, true
2020-11-20 18:07:38 +01:00
}
2022-02-09 19:18:11 +01:00
if t, ok := t.tmpls[pathpkg.Join("_default", tmpl)]; ok {
2021-04-21 20:39:13 +02:00
return t, true
}
2021-04-21 20:39:13 +02:00
// Failed to find template
return nil, false
2020-11-20 18:07:38 +01:00
}
// FindPartial returns the partial template of the given name.
2021-05-01 20:11:41 +02:00
func (t *Templates) FindPartial(name string) (Template, bool) {
2022-02-09 19:18:11 +01:00
if t, ok := t.tmpls[pathpkg.Join("_partials", name)]; ok {
return t, true
}
return nil, false
}
// ExecutePartial executes the partial with the given name.
func (t *Templates) ExecutePartial(name string, data interface{}) (string, error) {
tmpl, ok := t.FindPartial(name)
if !ok {
return "", fmt.Errorf("Error: partial %q not found", name)
}
var b strings.Builder
if err := tmpl.Execute(&b, data); err != nil {
return "", err
}
return b.String(), nil
}