Skip to content

Commit 958d82b

Browse files
committed
Cascade
1 parent 3a3a411 commit 958d82b

33 files changed

+497
-359
lines changed

‎config/allconfig/allconfig.go‎

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,15 @@ type Config struct {
142142
// The outputformats configuration sections maps a format name (a string) to a configuration object for that format.
143143
OutputFormats *config.ConfigNamespace[map[string]output.OutputFormatConfig, output.Formats] `mapstructure:"-"`
144144

145-
// The roles configuration section contains the top level roles configuration options.
146-
Roles *config.ConfigNamespace[map[string]roles.RoleConfig, roles.RolesInternal] `mapstructure:"-"`
145+
// The languages configuration sections maps a language code (a string) to a configuration object for that language.
146+
Languages *config.ConfigNamespace[map[string]langs.LanguageConfig, langs.LanguagesInternal] `mapstructure:"-"`
147147

148148
// The versions configuration section contains the top level versions configuration options.
149149
Versions *config.ConfigNamespace[map[string]versions.VersionConfig, versions.VersionsInternal] `mapstructure:"-"`
150150

151+
// The roles configuration section contains the top level roles configuration options.
152+
Roles *config.ConfigNamespace[map[string]roles.RoleConfig, roles.RolesInternal] `mapstructure:"-"`
153+
151154
// The outputs configuration section maps a Page Kind (a string) to a slice of output formats.
152155
// This can be overridden in the front matter.
153156
Outputs map[string][]string `mapstructure:"-"`
@@ -209,9 +212,6 @@ type Config struct {
209212
// <docsmeta>{"refs": ["config:languages:params"] }</docsmeta>
210213
Params maps.Params `mapstructure:"-"`
211214

212-
// The languages configuration sections maps a language code (a string) to a configuration object for that language.
213-
Languages map[string]langs.LanguageConfig `mapstructure:"-"`
214-
215215
// UglyURLs configuration. Either a boolean or a sections map.
216216
UglyURLs any `mapstructure:"-"`
217217
}
@@ -328,18 +328,6 @@ func (c *Config) CompileConfig(logger loggers.Logger) error {
328328
for _, lang := range c.DisableLanguages {
329329
disabledLangs[lang] = true
330330
}
331-
for lang, language := range c.Languages {
332-
if !language.Disabled && disabledLangs[lang] {
333-
language.Disabled = true
334-
c.Languages[lang] = language
335-
}
336-
if language.Disabled {
337-
disabledLangs[lang] = true
338-
if lang == c.DefaultContentLanguage {
339-
return fmt.Errorf("cannot disable default content language %q", lang)
340-
}
341-
}
342-
}
343331

344332
for i, s := range c.IgnoreLogs {
345333
c.IgnoreLogs[i] = strings.ToLower(s)
@@ -821,51 +809,10 @@ func (c *Configs) IsZero() bool {
821809
func (c *Configs) Init() error {
822810
var languages langs.Languages
823811

824-
var langKeys []string
825-
var hasEn bool
826-
827-
const en = "en"
828-
829-
for k := range c.LanguageConfigMap {
830-
langKeys = append(langKeys, k)
831-
if k == en {
832-
hasEn = true
833-
}
834-
}
835-
836-
// Sort the LanguageConfigSlice by language weight (if set) or lang.
837-
sort.Slice(langKeys, func(i, j int) bool {
838-
ki := langKeys[i]
839-
kj := langKeys[j]
840-
lki := c.LanguageConfigMap[ki]
841-
lkj := c.LanguageConfigMap[kj]
842-
li := lki.Languages[ki]
843-
lj := lkj.Languages[kj]
844-
if li.Weight != lj.Weight {
845-
return li.Weight < lj.Weight
846-
}
847-
return ki < kj
848-
})
849-
850-
// See issue #13646.
851-
defaultConfigLanguageFallback := en
852-
if !hasEn {
853-
// Pick the first one.
854-
defaultConfigLanguageFallback = langKeys[0]
855-
}
856-
857-
if c.Base.DefaultContentLanguage == "" {
858-
c.Base.DefaultContentLanguage = defaultConfigLanguageFallback
859-
}
860-
861-
for _, k := range langKeys {
862-
v := c.LanguageConfigMap[k]
863-
if v.DefaultContentLanguage == "" {
864-
v.DefaultContentLanguage = defaultConfigLanguageFallback
865-
}
866-
c.LanguageConfigSlice = append(c.LanguageConfigSlice, v)
867-
languageConf := v.Languages[k]
868-
language, err := langs.NewLanguage(k, c.Base.DefaultContentLanguage, v.TimeZone, languageConf)
812+
// TODO1 more cleanups, please.
813+
for _, f := range c.Base.Languages.Config.Sorted {
814+
v := c.LanguageConfigMap[f.Name]
815+
language, err := langs.NewLanguage(f.Name, c.Base.DefaultContentLanguage, v.TimeZone, f.LanguageConfig)
869816
if err != nil {
870817
return err
871818
}
@@ -968,7 +915,7 @@ func (c Configs) GetFirstLanguageConfig() config.AllProvider {
968915

969916
func (c Configs) GetByLang(lang string) config.AllProvider {
970917
for _, l := range c.configLangs {
971-
if l.Language().Lang == lang {
918+
if l.Language().(*langs.Language).Lang == lang {
972919
return l
973920
}
974921
}

‎config/allconfig/alldecoders.go‎

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strings"
1919

2020
"github.com/gohugoio/hugo/cache/filecache"
21+
"github.com/gohugoio/hugo/langs"
2122

2223
"github.com/gohugoio/hugo/cache/httpcache"
2324
"github.com/gohugoio/hugo/common/maps"
@@ -30,7 +31,6 @@ import (
3031
"github.com/gohugoio/hugo/hugolib/roles"
3132
"github.com/gohugoio/hugo/hugolib/segments"
3233
"github.com/gohugoio/hugo/hugolib/versions"
33-
"github.com/gohugoio/hugo/langs"
3434
"github.com/gohugoio/hugo/markup/markup_config"
3535
"github.com/gohugoio/hugo/media"
3636
"github.com/gohugoio/hugo/minifiers"
@@ -214,17 +214,32 @@ var allDecoderSetups = map[string]decodeWeight{
214214
return err
215215
},
216216
},
217-
"roles": {
218-
key: "roles",
217+
"languages": {
218+
key: "languages",
219219
decode: func(d decodeWeight, p decodeConfig) error {
220+
m := maps.CleanConfigStringMap(p.p.GetStringMap(d.key))
221+
if len(m) == 1 {
222+
// In v0.112.4 we moved this to the language config, but it's very commmon for mono language sites to have this at the top level.
223+
var first maps.Params
224+
var ok bool
225+
for _, v := range m {
226+
first, ok = v.(maps.Params)
227+
if ok {
228+
break
229+
}
230+
}
231+
if first != nil {
232+
if _, found := first["languagecode"]; !found {
233+
first["languagecode"] = p.p.GetString("languagecode")
234+
}
235+
}
236+
}
220237
var (
221-
err error
222-
defaultContentRole string
238+
err error
239+
defaultContentLanguage string
223240
)
224-
m := maps.CleanConfigStringMap(p.p.GetStringMap(d.key))
225-
p.c.Roles, defaultContentRole, err = roles.DecodeConfig(p.c.RootConfig.DefaultContentRole, m)
226-
p.c.RootConfig.DefaultContentRole = defaultContentRole
227-
241+
p.c.Languages, defaultContentLanguage, err = langs.DecodeConfig2(p.c.RootConfig.DefaultContentLanguage, p.c.RootConfig.DisableLanguages, m)
242+
p.c.RootConfig.DefaultContentLanguage = defaultContentLanguage
228243
return err
229244
},
230245
},
@@ -237,6 +252,21 @@ var allDecoderSetups = map[string]decodeWeight{
237252
return err
238253
},
239254
},
255+
"roles": {
256+
key: "roles",
257+
decode: func(d decodeWeight, p decodeConfig) error {
258+
var (
259+
err error
260+
defaultContentRole string
261+
)
262+
m := maps.CleanConfigStringMap(p.p.GetStringMap(d.key))
263+
p.c.Roles, defaultContentRole, err = roles.DecodeConfig(p.c.RootConfig.DefaultContentRole, m)
264+
p.c.RootConfig.DefaultContentRole = defaultContentRole
265+
266+
return err
267+
},
268+
},
269+
240270
"params": {
241271
key: "params",
242272
decode: func(d decodeWeight, p decodeConfig) error {
@@ -310,49 +340,7 @@ var allDecoderSetups = map[string]decodeWeight{
310340
return nil
311341
},
312342
},
313-
"languages": {
314-
key: "languages",
315-
decode: func(d decodeWeight, p decodeConfig) error {
316-
var err error
317-
m := p.p.GetStringMap(d.key)
318-
if len(m) == 1 {
319-
// In v0.112.4 we moved this to the language config, but it's very commmon for mono language sites to have this at the top level.
320-
var first maps.Params
321-
var ok bool
322-
for _, v := range m {
323-
first, ok = v.(maps.Params)
324-
if ok {
325-
break
326-
}
327-
}
328-
if first != nil {
329-
if _, found := first["languagecode"]; !found {
330-
first["languagecode"] = p.p.GetString("languagecode")
331-
}
332-
}
333-
}
334-
p.c.Languages, err = langs.DecodeConfig(m)
335-
if err != nil {
336-
return err
337-
}
338343

339-
// Validate defaultContentLanguage.
340-
if p.c.DefaultContentLanguage != "" {
341-
var found bool
342-
for lang := range p.c.Languages {
343-
if lang == p.c.DefaultContentLanguage {
344-
found = true
345-
break
346-
}
347-
}
348-
if !found {
349-
return fmt.Errorf("config value %q for defaultContentLanguage does not match any language definition", p.c.DefaultContentLanguage)
350-
}
351-
}
352-
353-
return nil
354-
},
355-
},
356344
"cascade": {
357345
key: "cascade",
358346
decode: func(d decodeWeight, p decodeConfig) error {

‎config/allconfig/configlanguage.go‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ type ConfigLanguage struct {
3131
language *langs.Language
3232
}
3333

34-
func (c ConfigLanguage) Language() *langs.Language {
34+
func (c ConfigLanguage) Language() any {
3535
return c.language
3636
}
3737

38-
func (c ConfigLanguage) Languages() langs.Languages {
38+
func (c ConfigLanguage) Languages() any {
3939
return c.m.Languages
4040
}
4141

42-
func (c ConfigLanguage) LanguagesDefaultFirst() langs.Languages {
42+
func (c ConfigLanguage) LanguagesDefaultFirst() any {
4343
return c.m.LanguagesDefaultFirst
4444
}
4545

@@ -48,14 +48,14 @@ func (c ConfigLanguage) PathParser() *paths.PathParser {
4848
}
4949

5050
func (c ConfigLanguage) LanguagePrefix() string {
51-
if c.DefaultContentLanguageInSubdir() && c.DefaultContentLanguage() == c.Language().Lang {
52-
return c.Language().Lang
51+
if c.DefaultContentLanguageInSubdir() && c.DefaultContentLanguage() == c.language.Lang {
52+
return c.language.Lang
5353
}
5454

55-
if !c.IsMultilingual() || c.DefaultContentLanguage() == c.Language().Lang {
55+
if !c.IsMultilingual() || c.DefaultContentLanguage() == c.language.Lang {
5656
return ""
5757
}
58-
return c.Language().Lang
58+
return c.language.Lang
5959
}
6060

6161
func (c ConfigLanguage) BaseURL() urls.BaseURL {
@@ -273,3 +273,11 @@ func (c ConfigLanguage) StaticDirs() []string {
273273
func (c ConfigLanguage) EnableEmoji() bool {
274274
return c.config.EnableEmoji
275275
}
276+
277+
func (c ConfigLanguage) ConfiguredDimensions() config.ConfiguredDimensions {
278+
return config.ConfiguredDimensions{
279+
ConfiguredLanguages: c.m.Base.Languages.Config,
280+
ConfiguredVersions: c.m.Base.Versions.Config,
281+
ConfiguredRoles: c.m.Base.Roles.Config,
282+
}
283+
}

‎config/configProvider.go‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ import (
1818

1919
"github.com/gohugoio/hugo/common/maps"
2020
"github.com/gohugoio/hugo/common/paths"
21+
"github.com/gohugoio/hugo/common/predicate"
2122
"github.com/gohugoio/hugo/common/types"
2223
"github.com/gohugoio/hugo/common/urls"
2324
"github.com/gohugoio/hugo/identity"
24-
"github.com/gohugoio/hugo/langs"
2525
)
2626

2727
// AllProvider is a sub set of all config settings.
2828
type AllProvider interface {
29-
Language() *langs.Language
30-
Languages() langs.Languages
31-
LanguagesDefaultFirst() langs.Languages
29+
// TODO1 get rid of these.
30+
Language() any
31+
Languages() any
32+
LanguagesDefaultFirst() any
33+
//
3234
LanguagePrefix() string
3335
BaseURL() urls.BaseURL
3436
BaseURLLiveReload() urls.BaseURL
@@ -75,6 +77,7 @@ type AllProvider interface {
7577
IgnoredLogs() map[string]bool
7678
WorkingDir() string
7779
EnableEmoji() bool
80+
ConfiguredDimensions() ConfiguredDimensions
7881
}
7982

8083
// We cannot import the media package as that would create a circular dependency.
@@ -112,3 +115,14 @@ func GetStringSlicePreserveString(cfg Provider, key string) []string {
112115
sd := cfg.Get(key)
113116
return types.ToStringSlicePreserveString(sd)
114117
}
118+
119+
type ConfiguredDimension interface {
120+
predicate.IndexMatcher
121+
IndexDefault() int
122+
}
123+
124+
type ConfiguredDimensions struct {
125+
ConfiguredLanguages ConfiguredDimension
126+
ConfiguredVersions ConfiguredDimension
127+
ConfiguredRoles ConfiguredDimension
128+
}

‎hugofs/fileinfo.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"github.com/gohugoio/hugo/hugofs/glob"
31+
"github.com/gohugoio/hugo/hugolib/dimensions"
3132

3233
"golang.org/x/text/unicode/norm"
3334

@@ -58,6 +59,10 @@ type FileMeta struct {
5859
IsProject bool
5960
Watch bool
6061

62+
Cascade map[string]any
63+
DimensionInts *dimensions.IntSets
64+
65+
// TODO1 remove Lang*
6166
// The lang associated with this file. This may be
6267
// either the language set in the filename or
6368
// the language defined in the source mount configuration.

‎hugofs/rootmapping_fs.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func (fs *RootMappingFs) getRoot(key string) []RootMapping {
407407
func (fs *RootMappingFs) getRoots(key string) (string, []RootMapping) {
408408
tree := fs.rootMapToReal
409409
levels := strings.Count(key, filepathSeparator)
410-
seen := make(map[RootMapping]bool)
410+
seen := make(map[RootMapping]bool) // TODO1 lang vs langugages etc.
411411

412412
var roots []RootMapping
413413
var s string

0 commit comments

Comments
 (0)