github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/hugolib/page__tree.go (about) 1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package hugolib 15 16 import ( 17 "path" 18 "strings" 19 20 "github.com/gohugoio/hugo/common/types" 21 "github.com/gohugoio/hugo/resources/page" 22 ) 23 24 type pageTree struct { 25 p *pageState 26 } 27 28 func (pt pageTree) IsAncestor(other interface{}) (bool, error) { 29 if pt.p == nil { 30 return false, nil 31 } 32 33 tp, ok := other.(treeRefProvider) 34 if !ok { 35 return false, nil 36 } 37 38 ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef() 39 40 if ref1 != nil && ref1.key == "/" { 41 return true, nil 42 } 43 44 if ref1 == nil || ref2 == nil { 45 if ref1 == nil { 46 // A 404 or other similar standalone page. 47 return false, nil 48 } 49 50 return ref1.n.p.IsHome(), nil 51 } 52 53 if ref1.key == ref2.key { 54 return true, nil 55 } 56 57 if strings.HasPrefix(ref2.key, ref1.key) { 58 return true, nil 59 } 60 61 return strings.HasPrefix(ref2.key, ref1.key+cmBranchSeparator), nil 62 } 63 64 func (pt pageTree) CurrentSection() page.Page { 65 p := pt.p 66 67 if p.IsHome() || p.IsSection() { 68 return p 69 } 70 71 return p.Parent() 72 } 73 74 func (pt pageTree) IsDescendant(other interface{}) (bool, error) { 75 if pt.p == nil { 76 return false, nil 77 } 78 79 tp, ok := other.(treeRefProvider) 80 if !ok { 81 return false, nil 82 } 83 84 ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef() 85 86 if ref2 != nil && ref2.key == "/" { 87 return true, nil 88 } 89 90 if ref1 == nil || ref2 == nil { 91 if ref2 == nil { 92 // A 404 or other similar standalone page. 93 return false, nil 94 } 95 96 return ref2.n.p.IsHome(), nil 97 } 98 99 if ref1.key == ref2.key { 100 return true, nil 101 } 102 103 if strings.HasPrefix(ref1.key, ref2.key) { 104 return true, nil 105 } 106 107 return strings.HasPrefix(ref1.key, ref2.key+cmBranchSeparator), nil 108 } 109 110 func (pt pageTree) FirstSection() page.Page { 111 ref := pt.p.getTreeRef() 112 if ref == nil { 113 return pt.p.s.home 114 } 115 key := ref.key 116 117 if !ref.isSection() { 118 key = path.Dir(key) 119 } 120 121 _, b := ref.m.getFirstSection(key) 122 if b == nil { 123 return nil 124 } 125 return b.p 126 } 127 128 func (pt pageTree) InSection(other interface{}) (bool, error) { 129 if pt.p == nil || types.IsNil(other) { 130 return false, nil 131 } 132 133 tp, ok := other.(treeRefProvider) 134 if !ok { 135 return false, nil 136 } 137 138 ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef() 139 140 if ref1 == nil || ref2 == nil { 141 if ref1 == nil { 142 // A 404 or other similar standalone page. 143 return false, nil 144 } 145 return ref1.n.p.IsHome(), nil 146 } 147 148 s1, _ := ref1.getCurrentSection() 149 s2, _ := ref2.getCurrentSection() 150 151 return s1 == s2, nil 152 } 153 154 func (pt pageTree) Page() page.Page { 155 return pt.p 156 } 157 158 func (pt pageTree) Parent() page.Page { 159 p := pt.p 160 161 if p.parent != nil { 162 return p.parent 163 } 164 165 if pt.p.IsHome() { 166 return nil 167 } 168 169 tree := p.getTreeRef() 170 171 if tree == nil || pt.p.Kind() == page.KindTaxonomy { 172 return pt.p.s.home 173 } 174 175 _, b := tree.getSection() 176 if b == nil { 177 return nil 178 } 179 180 return b.p 181 } 182 183 func (pt pageTree) Sections() page.Pages { 184 if pt.p.bucket == nil { 185 return nil 186 } 187 188 return pt.p.bucket.getSections() 189 }