summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-03-01 15:01:25 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-06-10 23:55:20 +0200
commit80230f26a3020ff33bac2bef01b2c0e314b89f86 (patch)
tree6da3114350477866065d265d6e1db9cb55639dc1 /tpl
parent6464981adb4d7d0f41e8e2c987342082982210a1 (diff)
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo. With this, it helps thinking about a theme as a set of ordered components: ```toml theme = ["my-shortcodes", "base-theme", "hyde"] ``` The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right. So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`. Hugo uses two different algorithms to merge the filesystems, depending on the file type: * For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files. * For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen. The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically. Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure: * `params` (global and per language) * `menu` (global and per language) * `outputformats` and `mediatypes` The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts. A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others. Fixes #4460 Fixes #4450
Diffstat (limited to 'tpl')
-rw-r--r--tpl/collections/collections_test.go3
-rw-r--r--tpl/data/resources_test.go3
-rw-r--r--tpl/template.go2
-rw-r--r--tpl/tplimpl/template.go157
-rw-r--r--tpl/tplimpl/template_funcs_test.go21
-rw-r--r--tpl/tplimpl/template_test.go4
-rw-r--r--tpl/transform/transform_test.go3
7 files changed, 92 insertions, 101 deletions
diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go
index 68e7c59d6..ac2c2fe63 100644
--- a/tpl/collections/collections_test.go
+++ b/tpl/collections/collections_test.go
@@ -29,6 +29,7 @@ import (
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
+ "github.com/gohugoio/hugo/langs"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
@@ -774,7 +775,7 @@ type TstX struct {
}
func newDeps(cfg config.Provider) *deps.Deps {
- l := helpers.NewLanguage("en", cfg)
+ l := langs.NewLanguage("en", cfg)
l.Set("i18nDir", "i18n")
cs, err := helpers.NewContentSpec(l)
if err != nil {
diff --git a/tpl/data/resources_test.go b/tpl/data/resources_test.go
index 79e9b3907..f6baae18b 100644
--- a/tpl/data/resources_test.go
+++ b/tpl/data/resources_test.go
@@ -27,6 +27,7 @@ import (
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
+ "github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
@@ -164,7 +165,7 @@ func TestScpGetRemoteParallel(t *testing.T) {
}
func newDeps(cfg config.Provider) *deps.Deps {
- l := helpers.NewLanguage("en", cfg)
+ l := langs.NewLanguage("en", cfg)
l.Set("i18nDir", "i18n")
cs, err := helpers.NewContentSpec(l)
if err != nil {
diff --git a/tpl/template.go b/tpl/template.go
index bdb917ba9..e04d2cc6c 100644
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -35,7 +35,7 @@ type TemplateHandler interface {
TemplateFinder
AddTemplate(name, tpl string) error
AddLateTemplate(name, tpl string) error
- LoadTemplates(absPath, prefix string)
+ LoadTemplates(prefix string)
PrintErrors()
MarkReady()
diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go
index 8f91113a8..e838ebc57 100644
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -86,6 +86,10 @@ type templateHandler struct {
errors []*templateErr
+ // This is the filesystem to load the templates from. All the templates are
+ // stored in the root of this filesystem.
+ layoutsFs afero.Fs
+
*deps.Deps
}
@@ -129,10 +133,11 @@ func (t *templateHandler) Lookup(name string) *tpl.TemplateAdapter {
func (t *templateHandler) clone(d *deps.Deps) *templateHandler {
c := &templateHandler{
- Deps: d,
- html: &htmlTemplates{t: template.Must(t.html.t.Clone()), overlays: make(map[string]*template.Template)},
- text: &textTemplates{t: texttemplate.Must(t.text.t.Clone()), overlays: make(map[string]*texttemplate.Template)},
- errors: make([]*templateErr, 0),
+ Deps: d,
+ layoutsFs: d.BaseFs.Layouts.Fs,
+ html: &htmlTemplates{t: template.Must(t.html.t.Clone()), overlays: make(map[string]*template.Template)},
+ text: &textTemplates{t: texttemplate.Must(t.text.t.Clone()), overlays: make(map[string]*texttemplate.Template)},
+ errors: make([]*templateErr, 0),
}
d.Tmpl = c
@@ -170,10 +175,11 @@ func newTemplateAdapter(deps *deps.Deps) *templateHandler {
overlays: make(map[string]*texttemplate.Template),
}
return &templateHandler{
- Deps: deps,
- html: htmlT,
- text: textT,
- errors: make([]*templateErr, 0),
+ Deps: deps,
+ layoutsFs: deps.BaseFs.Layouts.Fs,
+ html: htmlT,
+ text: textT,
+ errors: make([]*templateErr, 0),
}
}
@@ -208,15 +214,18 @@ func (t *htmlTemplates) Lookup(name string) *tpl.TemplateAdapter {
}
func (t *htmlTemplates) lookup(name string) *template.Template {
- if templ := t.t.Lookup(name); templ != nil {
- return templ
- }
+
+ // Need to check in the overlay registry first as it will also be found below.
if t.overlays != nil {
if templ, ok := t.overlays[name]; ok {
return templ
}
}
+ if templ := t.t.Lookup(name); templ != nil {
+ return templ
+ }
+
if t.clone != nil {
return t.clone.Lookup(name)
}
@@ -248,15 +257,18 @@ func (t *textTemplates) Lookup(name string) *tpl.TemplateAdapter {
}
func (t *textTemplates) lookup(name string) *texttemplate.Template {
- if templ := t.t.Lookup(name); templ != nil {
- return templ
- }
+
+ // Need to check in the overlay registry first as it will also be found below.
if t.overlays != nil {
if templ, ok := t.overlays[name]; ok {
return templ
}
}
+ if templ := t.t.Lookup(name); templ != nil {
+ return templ
+ }
+
if t.clone != nil {
return t.clone.Lookup(name)
}
@@ -287,11 +299,11 @@ func (t *textTemplates) setFuncs(funcMap map[string]interface{}) {
t.t.Funcs(funcMap)
}
-// LoadTemplates loads the templates, starting from the given absolute path.
+// LoadTemplates loads the templates from the layouts filesystem.
// A prefix can be given to indicate a template namespace to load the templates
// into, i.e. "_internal" etc.
-func (t *templateHandler) LoadTemplates(absPath, prefix string) {
- t.loadTemplates(absPath, prefix)
+func (t *templateHandler) LoadTemplates(prefix string) {
+ t.loadTemplates(prefix)
}
@@ -406,85 +418,49 @@ func (t *templateHandler) RebuildClone() {
t.text.clone = texttemplate.Must(t.text.cloneClone.Clone())
}
-func (t *templateHandler) loadTemplates(absPath string, prefix string) {
- t.Log.DEBUG.Printf("Load templates from path %q prefix %q", absPath, prefix)
+func (t *templateHandler) loadTemplates(prefix string) {
walker := func(path string, fi os.FileInfo, err error) error {
- if err != nil {
+ if err != nil || fi.IsDir() {
return nil
}
- t.Log.DEBUG.Println("Template path", path)
- if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
- link, err := filepath.EvalSymlinks(absPath)
- if err != nil {
- t.Log.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", absPath, err)
- return nil
- }
-
- linkfi, err := t.Fs.Source.Stat(link)
- if err != nil {
- t.Log.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
- return nil
- }
-
- if !linkfi.Mode().IsRegular() {
- t.Log.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", absPath)
- }
+ if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
return nil
}
- if !fi.IsDir() {
- if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
- return nil
- }
-
- var (
- workingDir = t.PathSpec.WorkingDir()
- themeDir = t.PathSpec.GetThemeDir()
- layoutDir = t.PathSpec.LayoutDir()
- )
-
- if themeDir != "" && strings.HasPrefix(absPath, themeDir) {
- layoutDir = "layouts"
- }
-
- li := strings.LastIndex(path, layoutDir) + len(layoutDir) + 1
- relPath := path[li:]
- templateDir := path[:li-len(layoutDir)-1]
-
- descriptor := output.TemplateLookupDescriptor{
- TemplateDir: templateDir,
- WorkingDir: workingDir,
- LayoutDir: layoutDir,
- RelPath: relPath,
- Prefix: prefix,
- ThemeDir: themeDir,
- OutputFormats: t.OutputFormatsConfig,
- FileExists: func(filename string) (bool, error) {
- return helpers.Exists(filename, t.Fs.Source)
- },
- ContainsAny: func(filename string, subslices [][]byte) (bool, error) {
- return helpers.FileContainsAny(filename, subslices, t.Fs.Source)
- },
- }
-
- tplID, err := output.CreateTemplateNames(descriptor)
- if err != nil {
- t.Log.ERROR.Printf("Failed to resolve template in path %q: %s", path, err)
+ workingDir := t.PathSpec.WorkingDir
+
+ descriptor := output.TemplateLookupDescriptor{
+ WorkingDir: workingDir,
+ RelPath: path,
+ Prefix: prefix,
+ OutputFormats: t.OutputFormatsConfig,
+ FileExists: func(filename string) (bool, error) {
+ return helpers.Exists(filename, t.Layouts.Fs)
+ },
+ ContainsAny: func(filename string, subslices [][]byte) (bool, error) {
+ return helpers.FileContainsAny(filename, subslices, t.Layouts.Fs)
+ },
+ }
- return nil
- }
+ tplID, err := output.CreateTemplateNames(descriptor)
+ if err != nil {
+ t.Log.ERROR.Printf("Failed to resolve template in path %q: %s", path, err)
- if err := t.addTemplateFile(tplID.Name, tplID.MasterFilename, tplID.OverlayFilename); err != nil {
- t.Log.ERROR.Printf("Failed to add template %q in path %q: %s", tplID.Name, path, err)
- }
+ return nil
+ }
+ if err := t.addTemplateFile(tplID.Name, tplID.MasterFilename, tplID.OverlayFilename); err != nil {
+ t.Log.ERROR.Printf("Failed to add template %q in path %q: %s", tplID.Name, path, err)
}
+
return nil
}
- if err := helpers.SymbolicWalk(t.Fs.Source, absPath, walker); err != nil {
+
+ if err := helpers.SymbolicWalk(t.Layouts.Fs, "", walker); err != nil {
t.Log.ERROR.Printf("Failed to load templates: %s", err)
}
+
}
func (t *templateHandler) initFuncs() {
@@ -534,6 +510,7 @@ func (t *templateHandler) handleMaster(name, overlayFilename, masterFilename str
}
func (t *htmlTemplates) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (string, error)) error {
+
masterTpl := t.lookup(masterFilename)
if masterTpl == nil {
@@ -565,6 +542,7 @@ func (t *htmlTemplates) handleMaster(name, overlayFilename, masterFilename strin
if err := applyTemplateTransformersToHMLTTemplate(overlayTpl); err != nil {
return err
}
+
t.overlays[name] = overlayTpl
return err
@@ -572,6 +550,7 @@ func (t *htmlTemplates) handleMaster(name, overlayFilename, masterFilename strin
}
func (t *textTemplates) handleMaster(name, overlayFilename, masterFilename string, onMissing func(filename string) (string, error)) error {
+
name = strings.TrimPrefix(name, textTmplNamePrefix)
masterTpl := t.lookup(masterFilename)
@@ -610,12 +589,16 @@ func (t *textTemplates) handleMaster(name, overlayFilename, masterFilename strin
func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) error {
t.checkState()
+ t.Log.DEBUG.Printf("Add template file: name %q, baseTemplatePath %q, path %q", name, baseTemplatePath, path)
+
getTemplate := func(filename string) (string, error) {
- b, err := afero.ReadFile(t.Fs.Source, filename)
+ b, err := afero.ReadFile(t.Layouts.Fs, filename)
if err != nil {
return "", err
}
- return string(b), nil
+ s := string(b)
+
+ return s, nil
}
// get the suffix and switch on that
@@ -625,7 +608,7 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
// Only HTML support for Amber
withoutExt := strings.TrimSuffix(name, filepath.Ext(name))
templateName := withoutExt + ".html"
- b, err := afero.ReadFile(t.Fs.Source, path)
+ b, err := afero.ReadFile(t.Layouts.Fs, path)
if err != nil {
return err
@@ -654,14 +637,14 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
case ".ace":
// Only HTML support for Ace
var innerContent, baseContent []byte
- innerContent, err := afero.ReadFile(t.Fs.Source, path)
+ innerContent, err := afero.ReadFile(t.Layouts.Fs, path)
if err != nil {
return err
}
if baseTemplatePath != "" {
- baseContent, err = afero.ReadFile(t.Fs.Source, baseTemplatePath)
+ baseContent, err = afero.ReadFile(t.Layouts.Fs, baseTemplatePath)
if err != nil {
return err
}
@@ -680,8 +663,6 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e
return err
}
- t.Log.DEBUG.Printf("Add template file from path %s", path)
-
return t.AddTemplate(name, templ)
}
}
diff --git a/tpl/tplimpl/template_funcs_test.go b/tpl/tplimpl/template_funcs_test.go
index 72842d308..a1745282d 100644
--- a/tpl/tplimpl/template_funcs_test.go
+++ b/tpl/tplimpl/template_funcs_test.go
@@ -30,6 +30,7 @@ import (
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/i18n"
+ "github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/tpl"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/gohugoio/hugo/tpl/partials"
@@ -43,9 +44,18 @@ var (
logger = jww.NewNotepad(jww.LevelFatal, jww.LevelFatal, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
)
+func newTestConfig() config.Provider {
+ v := viper.New()
+ v.Set("contentDir", "content")
+ v.Set("dataDir", "data")
+ v.Set("i18nDir", "i18n")
+ v.Set("layoutDir", "layouts")
+ v.Set("archetypeDir", "archetypes")
+ return v
+}
+
func newDepsConfig(cfg config.Provider) deps.DepsCfg {
- l := helpers.NewLanguage("en", cfg)
- l.Set("i18nDir", "i18n")
+ l := langs.NewLanguage("en", cfg)
return deps.DepsCfg{
Language: l,
Cfg: cfg,
@@ -61,13 +71,13 @@ func TestTemplateFuncsExamples(t *testing.T) {
workingDir := "/home/hugo"
- v := viper.New()
+ v := newTestConfig()
v.Set("workingDir", workingDir)
v.Set("multilingual", true)
v.Set("contentDir", "content")
v.Set("baseURL", "http://mysite.com/hugo/")
- v.Set("CurrentContentLanguage", helpers.NewLanguage("en", v))
+ v.Set("CurrentContentLanguage", langs.NewLanguage("en", v))
fs := hugofs.NewMem(v)
@@ -126,8 +136,7 @@ func TestPartialCached(t *testing.T) {
var data struct {
}
- v := viper.New()
- v.Set("contentDir", "content")
+ v := newTestConfig()
config := newDepsConfig(v)
diff --git a/tpl/tplimpl/template_test.go b/tpl/tplimpl/template_test.go
index 78682e9ab..3ce2a88a2 100644
--- a/tpl/tplimpl/template_test.go
+++ b/tpl/tplimpl/template_test.go
@@ -18,7 +18,6 @@ import (
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
- "github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
@@ -34,8 +33,7 @@ func TestHTMLEscape(t *testing.T) {
"html": "<h1>Hi!</h1>",
"other": "<h1>Hi!</h1>",
}
- v := viper.New()
- v.Set("contentDir", "content")
+ v := newTestConfig()
fs := hugofs.NewMem(v)
//afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go
index ab3beb804..34de4a6fd 100644
--- a/tpl/transform/transform_test.go
+++ b/tpl/transform/transform_test.go
@@ -22,6 +22,7 @@ import (
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
+ "github.com/gohugoio/hugo/langs"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -240,7 +241,7 @@ func TestPlainify(t *testing.T) {
}
func newDeps(cfg config.Provider) *deps.Deps {
- l := helpers.NewLanguage("en", cfg)
+ l := langs.NewLanguage("en", cfg)
l.Set("i18nDir", "i18n")
cs, err := helpers.NewContentSpec(l)
if err != nil {