mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 18:52:09 -05:00
1b7acfe763
Recently introduced in master. See https://github.com/gohugoio/hugo/issues/6897#issuecomment-587499907
142 lines
2.5 KiB
Go
142 lines
2.5 KiB
Go
// 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 (
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/gohugoio/hugo/common/types"
|
|
"github.com/gohugoio/hugo/resources/page"
|
|
)
|
|
|
|
type pageTree struct {
|
|
p *pageState
|
|
}
|
|
|
|
func (pt pageTree) IsAncestor(other interface{}) (bool, error) {
|
|
if pt.p == nil {
|
|
return false, nil
|
|
}
|
|
|
|
tp, ok := other.(treeRefProvider)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
|
|
|
|
if !ref1.isSection() {
|
|
return false, nil
|
|
}
|
|
|
|
return strings.HasPrefix(ref2.key, ref1.key), nil
|
|
}
|
|
|
|
func (pt pageTree) CurrentSection() page.Page {
|
|
p := pt.p
|
|
|
|
if p.IsHome() || p.IsSection() {
|
|
return p
|
|
}
|
|
|
|
return p.Parent()
|
|
}
|
|
|
|
func (pt pageTree) IsDescendant(other interface{}) (bool, error) {
|
|
if pt.p == nil {
|
|
return false, nil
|
|
}
|
|
|
|
tp, ok := other.(treeRefProvider)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
|
|
|
|
if !ref2.isSection() {
|
|
return false, nil
|
|
}
|
|
|
|
return strings.HasPrefix(ref1.key, ref2.key), nil
|
|
|
|
}
|
|
|
|
func (pt pageTree) FirstSection() page.Page {
|
|
ref := pt.p.getTreeRef()
|
|
key := ref.key
|
|
if !ref.isSection() {
|
|
key = path.Dir(key)
|
|
}
|
|
_, b := ref.m.getFirstSection(key)
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
return b.p
|
|
}
|
|
|
|
func (pt pageTree) InSection(other interface{}) (bool, error) {
|
|
if pt.p == nil || types.IsNil(other) {
|
|
return false, nil
|
|
}
|
|
|
|
tp, ok := other.(treeRefProvider)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
|
|
|
|
s1, _ := ref1.getCurrentSection()
|
|
s2, _ := ref2.getCurrentSection()
|
|
|
|
return s1 == s2, nil
|
|
|
|
}
|
|
|
|
func (pt pageTree) Page() page.Page {
|
|
return pt.p
|
|
}
|
|
|
|
func (pt pageTree) Parent() page.Page {
|
|
p := pt.p
|
|
|
|
if p.parent != nil {
|
|
return p.parent
|
|
}
|
|
|
|
if pt.p.IsHome() {
|
|
return nil
|
|
}
|
|
|
|
if pt.p.Kind() == page.KindTaxonomyTerm {
|
|
return pt.p.s.home
|
|
}
|
|
|
|
_, b := p.getTreeRef().getSection()
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
|
|
return b.p
|
|
}
|
|
|
|
func (pt pageTree) Sections() page.Pages {
|
|
if pt.p.bucket == nil {
|
|
return nil
|
|
}
|
|
|
|
return pt.p.bucket.getSections()
|
|
}
|