summaryrefslogtreecommitdiffstats
path: root/hugolib/page.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-03 17:27:40 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-08-08 20:13:39 +0200
commit7ff0a8ee9fe8d710d407e57faf1fda43bd635f28 (patch)
tree4baa7d913f735cc1089e465b51ff007014bfe25a /hugolib/page.go
parentdf374851a0683f1446f33a4afef74c42f7d3eaaf (diff)
Simplify page tree logic
This is preparation for #6041. For historic reasons, the code for bulding the section tree and the taxonomies were very much separate. This works, but makes it hard to extend, maintain, and possibly not so fast as it could be. This simplification also introduces 3 slightly breaking changes, which I suspect most people will be pleased about. See referenced issues: This commit also switches the radix tree dependency to a mutable implementation: github.com/armon/go-radix. Fixes #6154 Fixes #6153 Fixes #6152
Diffstat (limited to 'hugolib/page.go')
-rw-r--r--hugolib/page.go109
1 files changed, 56 insertions, 53 deletions
diff --git a/hugolib/page.go b/hugolib/page.go
index 676cba762..8dda33009 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -23,6 +23,8 @@ import (
"sort"
"strings"
+ "github.com/gohugoio/hugo/common/maps"
+
"github.com/gohugoio/hugo/hugofs/files"
"github.com/bep/gitmap"
@@ -121,31 +123,66 @@ func (p *pageState) MarshalJSON() ([]byte, error) {
return page.MarshalPageToJSON(p)
}
-func (p *pageState) Pages() page.Pages {
- p.pagesInit.Do(func() {
- if p.pages != nil {
- return
- }
+func (p *pageState) getPages() page.Pages {
+ b := p.bucket
+ if b == nil {
+ return nil
+ }
+ return b.getPages()
+}
+func (p *pageState) getPagesAndSections() page.Pages {
+ b := p.bucket
+ if b == nil {
+ return nil
+ }
+ return b.getPagesAndSections()
+}
+
+// TODO(bep) cm add a test
+func (p *pageState) RegularPages() page.Pages {
+ p.regularPagesInit.Do(func() {
var pages page.Pages
switch p.Kind() {
case page.KindPage:
- case page.KindHome:
- pages = p.s.RegularPages()
+ case page.KindSection, page.KindHome, page.KindTaxonomyTerm:
+ pages = p.getPages()
case page.KindTaxonomy:
- termInfo := p.getTaxonomyNodeInfo()
- taxonomy := p.s.Taxonomies[termInfo.plural].Get(termInfo.termKey)
- pages = taxonomy.Pages()
- case page.KindTaxonomyTerm:
- plural := p.getTaxonomyNodeInfo().plural
- // A list of all page.KindTaxonomy pages with matching plural
- for _, p := range p.s.findPagesByKind(page.KindTaxonomy) {
- if p.SectionsEntries()[0] == plural {
+ all := p.Pages()
+ for _, p := range all {
+ if p.IsPage() {
pages = append(pages, p)
}
}
- case kind404, kindSitemap, kindRobotsTXT:
+ default:
+ pages = p.s.RegularPages()
+ }
+
+ p.regularPages = pages
+
+ })
+
+ return p.regularPages
+}
+
+func (p *pageState) Pages() page.Pages {
+ p.pagesInit.Do(func() {
+ var pages page.Pages
+
+ switch p.Kind() {
+ case page.KindPage:
+ case page.KindSection, page.KindHome:
+ pages = p.getPagesAndSections()
+ case page.KindTaxonomy:
+ termInfo := p.bucket
+ plural := maps.GetString(termInfo.meta, "plural")
+ term := maps.GetString(termInfo.meta, "termKey")
+ taxonomy := p.s.Taxonomies[plural].Get(term)
+ pages = taxonomy.Pages()
+ case page.KindTaxonomyTerm:
+ pages = p.getPagesAndSections()
+ default:
pages = p.s.Pages()
}
@@ -295,10 +332,9 @@ func (p *pageState) getLayoutDescriptor() output.LayoutDescriptor {
if len(sections) > 0 {
section = sections[0]
}
- case page.KindTaxonomyTerm:
- section = p.getTaxonomyNodeInfo().singular
- case page.KindTaxonomy:
- section = p.getTaxonomyNodeInfo().parent.singular
+ case page.KindTaxonomyTerm, page.KindTaxonomy:
+ section = maps.GetString(p.bucket.meta, "singular")
+
default:
}
@@ -359,11 +395,6 @@ func (p *pageState) initPage() error {
return nil
}
-func (p *pageState) setPages(pages page.Pages) {
- page.SortByDefault(pages)
- p.pages = pages
-}
-
func (p *pageState) renderResources() (err error) {
p.resourcesPublishInit.Do(func() {
var toBeDeleted []int
@@ -489,13 +520,6 @@ func (p *pageState) addResources(r ...resource.Resource) {
p.resources = append(p.resources, r...)
}
-func (p *pageState) addSectionToParent() {
- if p.parent == nil {
- return
- }
- p.parent.subSections = append(p.parent.subSections, p)
-}
-
func (p *pageState) mapContent(meta *pageMeta) error {
s := p.shortcodeState
@@ -743,27 +767,6 @@ func (p *pageState) shiftToOutputFormat(isRenderingSite bool, idx int) error {
return nil
}
-func (p *pageState) getTaxonomyNodeInfo() *taxonomyNodeInfo {
- info := p.s.taxonomyNodes.Get(p.SectionsEntries()...)
-
- if info == nil {
- // There can be unused content pages for taxonomies (e.g. author that
- // has not written anything, yet), and these will not have a taxonomy
- // node created in the assemble taxonomies step.
- return nil
- }
-
- return info
-
-}
-
-func (p *pageState) sortParentSections() {
- if p.parent == nil {
- return
- }
- page.SortByDefault(p.parent.subSections)
-}
-
// sourceRef returns the reference used by GetPage and ref/relref shortcodes to refer to
// this page. It is prefixed with a "/".
//