diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-01 15:01:25 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-06-10 23:55:20 +0200 |
commit | 80230f26a3020ff33bac2bef01b2c0e314b89f86 (patch) | |
tree | 6da3114350477866065d265d6e1db9cb55639dc1 /output/layout_base.go | |
parent | 6464981adb4d7d0f41e8e2c987342082982210a1 (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 'output/layout_base.go')
-rw-r--r-- | output/layout_base.go | 91 |
1 files changed, 19 insertions, 72 deletions
diff --git a/output/layout_base.go b/output/layout_base.go index 49ae1d64e..31e1194f4 100644 --- a/output/layout_base.go +++ b/output/layout_base.go @@ -40,26 +40,16 @@ type TemplateNames struct { } type TemplateLookupDescriptor struct { - // TemplateDir is the project or theme root of the current template. - // This will be the same as WorkingDir for non-theme templates. - TemplateDir string - // The full path to the site root. WorkingDir string - // Main project layout dir, defaults to "layouts" - LayoutDir string - // The path to the template relative the the base. // I.e. shortcodes/youtube.html RelPath string - // The template name prefix to look for, i.e. "theme". + // The template name prefix to look for. Prefix string - // The theme dir if theme active. - ThemeDir string - // All the output formats in play. This is used to decide if text/template or // html/template. OutputFormats Formats @@ -71,6 +61,7 @@ type TemplateLookupDescriptor struct { func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { name := filepath.ToSlash(d.RelPath) + name = strings.TrimPrefix(name, "/") if d.Prefix != "" { name = strings.Trim(d.Prefix, "/") + "/" + name @@ -78,22 +69,8 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { var ( id TemplateNames - - // This is the path to the actual template in process. This may - // be in the theme's or the project's /layouts. - baseLayoutDir = filepath.Join(d.TemplateDir, d.LayoutDir) - fullPath = filepath.Join(baseLayoutDir, d.RelPath) - - // This is always the project's layout dir. - baseWorkLayoutDir = filepath.Join(d.WorkingDir, d.LayoutDir) - - baseThemeLayoutDir string ) - if d.ThemeDir != "" { - baseThemeLayoutDir = filepath.Join(d.ThemeDir, "layouts") - } - // The filename will have a suffix with an optional type indicator. // Examples: // index.html @@ -119,7 +96,7 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { filenameNoSuffix := parts[0] - id.OverlayFilename = fullPath + id.OverlayFilename = d.RelPath id.Name = name if isPlainText { @@ -127,7 +104,7 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { } // Ace and Go templates may have both a base and inner template. - pathDir := filepath.Dir(fullPath) + pathDir := filepath.Dir(d.RelPath) if ext == "amber" || strings.HasSuffix(pathDir, "partials") || strings.HasSuffix(pathDir, "shortcodes") { // No base template support @@ -150,7 +127,7 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { // This may be a view that shouldn't have base template // Have to look inside it to make sure - needsBase, err := d.ContainsAny(fullPath, innerMarkers) + needsBase, err := d.ContainsAny(d.RelPath, innerMarkers) if err != nil { return id, err } @@ -158,21 +135,14 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { if needsBase { currBaseFilename := fmt.Sprintf("%s-%s", filenameNoSuffix, baseFilename) - templateDir := filepath.Dir(fullPath) - - // Find the base, e.g. "_default". - baseTemplatedDir := strings.TrimPrefix(templateDir, baseLayoutDir) - baseTemplatedDir = strings.TrimPrefix(baseTemplatedDir, helpers.FilePathSeparator) - // Look for base template in the follwing order: // 1. <current-path>/<template-name>-baseof.<outputFormat>(optional).<suffix>, e.g. list-baseof.<outputFormat>(optional).<suffix>. // 2. <current-path>/baseof.<outputFormat>(optional).<suffix> // 3. _default/<template-name>-baseof.<outputFormat>(optional).<suffix>, e.g. list-baseof.<outputFormat>(optional).<suffix>. // 4. _default/baseof.<outputFormat>(optional).<suffix> - // For each of the steps above, it will first look in the project, then, if theme is set, - // in the theme's layouts folder. - // Also note that the <current-path> may be both the project's layout folder and the theme's. - pairsToCheck := createPairsToCheck(baseTemplatedDir, baseFilename, currBaseFilename) + // + // The filesystem it looks in a a composite of the project and potential theme(s). + pathsToCheck := createPathsToCheck(pathDir, baseFilename, currBaseFilename) // We may have language code and/or "terms" in the template name. We want the most specific, // but need to fall back to the baseof.html or baseof.ace if needed. @@ -183,20 +153,15 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { if len(p1) > 0 && len(p1) == len(p2) { for i := len(p1); i > 0; i-- { v1, v2 := strings.Join(p1[:i], ".")+"."+ext, strings.Join(p2[:i], ".")+"."+ext - pairsToCheck = append(pairsToCheck, createPairsToCheck(baseTemplatedDir, v1, v2)...) + pathsToCheck = append(pathsToCheck, createPathsToCheck(pathDir, v1, v2)...) } } - Loop: - for _, pair := range pairsToCheck { - pathsToCheck := basePathsToCheck(pair, baseLayoutDir, baseWorkLayoutDir, baseThemeLayoutDir) - - for _, pathToCheck := range pathsToCheck { - if ok, err := d.FileExists(pathToCheck); err == nil && ok { - id.MasterFilename = pathToCheck - break Loop - } + for _, p := range pathsToCheck { + if ok, err := d.FileExists(p); err == nil && ok { + id.MasterFilename = p + break } } } @@ -205,29 +170,11 @@ func CreateTemplateNames(d TemplateLookupDescriptor) (TemplateNames, error) { } -func createPairsToCheck(baseTemplatedDir, baseFilename, currBaseFilename string) [][]string { - return [][]string{ - {baseTemplatedDir, currBaseFilename}, - {baseTemplatedDir, baseFilename}, - {"_default", currBaseFilename}, - {"_default", baseFilename}, - } -} - -func basePathsToCheck(path []string, layoutDir, workLayoutDir, themeLayoutDir string) []string { - // workLayoutDir will always be the most specific, so start there. - pathsToCheck := []string{filepath.Join((append([]string{workLayoutDir}, path...))...)} - - if layoutDir != "" && layoutDir != workLayoutDir { - pathsToCheck = append(pathsToCheck, filepath.Join((append([]string{layoutDir}, path...))...)) +func createPathsToCheck(baseTemplatedDir, baseFilename, currBaseFilename string) []string { + return []string{ + filepath.Join(baseTemplatedDir, currBaseFilename), + filepath.Join(baseTemplatedDir, baseFilename), + filepath.Join("_default", currBaseFilename), + filepath.Join("_default", baseFilename), } - - // May have a theme - if themeLayoutDir != "" && themeLayoutDir != layoutDir { - pathsToCheck = append(pathsToCheck, filepath.Join((append([]string{themeLayoutDir}, path...))...)) - - } - - return pathsToCheck - } |