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

@@ -16,30 +16,33 @@ package hugolib
import (
"sync"
"github.com/gohugoio/hugo/common/maps"
"sort"
"errors"
"fmt"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
"github.com/spf13/cast"
)
// Multilingual manages the all languages used in a multilingual site.
type Multilingual struct {
Languages helpers.Languages
Languages langs.Languages
DefaultLang *helpers.Language
DefaultLang *langs.Language
langMap map[string]*helpers.Language
langMap map[string]*langs.Language
langMapInit sync.Once
}
// Language returns the Language associated with the given string.
func (ml *Multilingual) Language(lang string) *helpers.Language {
func (ml *Multilingual) Language(lang string) *langs.Language {
ml.langMapInit.Do(func() {
ml.langMap = make(map[string]*helpers.Language)
ml.langMap = make(map[string]*langs.Language)
for _, l := range ml.Languages {
ml.langMap[l.Lang] = l
}
@@ -47,16 +50,16 @@ func (ml *Multilingual) Language(lang string) *helpers.Language {
return ml.langMap[lang]
}
func getLanguages(cfg config.Provider) helpers.Languages {
func getLanguages(cfg config.Provider) langs.Languages {
if cfg.IsSet("languagesSorted") {
return cfg.Get("languagesSorted").(helpers.Languages)
return cfg.Get("languagesSorted").(langs.Languages)
}
return helpers.Languages{helpers.NewDefaultLanguage(cfg)}
return langs.Languages{langs.NewDefaultLanguage(cfg)}
}
func newMultiLingualFromSites(cfg config.Provider, sites ...*Site) (*Multilingual, error) {
languages := make(helpers.Languages, len(sites))
languages := make(langs.Languages, len(sites))
for i, s := range sites {
if s.Language == nil {
@@ -71,12 +74,12 @@ func newMultiLingualFromSites(cfg config.Provider, sites ...*Site) (*Multilingua
defaultLang = "en"
}
return &Multilingual{Languages: languages, DefaultLang: helpers.NewLanguage(defaultLang, cfg)}, nil
return &Multilingual{Languages: languages, DefaultLang: langs.NewLanguage(defaultLang, cfg)}, nil
}
func newMultiLingualForLanguage(language *helpers.Language) *Multilingual {
languages := helpers.Languages{language}
func newMultiLingualForLanguage(language *langs.Language) *Multilingual {
languages := langs.Languages{language}
return &Multilingual{Languages: languages, DefaultLang: language}
}
func (ml *Multilingual) enabled() bool {
@@ -90,8 +93,8 @@ func (s *Site) multilingualEnabled() bool {
return s.owner.multilingual != nil && s.owner.multilingual.enabled()
}
func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (helpers.Languages, error) {
langs := make(helpers.Languages, len(l))
func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (langs.Languages, error) {
languages := make(langs.Languages, len(l))
i := 0
for lang, langConf := range l {
@@ -101,7 +104,7 @@ func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (helpers.L
return nil, fmt.Errorf("Language config is not a map: %T", langConf)
}
language := helpers.NewLanguage(lang, cfg)
language := langs.NewLanguage(lang, cfg)
for loki, v := range langsMap {
switch loki {
@@ -118,7 +121,7 @@ func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (helpers.L
case "params":
m := cast.ToStringMap(v)
// Needed for case insensitive fetching of params values
helpers.ToLowerMap(m)
maps.ToLower(m)
for k, vv := range m {
language.SetParam(k, vv)
}
@@ -131,11 +134,11 @@ func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (helpers.L
language.Set(loki, v)
}
langs[i] = language
languages[i] = language
i++
}
sort.Sort(langs)
sort.Sort(languages)
return langs, nil
return languages, nil
}