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"
|
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"
|
|
|
|
)
|
2020-11-11 00:33:45 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTemplate loads a template from the provided path and content.
|
2021-02-27 20:56:23 +00:00
|
|
|
func (t *Templates) LoadTemplate(path string, content []byte) {
|
2020-11-20 17:07:38 +00:00
|
|
|
tmpl := template.New(path)
|
|
|
|
tmpl.Funcs(t.funcs)
|
2021-02-27 20:56:23 +00:00
|
|
|
template.Must(tmpl.Parse(string(content)))
|
2020-11-20 17:07:38 +00:00
|
|
|
t.tmpls[path] = tmpl
|
|
|
|
}
|
|
|
|
|
2021-05-01 18:11:41 +00:00
|
|
|
func (t *Templates) LoadHTMLTemplate(path string, content []byte) {
|
|
|
|
tmpl := htemplate.New(path)
|
|
|
|
tmpl.Funcs(htemplate.FuncMap(t.funcs))
|
|
|
|
htemplate.Must(tmpl.Parse(string(content)))
|
|
|
|
t.tmpls[path] = tmpl
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:07:38 +00:00
|
|
|
// Load loads templates from the provided directory
|
|
|
|
func (t *Templates) Load(dir string) error {
|
|
|
|
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove directory from beginning of path
|
|
|
|
path = strings.TrimPrefix(path, dir)
|
2021-05-01 18:11:41 +00:00
|
|
|
if pathpkg.Ext(path) == ".html" {
|
|
|
|
t.LoadHTMLTemplate(path, b)
|
|
|
|
} else {
|
|
|
|
t.LoadTemplate(path, b)
|
|
|
|
}
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
}
|
2021-03-21 03:17:58 +00:00
|
|
|
if t, ok := t.tmpls[pathpkg.Join("/_default", tmpl)]; ok {
|
2021-04-21 18:39:13 +00:00
|
|
|
return t, true
|
2021-03-21 03:17:58 +00:00
|
|
|
}
|
2021-04-21 18:39:13 +00:00
|
|
|
// Failed to find template
|
|
|
|
return nil, false
|
2020-11-20 17:07:38 +00:00
|
|
|
}
|
2021-05-01 17:56:41 +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) {
|
2021-05-01 17:56:41 +00:00
|
|
|
if t, ok := t.tmpls[pathpkg.Join("/_partials", name)]; ok {
|
|
|
|
return t, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|