Skip to content

Commit 372e6e1

Browse files
committed
TODOs
1 parent bca9379 commit 372e6e1

File tree

10 files changed

+51
-36
lines changed

10 files changed

+51
-36
lines changed

common/paths/pathparser.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -485,23 +485,6 @@ func (p *Path) Dir() (d string) {
485485
return
486486
}
487487

488-
// Used in template lookups.
489-
// For pages with Type set, we treat that as the section.
490-
func (p *Path) DirReTyped(typ string) (d string) {
491-
if typ == "" || p.Section() == typ {
492-
return p.Dir()
493-
}
494-
if p.posSectionHigh <= 0 {
495-
return ""
496-
}
497-
d = "/" + typ
498-
if p.posContainerHigh > 0 {
499-
d += p.s[p.posContainerHigh-1:]
500-
}
501-
d = p.norm(d)
502-
return
503-
}
504-
505488
// Path returns the full path.
506489
func (p *Path) Path() (d string) {
507490
return p.norm(p.s)
@@ -575,6 +558,21 @@ func (p *Path) Base() string {
575558
return p.base(!p.isContentPage(), p.IsBundle())
576559
}
577560

561+
// Used in template lookups.
562+
// For pages with Type set, we treat that as the section.
563+
func (p *Path) BaseReTyped(typ string) (d string) {
564+
base := p.Base()
565+
if typ == "" || p.Section() == typ {
566+
return base
567+
}
568+
d = "/" + typ
569+
if p.posSectionHigh != -1 {
570+
d += base[p.posSectionHigh:]
571+
}
572+
d = p.norm(d)
573+
return
574+
}
575+
578576
// BaseNoLeadingSlash returns the base path without the leading slash.
579577
// TODO1 fix/test this vs /pages/rss.xml etc.
580578
func (p *Path) BaseNoLeadingSlash() string {

common/paths/pathparser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func TestParse(t *testing.T) {
118118
c.Assert(p.IsLeafBundle(), qt.IsFalse)
119119
c.Assert(p.Name(), qt.Equals, "c.md")
120120
c.Assert(p.Base(), qt.Equals, "/a/b/c")
121+
c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo/b/c")
121122
c.Assert(p.Section(), qt.Equals, "a")
122123
c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "c")
123124
c.Assert(p.Path(), qt.Equals, "/a/b/c.md")
@@ -186,6 +187,7 @@ func TestParse(t *testing.T) {
186187
"/_index.md",
187188
func(c *qt.C, p *Path) {
188189
c.Assert(p.Base(), qt.Equals, "/")
190+
c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo")
189191
c.Assert(p.Path(), qt.Equals, "/_index.md")
190192
c.Assert(p.Container(), qt.Equals, "")
191193
c.Assert(p.ContainerDir(), qt.Equals, "/")
@@ -196,6 +198,7 @@ func TestParse(t *testing.T) {
196198
"/a/index.md",
197199
func(c *qt.C, p *Path) {
198200
c.Assert(p.Base(), qt.Equals, "/a")
201+
c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo/a")
199202
c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "a")
200203
c.Assert(p.Container(), qt.Equals, "a")
201204
c.Assert(p.Container(), qt.Equals, "a")
@@ -219,6 +222,7 @@ func TestParse(t *testing.T) {
219222
func(c *qt.C, p *Path) {
220223
c.Assert(p.Base(), qt.Equals, "/a/b")
221224
c.Assert(p.BaseNameNoIdentifier(), qt.Equals, "b")
225+
c.Assert(p.BaseReTyped("foo"), qt.Equals, "/foo/b")
222226
c.Assert(p.Container(), qt.Equals, "b")
223227
c.Assert(p.ContainerDir(), qt.Equals, "/a")
224228
c.Assert(p.Dir(), qt.Equals, "/a/b")

hugolib/alias.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ type aliasPage struct {
4949

5050
func (a aliasHandler) renderAlias(permalink string, p page.Page) (io.Reader, error) {
5151
var templateDesc tplimpl.TemplateDescriptor
52-
var dir string = ""
52+
var base string = ""
5353
if ps, ok := p.(*pageState); ok {
54-
dir, templateDesc = ps.getDirAndTemplateDescriptor()
54+
base, templateDesc = ps.getBaseAndTemplateDescriptor()
5555
}
5656

5757
// TODO1 double check this, ref. hooks.
@@ -60,7 +60,7 @@ func (a aliasHandler) renderAlias(permalink string, p page.Page) (io.Reader, err
6060
templateDesc.OutputFormat = output.AliasHTMLFormat.Name
6161

6262
q := tplimpl.TemplateQuery{
63-
Dir: dir,
63+
Path: base,
6464
Category: tplimpl.CategoryLayout,
6565
Desc: templateDesc,
6666
}

hugolib/cascade_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ func TestCascade(t *testing.T) {
207207
}
208208

209209
func TestCascadeEdit(t *testing.T) {
210-
t.Skip("TODO1")
211210
p1Content := `---
212211
title: P1
213212
---

hugolib/page.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ func (ps *pageState) initCommonProviders(pp pagePaths) error {
476476
return nil
477477
}
478478

479-
func (p *pageState) getDirAndTemplateDescriptor() (string, tplimpl.TemplateDescriptor) {
480-
dir := p.PathInfo().DirReTyped(p.m.pageConfig.Type) // TODO1 check bundle dirs?
481-
return dir, tplimpl.TemplateDescriptor{
479+
func (p *pageState) getBaseAndTemplateDescriptor() (string, tplimpl.TemplateDescriptor) {
480+
base := p.PathInfo().BaseReTyped(p.m.pageConfig.Type)
481+
return base, tplimpl.TemplateDescriptor{
482482
Kind: p.Kind(),
483483
Lang: p.Language().Lang,
484484
Layout: p.Layout(),
@@ -491,15 +491,15 @@ func (p *pageState) getDirAndTemplateDescriptor() (string, tplimpl.TemplateDescr
491491
func (p *pageState) resolveTemplate(layouts ...string) (*tplimpl.TemplInfo, bool, error) {
492492
timer := p.s.h.Debug.Timer("resolveTemplate")
493493
defer timer.Stop()
494-
dir, d := p.getDirAndTemplateDescriptor()
494+
dir, d := p.getBaseAndTemplateDescriptor()
495495

496496
if len(layouts) > 0 {
497497
d.Layout = layouts[0]
498498
d.LayoutMustMatch = true
499499
}
500500

501501
q := tplimpl.TemplateQuery{
502-
Dir: dir,
502+
Path: dir,
503503
Category: tplimpl.CategoryLayout,
504504
Desc: d,
505505
}

hugolib/page__per_output.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
278278
// Inherit the descriptor from the page/current output format.
279279
// This allows for fine-grained control of the template used for
280280
// rendering of e.g. links.
281-
dir, layoutDescriptor := pco.po.p.getDirAndTemplateDescriptor()
281+
base, layoutDescriptor := pco.po.p.getBaseAndTemplateDescriptor()
282282

283283
switch tp {
284284
case hooks.LinkRendererType:
@@ -346,7 +346,7 @@ func (pco *pageContentOutput) initRenderHooks() error {
346346

347347
getHookTemplate := func() (*tplimpl.TemplInfo, bool) {
348348
q := tplimpl.TemplateQuery{
349-
Dir: dir,
349+
Path: base,
350350
Category: tplimpl.CategoryMarkup,
351351
Desc: layoutDescriptor,
352352
Consider: consider,

hugolib/shortcode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func doRenderShortcode(
399399
return true
400400
}
401401
q := tplimpl.TemplateQuery{
402-
Dir: p.PathInfo().Dir(),
402+
Path: p.PathInfo().Dir(),
403403
Name: sc.name,
404404
Category: tplimpl.CategoryShortcode,
405405
Desc: tplDesc, // TODO1 page desc.
@@ -689,7 +689,7 @@ Loop:
689689

690690
// Used to check if the template expects inner content.
691691
q := tplimpl.TemplateQuery{
692-
Dir: "/", // TODO1
692+
Path: "/", // TODO1
693693
Name: sc.name,
694694
Category: tplimpl.CategoryShortcode,
695695
Desc: tplVariants, // TODO1 page desc.

tpl/tplimpl/templatedescriptor.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,20 @@ func (this TemplateDescriptor) doCompare(category Category, other TemplateDescri
9393
return w
9494
}
9595
if other.Layout != "" && other.Layout != this.Layout {
96-
return w
96+
if isCustomLayout(this.Layout) {
97+
if this.Kind == "" {
98+
this.Layout = ""
99+
} else if this.Kind == kinds.KindPage {
100+
this.Layout = layoutSingle
101+
} else {
102+
this.Layout = layoutList
103+
}
104+
}
105+
106+
// Test again.
107+
if other.Layout != this.Layout {
108+
return w
109+
}
97110
}
98111
if other.Lang != "" && other.Lang != this.Lang {
99112
return w

tpl/tplimpl/templatestore.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ type TemplWithBaseApplied struct {
348348

349349
// TemplateQuery is used in LookupPagesLayout to find the best matching template.
350350
type TemplateQuery struct {
351-
// The directory to walk down to.
352-
Dir string
351+
// The path to walk down to.
352+
Path string
353353

354354
// The name to look for. Used for shortcode queries.
355355
Name string
@@ -509,7 +509,7 @@ func (t *TemplateStore) LookupByPath(templatePath string) *TemplInfo {
509509

510510
func (s *TemplateStore) LookupPagesLayout(q TemplateQuery) *TemplInfo {
511511
q.init()
512-
key := s.key(q.Dir)
512+
key := s.key(q.Path)
513513

514514
slashCountKey := strings.Count(key, "/")
515515
best1 := s.findBestMatchWalkPath(q, key, slashCountKey)
@@ -542,7 +542,7 @@ func (s *TemplateStore) LookupPartial(pth string, desc TemplateDescriptor) *Temp
542542

543543
func (s *TemplateStore) LookupShortcode(q TemplateQuery) *TemplInfo {
544544
q.init()
545-
k1 := s.key(q.Dir)
545+
k1 := s.key(q.Path)
546546

547547
slashCountK1 := strings.Count(k1, "/")
548548
best := &bestMatch{
@@ -733,6 +733,7 @@ func (s *TemplateStore) findBestMatchWalkPath(q TemplateQuery, k1 string, slashC
733733

734734
weight := s.dh.compareDescriptors(q.Category, q.Desc, k.d)
735735
weight.distance = distance
736+
736737
if best.isBetter(weight, vv) {
737738
best.updateValues(weight, k2, k.d, vv)
738739
}

tpl/tplimpl/templatestore_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ func BenchmarkLookupPagesLayout(b *testing.B) {
455455
bb := hugolib.Test(b, newSetupTestSites)
456456
store := bb.H.TemplateStore
457457
q := tplimpl.TemplateQuery{
458-
Dir: "/categories/blue",
458+
Path: "/categories/blue",
459459
Category: tplimpl.CategoryLayout,
460460
Desc: tplimpl.TemplateDescriptor{Kind: "taxonomy", Layout: "", Lang: "fr", OutputFormat: "html"},
461461
}

0 commit comments

Comments
 (0)