Skip to content

Commit 5273a88

Browse files
committed
Fix language handling in shortcode templates
Fixes #13767
1 parent 6334948 commit 5273a88

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

common/paths/pathparser.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,20 @@ 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, numDots int) {
123+
func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot, numDots int, isLast bool) {
124124
if p.posContainerHigh != -1 {
125125
return
126126
}
127127
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
131-
mayHaveLayout := component == files.ComponentFolderLayouts
131+
var mayHaveLayout bool
132+
if p.pathType == TypeShortcode {
133+
mayHaveLayout = !isLast && component == files.ComponentFolderLayouts
134+
} else {
135+
mayHaveLayout = component == files.ComponentFolderLayouts
136+
}
132137

133138
var found bool
134139
var high int
@@ -235,19 +240,22 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
235240
lastDot := 0
236241
lastSlashIdx := strings.LastIndex(s, "/")
237242
numDots := strings.Count(s[lastSlashIdx+1:], ".")
243+
if strings.Contains(s, "/_shortcodes/") {
244+
p.pathType = TypeShortcode
245+
}
238246

239247
for i := len(s) - 1; i >= 0; i-- {
240248
c := s[i]
241249

242250
switch c {
243251
case '.':
244-
pp.parseIdentifier(component, s, p, i, lastDot, numDots)
252+
pp.parseIdentifier(component, s, p, i, lastDot, numDots, false)
245253
lastDot = i
246254
case '/':
247255
slashCount++
248256
if p.posContainerHigh == -1 {
249257
if lastDot > 0 {
250-
pp.parseIdentifier(component, s, p, i, lastDot, numDots)
258+
pp.parseIdentifier(component, s, p, i, lastDot, numDots, true)
251259
}
252260
p.posContainerHigh = i + 1
253261
} else if p.posContainerLow == -1 {
@@ -283,10 +291,9 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
283291
p.pathType = TypeContentData
284292
}
285293
}
286-
287294
}
288295

289-
if component == files.ComponentFolderLayouts {
296+
if p.pathType < TypeMarkup && component == files.ComponentFolderLayouts {
290297
if p.posIdentifierBaseof != -1 {
291298
p.pathType = TypeBaseof
292299
} else {
@@ -302,12 +309,10 @@ func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) {
302309
}
303310

304311
if p.pathType == TypeShortcode && p.posIdentifierLayout != -1 {
305-
// myshortcode or myshortcode.html, no layout.
306-
if len(p.identifiersKnown) <= 2 {
312+
id := p.identifiersKnown[p.posIdentifierLayout]
313+
if id.Low == p.posContainerHigh {
314+
// First identifier is shortcode name.
307315
p.posIdentifierLayout = -1
308-
} else {
309-
// First is always the name.
310-
p.posIdentifierLayout--
311316
}
312317
}
313318

common/paths/pathparser_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,12 @@ func TestParseLayouts(t *testing.T) {
434434
},
435435
{
436436
"Layout multiple",
437-
"/maylayout.list.section.no.html",
437+
"/mylayout.list.section.no.html",
438438
func(c *qt.C, p *Path) {
439-
c.Assert(p.Layout(), qt.Equals, "maylayout")
440-
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "no", "section", "list", "maylayout"})
439+
c.Assert(p.Layout(), qt.Equals, "mylayout")
440+
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "no", "section", "list", "mylayout"})
441441
c.Assert(p.IdentifiersUnknown(), qt.DeepEquals, []string{})
442-
c.Assert(p.Base(), qt.Equals, "/maylayout.html")
442+
c.Assert(p.Base(), qt.Equals, "/mylayout.html")
443443
c.Assert(p.Lang(), qt.Equals, "no")
444444
},
445445
},
@@ -487,7 +487,8 @@ func TestParseLayouts(t *testing.T) {
487487
func(c *qt.C, p *Path) {
488488
c.Assert(p.Base(), qt.Equals, "/_shortcodes/myshortcode.html")
489489
c.Assert(p.Type(), qt.Equals, TypeShortcode)
490-
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "list", "myshortcode"})
490+
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"html", "list"})
491+
c.Assert(p.Layout(), qt.Equals, "list")
491492
c.Assert(p.PathNoIdentifier(), qt.Equals, "/_shortcodes/myshortcode")
492493
c.Assert(p.PathBeforeLangAndOutputFormatAndExt(), qt.Equals, "/_shortcodes/myshortcode.list")
493494
c.Assert(p.Lang(), qt.Equals, "")
@@ -572,11 +573,21 @@ func TestParseLayouts(t *testing.T) {
572573
c.Assert(p.NameNoIdentifier(), qt.Equals, "no")
573574
},
574575
},
576+
{
577+
"Shortcode lang layout",
578+
"/_shortcodes/myshortcode.no.html",
579+
func(c *qt.C, p *Path) {
580+
c.Assert(p.Type(), qt.Equals, TypeShortcode)
581+
c.Assert(p.Lang(), qt.Equals, "no")
582+
c.Assert(p.Layout(), qt.Equals, "")
583+
c.Assert(p.NameNoIdentifier(), qt.Equals, "myshortcode")
584+
},
585+
},
575586
}
576587

577588
for _, test := range tests {
578589
c.Run(test.name, func(c *qt.C) {
579-
if test.name != "Shortcode lang in root" {
590+
if test.name != "Shortcode lang layout" {
580591
// return
581592
}
582593
test.assert(c, testParser.Parse(files.ComponentFolderLayouts, test.path))

tpl/tplimpl/shortcodes_integration_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,40 @@ title: home
757757

758758
b.AssertFileContent("public/index.html", "de.html")
759759
}
760+
761+
func TestShortcodeLanguage13767(t *testing.T) {
762+
t.Parallel()
763+
764+
files := `
765+
-- hugo.toml --
766+
defaultContentLanguage = 'pl'
767+
defaultContentLanguageInSubdir = true
768+
[languages.pl]
769+
weight = 1
770+
[languages.en]
771+
weight = 2
772+
-- content/_index.md --
773+
---
774+
title: dom
775+
---
776+
{{< myshortcode >}}
777+
-- content/_index.en.md --
778+
---
779+
title: home
780+
---
781+
{{< myshortcode >}}
782+
-- layouts/_shortcodes/myshortcode.html --
783+
myshortcode.html
784+
-- layouts/_shortcodes/myshortcode.en.html --
785+
myshortcode.en.html
786+
-- layouts/all.html --
787+
{{ .Content }}
788+
789+
790+
`
791+
792+
b := hugolib.Test(t, files)
793+
794+
b.AssertFileContent("public/pl/index.html", "myshortcode.html")
795+
b.AssertFileContent("public/en/index.html", "myshortcode.en.html")
796+
}

0 commit comments

Comments
 (0)