2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2019-09-10 05:26:34 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pageTree struct {
|
|
|
|
p *pageState
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (pt pageTree) IsAncestor(other any) (bool, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
if pt.p == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
tp, ok := other.(treeRefProvider)
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
|
2022-05-25 12:46:42 -04:00
|
|
|
if ref1 != nil && ref2 != nil && ref1.key == ref2.key {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
|
2020-03-26 04:41:30 -04:00
|
|
|
if ref1 != nil && ref1.key == "/" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-02-22 04:57:43 -05:00
|
|
|
if ref1 == nil || ref2 == nil {
|
|
|
|
if ref1 == nil {
|
|
|
|
// A 404 or other similar standalone page.
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref1.n.p.IsHome(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-21 05:25:00 -04:00
|
|
|
if strings.HasPrefix(ref2.key, ref1.key) {
|
2020-04-23 10:59:17 -04:00
|
|
|
return true, nil
|
2020-03-26 04:41:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.HasPrefix(ref2.key, ref1.key+cmBranchSeparator), nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pt pageTree) CurrentSection() page.Page {
|
|
|
|
p := pt.p
|
|
|
|
|
|
|
|
if p.IsHome() || p.IsSection() {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.Parent()
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (pt pageTree) IsDescendant(other any) (bool, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
if pt.p == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
|
|
|
|
tp, ok := other.(treeRefProvider)
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
|
2022-05-25 12:46:42 -04:00
|
|
|
if ref1 != nil && ref2 != nil && ref1.key == ref2.key {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
|
2020-03-26 04:41:30 -04:00
|
|
|
if ref2 != nil && ref2.key == "/" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-02-22 04:57:43 -05:00
|
|
|
if ref1 == nil || ref2 == nil {
|
|
|
|
if ref2 == nil {
|
|
|
|
// A 404 or other similar standalone page.
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref2.n.p.IsHome(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-21 05:25:00 -04:00
|
|
|
if strings.HasPrefix(ref1.key, ref2.key) {
|
2020-04-23 10:59:17 -04:00
|
|
|
return true, nil
|
2020-03-26 04:41:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.HasPrefix(ref1.key, ref2.key+cmBranchSeparator), nil
|
2019-09-10 05:26:34 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
func (pt pageTree) FirstSection() page.Page {
|
|
|
|
ref := pt.p.getTreeRef()
|
2020-02-22 04:57:43 -05:00
|
|
|
if ref == nil {
|
|
|
|
return pt.p.s.home
|
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
key := ref.key
|
2020-05-21 05:25:00 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if !ref.isSection() {
|
|
|
|
key = path.Dir(key)
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2020-05-21 05:25:00 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
_, b := ref.m.getFirstSection(key)
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
return b.p
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (pt pageTree) InSection(other any) (bool, error) {
|
2019-01-02 06:33:26 -05:00
|
|
|
if pt.p == nil || types.IsNil(other) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
tp, ok := other.(treeRefProvider)
|
|
|
|
if !ok {
|
2019-01-02 06:33:26 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
|
|
|
|
|
2020-02-22 04:57:43 -05:00
|
|
|
if ref1 == nil || ref2 == nil {
|
|
|
|
if ref1 == nil {
|
|
|
|
// A 404 or other similar standalone page.
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return ref1.n.p.IsHome(), nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
s1, _ := ref1.getCurrentSection()
|
|
|
|
s2, _ := ref2.getCurrentSection()
|
|
|
|
|
|
|
|
return s1 == s2, nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2019-03-25 02:54:10 -04:00
|
|
|
func (pt pageTree) Page() page.Page {
|
|
|
|
return pt.p
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func (pt pageTree) Parent() page.Page {
|
2019-09-10 05:26:34 -04:00
|
|
|
p := pt.p
|
|
|
|
|
|
|
|
if p.parent != nil {
|
|
|
|
return p.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
if pt.p.IsHome() {
|
|
|
|
return nil
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
|
2020-02-21 02:06:38 -05:00
|
|
|
tree := p.getTreeRef()
|
|
|
|
|
2020-06-16 09:43:50 -04:00
|
|
|
if tree == nil || pt.p.Kind() == page.KindTaxonomy {
|
2020-02-18 10:16:09 -05:00
|
|
|
return pt.p.s.home
|
|
|
|
}
|
|
|
|
|
2020-02-21 02:06:38 -05:00
|
|
|
_, b := tree.getSection()
|
2019-09-10 05:26:34 -04:00
|
|
|
if b == nil {
|
2019-08-03 11:27:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
return b.p
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pt pageTree) Sections() page.Pages {
|
2019-08-03 11:27:40 -04:00
|
|
|
if pt.p.bucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return pt.p.bucket.getSections()
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|