ref: be8c06757707cf240baaa72d6e659fcd2b6261f2
parent: 0921761e473588cc600a91d76d60844aa28fa387
author: bep <[email protected]>
date: Thu Mar 26 13:22:45 EDT 2015
Add support for Ace base and inner templates Fixes #994 Fixes #511
--- a/hugofs/fs.go
+++ b/hugofs/fs.go
@@ -17,5 +17,6 @@
var SourceFs afero.Fs = new(afero.OsFs)
var DestinationFS afero.Fs = new(afero.OsFs)
+var OsFs afero.Fs = new(afero.OsFs)
//var DestinationFS afero.Fs = new(afero.MemMapFs)
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -17,6 +17,13 @@
"bytes"
"errors"
"fmt"
+ "github.com/eknkc/amber"
+ "github.com/spf13/cast"
+ 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"
"html"
"html/template"
"io"
@@ -27,13 +34,6 @@
"sort"
"strconv"
"strings"
-
- "github.com/eknkc/amber"
- "github.com/spf13/cast"
- bp "github.com/spf13/hugo/bufferpool"
- "github.com/spf13/hugo/helpers"
- jww "github.com/spf13/jwalterweatherman"
- "github.com/yosssi/ace"
)
var localTemplates *template.Template
@@ -1275,7 +1275,7 @@
return err
}
-func (t *GoHTMLTemplate) AddTemplateFile(name, path string) error {
+func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
// get the suffix and switch on that
ext := filepath.Ext(path)
switch ext {
@@ -1294,9 +1294,21 @@
if err != nil {
return err
}
+
+ var base, inner *ace.File
+
name = name[:len(name)-len(ext)] + ".html"
- base := ace.NewFile(path, b)
- inner := ace.NewFile("", []byte{})
+ if baseTemplatePath != "" {
+ b2, err := ioutil.ReadFile(baseTemplatePath)
+ if err != nil {
+ return err
+ }
+ base = ace.NewFile(baseTemplatePath, b2)
+ inner = ace.NewFile(path, b)
+ } else {
+ base = ace.NewFile(path, b)
+ inner = ace.NewFile("", []byte{})
+ }
rslt, err := ace.ParseSource(ace.NewSource(base, inner, []*ace.File{}), nil)
if err != nil {
t.errors = append(t.errors, &templateErr{name: name, err: err})
@@ -1333,6 +1345,14 @@
return path[len(path)-1] == '~'
}
+// TODO(bep) split this file in two => template_funcs.go + tests.
+
+const baseAceFilename = "baseof.ace"
+
+func isBaseTemplate(path string) bool {
+ return strings.HasSuffix(path, baseAceFilename)
+}
+
func (t *GoHTMLTemplate) loadTemplates(absPath string, prefix string) {
walker := func(path string, fi os.FileInfo, err error) error {
if err != nil {
@@ -1357,7 +1377,7 @@
}
if !fi.IsDir() {
- if isDotFile(path) || isBackupFile(path) {
+ if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
return nil
}
@@ -1367,7 +1387,23 @@
tplName = strings.Trim(prefix, "/") + "/" + tplName
}
- t.AddTemplateFile(tplName, path)
+ var baseTemplatePath string
+
+ // ACE templates may have both a base and inner template.
+ if filepath.Ext(path) == ".ace" && !strings.HasSuffix(filepath.Dir(path), "partials") {
+ // Look for the base first in the current path, then in _default.
+ p := filepath.Join(filepath.Dir(path), baseAceFilename)
+ if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok {
+ baseTemplatePath = p
+ } else {
+ p := filepath.Join(absPath, "_default", baseAceFilename)
+ if ok, err := helpers.Exists(p, hugofs.OsFs); err == nil && ok {
+ baseTemplatePath = p
+ }
+ }
+ }
+
+ t.AddTemplateFile(tplName, baseTemplatePath, path)
}
return nil