2016-03-03 19:00:41 -05:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-11-20 12:32:21 -05:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-11-20 12:32:21 -05:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-20 12:32:21 -05:00
|
|
|
//
|
|
|
|
// 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 tpl
|
2013-08-31 20:35:17 -04:00
|
|
|
|
|
|
|
import (
|
2015-04-18 07:58:35 -04:00
|
|
|
"fmt"
|
2016-07-30 11:16:04 -04:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2015-03-26 12:22:45 -04:00
|
|
|
"github.com/eknkc/amber"
|
2016-02-09 12:39:17 -05:00
|
|
|
"github.com/spf13/afero"
|
2015-03-26 12:22:45 -04:00
|
|
|
bp "github.com/spf13/hugo/bufferpool"
|
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
"github.com/spf13/hugo/hugofs"
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
"github.com/yosssi/ace"
|
2013-08-31 20:35:17 -04:00
|
|
|
)
|
|
|
|
|
2014-06-06 16:15:19 -04:00
|
|
|
var localTemplates *template.Template
|
2014-11-20 12:32:21 -05:00
|
|
|
var tmpl Template
|
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
// TODO(bep) an interface with hundreds of methods ... remove it.
|
|
|
|
// And unexport most of these methods.
|
2014-11-20 12:32:21 -05:00
|
|
|
type Template interface {
|
|
|
|
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
|
|
|
|
Lookup(name string) *template.Template
|
|
|
|
Templates() []*template.Template
|
|
|
|
New(name string) *template.Template
|
2016-03-09 08:05:31 -05:00
|
|
|
GetClone() *template.Template
|
2014-11-20 12:32:21 -05:00
|
|
|
LoadTemplates(absPath string)
|
|
|
|
LoadTemplatesWithPrefix(absPath, prefix string)
|
2016-03-09 08:05:31 -05:00
|
|
|
MarkReady()
|
2014-11-20 12:32:21 -05:00
|
|
|
AddTemplate(name, tpl string) error
|
2016-02-09 12:39:17 -05:00
|
|
|
AddTemplateFileWithMaster(name, overlayFilename, masterFilename string) error
|
2015-05-31 07:01:20 -04:00
|
|
|
AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error
|
2014-11-20 12:32:21 -05:00
|
|
|
AddInternalTemplate(prefix, name, tpl string) error
|
|
|
|
AddInternalShortcode(name, tpl string) error
|
2015-01-30 18:56:25 -05:00
|
|
|
PrintErrors()
|
2014-11-20 12:32:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type templateErr struct {
|
|
|
|
name string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
type GoHTMLTemplate struct {
|
2014-11-20 12:32:21 -05:00
|
|
|
template.Template
|
2016-02-09 12:39:17 -05:00
|
|
|
clone *template.Template
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2014-11-20 12:32:21 -05:00
|
|
|
errors []*templateErr
|
|
|
|
}
|
|
|
|
|
2016-11-21 17:01:42 -05:00
|
|
|
// T is the "global" template system
|
2014-11-20 12:32:21 -05:00
|
|
|
func T() Template {
|
|
|
|
if tmpl == nil {
|
|
|
|
tmpl = New()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
2015-11-16 09:15:17 -05:00
|
|
|
// InitializeT resets the internal template state to its initial state
|
2014-11-24 17:10:38 -05:00
|
|
|
func InitializeT() Template {
|
|
|
|
tmpl = New()
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
2015-11-16 09:15:17 -05:00
|
|
|
// New returns a new Hugo Template System
|
|
|
|
// with all the additional features, templates & functions
|
2014-11-20 12:32:21 -05:00
|
|
|
func New() Template {
|
2015-03-11 13:34:57 -04:00
|
|
|
var templates = &GoHTMLTemplate{
|
2014-11-20 12:32:21 -05:00
|
|
|
Template: *template.New(""),
|
2016-02-09 12:39:17 -05:00
|
|
|
overlays: make(map[string]*template.Template),
|
2014-11-20 12:32:21 -05:00
|
|
|
errors: make([]*templateErr, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
localTemplates = &templates.Template
|
|
|
|
|
2016-10-24 07:45:30 -04:00
|
|
|
// The URL funcs in the funcMap is somewhat language dependent,
|
2016-10-24 08:32:31 -04:00
|
|
|
// so we need to wait until the language and site config is loaded.
|
2016-10-24 07:45:30 -04:00
|
|
|
initFuncMap()
|
|
|
|
|
2015-07-19 20:07:55 -04:00
|
|
|
for k, v := range funcMap {
|
|
|
|
amber.FuncMap[k] = v
|
|
|
|
}
|
2014-11-20 12:32:21 -05:00
|
|
|
templates.Funcs(funcMap)
|
|
|
|
templates.LoadEmbedded()
|
|
|
|
return templates
|
|
|
|
}
|
2014-06-06 16:15:19 -04:00
|
|
|
|
2016-03-24 17:56:50 -04:00
|
|
|
func partial(name string, contextList ...interface{}) template.HTML {
|
2014-06-06 16:15:19 -04:00
|
|
|
if strings.HasPrefix("partials/", name) {
|
|
|
|
name = name[8:]
|
|
|
|
}
|
2014-09-03 11:30:08 -04:00
|
|
|
var context interface{}
|
|
|
|
|
2016-03-24 17:56:50 -04:00
|
|
|
if len(contextList) == 0 {
|
2014-09-03 11:30:08 -04:00
|
|
|
context = nil
|
|
|
|
} else {
|
2016-03-24 17:56:50 -04:00
|
|
|
context = contextList[0]
|
2014-09-03 11:30:08 -04:00
|
|
|
}
|
2014-06-06 16:15:19 -04:00
|
|
|
return ExecuteTemplateToHTML(context, "partials/"+name, "theme/partials/"+name)
|
|
|
|
}
|
|
|
|
|
2016-03-03 19:00:41 -05:00
|
|
|
func executeTemplate(context interface{}, w io.Writer, layouts ...string) {
|
2016-10-12 02:26:39 -04:00
|
|
|
var worked bool
|
2014-06-06 16:15:19 -04:00
|
|
|
for _, layout := range layouts {
|
2016-10-12 02:26:39 -04:00
|
|
|
templ := Lookup(layout)
|
|
|
|
if templ == nil {
|
|
|
|
layout += ".html"
|
|
|
|
templ = Lookup(layout)
|
2014-11-01 00:15:22 -04:00
|
|
|
}
|
|
|
|
|
2016-10-12 02:26:39 -04:00
|
|
|
if templ != nil {
|
|
|
|
if err := templ.Execute(w, context); err != nil {
|
2016-10-12 04:12:15 -04:00
|
|
|
// Printing the err is spammy, see https://github.com/golang/go/issues/17414
|
|
|
|
helpers.DistinctErrorLog.Println(layout, "is an incomplete or empty template")
|
2014-10-02 13:37:38 -04:00
|
|
|
}
|
2014-06-06 16:15:19 -04:00
|
|
|
worked = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !worked {
|
|
|
|
jww.ERROR.Println("Unable to render", layouts)
|
|
|
|
jww.ERROR.Println("Expecting to find a template in either the theme/layouts or /layouts in one of the following relative locations", layouts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExecuteTemplateToHTML(context interface{}, layouts ...string) template.HTML {
|
2015-01-30 14:25:54 -05:00
|
|
|
b := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(b)
|
2016-03-03 19:00:41 -05:00
|
|
|
executeTemplate(context, b, layouts...)
|
2015-01-30 14:25:54 -05:00
|
|
|
return template.HTML(b.String())
|
2014-06-06 16:15:19 -04:00
|
|
|
}
|
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
func Lookup(name string) *template.Template {
|
|
|
|
return (tmpl.(*GoHTMLTemplate)).Lookup(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GoHTMLTemplate) Lookup(name string) *template.Template {
|
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
if templ := localTemplates.Lookup(name); templ != nil {
|
2016-03-09 08:05:31 -05:00
|
|
|
return templ
|
|
|
|
}
|
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
if t.overlays != nil {
|
|
|
|
if templ, ok := t.overlays[name]; ok {
|
|
|
|
return templ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
if t.clone != nil {
|
2016-02-09 12:39:17 -05:00
|
|
|
if templ := t.clone.Lookup(name); templ != nil {
|
|
|
|
return templ
|
|
|
|
}
|
2016-03-09 08:05:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GoHTMLTemplate) GetClone() *template.Template {
|
|
|
|
return t.clone
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) LoadEmbedded() {
|
2014-01-29 17:50:31 -05:00
|
|
|
t.EmbedShortcodes()
|
2014-04-09 17:45:34 -04:00
|
|
|
t.EmbedTemplates()
|
2014-01-09 17:27:39 -05:00
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
// MarkReady marks the template as "ready for execution". No changes allowed
|
|
|
|
// after this is set.
|
|
|
|
func (t *GoHTMLTemplate) MarkReady() {
|
|
|
|
if t.clone == nil {
|
|
|
|
t.clone = template.Must(t.Template.Clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *GoHTMLTemplate) checkState() {
|
|
|
|
if t.clone != nil {
|
|
|
|
panic("template is cloned and cannot be modfified")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) AddInternalTemplate(prefix, name, tpl string) error {
|
2014-04-23 02:52:01 -04:00
|
|
|
if prefix != "" {
|
|
|
|
return t.AddTemplate("_internal/"+prefix+"/"+name, tpl)
|
|
|
|
}
|
2016-03-24 17:56:50 -04:00
|
|
|
return t.AddTemplate("_internal/"+name, tpl)
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) AddInternalShortcode(name, content string) error {
|
2014-02-25 23:57:31 -05:00
|
|
|
return t.AddInternalTemplate("shortcodes", name, content)
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) AddTemplate(name, tpl string) error {
|
2016-03-09 08:05:31 -05:00
|
|
|
t.checkState()
|
2016-10-24 16:29:48 -04:00
|
|
|
templ, err := t.New(name).Parse(tpl)
|
2014-01-29 17:50:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
2016-10-24 16:29:48 -04:00
|
|
|
return err
|
2016-11-22 03:04:38 -05:00
|
|
|
}
|
2016-10-24 16:29:48 -04:00
|
|
|
if err := applyTemplateTransformers(templ); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
func (t *GoHTMLTemplate) AddTemplateFileWithMaster(name, overlayFilename, masterFilename string) error {
|
|
|
|
|
|
|
|
// There is currently no known way to associate a cloned template with an existing one.
|
|
|
|
// This funky master/overlay design will hopefully improve in a future version of Go.
|
|
|
|
//
|
|
|
|
// Simplicity is hard.
|
|
|
|
//
|
|
|
|
// Until then we'll have to live with this hackery.
|
|
|
|
//
|
|
|
|
// See https://github.com/golang/go/issues/14285
|
|
|
|
//
|
|
|
|
// So, to do minimum amount of changes to get this to work:
|
|
|
|
//
|
|
|
|
// 1. Lookup or Parse the master
|
|
|
|
// 2. Parse and store the overlay in a separate map
|
|
|
|
|
|
|
|
masterTpl := t.Lookup(masterFilename)
|
|
|
|
|
|
|
|
if masterTpl == nil {
|
2016-03-21 19:28:42 -04:00
|
|
|
b, err := afero.ReadFile(hugofs.Source(), masterFilename)
|
2016-02-09 12:39:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
masterTpl, err = t.New(masterFilename).Parse(string(b))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// TODO(bep) Add a method that does this
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:28:42 -04:00
|
|
|
b, err := afero.ReadFile(hugofs.Source(), overlayFilename)
|
2016-02-09 12:39:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
overlayTpl, err := template.Must(masterTpl.Clone()).Parse(string(b))
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
} else {
|
2016-10-14 05:01:49 -04:00
|
|
|
// The extra lookup is a workaround, see
|
|
|
|
// * https://github.com/golang/go/issues/16101
|
|
|
|
// * https://github.com/spf13/hugo/issues/2549
|
2016-10-24 16:29:48 -04:00
|
|
|
overlayTpl = overlayTpl.Lookup(overlayTpl.Name())
|
|
|
|
if err := applyTemplateTransformers(overlayTpl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.overlays[name] = overlayTpl
|
2016-02-09 12:39:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-29 05:31:23 -04:00
|
|
|
func (t *GoHTMLTemplate) AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error {
|
2016-03-09 08:05:31 -05:00
|
|
|
t.checkState()
|
2015-05-29 05:31:23 -04:00
|
|
|
var base, inner *ace.File
|
|
|
|
name = name[:len(name)-len(filepath.Ext(innerPath))] + ".html"
|
2015-05-31 07:13:26 -04:00
|
|
|
|
|
|
|
// Fixes issue #1178
|
|
|
|
basePath = strings.Replace(basePath, "\\", "/", -1)
|
|
|
|
innerPath = strings.Replace(innerPath, "\\", "/", -1)
|
|
|
|
|
2015-05-29 05:31:23 -04:00
|
|
|
if basePath != "" {
|
|
|
|
base = ace.NewFile(basePath, baseContent)
|
|
|
|
inner = ace.NewFile(innerPath, innerContent)
|
|
|
|
} else {
|
|
|
|
base = ace.NewFile(innerPath, innerContent)
|
|
|
|
inner = ace.NewFile("", []byte{})
|
|
|
|
}
|
|
|
|
parsed, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
return err
|
|
|
|
}
|
2016-10-24 16:29:48 -04:00
|
|
|
templ, err := ace.CompileResultWithTemplate(t.New(name), parsed, nil)
|
2015-05-29 05:31:23 -04:00
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
2016-10-24 16:29:48 -04:00
|
|
|
return err
|
2015-05-29 05:31:23 -04:00
|
|
|
}
|
2016-10-24 16:29:48 -04:00
|
|
|
return applyTemplateTransformers(templ)
|
2015-05-29 05:31:23 -04:00
|
|
|
}
|
|
|
|
|
2015-03-26 12:22:45 -04:00
|
|
|
func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
|
2016-03-09 08:05:31 -05:00
|
|
|
t.checkState()
|
2014-09-09 17:57:14 -04:00
|
|
|
// get the suffix and switch on that
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
switch ext {
|
|
|
|
case ".amber":
|
2015-07-19 14:12:05 -04:00
|
|
|
templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
|
2014-09-09 17:57:14 -04:00
|
|
|
compiler := amber.New()
|
2016-10-26 14:50:12 -04:00
|
|
|
b, err := afero.ReadFile(hugofs.Source(), path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the input data
|
|
|
|
if err := compiler.ParseData(b, path); err != nil {
|
2015-07-19 21:02:03 -04:00
|
|
|
return err
|
2014-09-09 17:57:14 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 16:29:48 -04:00
|
|
|
templ, err := compiler.CompileWithTemplate(t.New(templateName))
|
|
|
|
if err != nil {
|
2014-09-09 17:57:14 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-10-24 16:29:48 -04:00
|
|
|
|
|
|
|
return applyTemplateTransformers(templ)
|
2014-09-29 21:19:50 -04:00
|
|
|
case ".ace":
|
2015-05-29 05:31:23 -04:00
|
|
|
var innerContent, baseContent []byte
|
2016-03-21 19:28:42 -04:00
|
|
|
innerContent, err := afero.ReadFile(hugofs.Source(), path)
|
2015-05-29 05:31:23 -04:00
|
|
|
|
2014-09-29 21:19:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-26 12:22:45 -04:00
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
2016-03-21 19:28:42 -04:00
|
|
|
baseContent, err = afero.ReadFile(hugofs.Source(), baseTemplatePath)
|
2015-03-26 12:22:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-05-29 05:31:23 -04:00
|
|
|
|
|
|
|
return t.AddAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
|
2014-09-09 17:57:14 -04:00
|
|
|
default:
|
2016-02-09 12:39:17 -05:00
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
|
|
|
return t.AddTemplateFileWithMaster(name, path, baseTemplatePath)
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:28:42 -04:00
|
|
|
b, err := afero.ReadFile(hugofs.Source(), path)
|
2016-02-09 12:39:17 -05:00
|
|
|
|
2014-09-09 17:57:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-28 03:30:58 -04:00
|
|
|
jww.DEBUG.Printf("Add template file from path %s", path)
|
|
|
|
|
2014-09-09 17:57:14 -04:00
|
|
|
return t.AddTemplate(name, string(b))
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2014-09-09 17:57:14 -04:00
|
|
|
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) GenerateTemplateNameFrom(base, path string) string {
|
2014-12-10 10:48:51 -05:00
|
|
|
name, _ := filepath.Rel(base, path)
|
|
|
|
return filepath.ToSlash(name)
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
|
|
|
|
2015-01-20 07:21:50 -05:00
|
|
|
func isDotFile(path string) bool {
|
2014-01-29 17:50:31 -05:00
|
|
|
return filepath.Base(path)[0] == '.'
|
2013-09-03 18:38:20 -04:00
|
|
|
}
|
|
|
|
|
2015-03-10 12:18:40 -04:00
|
|
|
func isBackupFile(path string) bool {
|
|
|
|
return path[len(path)-1] == '~'
|
|
|
|
}
|
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
const baseFileBase = "baseof"
|
2015-03-26 12:22:45 -04:00
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
var aceTemplateInnerMarkers = [][]byte{[]byte("= content")}
|
|
|
|
var goTemplateInnerMarkers = [][]byte{[]byte("{{define"), []byte("{{ define")}
|
2015-03-29 15:12:13 -04:00
|
|
|
|
2015-03-26 12:22:45 -04:00
|
|
|
func isBaseTemplate(path string) bool {
|
2016-02-09 12:39:17 -05:00
|
|
|
return strings.Contains(path, baseFileBase)
|
2015-03-26 12:22:45 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) {
|
2016-07-28 03:30:58 -04:00
|
|
|
jww.DEBUG.Printf("Load templates from path %q prefix %q", absPath, prefix)
|
2014-01-29 17:50:31 -05:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-28 03:30:58 -04:00
|
|
|
jww.DEBUG.Println("Template path", path)
|
2014-12-10 10:48:51 -05:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2015-02-17 16:21:37 -05:00
|
|
|
link, err := filepath.EvalSymlinks(absPath)
|
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", absPath, err)
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-30 11:16:04 -04:00
|
|
|
linkfi, err := hugofs.Source().Stat(link)
|
2015-02-17 16:21:37 -05:00
|
|
|
if err != nil {
|
|
|
|
jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !linkfi.Mode().IsRegular() {
|
|
|
|
jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", absPath)
|
|
|
|
}
|
2014-12-10 10:48:51 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
if !fi.IsDir() {
|
2015-03-26 12:22:45 -04:00
|
|
|
if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
|
2014-01-29 17:50:31 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-07 13:48:00 -05:00
|
|
|
tplName := t.GenerateTemplateNameFrom(absPath, path)
|
2014-01-29 17:50:31 -05:00
|
|
|
|
2014-04-10 08:10:12 -04:00
|
|
|
if prefix != "" {
|
|
|
|
tplName = strings.Trim(prefix, "/") + "/" + tplName
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:22:45 -04:00
|
|
|
var baseTemplatePath string
|
|
|
|
|
2016-02-09 12:39:17 -05:00
|
|
|
// Ace and Go templates may have both a base and inner template.
|
2016-03-15 05:22:08 -04:00
|
|
|
pathDir := filepath.Dir(path)
|
|
|
|
if filepath.Ext(path) != ".amber" && !strings.HasSuffix(pathDir, "partials") && !strings.HasSuffix(pathDir, "shortcodes") {
|
2016-02-09 12:39:17 -05:00
|
|
|
|
|
|
|
innerMarkers := goTemplateInnerMarkers
|
|
|
|
baseFileName := fmt.Sprintf("%s.html", baseFileBase)
|
|
|
|
|
|
|
|
if filepath.Ext(path) == ".ace" {
|
|
|
|
innerMarkers = aceTemplateInnerMarkers
|
|
|
|
baseFileName = fmt.Sprintf("%s.ace", baseFileBase)
|
|
|
|
}
|
|
|
|
|
2015-03-29 15:12:13 -04:00
|
|
|
// This may be a view that shouldn't have base template
|
|
|
|
// Have to look inside it to make sure
|
2016-07-30 11:16:04 -04:00
|
|
|
needsBase, err := helpers.FileContainsAny(path, innerMarkers, hugofs.Source())
|
2015-03-29 15:12:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if needsBase {
|
2015-04-18 07:58:35 -04:00
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
layoutDir := helpers.GetLayoutDirPath()
|
|
|
|
currBaseFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseFileName)
|
|
|
|
templateDir := filepath.Dir(path)
|
|
|
|
themeDir := filepath.Join(helpers.GetThemeDir())
|
|
|
|
relativeThemeLayoutsDir := filepath.Join(helpers.GetRelativeThemeDir(), "layouts")
|
|
|
|
|
|
|
|
var baseTemplatedDir string
|
|
|
|
|
|
|
|
if strings.HasPrefix(templateDir, relativeThemeLayoutsDir) {
|
|
|
|
baseTemplatedDir = strings.TrimPrefix(templateDir, relativeThemeLayoutsDir)
|
|
|
|
} else {
|
|
|
|
baseTemplatedDir = strings.TrimPrefix(templateDir, layoutDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseTemplatedDir = strings.TrimPrefix(baseTemplatedDir, helpers.FilePathSeparator)
|
|
|
|
|
2015-04-18 07:58:35 -04:00
|
|
|
// Look for base template in the follwing order:
|
2016-02-09 12:39:17 -05:00
|
|
|
// 1. <current-path>/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
|
|
|
|
// 2. <current-path>/baseof.<suffix>
|
|
|
|
// 3. _default/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
|
|
|
|
// 4. _default/baseof.<suffix>
|
2016-12-14 14:12:03 -05:00
|
|
|
// For each of the steps above, it will first look in the project, then, if theme is set,
|
|
|
|
// in the theme's layouts folder.
|
|
|
|
|
|
|
|
pairsToCheck := [][]string{
|
|
|
|
[]string{baseTemplatedDir, currBaseFilename},
|
|
|
|
[]string{baseTemplatedDir, baseFileName},
|
|
|
|
[]string{"_default", currBaseFilename},
|
|
|
|
[]string{"_default", baseFileName},
|
2015-06-18 21:29:08 -04:00
|
|
|
}
|
2015-04-18 07:58:35 -04:00
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
Loop:
|
|
|
|
for _, pair := range pairsToCheck {
|
|
|
|
pathsToCheck := basePathsToCheck(pair, layoutDir, themeDir)
|
|
|
|
for _, pathToCheck := range pathsToCheck {
|
|
|
|
if ok, err := helpers.Exists(pathToCheck, hugofs.Source()); err == nil && ok {
|
|
|
|
baseTemplatePath = pathToCheck
|
|
|
|
break Loop
|
|
|
|
}
|
2015-03-29 15:12:13 -04:00
|
|
|
}
|
2015-03-26 12:22:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 16:56:44 -04:00
|
|
|
if err := t.AddTemplateFile(tplName, baseTemplatePath, path); err != nil {
|
2016-10-24 16:29:48 -04:00
|
|
|
jww.ERROR.Printf("Failed to add template %s in path %s: %s", tplName, path, err)
|
2016-10-25 16:56:44 -04:00
|
|
|
}
|
2014-01-29 17:50:31 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-30 11:28:40 -04:00
|
|
|
if err := helpers.SymbolicWalk(hugofs.Source(), absPath, walker); err != nil {
|
|
|
|
jww.ERROR.Printf("Failed to load templates: %s", err)
|
|
|
|
}
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
2014-04-10 08:10:12 -04:00
|
|
|
|
2016-12-14 14:12:03 -05:00
|
|
|
func basePathsToCheck(path []string, layoutDir, themeDir string) []string {
|
|
|
|
// Always look in the project.
|
|
|
|
pathsToCheck := []string{filepath.Join((append([]string{layoutDir}, path...))...)}
|
|
|
|
|
|
|
|
// May have a theme
|
|
|
|
if themeDir != "" {
|
|
|
|
pathsToCheck = append(pathsToCheck, filepath.Join((append([]string{themeDir, "layouts"}, path...))...))
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathsToCheck
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) LoadTemplatesWithPrefix(absPath string, prefix string) {
|
2014-04-10 08:10:12 -04:00
|
|
|
t.loadTemplates(absPath, prefix)
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) LoadTemplates(absPath string) {
|
2014-04-10 08:10:12 -04:00
|
|
|
t.loadTemplates(absPath, "")
|
|
|
|
}
|
2014-12-09 20:32:58 -05:00
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (t *GoHTMLTemplate) PrintErrors() {
|
2015-01-30 18:56:25 -05:00
|
|
|
for _, e := range t.errors {
|
|
|
|
jww.ERROR.Println(e.err)
|
|
|
|
}
|
|
|
|
}
|