hugo/hugolib/config.go

185 lines
4 KiB
Go
Raw Normal View History

// Copyright 2019 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 (
Add /config dir support This commit adds support for a configuration directory (default `config`). The different pieces in this puzzle are: * A new `--environment` (or `-e`) flag. This can also be set with the `HUGO_ENVIRONMENT` OS environment variable. The value for `environment` defaults to `production` when running `hugo` and `development` when running `hugo server`. You can set it to any value you want (e.g. `hugo server -e "Sensible Environment"`), but as it is used to load configuration from the file system, the letter case may be important. You can get this value in your templates with `{{ hugo.Environment }}`. * A new `--configDir` flag (defaults to `config` below your project). This can also be set with `HUGO_CONFIGDIR` OS environment variable. If the `configDir` exists, the configuration files will be read and merged on top of each other from left to right; the right-most value will win on duplicates. Given the example tree below: If `environment` is `production`, the left-most `config.toml` would be the one directly below the project (this can now be omitted if you want), and then `_default/config.toml` and finally `production/config.toml`. And since these will be merged, you can just provide the environment specific configuration setting in you production config, e.g. `enableGitInfo = true`. The order within the directories will be lexical (`config.toml` and then `params.toml`). ```bash config ├── _default │   ├── config.toml │   ├── languages.toml │   ├── menus │   │   ├── menus.en.toml │   │   └── menus.zh.toml │   └── params.toml ├── development │   └── params.toml └── production ├── config.toml └── params.toml ``` Some configuration maps support the language code in the filename (e.g. `menus.en.toml`): `menus` (`menu` also works) and `params`. Also note that the only folders with "a meaning" in the above listing is the top level directories below `config`. The `menus` sub folder is just added for better organization. We use `TOML` in the example above, but Hugo also supports `JSON` and `YAML` as configuration formats. These can be mixed. Fixes #5422
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
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/allconfig"
"github.com/spf13/afero"
)
// DefaultConfig returns the default configuration.
func DefaultConfig() *allconfig.Config {
fs := afero.NewMemMapFs()
all, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: fs, Environ: []string{"none"}})
if err != nil {
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 {
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 {
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 {
return nil, err
}
conf, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: fs, Flags: cfg, Environ: []string{"none"}})
if err != nil {
return nil, err
}
return conf.Base, err
}