2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2016-08-05 05:04:52 -04:00
|
|
|
//
|
|
|
|
// 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 (
|
2018-11-15 03:28:02 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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
|
|
|
|
2017-11-02 03:25:20 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/config/allconfig"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2016-08-05 05:04:52 -04:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// DefaultConfig returns the default configuration.
|
|
|
|
func DefaultConfig() *allconfig.Config {
|
|
|
|
fs := afero.NewMemMapFs()
|
2023-08-30 13:14:04 -04:00
|
|
|
all, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: fs, Environ: []string{"none"}})
|
2018-05-22 08:40:06 -04:00
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return all.Base
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExampleConfig returns the some example configuration for documentation.
|
|
|
|
func ExampleConfig() (*allconfig.Config, error) {
|
|
|
|
// Apply some example settings for the settings that does not come with a sensible default.
|
|
|
|
configToml := `
|
|
|
|
title = 'My Blog'
|
|
|
|
baseURL = "https://example.com/"
|
|
|
|
disableKinds = ["term", "taxonomy"]
|
|
|
|
|
|
|
|
[outputs]
|
|
|
|
home = ['html', 'html', 'rss']
|
|
|
|
page = ['html']
|
|
|
|
|
|
|
|
[imaging]
|
|
|
|
bgcolor = '#ffffff'
|
|
|
|
hint = 'photo'
|
|
|
|
quality = 81
|
|
|
|
resamplefilter = 'CatmullRom'
|
|
|
|
[imaging.exif]
|
|
|
|
disableDate = true
|
|
|
|
disableLatLong = true
|
|
|
|
excludeFields = 'ColorSpace|Metering'
|
|
|
|
|
|
|
|
[params]
|
|
|
|
color = 'blue'
|
|
|
|
style = 'dark'
|
|
|
|
|
|
|
|
|
|
|
|
[languages]
|
|
|
|
[languages.ar]
|
|
|
|
languagedirection = 'rtl'
|
|
|
|
title = 'مدونتي'
|
|
|
|
weight = 2
|
|
|
|
[languages.en]
|
|
|
|
weight = 1
|
|
|
|
[languages.fr]
|
|
|
|
weight = 2
|
|
|
|
[languages.fr.params]
|
|
|
|
linkedin = 'https://linkedin.com/fr/whoever'
|
|
|
|
color = 'green'
|
|
|
|
[[languages.fr.menus.main]]
|
|
|
|
name = 'Des produits'
|
|
|
|
pageRef = '/products'
|
|
|
|
weight = 20
|
|
|
|
|
|
|
|
[menus]
|
|
|
|
[[menus.main]]
|
|
|
|
name = 'Home'
|
|
|
|
pageRef = '/'
|
|
|
|
weight = 10
|
|
|
|
[[menus.main]]
|
|
|
|
name = 'Products'
|
|
|
|
pageRef = '/products'
|
|
|
|
weight = 20
|
|
|
|
[[menus.main]]
|
|
|
|
name = 'Services'
|
|
|
|
pageRef = '/services'
|
|
|
|
weight = 30
|
|
|
|
|
|
|
|
[deployment]
|
|
|
|
order = [".jpg$", ".gif$"]
|
|
|
|
[[deployment.targets]]
|
|
|
|
name = "mydeployment"
|
|
|
|
url = "s3://mybucket?region=us-east-1"
|
|
|
|
cloudFrontDistributionID = "mydistributionid"
|
|
|
|
[[deployment.matchers]]
|
|
|
|
pattern = "^.+\\.(js|css|svg|ttf)$"
|
|
|
|
cacheControl = "max-age=31536000, no-transform, public"
|
|
|
|
gzip = true
|
|
|
|
[[deployment.matchers]]
|
|
|
|
pattern = "^.+\\.(png|jpg)$"
|
|
|
|
cacheControl = "max-age=31536000, no-transform, public"
|
|
|
|
gzip = false
|
|
|
|
[[deployment.matchers]]
|
|
|
|
pattern = "^sitemap\\.xml$"
|
|
|
|
contentType = "application/xml"
|
|
|
|
gzip = true
|
|
|
|
[[deployment.matchers]]
|
|
|
|
pattern = "^.+\\.(html|xml|json)$"
|
|
|
|
gzip = true
|
|
|
|
|
|
|
|
[permalinks]
|
|
|
|
posts = '/posts/:year/:month/:title/'
|
|
|
|
|
|
|
|
[taxonomies]
|
|
|
|
category = 'categories'
|
|
|
|
series = 'series'
|
|
|
|
tag = 'tags'
|
|
|
|
|
|
|
|
[module]
|
|
|
|
[module.hugoVersion]
|
|
|
|
min = '0.80.0'
|
|
|
|
[[module.imports]]
|
|
|
|
path = "github.com/bep/hugo-mod-misc/dummy-content"
|
|
|
|
ignoreconfig = true
|
|
|
|
ignoreimports = true
|
|
|
|
[[module.mounts]]
|
|
|
|
source = "content/blog"
|
|
|
|
target = "content"
|
|
|
|
|
|
|
|
[minify]
|
|
|
|
[minify.tdewolff]
|
|
|
|
[minify.tdewolff.json]
|
|
|
|
precision = 2
|
|
|
|
|
|
|
|
[[cascade]]
|
|
|
|
background = 'yosemite.jpg'
|
|
|
|
[cascade._target]
|
|
|
|
kind = 'page'
|
|
|
|
path = '/blog/**'
|
|
|
|
[[cascade]]
|
|
|
|
background = 'goldenbridge.jpg'
|
|
|
|
[cascade._target]
|
|
|
|
kind = 'section'
|
|
|
|
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
goMod := `
|
|
|
|
module github.com/bep/mymod
|
|
|
|
`
|
|
|
|
|
|
|
|
cfg := config.New()
|
|
|
|
|
|
|
|
tempDir := os.TempDir()
|
|
|
|
cacheDir := filepath.Join(tempDir, "hugocache")
|
2024-01-28 16:34:10 -05:00
|
|
|
if err := os.MkdirAll(cacheDir, 0o777); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg.Set("cacheDir", cacheDir)
|
|
|
|
cfg.Set("workingDir", tempDir)
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(tempDir)
|
|
|
|
}()
|
|
|
|
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
|
2024-01-28 16:34:10 -05:00
|
|
|
if err := afero.WriteFile(fs, filepath.Join(tempDir, "hugo.toml"), []byte(configToml), 0o644); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-28 16:34:10 -05:00
|
|
|
if err := afero.WriteFile(fs, filepath.Join(tempDir, "go.mod"), []byte(goMod), 0o644); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-30 13:14:04 -04:00
|
|
|
conf, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: fs, Flags: cfg, Environ: []string{"none"}})
|
2018-05-22 08:40:06 -04:00
|
|
|
if err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return nil, err
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
return conf.Base, err
|
2016-08-05 05:04:52 -04:00
|
|
|
}
|