diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-03-16 11:37:57 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-03-16 17:37:36 +0100 |
commit | 03b93bb9884ea479c855c2699e8c7b039dce6224 (patch) | |
tree | d6453cf01896ab65596b34e02ebb561c26e672b7 | |
parent | 94fb4dc3dddf6803265316a7b8cfe81c29a83e91 (diff) |
Add .RegularPagesRecursive
Fixes #6411
-rw-r--r-- | hugolib/content_map.go | 13 | ||||
-rw-r--r-- | hugolib/content_map_page.go | 6 | ||||
-rw-r--r-- | hugolib/page.go | 26 | ||||
-rw-r--r-- | hugolib/page__common.go | 6 | ||||
-rw-r--r-- | hugolib/pagecollections_test.go | 39 | ||||
-rw-r--r-- | resources/page/page.go | 4 | ||||
-rw-r--r-- | resources/page/page_nop.go | 4 | ||||
-rw-r--r-- | resources/page/testhelpers_test.go | 4 |
8 files changed, 100 insertions, 2 deletions
diff --git a/hugolib/content_map.go b/hugolib/content_map.go index efa2254b2..2ea36ff31 100644 --- a/hugolib/content_map.go +++ b/hugolib/content_map.go @@ -941,6 +941,19 @@ func (c *contentTreeRef) collectPages() page.Pages { return pas } +func (c *contentTreeRef) collectPagesRecursive() page.Pages { + var pas page.Pages + c.m.collectPages(c.key+cmBranchSeparator, func(c *contentNode) { + pas = append(pas, c.p) + }) + c.m.collectPages(c.key+"/", func(c *contentNode) { + pas = append(pas, c.p) + }) + page.SortByDefault(pas) + + return pas +} + func (c *contentTreeRef) collectPagesAndSections() page.Pages { var pas page.Pages c.m.collectPagesAndSections(c.key, func(c *contentNode) { diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go index c60536285..4ba97f511 100644 --- a/hugolib/content_map_page.go +++ b/hugolib/content_map_page.go @@ -803,6 +803,12 @@ func (b *pagesMapBucket) getPages() page.Pages { return b.pages } +func (b *pagesMapBucket) getPagesRecursive() page.Pages { + pages := b.owner.treeRef.collectPagesRecursive() + page.SortByDefault(pages) + return pages +} + func (b *pagesMapBucket) getPagesAndSections() page.Pages { b.pagesAndSectionsInit.Do(func() { b.pagesAndSections = b.owner.treeRef.collectPagesAndSections() diff --git a/hugolib/page.go b/hugolib/page.go index f0f695227..16c320ecd 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -171,6 +171,14 @@ func (p *pageState) getPages() page.Pages { return b.getPages() } +func (p *pageState) getPagesRecursive() page.Pages { + b := p.bucket + if b == nil { + return nil + } + return b.getPagesRecursive() +} + func (p *pageState) getPagesAndSections() page.Pages { b := p.bucket if b == nil { @@ -179,6 +187,24 @@ func (p *pageState) getPagesAndSections() page.Pages { return b.getPagesAndSections() } +func (p *pageState) RegularPagesRecursive() page.Pages { + p.regularPagesRecursiveInit.Do(func() { + var pages page.Pages + switch p.Kind() { + case page.KindSection: + pages = p.getPagesRecursive() + default: + pages = p.RegularPages() + } + p.regularPagesRecursive = pages + }) + return p.regularPagesRecursive +} + +func (p *pageState) PagesRecursive() page.Pages { + return nil +} + func (p *pageState) RegularPages() page.Pages { p.regularPagesInit.Do(func() { var pages page.Pages diff --git a/hugolib/page__common.go b/hugolib/page__common.go index 8187f6719..be6bb090b 100644 --- a/hugolib/page__common.go +++ b/hugolib/page__common.go @@ -139,6 +139,8 @@ type pagePages struct { pagesInit sync.Once pages page.Pages - regularPagesInit sync.Once - regularPages page.Pages + regularPagesInit sync.Once + regularPages page.Pages + regularPagesRecursiveInit sync.Once + regularPagesRecursive page.Pages } diff --git a/hugolib/pagecollections_test.go b/hugolib/pagecollections_test.go index 4d95bcffa..bf3832c96 100644 --- a/hugolib/pagecollections_test.go +++ b/hugolib/pagecollections_test.go @@ -383,3 +383,42 @@ func TestShouldDoSimpleLookup(t *testing.T) { c.Assert(shouldDoSimpleLookup("docs/foo.md"), qt.Equals, false) } + +func TestRegularPagesRecursive(t *testing.T) { + b := newTestSitesBuilder(t) + + b.WithConfigFile("yaml", ` +baseURL: "http://example.org/" +title: "My New Hugo Site" + +`) + + b.WithContent( + "docs/1.md", "\n---title: docs1\n---", + "docs/sect1/_index.md", "\n---title: docs_sect1\n---", + "docs/sect1/ps1.md", "\n---title: docs_sect1_ps1\n---", + "docs/sect1/ps2.md", "\n---title: docs_sect1_ps2\n---", + "docs/sect1/sect1_s2/_index.md", "\n---title: docs_sect1_s2\n---", + "docs/sect1/sect1_s2/ps2_1.md", "\n---title: docs_sect1_s2_1\n---", + "docs/sect2/_index.md", "\n---title: docs_sect2\n---", + "docs/sect2/ps1.md", "\n---title: docs_sect2_ps1\n---", + "docs/sect2/ps2.md", "\n---title: docs_sect2_ps2\n---", + "news/1.md", "\n---title: news1\n---", + ) + + b.WithTemplates("index.html", ` +{{ $sect1 := site.GetPage "sect1" }} + +Sect1 RegularPagesRecursive: {{ range $sect1.RegularPagesRecursive }}{{ .Kind }}:{{ .RelPermalink}}|{{ end }}|End. + +`) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", ` +Sect1 RegularPagesRecursive: page:/docs/sect1/ps1/|page:/docs/sect1/ps2/|page:/docs/sect1/sect1_s2/ps2_1/||End. + + +`) + +} diff --git a/resources/page/page.go b/resources/page/page.go index 1225f43d0..934427b0e 100644 --- a/resources/page/page.go +++ b/resources/page/page.go @@ -64,6 +64,10 @@ type ChildCareProvider interface { // use RegularPages. RegularPages() Pages + // RegularPagesRecursive returns all regular pages below the current + // section. + RegularPagesRecursive() Pages + Resources() resource.Resources } diff --git a/resources/page/page_nop.go b/resources/page/page_nop.go index ccfbf525f..c24792157 100644 --- a/resources/page/page_nop.go +++ b/resources/page/page_nop.go @@ -294,6 +294,10 @@ func (p *nopPage) RegularPages() Pages { return nil } +func (p *nopPage) RegularPagesRecursive() Pages { + return nil +} + func (p *nopPage) Paginate(seq interface{}, options ...interface{}) (*Pager, error) { return nil, nil } diff --git a/resources/page/testhelpers_test.go b/resources/page/testhelpers_test.go index 9c8605dad..dcd37c41e 100644 --- a/resources/page/testhelpers_test.go +++ b/resources/page/testhelpers_test.go @@ -364,6 +364,10 @@ func (p *testPage) RegularPages() Pages { panic("not implemented") } +func (p *testPage) RegularPagesRecursive() Pages { + panic("not implemented") +} + func (p *testPage) Paginate(seq interface{}, options ...interface{}) (*Pager, error) { return nil, nil } |