summaryrefslogtreecommitdiffstats
path: root/hugolib/taxonomy.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-15 09:38:14 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-15 13:36:05 +0200
commitb799b12f4a693dfeae8a5a362f131081a727bb8f (patch)
treecec0b90c41aea0f091ac5aa8dc72fc7380f78253 /hugolib/taxonomy.go
parent701486728e21bc0c6c78c2a8edb988abdf6116c7 (diff)
hugolib: Fix panic for unused taxonomy content files
In Hugo 0.55 we connected the taxonomy nodes with their owning Page. This failed if you had, say, a content file for a author that did not author anything in the site: ``` content/authors/silent-persin/_index.md ``` Fixes #5847
Diffstat (limited to 'hugolib/taxonomy.go')
-rw-r--r--hugolib/taxonomy.go27
1 files changed, 22 insertions, 5 deletions
diff --git a/hugolib/taxonomy.go b/hugolib/taxonomy.go
index b91318ff1..e6c80161a 100644
--- a/hugolib/taxonomy.go
+++ b/hugolib/taxonomy.go
@@ -193,16 +193,33 @@ func (t *taxonomyNodeInfo) TransferValues(p *pageState) {
// Maps either plural or plural/term to a taxonomy node.
// TODO(bep) consolidate somehow with s.Taxonomies
-type taxonomyNodeInfos map[string]*taxonomyNodeInfo
+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...)
}
-func (t taxonomyNodeInfos) GetOrCreate(plural, termKey, term string) *taxonomyNodeInfo {
+// 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[key]
+ n, found := t.m[key]
if found {
return n
}
@@ -214,7 +231,7 @@ func (t taxonomyNodeInfos) GetOrCreate(plural, termKey, term string) *taxonomyNo
owner: &page.PageWrapper{}, // Page will be assigned later.
}
- t[key] = n
+ t.m[key] = n
return n
}
@@ -222,7 +239,7 @@ func (t taxonomyNodeInfos) GetOrCreate(plural, termKey, term string) *taxonomyNo
func (t taxonomyNodeInfos) Get(sections ...string) *taxonomyNodeInfo {
key := t.key(sections...)
- n, found := t[key]
+ n, found := t.m[key]
if found {
return n
}