Debian Codegraph @

hugolib/page__tree.go

raw
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 "context"
18 "fmt"
19 "strings"
20
21 "github.com/gohugoio/go-radix"
22 "github.com/gohugoio/hugo/common/paths"
23 "github.com/gohugoio/hugo/common/types"
24 "github.com/gohugoio/hugo/hugolib/doctree"
25 "github.com/gohugoio/hugo/resources/kinds"
26 "github.com/gohugoio/hugo/resources/page"
27 )
28
29 // pageTree holds the treen navigational method for a Page.
30 type pageTree struct {
31 p *pageState
32 }
33
34 func (pt pageTree) IsAncestor(other any) bool {
35 n, ok := other.(contentNode)
36 if !ok {
37 return false
38 }
39
40 if n.Path() == pt.p.Path() {
41 return false
42 }
43
44 return strings.HasPrefix(n.Path(), paths.AddTrailingSlash(pt.p.Path()))
45 }
46
47 func (pt pageTree) IsDescendant(other any) bool {
48 n, ok := other.(contentNode)
49 if !ok {
50 return false
51 }
52
53 if n.Path() == pt.p.Path() {
54 return false
55 }
56
57 return strings.HasPrefix(pt.p.Path(), paths.AddTrailingSlash(n.Path()))
58 }
59
60 func (pt pageTree) CurrentSection() page.Page {
61 if kinds.IsBranch(pt.p.Kind()) {
62 return pt.p
63 }
64
65 dir := pt.p.m.pathInfo.Dir()
66
67 if dir == "/" {
68 if pt.p.s.home == nil {
69 panic(fmt.Sprintf("[%v] home page is nil for %q", pt.p.s.siteVector, pt.p.Path()))
70 }
71 return pt.p.s.home
72 }
73
74 _, n := pt.p.s.pageMap.treePages.LongestPrefix(dir, false, func(n contentNode) bool {
75 return cnh.isBranchNode(n)
76 })
77
78 if n != nil {
79 return n.(page.Page)
80 }
81
82 panic(fmt.Sprintf("CurrentSection not found for %q for %s", pt.p.Path(), pt.p.s.resolveDimensionNames()))
83 }
84
85 func (pt pageTree) FirstSection() page.Page {
86 s := pt.p.m.pathInfo.Dir()
87 if s == "/" {
88 return pt.p.s.home
89 }
90
91 for {
92 k, n := pt.p.s.pageMap.treePages.LongestPrefix(s, false, func(n contentNode) bool { return cnh.isBranchNode(n) })
93 if n == nil {
94 return nil
95 }
96
97 // /blog
98 if strings.Count(k, "/") < 2 {
99 return n.(page.Page)
100 }
101
102 if s == "" {
103 return nil
104 }
105
106 s = paths.Dir(s)
107
108 }
109 }
110
111 func (pt pageTree) InSection(other any) bool {
112 if pt.p == nil || types.IsNil(other) {
113 return false
114 }
115
116 p, ok := other.(page.Page)
117 if !ok {
118 return false
119 }
120
121 return pt.CurrentSection() == p.CurrentSection()
122 }
123
124 func (pt pageTree) Parent() page.Page {
125 if pt.p.IsHome() {
126 return nil
127 }
128
129 dir := pt.p.m.pathInfo.ContainerDir()
130
131 if dir == "" {
132 return pt.p.s.home
133 }
134
135 for {
136 _, n := pt.p.s.pageMap.treePages.LongestPrefix(dir, false, nil)
137 if n == nil {
138 return pt.p.s.home
139 }
140 if pt.p.m.bundled || cnh.isBranchNode(n) {
141 return n.(page.Page)
142 }
143 dir = paths.Dir(dir)
144 }
145 }
146
147 func (pt pageTree) Ancestors() page.Pages {
148 var ancestors page.Pages
149 parent := pt.Parent()
150 for parent != nil {
151 ancestors = append(ancestors, parent)
152 parent = parent.Parent()
153
154 }
155 return ancestors
156 }
157
158 func (pt pageTree) Sections() page.Pages {
159 var (
160 pages page.Pages
161 currentBranchPrefix string
162 s = pt.p.Path()
163 prefix = paths.AddTrailingSlash(s)
164 tree = pt.p.s.pageMap.treePages
165 )
166
167 w := &doctree.NodeShiftTreeWalker[contentNode]{
168 Tree: tree,
169 Prefix: prefix,
170 }
171 w.Handle = func(ss string, n contentNode) (radix.WalkFlag, error) {
172 if !cnh.isBranchNode(n) {
173 return radix.WalkContinue, nil
174 }
175 if currentBranchPrefix == "" || !strings.HasPrefix(ss, currentBranchPrefix) {
176 if p, ok := n.(*pageState); ok && p.IsSection() && p.m.shouldList(false) && p.Parent() == pt.p {
177 pages = append(pages, p)
178 } else {
179 w.SkipPrefix(ss + "/")
180 }
181 }
182 currentBranchPrefix = ss + "/"
183 return radix.WalkContinue, nil
184 }
185
186 if err := w.Walk(context.Background()); err != nil {
187 panic(err)
188 }
189
190 page.SortByDefault(pages)
191 return pages
192 }
193
194 func (pt pageTree) Page() page.Page {
195 return pt.p
196 }
197
198 func (p pageTree) SectionsEntries() []string {
199 sp := p.SectionsPath()
200 if sp == "/" {
201 return nil
202 }
203 entries := strings.Split(sp[1:], "/")
204 if len(entries) == 0 {
205 return nil
206 }
207 return entries
208 }
209
210 func (p pageTree) SectionsPath() string {
211 return p.CurrentSection().Path()
212 }