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
This commit is contained in:
Bjørn Erik Pedersen
2019-08-03 17:27:40 +02:00
parent df374851a0
commit 7ff0a8ee9f
33 changed files with 836 additions and 938 deletions

View File

@@ -15,13 +15,11 @@ package hugolib
import (
"fmt"
"path"
"sort"
"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
)
// The TaxonomyList is a list of all taxonomies and their values
@@ -156,95 +154,3 @@ func (s *orderedTaxonomySorter) Swap(i, j int) {
func (s *orderedTaxonomySorter) Less(i, j int) bool {
return s.by(&s.taxonomy[i], &s.taxonomy[j])
}
// taxonomyNodeInfo stores additional metadata about a taxonomy.
type taxonomyNodeInfo struct {
plural string
// Maps "tags" to "tag".
singular string
// The term key as used in the taxonomy map, e.g "tag1".
// The value is normalized for paths, but may or not be lowercased
// depending on the disablePathToLower setting.
termKey string
// The original, unedited term name. Useful for titles etc.
term string
dates resource.Dates
parent *taxonomyNodeInfo
// Either of Kind taxonomyTerm (parent) or taxonomy
owner *page.PageWrapper
}
func (t *taxonomyNodeInfo) UpdateFromPage(p page.Page) {
// Select the latest dates
t.dates.UpdateDateAndLastmodIfAfter(p)
}
func (t *taxonomyNodeInfo) TransferValues(p *pageState) {
t.owner.Page = p
if p.Lastmod().IsZero() && p.Date().IsZero() {
p.m.Dates.UpdateDateAndLastmodIfAfter(t.dates)
}
}
// Maps either plural or plural/term to a taxonomy node.
// TODO(bep) consolidate somehow with s.Taxonomies
type taxonomyNodeInfos struct {
m map[string]*taxonomyNodeInfo
getKey func(string) string
}
// map[string]*taxonomyNodeInfo
func (t taxonomyNodeInfos) key(parts ...string) string {
return path.Join(parts...)
}
// GetOrAdd will get or create and add a new taxonomy node to the parent identified with plural.
// It will panic if the parent does not exist.
func (t taxonomyNodeInfos) GetOrAdd(plural, term string) *taxonomyNodeInfo {
parent := t.GetOrCreate(plural, "")
if parent == nil {
panic(fmt.Sprintf("no parent found with plural %q", plural))
}
child := t.GetOrCreate(plural, term)
child.parent = parent
return child
}
func (t taxonomyNodeInfos) GetOrCreate(plural, term string) *taxonomyNodeInfo {
termKey := t.getKey(term)
key := t.key(plural, termKey)
n, found := t.m[key]
if found {
return n
}
n = &taxonomyNodeInfo{
plural: plural,
termKey: termKey,
term: term,
owner: &page.PageWrapper{}, // Page will be assigned later.
}
t.m[key] = n
return n
}
func (t taxonomyNodeInfos) Get(sections ...string) *taxonomyNodeInfo {
key := t.key(sections...)
n, found := t.m[key]
if found {
return n
}
return nil
}