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:
Bjørn Erik Pedersen
2017-02-05 10:20:06 +07:00
parent e34af6ee30
commit 93ca7c9e95
99 changed files with 2843 additions and 2458 deletions

View File

@@ -16,6 +16,7 @@ package helpers
import (
"fmt"
"github.com/spf13/hugo/config"
"github.com/spf13/hugo/hugofs"
)
@@ -26,11 +27,20 @@ type PathSpec struct {
uglyURLs bool
canonifyURLs bool
currentContentLanguage *Language
language *Language
// pagination path handling
paginatePath string
baseURL string
theme string
// Directories
themesDir string
layoutDir string
workingDir string
staticDir string
// The PathSpec looks up its config settings in both the current language
// and then in the global Viper config.
// Some settings, the settings listed below, does not make sense to be set
@@ -45,31 +55,35 @@ type PathSpec struct {
}
func (p PathSpec) String() string {
return fmt.Sprintf("PathSpec, language %q, prefix %q, multilingual: %T", p.currentContentLanguage.Lang, p.getLanguagePrefix(), p.multilingual)
return fmt.Sprintf("PathSpec, language %q, prefix %q, multilingual: %T", p.language.Lang, p.getLanguagePrefix(), p.multilingual)
}
// NewPathSpec creats a new PathSpec from the given filesystems and ConfigProvider.
func NewPathSpec(fs *hugofs.Fs, config ConfigProvider) *PathSpec {
// NewPathSpec creats a new PathSpec from the given filesystems and Language.
func NewPathSpec(fs *hugofs.Fs, cfg config.Provider) *PathSpec {
currCl, ok := config.Get("currentContentLanguage").(*Language)
if !ok {
// TODO(bep) globals
currCl = NewLanguage("en")
}
return &PathSpec{
ps := &PathSpec{
fs: fs,
disablePathToLower: config.GetBool("disablePathToLower"),
removePathAccents: config.GetBool("removePathAccents"),
uglyURLs: config.GetBool("uglyURLs"),
canonifyURLs: config.GetBool("canonifyURLs"),
multilingual: config.GetBool("multilingual"),
defaultContentLanguageInSubdir: config.GetBool("defaultContentLanguageInSubdir"),
defaultContentLanguage: config.GetString("defaultContentLanguage"),
currentContentLanguage: currCl,
paginatePath: config.GetString("paginatePath"),
disablePathToLower: cfg.GetBool("disablePathToLower"),
removePathAccents: cfg.GetBool("removePathAccents"),
uglyURLs: cfg.GetBool("uglyURLs"),
canonifyURLs: cfg.GetBool("canonifyURLs"),
multilingual: cfg.GetBool("multilingual"),
defaultContentLanguageInSubdir: cfg.GetBool("defaultContentLanguageInSubdir"),
defaultContentLanguage: cfg.GetString("defaultContentLanguage"),
paginatePath: cfg.GetString("paginatePath"),
baseURL: cfg.GetString("baseURL"),
themesDir: cfg.GetString("themesDir"),
layoutDir: cfg.GetString("layoutDir"),
workingDir: cfg.GetString("workingDir"),
staticDir: cfg.GetString("staticDir"),
theme: cfg.GetString("theme"),
}
if language, ok := cfg.(*Language); ok {
ps.language = language
}
return ps
}
// PaginatePath returns the configured root path used for paginator pages.