Fix disabled languages regression

Fixes #11959
This commit is contained in:
Bjørn Erik Pedersen 2024-02-01 09:37:05 +01:00
parent 5dd06b4136
commit 4174a7866b
7 changed files with 113 additions and 19 deletions

View 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 {

View 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 {

View 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) {

View 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() {

View 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)
}

View 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

View 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)
}