Skip to content

Commit 9ad26b6

Browse files
committed
Fix it so e.g. de in layouts/_shortcodes/de.html is not interpreted as a language code
Fixes #13740
1 parent f471936 commit 9ad26b6

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

common/paths/pathparser.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ func (pp *PathParser) parse(component, s string) (*Path, error) {
120120
return p, nil
121121
}
122122

123-
func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot int) {
123+
func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot, numDots int) {
124124
if p.posContainerHigh != -1 {
125125
return
126126
}
127-
mayHaveLang := p.posIdentifierLanguage == -1 && pp.LanguageIndex != nil
127+
mayHaveLang := numDots > 1 && p.posIdentifierLanguage == -1 && pp.LanguageIndex != nil
128128
mayHaveLang = mayHaveLang && (component == files.ComponentFolderContent || component == files.ComponentFolderLayouts)
129129
mayHaveOutputFormat := component == files.ComponentFolderLayouts
130130
mayHaveKind := p.posIdentifierKind == -1 && mayHaveOutputFormat
@@ -167,7 +167,6 @@ func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot i
167167
if langFound {
168168
p.identifiersKnown = append(p.identifiersKnown, id)
169169
p.posIdentifierLanguage = len(p.identifiersKnown) - 1
170-
171170
}
172171
}
173172

@@ -234,19 +233,21 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
234233
p.s = s
235234
slashCount := 0
236235
lastDot := 0
236+
lastSlashIdx := strings.LastIndex(s, "/")
237+
numDots := strings.Count(s[lastSlashIdx+1:], ".")
237238

238239
for i := len(s) - 1; i >= 0; i-- {
239240
c := s[i]
240241

241242
switch c {
242243
case '.':
243-
pp.parseIdentifier(component, s, p, i, lastDot)
244+
pp.parseIdentifier(component, s, p, i, lastDot, numDots)
244245
lastDot = i
245246
case '/':
246247
slashCount++
247248
if p.posContainerHigh == -1 {
248249
if lastDot > 0 {
249-
pp.parseIdentifier(component, s, p, i, lastDot)
250+
pp.parseIdentifier(component, s, p, i, lastDot, numDots)
250251
}
251252
p.posContainerHigh = i + 1
252253
} else if p.posContainerLow == -1 {

common/paths/pathparser_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,20 @@ func TestParseLayouts(t *testing.T) {
563563
c.Assert(p.Type(), qt.Equals, TypePartial)
564564
},
565565
},
566+
{
567+
"Shortcode lang in root",
568+
"/_shortcodes/no.html",
569+
func(c *qt.C, p *Path) {
570+
c.Assert(p.Type(), qt.Equals, TypeShortcode)
571+
c.Assert(p.Lang(), qt.Equals, "")
572+
c.Assert(p.NameNoIdentifier(), qt.Equals, "no")
573+
},
574+
},
566575
}
567576

568577
for _, test := range tests {
569578
c.Run(test.name, func(c *qt.C) {
570-
if test.name != "Baseof" {
579+
if test.name != "Shortcode lang in root" {
571580
// return
572581
}
573582
test.assert(c, testParser.Parse(files.ComponentFolderLayouts, test.path))

tpl/tplimpl/shortcodes_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,16 +730,16 @@ SHORTCODE
730730
b.Assert(err.Error(), qt.Contains, `no compatible template found for shortcode "mymarkdown" in [/_shortcodes/mymarkdown.md]; note that to use plain text template shortcodes in HTML you need to use the shortcode {{% delimiter`)
731731
}
732732

733-
func TestShortcodeOnlyLanguageInBaseIssue13699(t *testing.T) {
733+
func TestShortcodeOnlyLanguageInBaseIssue13699And13740(t *testing.T) {
734734
t.Parallel()
735735

736736
files := `
737737
-- hugo.toml --
738738
baseURL = 'https://example.org/'
739+
disableLanguages = ['de']
739740
[languages]
740741
[languages.en]
741742
weight = 1
742-
disableLanguages = ['de']
743743
[languages.de]
744744
weight = 2
745745
-- layouts/_shortcodes/de.html --

0 commit comments

Comments
 (0)