| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/config" |
| 21 |
"github.com/gohugoio/hugo/config/allconfig" |
| 22 |
"github.com/spf13/afero" |
| 23 |
) |
| 24 |
|
| 25 |
// DefaultConfig returns the default configuration. |
| 26 |
func DefaultConfig() *allconfig.Config { |
| 27 |
fs := afero.NewMemMapFs() |
| 28 |
all, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: fs, Environ: []string{"none"}}) |
| 29 |
if err != nil { |
| 30 |
panic(err) |
| 31 |
} |
| 32 |
return all.Base |
| 33 |
} |
| 34 |
|
| 35 |
// ExampleConfig returns the some example configuration for documentation. |
| 36 |
func ExampleConfig() (*allconfig.Config, error) { |
| 37 |
// Apply some example settings for the settings that does not come with a sensible default. |
| 38 |
configToml := ` |
| 39 |
title = 'My Blog' |
| 40 |
baseURL = "https://example.com/" |
| 41 |
disableKinds = ["term", "taxonomy"] |
| 42 |
|
| 43 |
[outputs] |
| 44 |
home = ['html', 'html', 'rss'] |
| 45 |
page = ['html'] |
| 46 |
|
| 47 |
[imaging] |
| 48 |
bgcolor = '#ffffff' |
| 49 |
hint = 'photo' |
| 50 |
quality = 81 |
| 51 |
resamplefilter = 'CatmullRom' |
| 52 |
[imaging.exif] |
| 53 |
disableDate = true |
| 54 |
disableLatLong = true |
| 55 |
excludeFields = 'ColorSpace|Metering' |
| 56 |
|
| 57 |
[params] |
| 58 |
color = 'blue' |
| 59 |
style = 'dark' |
| 60 |
|
| 61 |
|
| 62 |
[languages] |
| 63 |
[languages.ar] |
| 64 |
languagedirection = 'rtl' |
| 65 |
title = 'مدونتي' |
| 66 |
weight = 2 |
| 67 |
[languages.en] |
| 68 |
weight = 1 |
| 69 |
[languages.fr] |
| 70 |
weight = 2 |
| 71 |
[languages.fr.params] |
| 72 |
linkedin = 'https://linkedin.com/fr/whoever' |
| 73 |
color = 'green' |
| 74 |
[[languages.fr.menus.main]] |
| 75 |
name = 'Des produits' |
| 76 |
pageRef = '/products' |
| 77 |
weight = 20 |
| 78 |
|
| 79 |
[menus] |
| 80 |
[[menus.main]] |
| 81 |
name = 'Home' |
| 82 |
pageRef = '/' |
| 83 |
weight = 10 |
| 84 |
[[menus.main]] |
| 85 |
name = 'Products' |
| 86 |
pageRef = '/products' |
| 87 |
weight = 20 |
| 88 |
[[menus.main]] |
| 89 |
name = 'Services' |
| 90 |
pageRef = '/services' |
| 91 |
weight = 30 |
| 92 |
|
| 93 |
[deployment] |
| 94 |
order = [".jpg$", ".gif$"] |
| 95 |
[[deployment.targets]] |
| 96 |
name = "mydeployment" |
| 97 |
url = "s3://mybucket?region=us-east-1" |
| 98 |
cloudFrontDistributionID = "mydistributionid" |
| 99 |
[[deployment.matchers]] |
| 100 |
pattern = "^.+\\.(js|css|svg|ttf)$" |
| 101 |
cacheControl = "max-age=31536000, no-transform, public" |
| 102 |
gzip = true |
| 103 |
[[deployment.matchers]] |
| 104 |
pattern = "^.+\\.(png|jpg)$" |
| 105 |
cacheControl = "max-age=31536000, no-transform, public" |
| 106 |
gzip = false |
| 107 |
[[deployment.matchers]] |
| 108 |
pattern = "^sitemap\\.xml$" |
| 109 |
contentType = "application/xml" |
| 110 |
gzip = true |
| 111 |
[[deployment.matchers]] |
| 112 |
pattern = "^.+\\.(html|xml|json)$" |
| 113 |
gzip = true |
| 114 |
|
| 115 |
[permalinks] |
| 116 |
posts = '/posts/:year/:month/:title/' |
| 117 |
|
| 118 |
[taxonomies] |
| 119 |
category = 'categories' |
| 120 |
series = 'series' |
| 121 |
tag = 'tags' |
| 122 |
|
| 123 |
[module] |
| 124 |
[module.hugoVersion] |
| 125 |
min = '0.80.0' |
| 126 |
[[module.imports]] |
| 127 |
path = "github.com/bep/hugo-mod-misc/dummy-content" |
| 128 |
ignoreconfig = true |
| 129 |
ignoreimports = true |
| 130 |
[[module.mounts]] |
| 131 |
source = "content/blog" |
| 132 |
target = "content" |
| 133 |
|
| 134 |
[minify] |
| 135 |
[minify.tdewolff] |
| 136 |
[minify.tdewolff.json] |
| 137 |
precision = 2 |
| 138 |
|
| 139 |
[[cascade]] |
| 140 |
background = 'yosemite.jpg' |
| 141 |
[cascade.target] |
| 142 |
kind = 'page' |
| 143 |
path = '/blog/**' |
| 144 |
[[cascade]] |
| 145 |
background = 'goldenbridge.jpg' |
| 146 |
[cascade.target] |
| 147 |
kind = 'section' |
| 148 |
|
| 149 |
|
| 150 |
` |
| 151 |
|
| 152 |
goMod := ` |
| 153 |
module github.com/bep/mymod |
| 154 |
` |
| 155 |
|
| 156 |
cfg := config.New() |
| 157 |
|
| 158 |
tempDir := os.TempDir() |
| 159 |
cacheDir := filepath.Join(tempDir, "hugocache") |
| 160 |
if err := os.MkdirAll(cacheDir, 0o777); err != nil { |
| 161 |
return nil, err |
| 162 |
} |
| 163 |
cfg.Set("cacheDir", cacheDir) |
| 164 |
cfg.Set("workingDir", tempDir) |
| 165 |
defer func() { |
| 166 |
os.RemoveAll(tempDir) |
| 167 |
}() |
| 168 |
|
| 169 |
fs := afero.NewOsFs() |
| 170 |
|
| 171 |
if err := afero.WriteFile(fs, filepath.Join(tempDir, "hugo.toml"), []byte(configToml), 0o644); err != nil { |
| 172 |
return nil, err |
| 173 |
} |
| 174 |
|
| 175 |
if err := afero.WriteFile(fs, filepath.Join(tempDir, "go.mod"), []byte(goMod), 0o644); err != nil { |
| 176 |
return nil, err |
| 177 |
} |
| 178 |
|
| 179 |
conf, err := allconfig.LoadConfig(allconfig.ConfigSourceDescriptor{Fs: fs, Flags: cfg, Environ: []string{"none"}}) |
| 180 |
if err != nil { |
| 181 |
return nil, err |
| 182 |
} |
| 183 |
return conf.Base, err |
| 184 |
} |