2016-10-24 16:29:48 -04:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
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
2018-03-01 09:01:25 -05:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
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
2018-03-01 09:01:25 -05:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2016-10-24 16:29:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
caseMixingSiteConfigTOML = `
|
|
|
|
Title = "In an Insensitive Mood"
|
|
|
|
DefaultContentLanguage = "nn"
|
|
|
|
defaultContentLanguageInSubdir = true
|
|
|
|
|
|
|
|
[Blackfriday]
|
|
|
|
AngledQuotes = true
|
|
|
|
HrefTargetBlank = true
|
|
|
|
|
|
|
|
[Params]
|
|
|
|
Search = true
|
|
|
|
Color = "green"
|
|
|
|
mood = "Happy"
|
|
|
|
[Params.Colors]
|
|
|
|
Blue = "blue"
|
|
|
|
Yellow = "yellow"
|
|
|
|
|
|
|
|
[Languages]
|
|
|
|
[Languages.nn]
|
|
|
|
title = "Nynorsk title"
|
|
|
|
languageName = "Nynorsk"
|
|
|
|
weight = 1
|
|
|
|
|
|
|
|
[Languages.en]
|
|
|
|
TITLE = "English title"
|
|
|
|
LanguageName = "English"
|
|
|
|
Mood = "Thoughtful"
|
|
|
|
Weight = 2
|
|
|
|
COLOR = "Pink"
|
|
|
|
[Languages.en.blackfriday]
|
|
|
|
angledQuotes = false
|
|
|
|
hrefTargetBlank = false
|
|
|
|
[Languages.en.Colors]
|
|
|
|
BLUE = "blues"
|
2019-11-21 15:59:38 -05:00
|
|
|
Yellow = "golden"
|
2016-10-24 16:29:48 -04:00
|
|
|
`
|
|
|
|
caseMixingPage1En = `
|
|
|
|
---
|
|
|
|
TITLE: Page1 En Translation
|
|
|
|
BlackFriday:
|
|
|
|
AngledQuotes: false
|
|
|
|
Color: "black"
|
|
|
|
Search: true
|
|
|
|
mooD: "sad and lonely"
|
|
|
|
ColorS:
|
|
|
|
Blue: "bluesy"
|
|
|
|
Yellow: "sunny"
|
|
|
|
---
|
|
|
|
# "Hi"
|
|
|
|
{{< shortcode >}}
|
|
|
|
`
|
|
|
|
|
|
|
|
caseMixingPage1 = `
|
|
|
|
---
|
|
|
|
titLe: Side 1
|
|
|
|
blackFriday:
|
|
|
|
angledQuotes: true
|
|
|
|
color: "red"
|
|
|
|
search: false
|
|
|
|
MooD: "sad"
|
|
|
|
COLORS:
|
|
|
|
blue: "heavenly"
|
|
|
|
yelloW: "Sunny"
|
|
|
|
---
|
|
|
|
# "Hi"
|
|
|
|
{{< shortcode >}}
|
|
|
|
`
|
|
|
|
|
|
|
|
caseMixingPage2 = `
|
|
|
|
---
|
|
|
|
TITLE: Page2 Title
|
|
|
|
BlackFriday:
|
|
|
|
AngledQuotes: false
|
|
|
|
Color: "black"
|
|
|
|
search: true
|
|
|
|
MooD: "moody"
|
|
|
|
ColorS:
|
|
|
|
Blue: "sky"
|
|
|
|
YELLOW: "flower"
|
|
|
|
---
|
|
|
|
# Hi
|
|
|
|
{{< shortcode >}}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
func caseMixingTestsWriteCommonSources(t *testing.T, fs afero.Fs) {
|
|
|
|
writeToFs(t, fs, filepath.Join("content", "sect1", "page1.md"), caseMixingPage1)
|
|
|
|
writeToFs(t, fs, filepath.Join("content", "sect2", "page2.md"), caseMixingPage2)
|
|
|
|
writeToFs(t, fs, filepath.Join("content", "sect1", "page1.en.md"), caseMixingPage1En)
|
2016-10-24 16:29:48 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
writeToFs(t, fs, "layouts/shortcodes/shortcode.html", `
|
2016-10-24 16:29:48 -04:00
|
|
|
Shortcode Page: {{ .Page.Params.COLOR }}|{{ .Page.Params.Colors.Blue }}
|
|
|
|
Shortcode Site: {{ .Page.Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}
|
|
|
|
`)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
writeToFs(t, fs, "layouts/partials/partial.html", `
|
2016-10-24 16:29:48 -04:00
|
|
|
Partial Page: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}
|
|
|
|
Partial Site: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}
|
2019-01-18 12:48:19 -05:00
|
|
|
Partial Site Global: {{ site.Params.COLOR }}|{{ site.Params.COLORS.YELLOW }}
|
2016-10-24 16:29:48 -04:00
|
|
|
`)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
writeToFs(t, fs, "config.toml", caseMixingSiteConfigTOML)
|
2016-10-24 16:29:48 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaseInsensitiveConfigurationVariations(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
mm := afero.NewMemMapFs()
|
2016-10-24 16:29:48 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
caseMixingTestsWriteCommonSources(t, mm)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2018-03-18 06:07:24 -04:00
|
|
|
cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "config.toml"})
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
fs := hugofs.NewFrom(mm, cfg)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
th := newTestHelper(cfg, fs, t)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "baseof.html"), `
|
2016-10-24 16:29:48 -04:00
|
|
|
Block Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}
|
|
|
|
{{ block "main" . }}default{{end}}`)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "sect2", "single.html"), `
|
2016-10-24 16:29:48 -04:00
|
|
|
{{ define "main"}}
|
|
|
|
Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }}
|
|
|
|
Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }}
|
2019-11-21 15:59:38 -05:00
|
|
|
{{ template "index-color" (dict "name" "Page" "params" .Params) }}
|
|
|
|
{{ template "index-color" (dict "name" "Site" "params" .Site.Params) }}
|
|
|
|
|
2016-10-24 16:29:48 -04:00
|
|
|
{{ .Content }}
|
|
|
|
{{ partial "partial.html" . }}
|
|
|
|
{{ end }}
|
2019-11-21 15:59:38 -05:00
|
|
|
{{ define "index-color" }}
|
|
|
|
{{ $yellow := index .params "COLoRS" "yELLOW" }}
|
|
|
|
{{ $colors := index .params "COLoRS" }}
|
|
|
|
{{ $yellow2 := index $colors "yEllow" }}
|
|
|
|
index1|{{ .name }}: {{ $yellow }}|
|
|
|
|
index2|{{ .name }}: {{ $yellow2 }}|
|
|
|
|
{{ end }}
|
2016-10-24 16:29:48 -04:00
|
|
|
`)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), `
|
2016-10-24 16:29:48 -04:00
|
|
|
Page Title: {{ .Title }}
|
|
|
|
Site Title: {{ .Site.Title }}
|
|
|
|
Site Lang Mood: {{ .Site.Language.Params.MOoD }}
|
2019-11-21 15:59:38 -05:00
|
|
|
Page Colors: {{ .Params.COLOR }}|{{ .Params.Colors.Blue }}|{{ index .Params "ColOR" }}
|
|
|
|
Site Colors: {{ .Site.Params.COLOR }}|{{ .Site.Params.COLORS.YELLOW }}|{{ index .Site.Params "ColOR" }}
|
2019-11-21 13:45:03 -05:00
|
|
|
{{ $page2 := .Site.GetPage "/sect2/page2" }}
|
|
|
|
{{ if $page2 }}
|
|
|
|
Page2: {{ $page2.Params.ColoR }}
|
|
|
|
{{ end }}
|
2016-10-24 16:29:48 -04:00
|
|
|
{{ .Content }}
|
|
|
|
{{ partial "partial.html" . }}
|
|
|
|
`)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
|
2016-10-24 16:29:48 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create sites: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sites.Build(BuildCfg{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to build sites: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:14:52 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"),
|
2019-11-21 15:59:38 -05:00
|
|
|
"Page Colors: red|heavenly|red",
|
|
|
|
"Site Colors: green|yellow|green",
|
2016-10-24 16:29:48 -04:00
|
|
|
"Site Lang Mood: Happy",
|
|
|
|
"Shortcode Page: red|heavenly",
|
|
|
|
"Shortcode Site: green|yellow",
|
|
|
|
"Partial Page: red|heavenly",
|
|
|
|
"Partial Site: green|yellow",
|
2019-01-18 12:48:19 -05:00
|
|
|
"Partial Site Global: green|yellow",
|
2016-10-24 16:29:48 -04:00
|
|
|
"Page Title: Side 1",
|
|
|
|
"Site Title: Nynorsk title",
|
2019-11-21 13:45:03 -05:00
|
|
|
"Page2: black ",
|
2016-10-24 16:29:48 -04:00
|
|
|
)
|
|
|
|
|
2017-02-17 15:14:52 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "en", "sect1", "page1", "index.html"),
|
2016-10-24 16:29:48 -04:00
|
|
|
"Site Colors: Pink|golden",
|
|
|
|
"Page Colors: black|bluesy",
|
|
|
|
"Site Lang Mood: Thoughtful",
|
|
|
|
"Page Title: Page1 En Translation",
|
|
|
|
"Site Title: English title",
|
|
|
|
"“Hi”",
|
|
|
|
)
|
|
|
|
|
2017-02-17 15:14:52 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "nn", "sect2", "page2", "index.html"),
|
2016-10-24 16:29:48 -04:00
|
|
|
"Page Colors: black|sky",
|
|
|
|
"Site Colors: green|yellow",
|
|
|
|
"Shortcode Page: black|sky",
|
|
|
|
"Block Page Colors: black|sky",
|
|
|
|
"Partial Page: black|sky",
|
|
|
|
"Partial Site: green|yellow",
|
2019-11-21 15:59:38 -05:00
|
|
|
"index1|Page: flower|",
|
|
|
|
"index1|Site: yellow|",
|
|
|
|
"index2|Page: flower|",
|
|
|
|
"index2|Site: yellow|",
|
2016-10-24 16:29:48 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaseInsensitiveConfigurationForAllTemplateEngines(t *testing.T) {
|
2017-02-04 22:20:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-10-24 16:29:48 -04:00
|
|
|
noOp := func(s string) string {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
amberFixer := func(s string) string {
|
|
|
|
fixed := strings.Replace(s, "{{ .Site.Params", "{{ Site.Params", -1)
|
|
|
|
fixed = strings.Replace(fixed, "{{ .Params", "{{ Params", -1)
|
|
|
|
fixed = strings.Replace(fixed, ".Content", "Content", -1)
|
|
|
|
fixed = strings.Replace(fixed, "{{", "#{", -1)
|
|
|
|
fixed = strings.Replace(fixed, "}}", "}", -1)
|
|
|
|
|
|
|
|
return fixed
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, config := range []struct {
|
|
|
|
suffix string
|
|
|
|
templateFixer func(s string) string
|
|
|
|
}{
|
|
|
|
{"amber", amberFixer},
|
|
|
|
{"html", noOp},
|
|
|
|
{"ace", noOp},
|
|
|
|
} {
|
|
|
|
doTestCaseInsensitiveConfigurationForTemplateEngine(t, config.suffix, config.templateFixer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestCaseInsensitiveConfigurationForTemplateEngine(t *testing.T, suffix string, templateFixer func(s string) string) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-02-04 22:20:06 -05:00
|
|
|
mm := afero.NewMemMapFs()
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
caseMixingTestsWriteCommonSources(t, mm)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2018-03-18 05:32:29 -04:00
|
|
|
cfg, err := LoadConfigDefault(mm)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
fs := hugofs.NewFrom(mm, cfg)
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
th := newTestHelper(cfg, fs, t)
|
2016-10-24 16:29:48 -04:00
|
|
|
|
|
|
|
t.Log("Testing", suffix)
|
|
|
|
|
|
|
|
templTemplate := `
|
|
|
|
p
|
|
|
|
|
|
|
|
|
| Page Colors: {{ .Params.CoLOR }}|{{ .Params.Colors.Blue }}
|
|
|
|
| Site Colors: {{ .Site.Params.COlOR }}|{{ .Site.Params.COLORS.YELLOW }}
|
|
|
|
| {{ .Content }}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
templ := templateFixer(templTemplate)
|
|
|
|
|
|
|
|
t.Log(templ)
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ)
|
2016-10-24 16:29:48 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
|
2016-10-24 16:29:48 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create sites: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sites.Build(BuildCfg{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to build sites: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:14:52 -05:00
|
|
|
th.assertFileContent(filepath.Join("public", "nn", "sect1", "page1", "index.html"),
|
2016-10-24 16:29:48 -04:00
|
|
|
"Page Colors: red|heavenly",
|
|
|
|
"Site Colors: green|yellow",
|
|
|
|
"Shortcode Page: red|heavenly",
|
|
|
|
"Shortcode Site: green|yellow",
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|