Add .RegularPagesRecursive
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 16 Mar 2020 10:37:57 +0000 (11:37 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 16 Mar 2020 16:37:36 +0000 (17:37 +0100)
Fixes #6411

hugolib/content_map.go
hugolib/content_map_page.go
hugolib/page.go
hugolib/page__common.go
hugolib/pagecollections_test.go
resources/page/page.go
resources/page/page_nop.go
resources/page/testhelpers_test.go

index efa2254b2a778dbb391f999cfc0b07ff92e02422..2ea36ff31eca3d8b0755dda8efeb287788485f3d 100644 (file)
@@ -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) {
index c605362858d967c18aed3b99c1c74b8fcde9b39b..4ba97f511852a817bffeb402d822aa71a76f18a5 100644 (file)
@@ -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()
index f0f695227c3f368ee5f5f45fc448848437ad097c..16c320ecd4eaf65388e698529dc1a1cda070bde5 100644 (file)
@@ -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
index 8187f67193582eff9df8cee4dd1e1b5e03e3a5f6..be6bb090b2edb02db72189d71cb1dd474af07bd4 100644 (file)
@@ -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
 }
index 4d95bcffa3a95a710fa3db8e4ec4b8b0e3194f1f..bf3832c9642ad9b0442299c8a0a8838a682ccc56 100644 (file)
@@ -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.
+
+
+`)
+
+}
index 1225f43d0e5603cb68a235159e0ae5f228d03ee8..934427b0ed099c0b9096d88c3c19b8f3b1eefc5e 100644 (file)
@@ -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
 }
 
index ccfbf525f6f2830365038c9dcf65da00e8afe0fc..c24792157ae443bb841dc06629d8e9b662277d89 100644 (file)
@@ -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
 }
index 9c8605dad0a32803ccacc7e9c5ba04043b92935d..dcd37c41e2d9f22515993150742aa415f7befd66 100644 (file)
@@ -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
 }