2014-11-20 17:32:21 +00: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-09-01 00:35:17 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-06 20:15:19 +00:00
|
|
|
"bytes"
|
2015-04-18 11:58:35 +00:00
|
|
|
"fmt"
|
2015-03-26 16:22:45 +00: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 22:50:31 +00:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-09-01 00:35:17 +00:00
|
|
|
)
|
|
|
|
|
2014-06-06 20:15:19 +00:00
|
|
|
var localTemplates *template.Template
|
2014-11-20 17:32:21 +00: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 11:01:20 +00:00
|
|
|
AddAceTemplate(name, basePath, innerPath string, baseContent, innerContent []byte) error
|
2014-11-20 17:32:21 +00:00
|
|
|
AddInternalTemplate(prefix, name, tpl string) error
|
|
|
|
AddInternalShortcode(name, tpl string) error
|
2015-01-30 23:56:25 +00:00
|
|
|
PrintErrors()
|
2014-11-20 17:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type templateErr struct {
|
|
|
|
name string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
type GoHTMLTemplate struct {
|
2014-11-20 17:32:21 +00:00
|
|
|
template.Template
|
|
|
|
errors []*templateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "Global" Template System
|
|
|
|
func T() Template {
|
|
|
|
if tmpl == nil {
|
|
|
|
tmpl = New()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
2014-11-24 22:10:38 +00:00
|
|
|
// Resets the internal template state to it's initial state
|
|
|
|
func InitializeT() Template {
|
|
|
|
tmpl = New()
|
|
|
|
return tmpl
|
|
|
|
}
|
|
|
|
|
2014-11-20 17:32:21 +00:00
|
|
|
// Return a new Hugo Template System
|
|
|
|
// With all the additional features, templates & functions
|
|
|
|
func New() Template {
|
2015-03-11 17:34:57 +00:00
|
|
|
var templates = &GoHTMLTemplate{
|
2014-11-20 17:32:21 +00:00
|
|
|
Template: *template.New(""),
|
|
|
|
errors: make([]*templateErr, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
localTemplates = &templates.Template
|
|
|
|
|
2015-07-20 00:07:55 +00:00
|
|
|
for k, v := range funcMap {
|
|
|
|
amber.FuncMap[k] = v
|
|
|
|
}
|
2014-11-20 17:32:21 +00:00
|
|
|
templates.Funcs(funcMap)
|
|
|
|
templates.LoadEmbedded()
|
|
|
|
return templates
|
|
|
|
}
|
2014-06-06 20:15:19 +00:00
|
|
|
|
2014-09-03 15:30:08 +00:00
|
|
|
func Partial(name string, context_list ...interface{}) template.HTML {
|
2014-06-06 20:15:19 +00:00
|
|
|
if strings.HasPrefix("partials/", name) {
|
|
|
|
name = name[8:]
|
|
|
|
}
|
2014-09-03 15:30:08 +00:00
|
|
|
var context interface{}
|
|
|
|
|
|
|
|
if len(context_list) == 0 {
|
|
|
|
context = nil
|
|
|
|
} else {
|
|
|
|
context = context_list[0]
|
|
|
|
}
|
2014-06-06 20:15:19 +00:00
|
|
|
return ExecuteTemplateToHTML(context, "partials/"+name, "theme/partials/"+name)
|
|
|
|
}
|
|
|
|
|
2015-01-30 19:25:54 +00:00
|
|
|
func ExecuteTemplate(context interface{}, buffer *bytes.Buffer, layouts ...string) {
|
2014-06-06 20:15:19 +00:00
|
|
|
worked := false
|
|
|
|
for _, layout := range layouts {
|
2014-12-07 18:48:00 +00:00
|
|
|
|
2014-11-01 04:15:22 +00: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 17:37:38 +00:00
|
|
|
if err != nil {
|
2014-11-01 04:15:22 +00:00
|
|
|
jww.ERROR.Println(err, "in", name)
|
2014-10-02 17:37:38 +00:00
|
|
|
}
|
2014-06-06 20:15:19 +00: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 19:25:54 +00:00
|
|
|
b := bp.GetBuffer()
|
|
|
|
defer bp.PutBuffer(b)
|
|
|
|
ExecuteTemplate(context, b, layouts...)
|
|
|
|
return template.HTML(b.String())
|
2014-06-06 20:15:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) LoadEmbedded() {
|
2014-01-29 22:50:31 +00:00
|
|
|
t.EmbedShortcodes()
|
2014-04-09 21:45:34 +00:00
|
|
|
t.EmbedTemplates()
|
2014-01-09 22:27:39 +00:00
|
|
|
}
|
2013-09-01 00:47:21 +00:00
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) AddInternalTemplate(prefix, name, tpl string) error {
|
2014-04-23 06:52:01 +00:00
|
|
|
if prefix != "" {
|
|
|
|
return t.AddTemplate("_internal/"+prefix+"/"+name, tpl)
|
|
|
|
} else {
|
|
|
|
return t.AddTemplate("_internal/"+name, tpl)
|
|
|
|
}
|
2013-09-01 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) AddInternalShortcode(name, content string) error {
|
2014-02-26 04:57:31 +00:00
|
|
|
return t.AddInternalTemplate("shortcodes", name, content)
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) AddTemplate(name, tpl string) error {
|
2014-01-29 22:50:31 +00:00
|
|
|
_, err := t.New(name).Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
t.errors = append(t.errors, &templateErr{name: name, err: err})
|
|
|
|
}
|
|
|
|
return err
|
2013-09-01 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 09:31:23 +00: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 11:13:26 +00:00
|
|
|
|
|
|
|
// Fixes issue #1178
|
|
|
|
basePath = strings.Replace(basePath, "\\", "/", -1)
|
|
|
|
innerPath = strings.Replace(innerPath, "\\", "/", -1)
|
|
|
|
|
2015-05-29 09:31:23 +00: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 16:22:45 +00:00
|
|
|
func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
|
2014-09-09 21:57:14 +00:00
|
|
|
// get the suffix and switch on that
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
switch ext {
|
|
|
|
case ".amber":
|
2015-07-19 18:12:05 +00:00
|
|
|
templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
|
2014-09-09 21:57:14 +00:00
|
|
|
compiler := amber.New()
|
|
|
|
// Parse the input file
|
|
|
|
if err := compiler.ParseFile(path); err != nil {
|
2015-07-20 01:02:03 +00:00
|
|
|
return err
|
2014-09-09 21:57:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 18:12:05 +00:00
|
|
|
if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil {
|
2014-09-09 21:57:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-09-30 01:19:50 +00:00
|
|
|
case ".ace":
|
2015-05-29 09:31:23 +00:00
|
|
|
var innerContent, baseContent []byte
|
|
|
|
innerContent, err := ioutil.ReadFile(path)
|
|
|
|
|
2014-09-30 01:19:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-26 16:22:45 +00:00
|
|
|
|
|
|
|
if baseTemplatePath != "" {
|
2015-05-29 09:31:23 +00:00
|
|
|
baseContent, err = ioutil.ReadFile(baseTemplatePath)
|
2015-03-26 16:22:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-05-29 09:31:23 +00:00
|
|
|
|
|
|
|
return t.AddAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
|
2014-09-09 21:57:14 +00:00
|
|
|
default:
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.AddTemplate(name, string(b))
|
2014-01-29 22:50:31 +00:00
|
|
|
}
|
2014-09-09 21:57:14 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2013-09-01 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) GenerateTemplateNameFrom(base, path string) string {
|
2014-12-10 15:48:51 +00:00
|
|
|
name, _ := filepath.Rel(base, path)
|
|
|
|
return filepath.ToSlash(name)
|
2013-09-01 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 12:21:50 +00:00
|
|
|
func isDotFile(path string) bool {
|
2014-01-29 22:50:31 +00:00
|
|
|
return filepath.Base(path)[0] == '.'
|
2013-09-03 22:38:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 16:18:40 +00:00
|
|
|
func isBackupFile(path string) bool {
|
|
|
|
return path[len(path)-1] == '~'
|
|
|
|
}
|
|
|
|
|
2015-03-26 16:22:45 +00:00
|
|
|
const baseAceFilename = "baseof.ace"
|
|
|
|
|
2015-03-29 19:12:13 +00:00
|
|
|
var aceTemplateInnerMarker = []byte("= content")
|
|
|
|
|
2015-03-26 16:22:45 +00:00
|
|
|
func isBaseTemplate(path string) bool {
|
|
|
|
return strings.HasSuffix(path, baseAceFilename)
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) {
|
2014-01-29 22:50:31 +00:00
|
|
|
walker := func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:48:51 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
|
2015-02-17 21:21:37 +00: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 15:48:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:50:31 +00:00
|
|
|
if !fi.IsDir() {
|
2015-03-26 16:22:45 +00:00
|
|
|
if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
|
2014-01-29 22:50:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-07 18:48:00 +00:00
|
|
|
tplName := t.GenerateTemplateNameFrom(absPath, path)
|
2014-01-29 22:50:31 +00:00
|
|
|
|
2014-04-10 12:10:12 +00:00
|
|
|
if prefix != "" {
|
|
|
|
tplName = strings.Trim(prefix, "/") + "/" + tplName
|
|
|
|
}
|
|
|
|
|
2015-03-26 16:22:45 +00: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 19:12:13 +00: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 11:58:35 +00: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-19 01:29:08 +00:00
|
|
|
// 5. <themedir>/layouts/_default/<template-name>-baseof.ace
|
|
|
|
// 6. <themedir>/layouts/_default/baseof.ace
|
2015-04-18 11:58:35 +00:00
|
|
|
|
|
|
|
currBaseAceFilename := fmt.Sprintf("%s-%s", helpers.Filename(path), baseAceFilename)
|
|
|
|
templateDir := filepath.Dir(path)
|
2015-06-19 13:30:34 +00:00
|
|
|
themeDir := helpers.GetThemeDir()
|
2015-04-18 11:58:35 +00:00
|
|
|
|
|
|
|
pathsToCheck := []string{
|
|
|
|
filepath.Join(templateDir, currBaseAceFilename),
|
|
|
|
filepath.Join(templateDir, baseAceFilename),
|
|
|
|
filepath.Join(absPath, "_default", currBaseAceFilename),
|
2015-06-19 01:29:08 +00:00
|
|
|
filepath.Join(absPath, "_default", baseAceFilename),
|
|
|
|
filepath.Join(themeDir, "layouts", "_default", currBaseAceFilename),
|
|
|
|
filepath.Join(themeDir, "layouts", "_default", baseAceFilename),
|
|
|
|
}
|
2015-04-18 11:58:35 +00:00
|
|
|
|
|
|
|
for _, pathToCheck := range pathsToCheck {
|
|
|
|
if ok, err := helpers.Exists(pathToCheck, hugofs.OsFs); err == nil && ok {
|
|
|
|
baseTemplatePath = pathToCheck
|
|
|
|
break
|
2015-03-29 19:12:13 +00:00
|
|
|
}
|
2015-03-26 16:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.AddTemplateFile(tplName, baseTemplatePath, path)
|
2014-01-29 22:50:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath.Walk(absPath, walker)
|
2013-09-01 00:47:21 +00:00
|
|
|
}
|
2014-04-10 12:10:12 +00:00
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) LoadTemplatesWithPrefix(absPath string, prefix string) {
|
2014-04-10 12:10:12 +00:00
|
|
|
t.loadTemplates(absPath, prefix)
|
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) LoadTemplates(absPath string) {
|
2014-04-10 12:10:12 +00:00
|
|
|
t.loadTemplates(absPath, "")
|
|
|
|
}
|
2014-12-10 01:32:58 +00:00
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
func (t *GoHTMLTemplate) PrintErrors() {
|
2015-01-30 23:56:25 +00:00
|
|
|
for _, e := range t.errors {
|
|
|
|
jww.ERROR.Println(e.err)
|
|
|
|
}
|
|
|
|
}
|