2014-11-20 12:32:21 -05:00
|
|
|
// Copyright © 2013-14 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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 tpl
|
2013-08-31 20:35:17 -04:00
|
|
|
|
|
|
|
import (
|
2014-06-06 16:15:19 -04:00
|
|
|
"bytes"
|
2015-04-18 07:58:35 -04:00
|
|
|
"fmt"
|
2015-03-26 12:22:45 -04:00
|
|
|
"github.com/eknkc/amber"
|
|
|
|
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"
|
|
|
|
"io/ioutil"
|
|
|
|
"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
|
|
|
|
|
|
|
|
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
|
|
|
|
LoadTemplates(absPath string)
|
|
|
|
LoadTemplatesWithPrefix(absPath, prefix string)
|
|
|
|
AddTemplate(name, tpl 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
|
|
|
|
errors []*templateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "Global" Template System
|
|
|
|
func T() Template {
|
|
|
|
if tmpl == nil {
|
|
|
|
tmpl = New()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:10:38 -05:00
|
|
|
// Resets the internal template state to it's initial state
|
|
|
|
func InitializeT() Template {
|
|
|
|
tmpl = New()
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
2014-11-20 12:32:21 -05:00
|
|
|
// Return a new Hugo Template System
|
|
|
|
// With all the additional features, templates & functions
|
|
|
|
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(""),
|
|
|
|
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
|
|
|
|
2014-09-03 11:30:08 -04:00
|
|
|
func Partial(name string, context_list ...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{}
|
|
|
|
|
|
|
|
if len(context_list) == 0 {
|
|
|
|
context = nil
|
|
|
|
} else {
|
|
|
|
context = context_list[0]
|
|
|
|
}
|
2014-06-06 16:15:19 -04:00
|
|
|
return ExecuteTemplateToHTML(context, "partials/"+name, "theme/partials/"+name)
|
|
|
|
}
|
|
|
|
|
2015-01-30 14:25:54 -05:00
|
|
|
func ExecuteTemplate(context interface{}, buffer *bytes.Buffer, 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
|
|
|
|
|
|
|
|
if localTemplates.Lookup(name) == nil {
|
|
|
|
name = layout + ".html"
|
|
|
|
}
|
|
|
|
|
|
|
|
if localTemplates.Lookup(name) != nil {
|
|
|
|
err := localTemplates.ExecuteTemplate(buffer, name, 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)
|
|
|
|
ExecuteTemplate(context, b, layouts...)
|
|
|
|
return template.HTML(b.String())
|
2014-06-06 16:15:19 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
2015-05-29 05:31:23 -04:00
|
|
|
func (t *GoHTMLTemplate) AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error {
|
|
|
|
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 {
|
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
|
|
|
|
innerContent, err := ioutil.ReadFile(path)
|
|
|
|
|
2014-09-29 21:19:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-26 12:22:45 -04:00
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
2015-05-29 05:31:23 -04:00
|
|
|
baseContent, err = ioutil.ReadFile(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:
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
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] == '~'
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:22:45 -04:00
|
|
|
const baseAceFilename = "baseof.ace"
|
|
|
|
|
2015-03-29 15:12:13 -04:00
|
|
|
var aceTemplateInnerMarker = []byte("= content")
|
|
|
|
|
2015-03-26 12:22:45 -04:00
|
|
|
func isBaseTemplate(path string) bool {
|
|
|
|
return strings.HasSuffix(path, baseAceFilename)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// ACE templates may have both a base and inner template.
|
|
|
|
if filepath.Ext(path) == ".ace" && !strings.HasSuffix(filepath.Dir(path), "partials") {
|
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
|
|
|
|
needsBase, err := helpers.FileContains(path, aceTemplateInnerMarker, hugofs.OsFs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if needsBase {
|
2015-04-18 07:58:35 -04:00
|
|
|
|
|
|
|
// Look for base template in the follwing order:
|
|
|
|
// 1. <current-path>/<template-name>-baseof.ace, e.g. list-baseof.ace.
|
|
|
|
// 2. <current-path>/baseof.ace
|
|
|
|
// 3. _default/<template-name>-baseof.ace, e.g. list-baseof.ace.
|
|
|
|
// 4. _default/baseof.ace
|
2015-06-18 21:29:08 -04:00
|
|
|
// 5. <themedir>/layouts/_default/<template-name>-baseof.ace
|
|
|
|
// 6. <themedir>/layouts/_default/baseof.ace
|
2015-04-18 07:58:35 -04:00
|
|
|
|
|
|
|
currBaseAceFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseAceFilename)
|
|
|
|
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{
|
|
|
|
filepath.Join(templateDir, currBaseAceFilename),
|
|
|
|
filepath.Join(templateDir, baseAceFilename),
|
|
|
|
filepath.Join(absPath, "_default", currBaseAceFilename),
|
2015-06-18 21:29:08 -04:00
|
|
|
filepath.Join(absPath, "_default", baseAceFilename),
|
|
|
|
filepath.Join(themeDir, "layouts", "_default", currBaseAceFilename),
|
|
|
|
filepath.Join(themeDir, "layouts", "_default", baseAceFilename),
|
|
|
|
}
|
2015-04-18 07:58:35 -04:00
|
|
|
|
|
|
|
for _, pathToCheck := range pathsToCheck {
|
|
|
|
if ok, err := helpers.Exists(pathToCheck, hugofs.OsFs); err == nil && ok {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|