Create a struct with all of Hugo's config options

Primary motivation is documentation, but it will also hopefully simplify the code.

Also,

* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.

Closes #10896
Closes #10620
This commit is contained in:
Bjørn Erik Pedersen
2023-01-04 18:24:36 +01:00
parent 6aededf6b4
commit 241b21b0fd
337 changed files with 13377 additions and 14898 deletions

View File

@@ -14,14 +14,12 @@
package paths
import (
"fmt"
"path/filepath"
"strings"
hpaths "github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/modules"
"github.com/gohugoio/hugo/hugofs"
@@ -31,100 +29,26 @@ var FilePathSeparator = string(filepath.Separator)
type Paths struct {
Fs *hugofs.Fs
Cfg config.Provider
BaseURL
BaseURLString string
BaseURLNoPathString string
// If the baseURL contains a base path, e.g. https://example.com/docs, then "/docs" will be the BasePath.
BasePath string
// Directories
// TODO(bep) when we have trimmed down most of the dirs usage outside of this package, make
// these into an interface.
ThemesDir string
WorkingDir string
Cfg config.AllProvider
// Directories to store Resource related artifacts.
AbsResourcesDir string
AbsPublishDir string
// pagination path handling
PaginatePath string
// When in multihost mode, this returns a list of base paths below PublishDir
// for each language.
MultihostTargetBasePaths []string
DisablePathToLower bool
RemovePathAccents bool
UglyURLs bool
CanonifyURLs bool
Language *langs.Language
Languages langs.Languages
LanguagesDefaultFirst langs.Languages
// 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
// on per-language-basis. We have no good way of protecting against this
// other than a "white-list". See language.go.
defaultContentLanguageInSubdir bool
DefaultContentLanguage string
multilingual bool
AllModules modules.Modules
ModulesClient *modules.Client
}
func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
baseURLstr := cfg.GetString("baseURL")
baseURL, err := newBaseURLFromString(baseURLstr)
if err != nil {
return nil, fmt.Errorf("Failed to create baseURL from %q:: %w", baseURLstr, err)
}
contentDir := filepath.Clean(cfg.GetString("contentDir"))
workingDir := filepath.Clean(cfg.GetString("workingDir"))
resourceDir := filepath.Clean(cfg.GetString("resourceDir"))
publishDir := filepath.Clean(cfg.GetString("publishDir"))
func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) {
bcfg := cfg.BaseConfig()
publishDir := bcfg.PublishDir
if publishDir == "" {
return nil, fmt.Errorf("publishDir not set")
panic("publishDir not set")
}
defaultContentLanguage := cfg.GetString("defaultContentLanguage")
var (
language *langs.Language
languages langs.Languages
languagesDefaultFirst langs.Languages
)
if l, ok := cfg.(*langs.Language); ok {
language = l
}
if l, ok := cfg.Get("languagesSorted").(langs.Languages); ok {
languages = l
}
if l, ok := cfg.Get("languagesSortedDefaultFirst").(langs.Languages); ok {
languagesDefaultFirst = l
}
//
if len(languages) == 0 {
// We have some old tests that does not test the entire chain, hence
// they have no languages. So create one so we get the proper filesystem.
languages = langs.Languages{&langs.Language{Lang: "en", Cfg: cfg, ContentDir: contentDir}}
}
absPublishDir := hpaths.AbsPathify(workingDir, publishDir)
absPublishDir := hpaths.AbsPathify(bcfg.WorkingDir, publishDir)
if !strings.HasSuffix(absPublishDir, FilePathSeparator) {
absPublishDir += FilePathSeparator
}
@@ -132,7 +56,7 @@ func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
if absPublishDir == "//" {
absPublishDir = FilePathSeparator
}
absResourcesDir := hpaths.AbsPathify(workingDir, resourceDir)
absResourcesDir := hpaths.AbsPathify(bcfg.WorkingDir, cfg.Dirs().ResourceDir)
if !strings.HasSuffix(absResourcesDir, FilePathSeparator) {
absResourcesDir += FilePathSeparator
}
@@ -141,76 +65,45 @@ func New(fs *hugofs.Fs, cfg config.Provider) (*Paths, error) {
}
var multihostTargetBasePaths []string
if languages.IsMultihost() {
for _, l := range languages {
if cfg.IsMultihost() && len(cfg.Languages()) > 1 {
for _, l := range cfg.Languages() {
multihostTargetBasePaths = append(multihostTargetBasePaths, l.Lang)
}
}
var baseURLString = baseURL.String()
var baseURLNoPath = baseURL.URL()
baseURLNoPath.Path = ""
var baseURLNoPathString = baseURLNoPath.String()
p := &Paths{
Fs: fs,
Cfg: cfg,
BaseURL: baseURL,
BaseURLString: baseURLString,
BaseURLNoPathString: baseURLNoPathString,
DisablePathToLower: cfg.GetBool("disablePathToLower"),
RemovePathAccents: cfg.GetBool("removePathAccents"),
UglyURLs: cfg.GetBool("uglyURLs"),
CanonifyURLs: cfg.GetBool("canonifyURLs"),
ThemesDir: cfg.GetString("themesDir"),
WorkingDir: workingDir,
AbsResourcesDir: absResourcesDir,
AbsPublishDir: absPublishDir,
multilingual: cfg.GetBool("multilingual"),
defaultContentLanguageInSubdir: cfg.GetBool("defaultContentLanguageInSubdir"),
DefaultContentLanguage: defaultContentLanguage,
Language: language,
Languages: languages,
LanguagesDefaultFirst: languagesDefaultFirst,
Fs: fs,
Cfg: cfg,
AbsResourcesDir: absResourcesDir,
AbsPublishDir: absPublishDir,
MultihostTargetBasePaths: multihostTargetBasePaths,
PaginatePath: cfg.GetString("paginatePath"),
}
if cfg.IsSet("allModules") {
p.AllModules = cfg.Get("allModules").(modules.Modules)
}
if cfg.IsSet("modulesClient") {
p.ModulesClient = cfg.Get("modulesClient").(*modules.Client)
}
return p, nil
}
func (p *Paths) AllModules() modules.Modules {
return p.Cfg.GetConfigSection("activeModules").(modules.Modules)
}
// GetBasePath returns any path element in baseURL if needed.
func (p *Paths) GetBasePath(isRelativeURL bool) string {
if isRelativeURL && p.CanonifyURLs {
if isRelativeURL && p.Cfg.CanonifyURLs() {
// The baseURL will be prepended later.
return ""
}
return p.BasePath
return p.Cfg.BaseURL().BasePath
}
func (p *Paths) Lang() string {
if p == nil || p.Language == nil {
if p == nil || p.Cfg.Language() == nil {
return ""
}
return p.Language.Lang
return p.Cfg.Language().Lang
}
func (p *Paths) GetTargetLanguageBasePath() string {
if p.Languages.IsMultihost() {
if len(p.Cfg.Languages()) > 1 {
// In a multihost configuration all assets will be published below the language code.
return p.Lang()
}
@@ -218,21 +111,19 @@ func (p *Paths) GetTargetLanguageBasePath() string {
}
func (p *Paths) GetURLLanguageBasePath() string {
if p.Languages.IsMultihost() {
if len(p.Cfg.Languages()) > 1 {
return ""
}
return p.GetLanguagePrefix()
}
func (p *Paths) GetLanguagePrefix() string {
if !p.multilingual {
if len(p.Cfg.Languages()) < 2 {
return ""
}
defaultLang := p.DefaultContentLanguage
defaultInSubDir := p.defaultContentLanguageInSubdir
currentLang := p.Language.Lang
defaultLang := p.Cfg.DefaultContentLanguage()
defaultInSubDir := p.Cfg.DefaultContentLanguageInSubdir()
currentLang := p.Cfg.Language().Lang
if currentLang == "" || (currentLang == defaultLang && !defaultInSubDir) {
return ""
}
@@ -241,15 +132,15 @@ func (p *Paths) GetLanguagePrefix() string {
// GetLangSubDir returns the given language's subdir if needed.
func (p *Paths) GetLangSubDir(lang string) string {
if !p.multilingual {
if len(p.Cfg.Languages()) < 2 {
return ""
}
if p.Languages.IsMultihost() {
if p.Cfg.IsMultihost() {
return ""
}
if lang == "" || (lang == p.DefaultContentLanguage && !p.defaultContentLanguageInSubdir) {
if lang == "" || (lang == p.Cfg.DefaultContentLanguage() && !p.Cfg.DefaultContentLanguageInSubdir()) {
return ""
}
@@ -259,7 +150,7 @@ func (p *Paths) GetLangSubDir(lang string) string {
// AbsPathify creates an absolute path if given a relative path. If already
// absolute, the path is just cleaned.
func (p *Paths) AbsPathify(inPath string) string {
return hpaths.AbsPathify(p.WorkingDir, inPath)
return hpaths.AbsPathify(p.Cfg.BaseConfig().WorkingDir, inPath)
}
// RelPathify trims any WorkingDir prefix from the given filename. If
@@ -270,5 +161,5 @@ func (p *Paths) RelPathify(filename string) string {
return filename
}
return strings.TrimPrefix(strings.TrimPrefix(filename, p.WorkingDir), FilePathSeparator)
return strings.TrimPrefix(strings.TrimPrefix(filename, p.Cfg.BaseConfig().WorkingDir), FilePathSeparator)
}