1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00

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

@ -19,6 +19,7 @@ import (
"github.com/gohugoio/hugo/tpl/tplimpl"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/deps"
@ -26,8 +27,6 @@ import (
"io/ioutil"
"os"
"github.com/gohugoio/hugo/helpers"
"log"
"github.com/gohugoio/hugo/config"
@ -168,15 +167,16 @@ func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) strin
assert := require.New(t)
fs := hugofs.NewMem(cfg)
tp := NewTranslationProvider()
depsCfg := newDepsConfig(tp, cfg, fs)
d, err := deps.New(depsCfg)
assert.NoError(err)
for file, content := range test.data {
err := afero.WriteFile(fs.Source, filepath.Join("i18n", file), []byte(content), 0755)
assert.NoError(err)
}
depsCfg := newDepsConfig(tp, cfg, fs)
d, err := deps.New(depsCfg)
assert.NoError(err)
assert.NoError(d.LoadResources())
f := tp.t.Func(test.lang)
return f(test.id, test.args)
@ -184,7 +184,7 @@ func doTestI18nTranslate(t *testing.T, test i18nTest, cfg config.Provider) strin
}
func newDepsConfig(tp *TranslationProvider, cfg config.Provider, fs *hugofs.Fs) deps.DepsCfg {
l := helpers.NewLanguage("en", cfg)
l := langs.NewLanguage("en", cfg)
l.Set("i18nDir", "i18n")
return deps.DepsCfg{
Language: l,
@ -201,6 +201,10 @@ func TestI18nTranslate(t *testing.T) {
v := viper.New()
v.SetDefault("defaultContentLanguage", "en")
v.Set("contentDir", "content")
v.Set("dataDir", "data")
v.Set("i18nDir", "i18n")
v.Set("layoutDir", "layouts")
v.Set("archetypeDir", "archetypes")
// Test without and with placeholders
for _, enablePlaceholders := range []bool{false, true} {

View File

@ -38,17 +38,8 @@ func NewTranslationProvider() *TranslationProvider {
// Update updates the i18n func in the provided Deps.
func (tp *TranslationProvider) Update(d *deps.Deps) error {
dir := d.PathSpec.AbsPathify(d.Cfg.GetString("i18nDir"))
sp := source.NewSourceSpec(d.PathSpec, d.Fs.Source)
sources := []source.Input{sp.NewFilesystem(dir)}
themeI18nDir, err := d.PathSpec.GetThemeI18nDirPath()
if err == nil {
sources = []source.Input{sp.NewFilesystem(themeI18nDir), sources[0]}
}
d.Log.DEBUG.Printf("Load I18n from %q", sources)
sp := source.NewSourceSpec(d.PathSpec, d.BaseFs.SourceFilesystems.I18n.Fs)
src := sp.NewFilesystem("")
i18nBundle := bundle.New()
@ -58,14 +49,12 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error {
}
var newLangs []string
for _, currentSource := range sources {
for _, r := range currentSource.Files() {
currentSpec := language.GetPluralSpec(r.BaseFileName())
if currentSpec == nil {
// This may is a language code not supported by go-i18n, it may be
// Klingon or ... not even a fake language. Make sure it works.
newLangs = append(newLangs, r.BaseFileName())
}
for _, r := range src.Files() {
currentSpec := language.GetPluralSpec(r.BaseFileName())
if currentSpec == nil {
// This may is a language code not supported by go-i18n, it may be
// Klingon or ... not even a fake language. Make sure it works.
newLangs = append(newLangs, r.BaseFileName())
}
}
@ -73,11 +62,12 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error {
language.RegisterPluralSpec(newLangs, en)
}
for _, currentSource := range sources {
for _, r := range currentSource.Files() {
if err := addTranslationFile(i18nBundle, r); err != nil {
return err
}
// The source files are ordered so the most important comes first. Since this is a
// last key win situation, we have to reverse the iteration order.
files := src.Files()
for i := len(files) - 1; i >= 0; i-- {
if err := addTranslationFile(i18nBundle, files[i]); err != nil {
return err
}
}