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
This commit is contained in:
Bjørn Erik Pedersen
2018-03-01 15:01:25 +01:00
parent 6464981adb
commit 80230f26a3
86 changed files with 2831 additions and 1925 deletions

View File

@@ -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 || fi.IsDir() {
return nil
}
if isDotFile(path) || isBackupFile(path) || isBaseTemplate(path) {
return nil
}
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)
},
}
tplID, err := output.CreateTemplateNames(descriptor)
if err != nil {
t.Log.ERROR.Printf("Failed to resolve template in path %q: %s", path, err)
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)
}
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)
}
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)
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)
}
}

View File

@@ -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)

View File

@@ -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)