2017-03-27 14:43:49 -04:00
|
|
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
|
2017-02-17 07:30:50 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tplimpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2017-04-02 08:20:34 -04:00
|
|
|
"strings"
|
2017-03-27 14:43:49 -04:00
|
|
|
texttemplate "text/template"
|
|
|
|
|
|
|
|
"github.com/eknkc/amber"
|
|
|
|
|
|
|
|
"os"
|
2017-04-02 08:20:34 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
"github.com/spf13/hugo/output"
|
|
|
|
|
|
|
|
"path/filepath"
|
2017-02-17 07:30:50 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/hugo/deps"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2017-03-27 14:43:49 -04:00
|
|
|
"github.com/spf13/hugo/tpl"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
textTmplNamePrefix = "_text/"
|
2017-02-17 07:30:50 -05:00
|
|
|
)
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
var (
|
|
|
|
_ tpl.TemplateHandler = (*templateHandler)(nil)
|
|
|
|
_ tpl.TemplateTestMocker = (*templateHandler)(nil)
|
|
|
|
_ tpl.TemplateFinder = (*htmlTemplates)(nil)
|
|
|
|
_ tpl.TemplateFinder = (*textTemplates)(nil)
|
|
|
|
_ templateLoader = (*htmlTemplates)(nil)
|
|
|
|
_ templateLoader = (*textTemplates)(nil)
|
|
|
|
_ templateLoader = (*templateHandler)(nil)
|
|
|
|
_ templateFuncsterTemplater = (*htmlTemplates)(nil)
|
|
|
|
_ templateFuncsterTemplater = (*textTemplates)(nil)
|
|
|
|
)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
|
|
|
// Protecting global map access (Amber)
|
|
|
|
var amberMu sync.Mutex
|
|
|
|
|
|
|
|
type templateErr struct {
|
|
|
|
name string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
type templateLoader interface {
|
|
|
|
handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (string, error)) error
|
|
|
|
addTemplate(name, tpl string) error
|
|
|
|
addLateTemplate(name, tpl string) error
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
type templateFuncsterTemplater interface {
|
|
|
|
tpl.TemplateFinder
|
|
|
|
setFuncs(funcMap map[string]interface{})
|
|
|
|
setTemplateFuncster(f *templateFuncster)
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// templateHandler holds the templates in play.
|
|
|
|
// It implements the templateLoader and tpl.TemplateHandler interfaces.
|
|
|
|
type templateHandler struct {
|
|
|
|
// text holds all the pure text templates.
|
|
|
|
text *textTemplates
|
|
|
|
html *htmlTemplates
|
2017-04-02 08:20:34 -04:00
|
|
|
|
|
|
|
amberFuncMap template.FuncMap
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
errors []*templateErr
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
*deps.Deps
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateHandler) addError(name string, err error) {
|
|
|
|
t.errors = append(t.errors, &templateErr{name, err})
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// PrintErrors prints the accumulated errors as ERROR to the log.
|
|
|
|
func (t *templateHandler) PrintErrors() {
|
|
|
|
for _, e := range t.errors {
|
|
|
|
t.Log.ERROR.Println(e.name, ":", e.err)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// Lookup tries to find a template with the given name in both template
|
|
|
|
// collections: First HTML, then the plain text template collection.
|
|
|
|
func (t *templateHandler) Lookup(name string) *tpl.TemplateAdapter {
|
2017-04-02 08:20:34 -04:00
|
|
|
|
2017-04-15 05:33:53 -04:00
|
|
|
if strings.HasPrefix(name, textTmplNamePrefix) {
|
|
|
|
// The caller has explicitly asked for a text template, so only look
|
|
|
|
// in the text template collection.
|
2017-03-27 14:43:49 -04:00
|
|
|
// The templates are stored without the prefix identificator.
|
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
2017-04-15 05:33:53 -04:00
|
|
|
return t.text.Lookup(name)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-04-15 05:33:53 -04:00
|
|
|
// Look in both
|
|
|
|
if te := t.html.Lookup(name); te != nil {
|
|
|
|
return te
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-04-02 08:20:34 -04:00
|
|
|
|
2017-04-15 05:33:53 -04:00
|
|
|
return t.text.Lookup(name)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateHandler) clone(d *deps.Deps) *templateHandler {
|
|
|
|
c := &templateHandler{
|
|
|
|
Deps: d,
|
|
|
|
html: &htmlTemplates{t: template.Must(t.html.t.Clone()), overlays: make(map[string]*template.Template)},
|
|
|
|
text: &textTemplates{t: texttemplate.Must(t.text.t.Clone()), overlays: make(map[string]*texttemplate.Template)},
|
|
|
|
errors: make([]*templateErr, 0),
|
2017-04-02 08:20:34 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
d.Tmpl = c
|
2017-04-02 08:20:34 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
c.initFuncs()
|
|
|
|
|
|
|
|
for k, v := range t.html.overlays {
|
2017-02-17 07:30:50 -05:00
|
|
|
vc := template.Must(v.Clone())
|
|
|
|
// The extra lookup is a workaround, see
|
|
|
|
// * https://github.com/golang/go/issues/16101
|
|
|
|
// * https://github.com/spf13/hugo/issues/2549
|
|
|
|
vc = vc.Lookup(vc.Name())
|
2017-03-27 14:43:49 -04:00
|
|
|
vc.Funcs(c.html.funcster.funcMap)
|
|
|
|
c.html.overlays[k] = vc
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range t.text.overlays {
|
|
|
|
vc := texttemplate.Must(v.Clone())
|
|
|
|
vc = vc.Lookup(vc.Name())
|
|
|
|
vc.Funcs(texttemplate.FuncMap(c.text.funcster.funcMap))
|
|
|
|
c.text.overlays[k] = vc
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
return c
|
2017-02-17 07:30:50 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func newTemplateAdapter(deps *deps.Deps) *templateHandler {
|
|
|
|
htmlT := &htmlTemplates{
|
|
|
|
t: template.New(""),
|
|
|
|
overlays: make(map[string]*template.Template),
|
|
|
|
}
|
|
|
|
textT := &textTemplates{
|
|
|
|
t: texttemplate.New(""),
|
|
|
|
overlays: make(map[string]*texttemplate.Template),
|
|
|
|
}
|
|
|
|
return &templateHandler{
|
|
|
|
Deps: deps,
|
|
|
|
html: htmlT,
|
|
|
|
text: textT,
|
|
|
|
errors: make([]*templateErr, 0),
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
type htmlTemplates struct {
|
|
|
|
funcster *templateFuncster
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
t *template.Template
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// This looks, and is, strange.
|
|
|
|
// The clone is used by non-renderable content pages, and these need to be
|
|
|
|
// re-parsed on content change, and to avoid the
|
|
|
|
// "cannot Parse after Execute" error, we need to re-clone it from the original clone.
|
|
|
|
clone *template.Template
|
|
|
|
cloneClone *template.Template
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// a separate storage for the overlays created from cloned master templates.
|
|
|
|
// note: No mutex protection, so we add these in one Go routine, then just read.
|
|
|
|
overlays map[string]*template.Template
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) setTemplateFuncster(f *templateFuncster) {
|
|
|
|
t.funcster = f
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) Lookup(name string) *tpl.TemplateAdapter {
|
|
|
|
templ := t.lookup(name)
|
|
|
|
if templ == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &tpl.TemplateAdapter{Template: templ}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) lookup(name string) *template.Template {
|
|
|
|
if templ := t.t.Lookup(name); templ != nil {
|
|
|
|
return templ
|
|
|
|
}
|
|
|
|
if t.overlays != nil {
|
|
|
|
if templ, ok := t.overlays[name]; ok {
|
|
|
|
return templ
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
if t.clone != nil {
|
|
|
|
return t.clone.Lookup(name)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
return nil
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
type textTemplates struct {
|
|
|
|
funcster *templateFuncster
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
t *texttemplate.Template
|
|
|
|
|
|
|
|
clone *texttemplate.Template
|
|
|
|
cloneClone *texttemplate.Template
|
|
|
|
|
|
|
|
overlays map[string]*texttemplate.Template
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *textTemplates) setTemplateFuncster(f *templateFuncster) {
|
|
|
|
t.funcster = f
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *textTemplates) Lookup(name string) *tpl.TemplateAdapter {
|
|
|
|
templ := t.lookup(name)
|
|
|
|
if templ == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &tpl.TemplateAdapter{Template: templ}
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *textTemplates) lookup(name string) *texttemplate.Template {
|
|
|
|
if templ := t.t.Lookup(name); templ != nil {
|
2017-02-17 07:30:50 -05:00
|
|
|
return templ
|
|
|
|
}
|
|
|
|
if t.overlays != nil {
|
|
|
|
if templ, ok := t.overlays[name]; ok {
|
|
|
|
return templ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.clone != nil {
|
2017-03-27 14:43:49 -04:00
|
|
|
return t.clone.Lookup(name)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateHandler) setFuncs(funcMap map[string]interface{}) {
|
|
|
|
t.html.setFuncs(funcMap)
|
|
|
|
t.text.setFuncs(funcMap)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// SetFuncs replaces the funcs in the func maps with new definitions.
|
|
|
|
// This is only used in tests.
|
|
|
|
func (t *templateHandler) SetFuncs(funcMap map[string]interface{}) {
|
|
|
|
t.setFuncs(funcMap)
|
2017-02-21 07:55:08 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) setFuncs(funcMap map[string]interface{}) {
|
|
|
|
t.t.Funcs(funcMap)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *textTemplates) setFuncs(funcMap map[string]interface{}) {
|
|
|
|
t.t.Funcs(funcMap)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// LoadTemplates loads the templates, starting from the given absolute path.
|
|
|
|
// A prefix can be given to indicate a template namespace to load the templates
|
|
|
|
// into, i.e. "_internal" etc.
|
|
|
|
func (t *templateHandler) LoadTemplates(absPath, prefix string) {
|
2017-04-04 08:10:20 -04:00
|
|
|
t.loadTemplates(absPath, prefix)
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-04-02 08:20:34 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) addTemplateIn(tt *template.Template, name, tpl string) error {
|
|
|
|
templ, err := tt.New(name).Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
if err := applyTemplateTransformersToHMLTTemplate(templ); err != nil {
|
|
|
|
return err
|
2017-04-02 08:20:34 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
return nil
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) addTemplate(name, tpl string) error {
|
|
|
|
return t.addTemplateIn(t.t, name, tpl)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *htmlTemplates) addLateTemplate(name, tpl string) error {
|
|
|
|
return t.addTemplateIn(t.clone, name, tpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textTemplates) addTemplateIn(tt *texttemplate.Template, name, tpl string) error {
|
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
|
|
|
templ, err := tt.New(name).Parse(tpl)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
if err := applyTemplateTransformersToTextTemplate(templ); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textTemplates) addTemplate(name, tpl string) error {
|
|
|
|
return t.addTemplateIn(t.t, name, tpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *textTemplates) addLateTemplate(name, tpl string) error {
|
|
|
|
return t.addTemplateIn(t.clone, name, tpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) addTemplate(name, tpl string) error {
|
|
|
|
return t.AddTemplate(name, tpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) addLateTemplate(name, tpl string) error {
|
|
|
|
return t.AddLateTemplate(name, tpl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLateTemplate is used to add a template late, i.e. after the
|
|
|
|
// regular templates have started its execution.
|
|
|
|
func (t *templateHandler) AddLateTemplate(name, tpl string) error {
|
|
|
|
h := t.getTemplateHandler(name)
|
|
|
|
if err := h.addLateTemplate(name, tpl); err != nil {
|
|
|
|
t.addError(name, err)
|
2017-02-17 07:30:50 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// AddTemplate parses and adds a template to the collection.
|
|
|
|
// Templates with name prefixed with "_text" will be handled as plain
|
|
|
|
// text templates.
|
|
|
|
func (t *templateHandler) AddTemplate(name, tpl string) error {
|
|
|
|
h := t.getTemplateHandler(name)
|
|
|
|
if err := h.addTemplate(name, tpl); err != nil {
|
|
|
|
t.addError(name, err)
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// MarkReady marks the templates as "ready for execution". No changes allowed
|
|
|
|
// after this is set.
|
|
|
|
// TODO(bep) if this proves to be resource heavy, we could detect
|
|
|
|
// earlier if we really need this, or make it lazy.
|
|
|
|
func (t *templateHandler) MarkReady() {
|
|
|
|
if t.html.clone == nil {
|
|
|
|
t.html.clone = template.Must(t.html.t.Clone())
|
|
|
|
t.html.cloneClone = template.Must(t.html.clone.Clone())
|
|
|
|
}
|
|
|
|
if t.text.clone == nil {
|
|
|
|
t.text.clone = texttemplate.Must(t.text.t.Clone())
|
|
|
|
t.text.cloneClone = texttemplate.Must(t.text.clone.Clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RebuildClone rebuilds the cloned templates. Used for live-reloads.
|
|
|
|
func (t *templateHandler) RebuildClone() {
|
|
|
|
t.html.clone = template.Must(t.html.cloneClone.Clone())
|
|
|
|
t.text.clone = texttemplate.Must(t.text.cloneClone.Clone())
|
|
|
|
}
|
|
|
|
|
2017-04-04 08:10:20 -04:00
|
|
|
func (t *templateHandler) loadTemplates(absPath string, prefix string) {
|
2017-03-27 14:43:49 -04:00
|
|
|
t.Log.DEBUG.Printf("Load templates from path %q prefix %q", absPath, prefix)
|
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log.DEBUG.Println("Template path", path)
|
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
link, err := filepath.EvalSymlinks(absPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Log.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", absPath, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
linkfi, err := t.Fs.Source.Stat(link)
|
|
|
|
if err != nil {
|
|
|
|
t.Log.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !linkfi.Mode().IsRegular() {
|
|
|
|
t.Log.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", absPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
workingDir = t.PathSpec.WorkingDir()
|
|
|
|
themeDir = t.PathSpec.GetThemeDir()
|
|
|
|
layoutDir = t.PathSpec.LayoutDir()
|
|
|
|
)
|
|
|
|
|
|
|
|
if themeDir != "" && strings.HasPrefix(absPath, themeDir) {
|
|
|
|
workingDir = themeDir
|
|
|
|
layoutDir = "layouts"
|
|
|
|
}
|
|
|
|
|
|
|
|
li := strings.LastIndex(path, layoutDir) + len(layoutDir) + 1
|
|
|
|
relPath := path[li:]
|
2017-04-12 14:40:36 -04:00
|
|
|
templateDir := path[:li-len(layoutDir)-1]
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
descriptor := output.TemplateLookupDescriptor{
|
2017-04-12 14:40:36 -04:00
|
|
|
TemplateDir: templateDir,
|
2017-03-27 14:43:49 -04:00
|
|
|
WorkingDir: workingDir,
|
|
|
|
LayoutDir: layoutDir,
|
|
|
|
RelPath: relPath,
|
|
|
|
Prefix: prefix,
|
2017-04-12 14:40:36 -04:00
|
|
|
ThemeDir: themeDir,
|
2017-04-04 08:10:20 -04:00
|
|
|
OutputFormats: t.OutputFormatsConfig,
|
2017-03-27 14:43:49 -04:00
|
|
|
FileExists: func(filename string) (bool, error) {
|
|
|
|
return helpers.Exists(filename, t.Fs.Source)
|
|
|
|
},
|
|
|
|
ContainsAny: func(filename string, subslices [][]byte) (bool, error) {
|
|
|
|
return helpers.FileContainsAny(filename, subslices, t.Fs.Source)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tplID, err := output.CreateTemplateNames(descriptor)
|
|
|
|
if err != nil {
|
|
|
|
t.Log.ERROR.Printf("Failed to resolve template in path %q: %s", path, err)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.addTemplateFile(tplID.Name, tplID.MasterFilename, tplID.OverlayFilename); err != nil {
|
|
|
|
t.Log.ERROR.Printf("Failed to add template %q in path %q: %s", tplID.Name, path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := helpers.SymbolicWalk(t.Fs.Source, absPath, walker); err != nil {
|
|
|
|
t.Log.ERROR.Printf("Failed to load templates: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) initFuncs() {
|
|
|
|
|
2017-04-15 05:33:53 -04:00
|
|
|
// Both template types will get their own funcster instance, which
|
|
|
|
// in the current case contains the same set of funcs.
|
2017-03-27 14:43:49 -04:00
|
|
|
for _, funcsterHolder := range []templateFuncsterTemplater{t.html, t.text} {
|
2017-04-15 05:33:53 -04:00
|
|
|
funcster := newTemplateFuncster(t.Deps)
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
// The URL funcs in the funcMap is somewhat language dependent,
|
|
|
|
// so we need to wait until the language and site config is loaded.
|
|
|
|
funcster.initFuncMap()
|
|
|
|
|
|
|
|
funcsterHolder.setTemplateFuncster(funcster)
|
|
|
|
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// Amber is HTML only.
|
|
|
|
t.amberFuncMap = template.FuncMap{}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
amberMu.Lock()
|
|
|
|
for k, v := range amber.FuncMap {
|
|
|
|
t.amberFuncMap[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range t.html.funcster.funcMap {
|
|
|
|
t.amberFuncMap[k] = v
|
|
|
|
// Hacky, but we need to make sure that the func names are in the global map.
|
|
|
|
amber.FuncMap[k] = func() string {
|
|
|
|
panic("should never be invoked")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
amberMu.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) getTemplateHandler(name string) templateLoader {
|
|
|
|
if strings.HasPrefix(name, textTmplNamePrefix) {
|
|
|
|
return t.text
|
|
|
|
}
|
|
|
|
return t.html
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (string, error)) error {
|
|
|
|
h := t.getTemplateHandler(name)
|
|
|
|
return h.handleMaster(name, overlayFilename, masterFilename, onMissing)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *htmlTemplates) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (string, error)) error {
|
|
|
|
masterTpl := t.lookup(masterFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
|
|
|
if masterTpl == nil {
|
2017-03-27 14:43:49 -04:00
|
|
|
templ, err := onMissing(masterFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
masterTpl, err = t.t.New(overlayFilename).Parse(templ)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
templ, err := onMissing(overlayFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
overlayTpl, err := template.Must(masterTpl.Clone()).Parse(templ)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
2017-03-27 14:43:49 -04:00
|
|
|
return err
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
// The extra lookup is a workaround, see
|
|
|
|
// * https://github.com/golang/go/issues/16101
|
|
|
|
// * https://github.com/spf13/hugo/issues/2549
|
|
|
|
overlayTpl = overlayTpl.Lookup(overlayTpl.Name())
|
|
|
|
if err := applyTemplateTransformersToHMLTTemplate(overlayTpl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.overlays[name] = overlayTpl
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
return err
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *textTemplates) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (string, error)) error {
|
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
|
|
|
masterTpl := t.lookup(masterFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
if masterTpl == nil {
|
|
|
|
templ, err := onMissing(masterFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
masterTpl, err = t.t.New(overlayFilename).Parse(templ)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
templ, err := onMissing(overlayFilename)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
overlayTpl, err := texttemplate.Must(masterTpl.Clone()).Parse(templ)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
overlayTpl = overlayTpl.Lookup(overlayTpl.Name())
|
|
|
|
if err := applyTemplateTransformersToTextTemplate(overlayTpl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.overlays[name] = overlayTpl
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) error {
|
2017-02-17 07:30:50 -05:00
|
|
|
t.checkState()
|
2017-03-27 14:43:49 -04:00
|
|
|
|
|
|
|
getTemplate := func(filename string) (string, error) {
|
|
|
|
b, err := afero.ReadFile(t.Fs.Source, filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
// get the suffix and switch on that
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
switch ext {
|
|
|
|
case ".amber":
|
2017-03-27 14:43:49 -04:00
|
|
|
// Only HTML support for Amber
|
2017-02-17 07:30:50 -05:00
|
|
|
templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
|
|
|
|
b, err := afero.ReadFile(t.Fs.Source, path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
amberMu.Lock()
|
2017-03-27 14:43:49 -04:00
|
|
|
templ, err := t.compileAmberWithTemplate(b, path, t.html.t.New(templateName))
|
2017-02-17 07:30:50 -05:00
|
|
|
amberMu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
return applyTemplateTransformersToHMLTTemplate(templ)
|
2017-02-17 07:30:50 -05:00
|
|
|
case ".ace":
|
2017-03-27 14:43:49 -04:00
|
|
|
// Only HTML support for Ace
|
2017-02-17 07:30:50 -05:00
|
|
|
var innerContent, baseContent []byte
|
|
|
|
innerContent, err := afero.ReadFile(t.Fs.Source, path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
|
|
|
baseContent, err = afero.ReadFile(t.Fs.Source, baseTemplatePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
return t.addAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
|
2017-02-17 07:30:50 -05:00
|
|
|
default:
|
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
2017-03-27 14:43:49 -04:00
|
|
|
return t.handleMaster(name, path, baseTemplatePath, getTemplate)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
templ, err := getTemplate(path)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log.DEBUG.Printf("Add template file from path %s", path)
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
return t.AddTemplate(name, templ)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) loadEmbedded() {
|
|
|
|
t.embedShortcodes()
|
|
|
|
t.embedTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *templateHandler) addInternalTemplate(prefix, name, tpl string) error {
|
|
|
|
if prefix != "" {
|
|
|
|
return t.AddTemplate("_internal/"+prefix+"/"+name, tpl)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
return t.AddTemplate("_internal/"+name, tpl)
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateHandler) addInternalShortcode(name, content string) error {
|
|
|
|
return t.addInternalTemplate("shortcodes", name, content)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateHandler) checkState() {
|
|
|
|
if t.html.clone != nil || t.text.clone != nil {
|
|
|
|
panic("template is cloned and cannot be modfified")
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func isDotFile(path string) bool {
|
|
|
|
return filepath.Base(path)[0] == '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
func isBackupFile(path string) bool {
|
|
|
|
return path[len(path)-1] == '~'
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseFileBase = "baseof"
|
|
|
|
|
|
|
|
func isBaseTemplate(path string) bool {
|
|
|
|
return strings.Contains(path, baseFileBase)
|
|
|
|
}
|