mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-25 22:00:58 +02:00
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables the use if `t.Parallel` in tests. Updates #2701 Fixes #3016
This commit is contained in:
@@ -16,94 +16,100 @@ package hugolib
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"github.com/spf13/hugo/helpers"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// LoadGlobalConfig loads Hugo configuration into the global Viper.
|
||||
func LoadGlobalConfig(relativeSourcePath, configFilename string) error {
|
||||
// LoadConfig loads Hugo configuration into a new Viper and then adds
|
||||
// a set of defaults.
|
||||
func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.Viper, error) {
|
||||
v := viper.New()
|
||||
v.SetFs(fs)
|
||||
if relativeSourcePath == "" {
|
||||
relativeSourcePath = "."
|
||||
}
|
||||
|
||||
viper.AutomaticEnv()
|
||||
viper.SetEnvPrefix("hugo")
|
||||
viper.SetConfigFile(configFilename)
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvPrefix("hugo")
|
||||
v.SetConfigFile(configFilename)
|
||||
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
|
||||
if relativeSourcePath == "" {
|
||||
viper.AddConfigPath(".")
|
||||
v.AddConfigPath(".")
|
||||
} else {
|
||||
viper.AddConfigPath(relativeSourcePath)
|
||||
v.AddConfigPath(relativeSourcePath)
|
||||
}
|
||||
err := viper.ReadInConfig()
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
if _, ok := err.(viper.ConfigParseError); ok {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return fmt.Errorf("Unable to locate Config file. Perhaps you need to create a new site.\n Run `hugo help new` for details. (%s)\n", err)
|
||||
return nil, fmt.Errorf("Unable to locate Config file. Perhaps you need to create a new site.\n Run `hugo help new` for details. (%s)\n", err)
|
||||
}
|
||||
|
||||
viper.RegisterAlias("indexes", "taxonomies")
|
||||
v.RegisterAlias("indexes", "taxonomies")
|
||||
|
||||
loadDefaultSettings()
|
||||
loadDefaultSettingsFor(v)
|
||||
|
||||
return nil
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func loadDefaultSettings() {
|
||||
viper.SetDefault("cleanDestinationDir", false)
|
||||
viper.SetDefault("watch", false)
|
||||
viper.SetDefault("metaDataFormat", "toml")
|
||||
viper.SetDefault("disable404", false)
|
||||
viper.SetDefault("disableRSS", false)
|
||||
viper.SetDefault("disableSitemap", false)
|
||||
viper.SetDefault("disableRobotsTXT", false)
|
||||
viper.SetDefault("contentDir", "content")
|
||||
viper.SetDefault("layoutDir", "layouts")
|
||||
viper.SetDefault("staticDir", "static")
|
||||
viper.SetDefault("archetypeDir", "archetypes")
|
||||
viper.SetDefault("publishDir", "public")
|
||||
viper.SetDefault("dataDir", "data")
|
||||
viper.SetDefault("i18nDir", "i18n")
|
||||
viper.SetDefault("themesDir", "themes")
|
||||
viper.SetDefault("defaultLayout", "post")
|
||||
viper.SetDefault("buildDrafts", false)
|
||||
viper.SetDefault("buildFuture", false)
|
||||
viper.SetDefault("buildExpired", false)
|
||||
viper.SetDefault("uglyURLs", false)
|
||||
viper.SetDefault("verbose", false)
|
||||
viper.SetDefault("ignoreCache", false)
|
||||
viper.SetDefault("canonifyURLs", false)
|
||||
viper.SetDefault("relativeURLs", false)
|
||||
viper.SetDefault("removePathAccents", false)
|
||||
viper.SetDefault("taxonomies", map[string]string{"tag": "tags", "category": "categories"})
|
||||
viper.SetDefault("permalinks", make(PermalinkOverrides, 0))
|
||||
viper.SetDefault("sitemap", Sitemap{Priority: -1, Filename: "sitemap.xml"})
|
||||
viper.SetDefault("defaultExtension", "html")
|
||||
viper.SetDefault("pygmentsStyle", "monokai")
|
||||
viper.SetDefault("pygmentsUseClasses", false)
|
||||
viper.SetDefault("pygmentsCodeFences", false)
|
||||
viper.SetDefault("pygmentsOptions", "")
|
||||
viper.SetDefault("disableLiveReload", false)
|
||||
viper.SetDefault("pluralizeListTitles", true)
|
||||
viper.SetDefault("preserveTaxonomyNames", false)
|
||||
viper.SetDefault("forceSyncStatic", false)
|
||||
viper.SetDefault("footnoteAnchorPrefix", "")
|
||||
viper.SetDefault("footnoteReturnLinkContents", "")
|
||||
viper.SetDefault("newContentEditor", "")
|
||||
viper.SetDefault("paginate", 10)
|
||||
viper.SetDefault("paginatePath", "page")
|
||||
viper.SetDefault("blackfriday", helpers.NewBlackfriday(viper.GetViper()))
|
||||
viper.SetDefault("rSSUri", "index.xml")
|
||||
viper.SetDefault("sectionPagesMenu", "")
|
||||
viper.SetDefault("disablePathToLower", false)
|
||||
viper.SetDefault("hasCJKLanguage", false)
|
||||
viper.SetDefault("enableEmoji", false)
|
||||
viper.SetDefault("pygmentsCodeFencesGuessSyntax", false)
|
||||
viper.SetDefault("useModTimeAsFallback", false)
|
||||
viper.SetDefault("currentContentLanguage", helpers.NewDefaultLanguage())
|
||||
viper.SetDefault("defaultContentLanguage", "en")
|
||||
viper.SetDefault("defaultContentLanguageInSubdir", false)
|
||||
viper.SetDefault("enableMissingTranslationPlaceholders", false)
|
||||
viper.SetDefault("enableGitInfo", false)
|
||||
func loadDefaultSettingsFor(v *viper.Viper) {
|
||||
|
||||
c := helpers.NewContentSpec(v)
|
||||
|
||||
v.SetDefault("cleanDestinationDir", false)
|
||||
v.SetDefault("watch", false)
|
||||
v.SetDefault("metaDataFormat", "toml")
|
||||
v.SetDefault("disable404", false)
|
||||
v.SetDefault("disableRSS", false)
|
||||
v.SetDefault("disableSitemap", false)
|
||||
v.SetDefault("disableRobotsTXT", false)
|
||||
v.SetDefault("contentDir", "content")
|
||||
v.SetDefault("layoutDir", "layouts")
|
||||
v.SetDefault("staticDir", "static")
|
||||
v.SetDefault("archetypeDir", "archetypes")
|
||||
v.SetDefault("publishDir", "public")
|
||||
v.SetDefault("dataDir", "data")
|
||||
v.SetDefault("i18nDir", "i18n")
|
||||
v.SetDefault("themesDir", "themes")
|
||||
v.SetDefault("defaultLayout", "post")
|
||||
v.SetDefault("buildDrafts", false)
|
||||
v.SetDefault("buildFuture", false)
|
||||
v.SetDefault("buildExpired", false)
|
||||
v.SetDefault("uglyURLs", false)
|
||||
v.SetDefault("verbose", false)
|
||||
v.SetDefault("ignoreCache", false)
|
||||
v.SetDefault("canonifyURLs", false)
|
||||
v.SetDefault("relativeURLs", false)
|
||||
v.SetDefault("removePathAccents", false)
|
||||
v.SetDefault("taxonomies", map[string]string{"tag": "tags", "category": "categories"})
|
||||
v.SetDefault("permalinks", make(PermalinkOverrides, 0))
|
||||
v.SetDefault("sitemap", Sitemap{Priority: -1, Filename: "sitemap.xml"})
|
||||
v.SetDefault("defaultExtension", "html")
|
||||
v.SetDefault("pygmentsStyle", "monokai")
|
||||
v.SetDefault("pygmentsUseClasses", false)
|
||||
v.SetDefault("pygmentsCodeFences", false)
|
||||
v.SetDefault("pygmentsOptions", "")
|
||||
v.SetDefault("disableLiveReload", false)
|
||||
v.SetDefault("pluralizeListTitles", true)
|
||||
v.SetDefault("preserveTaxonomyNames", false)
|
||||
v.SetDefault("forceSyncStatic", false)
|
||||
v.SetDefault("footnoteAnchorPrefix", "")
|
||||
v.SetDefault("footnoteReturnLinkContents", "")
|
||||
v.SetDefault("newContentEditor", "")
|
||||
v.SetDefault("paginate", 10)
|
||||
v.SetDefault("paginatePath", "page")
|
||||
v.SetDefault("blackfriday", c.NewBlackfriday())
|
||||
v.SetDefault("rSSUri", "index.xml")
|
||||
v.SetDefault("sectionPagesMenu", "")
|
||||
v.SetDefault("disablePathToLower", false)
|
||||
v.SetDefault("hasCJKLanguage", false)
|
||||
v.SetDefault("enableEmoji", false)
|
||||
v.SetDefault("pygmentsCodeFencesGuessSyntax", false)
|
||||
v.SetDefault("useModTimeAsFallback", false)
|
||||
v.SetDefault("defaultContentLanguage", "en")
|
||||
v.SetDefault("defaultContentLanguageInSubdir", false)
|
||||
v.SetDefault("enableMissingTranslationPlaceholders", false)
|
||||
v.SetDefault("enableGitInfo", false)
|
||||
}
|
||||
|
Reference in New Issue
Block a user