diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-04-13 11:40:51 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-04-13 18:52:38 +0200 |
commit | f2795d4d2cef30170af43327f3ff7114923833b1 (patch) | |
tree | 2b48f8f3903313f9bdbb17991bcfd29e186d61bb /hugolib | |
parent | e85c057f99dc2eeb6994bf24105bc48196841b88 (diff) |
Fix WeightedPages in union etc.
We introduced a callback func() to get the owner Page in 0.55.0.
Sadly, funcs is not comparable type in Go.
This commit replaces the func with a struct pointer that wraps the Page.
Fixes #5850
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/collections_test.go | 31 | ||||
-rw-r--r-- | hugolib/site.go | 2 | ||||
-rw-r--r-- | hugolib/taxonomy.go | 10 |
3 files changed, 35 insertions, 8 deletions
diff --git a/hugolib/collections_test.go b/hugolib/collections_test.go index bc55bdbe8..b7e89eec9 100644 --- a/hugolib/collections_test.go +++ b/hugolib/collections_test.go @@ -87,6 +87,37 @@ tags_weight: %d `weightedPages:2::page.WeightedPages:[WeightedPage(10,"Page") WeightedPage(20,"Page")]`) } +func TestUnionFunc(t *testing.T) { + assert := require.New(t) + + pageContent := ` +--- +title: "Page" +tags: ["blue", "green"] +tags_weight: %d +--- + +` + b := newTestSitesBuilder(t) + b.WithSimpleConfigFile(). + WithContent("page1.md", fmt.Sprintf(pageContent, 10), "page2.md", fmt.Sprintf(pageContent, 20), + "page3.md", fmt.Sprintf(pageContent, 30)). + WithTemplatesAdded("index.html", ` +{{ $unionPages := first 2 .Site.RegularPages | union .Site.RegularPages }} +{{ $unionWeightedPages := .Site.Taxonomies.tags.blue | union .Site.Taxonomies.tags.green }} +{{ printf "unionPages: %T %d" $unionPages (len $unionPages) }} +{{ printf "unionWeightedPages: %T %d" $unionWeightedPages (len $unionWeightedPages) }} +`) + b.CreateSites().Build(BuildCfg{}) + + assert.Equal(1, len(b.H.Sites)) + require.Len(t, b.H.Sites[0].RegularPages(), 3) + + b.AssertFileContent("public/index.html", + "unionPages: page.Pages 3", + "unionWeightedPages: page.WeightedPages 6") +} + func TestAppendFunc(t *testing.T) { assert := require.New(t) diff --git a/hugolib/site.go b/hugolib/site.go index 616b5b37d..2653479ea 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1584,7 +1584,7 @@ func (s *Site) assembleTaxonomies() error { // last one will win, e.g. "hugo" vs "Hugo". n.term = term - w := page.NewWeightedPage(weight, p, n.getOwner) + w := page.NewWeightedPage(weight, p, n.owner) s.Taxonomies[plural].add(key, w) diff --git a/hugolib/taxonomy.go b/hugolib/taxonomy.go index 9d9e4f9ec..b91318ff1 100644 --- a/hugolib/taxonomy.go +++ b/hugolib/taxonomy.go @@ -175,7 +175,7 @@ type taxonomyNodeInfo struct { parent *taxonomyNodeInfo // Either of Kind taxonomyTerm (parent) or taxonomy - owner page.Page + owner *page.PageWrapper } func (t *taxonomyNodeInfo) UpdateFromPage(p page.Page) { @@ -185,17 +185,12 @@ func (t *taxonomyNodeInfo) UpdateFromPage(p page.Page) { } func (t *taxonomyNodeInfo) TransferValues(p *pageState) { - t.owner = p + t.owner.Page = p if p.Lastmod().IsZero() && p.Date().IsZero() { p.m.Dates.UpdateDateAndLastmodIfAfter(t.dates) } } -// callback sent to the child nodes. -func (t *taxonomyNodeInfo) getOwner() page.Page { - return t.owner -} - // Maps either plural or plural/term to a taxonomy node. // TODO(bep) consolidate somehow with s.Taxonomies type taxonomyNodeInfos map[string]*taxonomyNodeInfo @@ -216,6 +211,7 @@ func (t taxonomyNodeInfos) GetOrCreate(plural, termKey, term string) *taxonomyNo plural: plural, termKey: termKey, term: term, + owner: &page.PageWrapper{}, // Page will be assigned later. } t[key] = n |