2020-09-29 22:42:27 +02:00
|
|
|
package main
|
|
|
|
|
2020-11-20 18:07:38 +01:00
|
|
|
import (
|
2021-05-14 06:17:10 +02:00
|
|
|
"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-12 09:35:30 +02:00
|
|
|
"text/template/parse"
|
2020-11-20 18:07:38 +01:00
|
|
|
)
|
2020-11-11 01:33:45 +01:00
|
|
|
|
2021-05-01 20:11:41 +02:00
|
|
|
// Template represents a template.
|
|
|
|
type Template interface {
|
2021-05-12 09:35:30 +02:00
|
|
|
AddParseTree(*parse.Tree) error
|
2021-05-01 20:11:41 +02:00
|
|
|
Execute(io.Writer, interface{}) error
|
2021-05-12 09:35:30 +02:00
|
|
|
Tree() *parse.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
type textTemplate struct {
|
|
|
|
tmpl *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t textTemplate) AddParseTree(tree *parse.Tree) error {
|
|
|
|
_, err := t.tmpl.AddParseTree(t.tmpl.Name(), tree)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t textTemplate) Execute(w io.Writer, data interface{}) error {
|
|
|
|
return t.tmpl.Execute(w, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t textTemplate) Tree() *parse.Tree {
|
|
|
|
return t.tmpl.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
type htmlTemplate struct {
|
|
|
|
tmpl *htemplate.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t htmlTemplate) AddParseTree(tree *parse.Tree) error {
|
|
|
|
_, err := t.tmpl.AddParseTree(t.tmpl.Name(), tree)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t htmlTemplate) Execute(w io.Writer, data interface{}) error {
|
|
|
|
return t.tmpl.Execute(w, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t htmlTemplate) Tree() *parse.Tree {
|
|
|
|
return t.tmpl.Tree
|
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 {
|
2021-05-14 06:17:10 +02:00
|
|
|
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 {
|
2021-05-14 05:56:52 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-05-10 01:28:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:35:30 +02:00
|
|
|
// 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
|
|
|
}
|
2021-05-12 09:35:30 +02:00
|
|
|
base := pathpkg.Join(pathpkg.Dir(path), "base"+ext)
|
|
|
|
if tmpl, ok := t.tmpls[base]; ok {
|
|
|
|
err := t.tmpls[path].AddParseTree(tmpl.Tree())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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-03-21 04:17:58 +01:00
|
|
|
}
|
2021-04-21 20:39:13 +02:00
|
|
|
// Failed to find template
|
|
|
|
return nil, false
|
2020-11-20 18:07:38 +01:00
|
|
|
}
|
2021-05-01 19:56:41 +02: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 {
|
2021-05-01 19:56:41 +02:00
|
|
|
return t, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-05-14 06:17:10 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|