shithub: hugo

Download patch

ref: a524124beb0e7ca226c207ea48a90cea2cbef76e
parent: c1eb62512446fd80ff5e4c0f45c8614788595e0e
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Sat Feb 22 05:57:43 EST 2020

Fix crashes for 404 in IsAncestor etc.

Fixes #6931

--- a/docs/content/en/readfiles/sectionvars.md
+++ b/docs/content/en/readfiles/sectionvars.md
@@ -5,13 +5,13 @@
 : The page's first section below root, e.g. `/docs`, `/blog` etc.
 
 .InSection $anotherPage
-: Whether the given page is in the current section. Note that this will always return false for pages that are not either regular, home or section pages.
+: Whether the given page is in the current section.
 
 .IsAncestor $anotherPage
-: Whether the current page is an ancestor of the given page. Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
+: Whether the current page is an ancestor of the given page.
 
 .IsDescendant $anotherPage
-: Whether the current page is a descendant of the given page. Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
+: Whether the current page is a descendant of the given page.
 
 .Parent
 : A section's parent section or a page's section.
--- a/hugolib/404_test.go
+++ b/hugolib/404_test.go
@@ -21,7 +21,23 @@
 	t.Parallel()
 
 	b := newTestSitesBuilder(t)
-	b.WithSimpleConfigFile().WithTemplatesAdded("404.html", "<html><body>Not Found! Parent: {{ .Parent.Kind }}</body></html>")
+	b.WithSimpleConfigFile().WithTemplatesAdded(
+		"404.html",
+		`
+{{ $home := site.Home }}
+404: 
+Parent: {{ .Parent.Kind }}
+IsAncestor: {{ .IsAncestor $home }}/{{ $home.IsAncestor . }}
+IsDescendant: {{ .IsDescendant $home }}/{{ $home.IsDescendant . }}
+CurrentSection: {{ .CurrentSection.Kind }}|
+FirstSection: {{ .FirstSection.Kind }}|
+InSection: {{ .InSection $home.Section }}|{{ $home.InSection . }}
+Sections: {{ len .Sections }}|
+Page: {{ .Page.RelPermalink }}|
+Data: {{ len .Data }}|
+
+`,
+	)
 	b.Build(BuildCfg{})
 
 	// Note: We currently have only 1 404 page. One might think that we should have
@@ -28,8 +44,18 @@
 	// multiple, to follow the Custom Output scheme, but I don't see how that would work
 	// right now.
 	b.AssertFileContent("public/404.html", `
-Not Found
+
+  404:
 Parent: home
+IsAncestor: false/true
+IsDescendant: true/false
+CurrentSection: home|
+FirstSection: home|
+InSection: false|true
+Sections: 0|
+Page: /404.html|
+Data: 1|
+        
 `)
 
 }
--- a/hugolib/content_map_page.go
+++ b/hugolib/content_map_page.go
@@ -806,6 +806,9 @@
 
 func (b *pagesMapBucket) getSections() page.Pages {
 	b.sectionsInit.Do(func() {
+		if b.owner.treeRef == nil {
+			return
+		}
 		b.sections = b.owner.treeRef.collectSections()
 	})
 
--- a/hugolib/page__tree.go
+++ b/hugolib/page__tree.go
@@ -37,6 +37,15 @@
 
 	ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
 
+	if ref1 == nil || ref2 == nil {
+		if ref1 == nil {
+			// A 404 or other similar standalone page.
+			return false, nil
+		}
+
+		return ref1.n.p.IsHome(), nil
+	}
+
 	if !ref1.isSection() {
 		return false, nil
 	}
@@ -66,6 +75,15 @@
 
 	ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
 
+	if ref1 == nil || ref2 == nil {
+		if ref2 == nil {
+			// A 404 or other similar standalone page.
+			return false, nil
+		}
+
+		return ref2.n.p.IsHome(), nil
+	}
+
 	if !ref2.isSection() {
 		return false, nil
 	}
@@ -76,6 +94,9 @@
 
 func (pt pageTree) FirstSection() page.Page {
 	ref := pt.p.getTreeRef()
+	if ref == nil {
+		return pt.p.s.home
+	}
 	key := ref.key
 	if !ref.isSection() {
 		key = path.Dir(key)
@@ -99,6 +120,14 @@
 
 	ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
 
+	if ref1 == nil || ref2 == nil {
+		if ref1 == nil {
+			// A 404 or other similar standalone page.
+			return false, nil
+		}
+		return ref1.n.p.IsHome(), nil
+	}
+
 	s1, _ := ref1.getCurrentSection()
 	s2, _ := ref2.getCurrentSection()
 
@@ -123,7 +152,7 @@
 
 	tree := p.getTreeRef()
 
-	if pt.p.Kind() == page.KindTaxonomyTerm || tree == nil {
+	if tree == nil || pt.p.Kind() == page.KindTaxonomyTerm {
 		return pt.p.s.home
 	}