Debian Codegraph @

hugolib/content_map_page_contentnodeshifter.go

raw
1 // Copyright 2025 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 "fmt"
18
19 "github.com/gohugoio/hugo/config"
20 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
21 "github.com/gohugoio/hugo/resources/resource"
22 )
23
24 type contentNodeShifter struct {
25 conf config.AllProvider // Used for logging/debugging.
26 }
27
28 func (s *contentNodeShifter) Delete(n contentNode, vec sitesmatrix.Vector) (contentNode, bool, bool) {
29 switch v := n.(type) {
30 case contentNodesMap:
31 deleted, wasDeleted := v[vec]
32 if wasDeleted {
33 delete(v, vec)
34 resource.MarkStale(deleted)
35 }
36 return deleted, wasDeleted, len(v) == 0
37 case contentNodeForSite:
38 if v.siteVector() != vec {
39 return nil, false, false
40 }
41 resource.MarkStale(v)
42 return v, true, true
43 case contentNodes:
44 var deleted contentNodes
45 for i, nn := range v {
46 if vv, ok, _ := s.Delete(nn, vec); ok {
47 deleted = append(deleted, vv)
48 v = append(v[:i], v[i+1:]...)
49 }
50 }
51 if len(deleted) == 0 {
52 return nil, false, false
53 }
54 return deleted, true, len(v) == 0
55 default:
56 v = v.(contentNodeSingle) // Ensure single node.
57 resource.MarkStale(v)
58 return v, true, true
59
60 }
61 }
62
63 func (s *contentNodeShifter) DeleteFunc(v contentNode, f func(n contentNode) bool) bool {
64 switch ss := v.(type) {
65 case contentNodeSingle:
66 if f(ss) {
67 resource.MarkStale(ss)
68 return true
69 }
70 return false
71 case contentNodes:
72 for i, n := range ss {
73 if f(n) {
74 resource.MarkStale(n)
75 ss = append(ss[:i], ss[i+1:]...)
76 }
77 }
78 return len(ss) == 0
79 case contentNodesMap:
80 for k, n := range ss {
81 if f(n) {
82 resource.MarkStale(n)
83 delete(ss, k)
84 }
85 }
86 return len(ss) == 0
87 default:
88 panic(fmt.Sprintf("DeleteFunc: unknown type %T", v))
89 }
90 }
91
92 func (s *contentNodeShifter) ForEeachInAllDimensions(n contentNode, f func(contentNode) bool) {
93 if n == nil {
94 return
95 }
96 if v, ok := n.(interface {
97 // Implemented by all the list nodes.
98 ForEeachInAllDimensions(f func(contentNode) bool)
99 }); ok {
100 v.ForEeachInAllDimensions(f)
101 return
102 }
103 f(n)
104 }
105
106 func (s *contentNodeShifter) ForEeachInDimension(n contentNode, vec sitesmatrix.Vector, d int, f func(contentNode) bool) {
107 LOOP1:
108 for vec2, v := range cnh.contentNodeToSeq2(n) {
109 for i, v := range vec2 {
110 if i != d && v != vec[i] {
111 continue LOOP1
112 }
113 }
114 if !f(v) {
115 return
116 }
117 }
118 }
119
120 func (s *contentNodeShifter) Insert(old, new contentNode) (contentNode, contentNode, bool) {
121 new = new.(contentNodeSingle) // Ensure single node.
122
123 switch vv := old.(type) {
124 case contentNodeSingle:
125 return contentNodes{vv, new}, old, false
126 case contentNodes:
127 s := make(contentNodes, 0, len(vv)+1)
128 s = append(s, vv...)
129 s = append(s, new)
130 return s, old, false
131 case contentNodesMap:
132 switch new := new.(type) {
133 case contentNodeForSite:
134 oldp := vv[new.siteVector()]
135 updated := oldp != new
136 if updated {
137 resource.MarkStale(oldp)
138 }
139 vv[new.siteVector()] = new
140 return vv, oldp, updated
141 default:
142 s := make(contentNodes, 0, len(vv)+1)
143 for _, v := range vv {
144 s = append(s, v)
145 }
146 s = append(s, new)
147 return s, vv, false
148 }
149 default:
150 panic(fmt.Sprintf("Insert: unknown type %T", old))
151 }
152 }
153
154 func (s *contentNodeShifter) Shift(n contentNode, siteVector sitesmatrix.Vector, fallback bool) (contentNode, bool) {
155 var exact contentNode
156 switch v := n.(type) {
157 case contentNodeLookupContentNode:
158 exact = v.lookupContentNode(siteVector)
159 default:
160 panic(fmt.Sprintf("Shift: unknown type %T for %q", n, n.Path()))
161 }
162
163 if exact != nil {
164 if !fallback {
165 return exact, true
166 }
167 // If the exact match is backed by a file, return it directly.
168 if wp, ok := exact.(contentNodeContentWeightProvider); ok && wp.contentWeight() > 0 {
169 return exact, true
170 }
171 // The exact match is an auto page (not backed by a file).
172 // Check if there's a file-backed complement that should take precedence.
173 if vvv := cnh.findContentNodeForSiteVector(siteVector, fallback, cnh.contentNodeToSeq(n)); vvv != nil {
174 return vvv, true
175 }
176 return exact, true
177 }
178
179 if !fallback {
180 return nil, false
181 }
182
183 if vvv := cnh.findContentNodeForSiteVector(siteVector, fallback, cnh.contentNodeToSeq(n)); vvv != nil {
184 return vvv, true
185 }
186
187 return nil, false
188 }