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"
|
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"
|
2014-01-29 17:50:31 -05:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "Global" Template System
|
|
|
|
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
|
|
|
|
|
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) {
|
2014-06-06 16:15:19 -04:00
|
|
|
worked := false
|
|
|
|
for _, layout := range layouts {
|
2014-12-07 13:48:00 -05:00
|
|
|
|
2014-11-01 00:15:22 -04:00
|
|
|
name := layout
|
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
if Lookup(name) == nil {
|
2014-11-01 00:15:22 -04:00
|
|
|
name = layout + ".html"
|
|
|
|
}
|
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
if templ := Lookup(name); templ != nil {
|
|
|
|
err := templ.Execute(w, context)
|
2014-10-02 13:37:38 -04:00
|
|
|
if err != nil {
|
2014-11-01 00:15:22 -04:00
|
|
|
jww.ERROR.Println(err, "in", name)
|
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()
|
2014-01-29 17:50:31 -05:00
|
|
|
_, err := t.New(name).Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
}
|
|
|
|
return err
|
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 {
|
|
|
|
t.overlays[name] = overlayTpl
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
_, err = ace.CompileResultWithTemplate(t.New(name), parsed, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
// Parse the input file
|
|
|
|
if err := compiler.ParseFile(path); err != nil {
|
2015-07-19 21:02:03 -04:00
|
|
|
return err
|
2014-09-09 17:57:14 -04:00
|
|
|
}
|
|
|
|
|
2015-07-19 14:12:05 -04:00
|
|
|
if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil {
|
2014-09-09 17:57:14 -04:00
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.AddTemplate(name, string(b))
|
2014-01-29 17:50:31 -05:00
|
|
|
}
|
2014-09-09 17:57:14 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
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) {
|
2014-01-29 17:50:31 -05:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
linkfi, err := os.Stat(link)
|
|
|
|
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-03-21 19:28:42 -04:00
|
|
|
needsBase, err := helpers.FileContainsAny(path, innerMarkers, hugofs.Os())
|
2015-03-29 15:12:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if needsBase {
|
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>
|
|
|
|
// 5. <themedir>/layouts/_default/<template-name>-baseof.<suffix>
|
|
|
|
// 6. <themedir>/layouts/_default/baseof.<suffix>
|
|
|
|
|
|
|
|
currBaseFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseFileName)
|
2015-04-18 07:58:35 -04:00
|
|
|
templateDir := filepath.Dir(path)
|
2015-06-19 09:30:34 -04:00
|
|
|
themeDir := helpers.GetThemeDir()
|
2015-04-18 07:58:35 -04:00
|
|
|
|
|
|
|
pathsToCheck := []string{
|
2016-02-09 12:39:17 -05:00
|
|
|
filepath.Join(templateDir, currBaseFilename),
|
|
|
|
filepath.Join(templateDir, baseFileName),
|
|
|
|
filepath.Join(absPath, "_default", currBaseFilename),
|
|
|
|
filepath.Join(absPath, "_default", baseFileName),
|
|
|
|
filepath.Join(themeDir, "layouts", "_default", currBaseFilename),
|
|
|
|
filepath.Join(themeDir, "layouts", "_default", baseFileName),
|
2015-06-18 21:29:08 -04:00
|
|
|
}
|
2015-04-18 07:58:35 -04:00
|
|
|
|
|
|
|
for _, pathToCheck := range pathsToCheck {
|
2016-03-21 19:28:42 -04:00
|
|
|
if ok, err := helpers.Exists(pathToCheck, hugofs.Os()); err == nil && ok {
|
2015-04-18 07:58:35 -04:00
|
|
|
baseTemplatePath = pathToCheck
|
|
|
|
break
|
2015-03-29 15:12:13 -04:00
|
|
|
}
|
2015-03-26 12:22:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.AddTemplateFile(tplName, baseTemplatePath, path)
|
2014-01-29 17:50:31 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath.Walk(absPath, walker)
|
2013-08-31 20:47:21 -04:00
|
|
|
}
|
2014-04-10 08:10:12 -04:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|