]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix disabled languages regression
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 1 Feb 2024 08:37:05 +0000 (09:37 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 1 Feb 2024 18:42:55 +0000 (19:42 +0100)
Fixes #11959

common/paths/pathparser.go
config/allconfig/allconfig.go
hugofs/component_fs.go
hugolib/content_map.go
hugolib/disableKinds_test.go
hugolib/page__new.go
hugolib/pagebundler_test.go

index 842d9307b064577fd85c2218c9ed7a11bbf7ebfd..897edb9b73d3186de7bdefa8ca77d97a2263d483 100644 (file)
@@ -29,6 +29,9 @@ var defaultPathParser PathParser
 type PathParser struct {
        // Maps the language code to its index in the languages/sites slice.
        LanguageIndex map[string]int
+
+       // Reports whether the given language is disabled.
+       IsLangDisabled func(string) bool
 }
 
 // Parse parses component c with path s into Path using the default path parser.
@@ -134,7 +137,16 @@ func (pp *PathParser) doParse(component, s string) (*Path, error) {
                                        s := p.s[id.Low:id.High]
 
                                        if hasLang {
-                                               if _, found := pp.LanguageIndex[s]; found {
+                                               var disabled bool
+                                               _, langFound := pp.LanguageIndex[s]
+                                               if !langFound {
+                                                       disabled = pp.IsLangDisabled != nil && pp.IsLangDisabled(s)
+                                                       if disabled {
+                                                               p.disabled = true
+                                                               langFound = true
+                                                       }
+                                               }
+                                               if langFound {
                                                        p.posIdentifierLanguage = 1
                                                        p.identifiers = append(p.identifiers, id)
                                                }
@@ -220,6 +232,7 @@ type Path struct {
        identifiers []types.LowHigh
 
        posIdentifierLanguage int
+       disabled              bool
 
        trimLeadingSlash bool
 
@@ -435,6 +448,10 @@ func (p *Path) Identifier(i int) string {
        return p.identifierAsString(i)
 }
 
+func (p *Path) Disabled() bool {
+       return p.disabled
+}
+
 func (p *Path) Identifiers() []string {
        ids := make([]string, len(p.identifiers))
        for i, id := range p.identifiers {
index 742a281504c5a86c74b21c8a629d8230e56aec7e..413baebd12fe6ac9d28346dd719e6f3f28575977 100644 (file)
@@ -733,7 +733,8 @@ func (c *Configs) Init() error {
 
        c.Languages = languages
        c.LanguagesDefaultFirst = languagesDefaultFirst
-       c.ContentPathParser = paths.PathParser{LanguageIndex: languagesDefaultFirst.AsIndexSet()}
+
+       c.ContentPathParser = paths.PathParser{LanguageIndex: languagesDefaultFirst.AsIndexSet(), IsLangDisabled: c.Base.IsLangDisabled}
 
        c.configLangs = make([]config.AllProvider, len(c.Languages))
        for i, l := range c.LanguagesDefaultFirst {
index 62d977ae889fbeb4a86696263635892a20c8251f..e75e23f84381c10ea27afe413ee873d928352ade 100644 (file)
@@ -94,11 +94,15 @@ func (f *componentFsDir) ReadDir(count int) ([]iofs.DirEntry, error) {
 
        fis = fis[:n]
 
+       n = 0
        for _, fi := range fis {
                s := path.Join(f.name, fi.Name())
-               _ = f.fs.applyMeta(fi, s)
-
+               if _, ok := f.fs.applyMeta(fi, s); ok {
+                       fis[n] = fi
+                       n++
+               }
        }
+       fis = fis[:n]
 
        sort.Slice(fis, func(i, j int) bool {
                fimi, fimj := fis[i].(FileMetaInfo), fis[j].(FileMetaInfo)
@@ -180,7 +184,8 @@ func (f *componentFsDir) Stat() (iofs.FileInfo, error) {
        if err != nil {
                return nil, err
        }
-       return f.fs.applyMeta(fi, f.name), nil
+       fim, _ := f.fs.applyMeta(fi, f.name)
+       return fim, nil
 }
 
 func (fs *componentFs) Stat(name string) (os.FileInfo, error) {
@@ -188,16 +193,26 @@ func (fs *componentFs) Stat(name string) (os.FileInfo, error) {
        if err != nil {
                return nil, err
        }
-       return fs.applyMeta(fi, name), nil
+       fim, _ := fs.applyMeta(fi, name)
+       return fim, nil
 }
 
-func (fs *componentFs) applyMeta(fi FileNameIsDir, name string) FileMetaInfo {
+func (fs *componentFs) applyMeta(fi FileNameIsDir, name string) (FileMetaInfo, bool) {
        if runtime.GOOS == "darwin" {
                name = norm.NFC.String(name)
        }
        fim := fi.(FileMetaInfo)
        meta := fim.Meta()
-       meta.PathInfo = fs.opts.PathParser.Parse(fs.opts.Component, name)
+       pi := fs.opts.PathParser.Parse(fs.opts.Component, name)
+       if pi.Disabled() {
+               return fim, false
+       }
+       if meta.Lang != "" {
+               if isLangDisabled := fs.opts.PathParser.IsLangDisabled; isLangDisabled != nil && isLangDisabled(meta.Lang) {
+                       return fim, false
+               }
+       }
+       meta.PathInfo = pi
        if !fim.IsDir() {
                if fileLang := meta.PathInfo.Lang(); fileLang != "" {
                        // A valid lang set in filename.
@@ -223,7 +238,7 @@ func (fs *componentFs) applyMeta(fi FileNameIsDir, name string) FileMetaInfo {
                }
        }
 
-       return fim
+       return fim, true
 }
 
 func (f *componentFsDir) Readdir(count int) ([]os.FileInfo, error) {
index 96013c4ed8c0aed7e7a1e606c4ef385f951809fe..85300e3db21503fa82c74e783cd413e9aa2ee9b2 100644 (file)
@@ -166,11 +166,6 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
                return nil
        }
 
-       meta := fi.Meta()
-       if m.s.conf.IsLangDisabled(meta.Lang) {
-               return nil
-       }
-
        insertResource := func(fim hugofs.FileMetaInfo) error {
                pi := fi.Meta().PathInfo
                key := pi.Base()
@@ -209,6 +204,7 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo) error {
                return nil
        }
 
+       meta := fi.Meta()
        pi := meta.PathInfo
 
        switch pi.BundleType() {
index 00652058010bd0cca95338fc25fc586545fd0f67..8262f562a084161f53d125820cddd2799b116e03 100644 (file)
@@ -416,3 +416,50 @@ Section: MySection|RelPermalink: |Outputs: 0
        b.Assert(b.CheckExists("public/sect/no-render/index.html"), qt.Equals, false)
        b.Assert(b.CheckExists("public/sect-no-render/index.html"), qt.Equals, false)
 }
+
+func TestDisableOneOfThreeLanguages(t *testing.T) {
+       files := `
+-- hugo.toml --
+baseURL = "https://example.com"
+defaultContentLanguage = "en"
+defaultContentLanguageInSubdir = true
+[languages]
+[languages.en]
+weight = 1
+title = "English"
+[languages.nn]
+weight = 2
+title = "Nynorsk"
+disabled = true
+[languages.nb]
+weight = 3
+title = "Bokmål"
+-- content/p1.nn.md --
+---
+title: "Page 1 nn"
+---
+-- content/p1.nb.md --
+---
+title: "Page 1 nb"
+---
+-- content/p1.en.md --
+---
+title: "Page 1 en"
+---
+-- content/p2.nn.md --
+---
+title: "Page 2 nn"
+---
+-- layouts/_default/single.html --
+{{ .Title }}
+`
+       b := Test(t, files)
+
+       b.Assert(len(b.H.Sites), qt.Equals, 2)
+       b.AssertFileContent("public/en/p1/index.html", "Page 1 en")
+       b.AssertFileContent("public/nb/p1/index.html", "Page 1 nb")
+
+       b.AssertFileExists("public/en/p2/index.html", false)
+       b.AssertFileExists("public/nn/p1/index.html", false)
+       b.AssertFileExists("public/nn/p2/index.html", false)
+}
index 3bef55f43fdf97d786a00d288f5d5b8639b07a9f..d846fe03c25e2089b3aa04ae79be4b68f496eb7d 100644 (file)
@@ -60,6 +60,11 @@ func (h *HugoSites) newPage(m *pageMeta) (*pageState, *paths.Path, error) {
                return nil, nil, m.wrapError(err, h.BaseFs.SourceFs)
        }
        pcfg := m.pageConfig
+       if pcfg.Lang != "" {
+               if h.Conf.IsLangDisabled(pcfg.Lang) {
+                       return nil, nil, nil
+               }
+       }
 
        if pcfg.Path != "" {
                s := m.pageConfig.Path
index 54ea04c7a8b2d1e24d2f51c9bda63821ab0e73bf..c6f9155ea1e2cdf3e0962f8d5da6c83181540cb5 100644 (file)
@@ -188,30 +188,43 @@ baseURL = "https://example.com"
 disableKinds = ["taxonomy", "term"]
 defaultContentLanguage = "en"
 defaultContentLanguageInSubdir = true
-disableLanguages = ["nn"]
 [languages]
 [languages.en]
 weight = 1
 [languages.nn]
 weight = 2
--- content/p1.md --
+disabled = true
+-- content/mysect/_index.md --
+---
+title: "My Sect En"
+---
+-- content/mysect/p1/index.md --
 ---
 title: "P1"
 ---
 P1
--- content/p1.nn.md --
+-- content/mysect/_index.nn.md --
+---
+title: "My Sect Nn"
+---
+-- content/mysect/p1/index.nn.md --
 ---
 title: "P1nn"
 ---
 P1nn
+-- layouts/index.html --
+Len RegularPages: {{ len .Site.RegularPages }}|RegularPages: {{ range site.RegularPages }}{{ .RelPermalink }}: {{ .Title }}|{{ end }}|
+Len Pages: {{ len .Site.Pages }}|
+Len Sites: {{ len .Site.Sites }}|
 -- layouts/_default/single.html --
 {{ .Title }}|{{ .Content }}|{{ .Lang }}|
 
 `
        b := Test(t, files)
 
-       b.AssertFileContent("public/en/p1/index.html", "P1|<p>P1</p>\n|en|")
-       b.AssertFileExists("public/public/nn/p1/index.html", false)
+       b.AssertFileContent("public/en/index.html", "Len RegularPages: 1|")
+       b.AssertFileContent("public/en/mysect/p1/index.html", "P1|<p>P1</p>\n|en|")
+       b.AssertFileExists("public/public/nn/mysect/p1/index.html", false)
        b.Assert(len(b.H.Sites), qt.Equals, 1)
 }