| 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 |
"iter" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/paths" |
| 21 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 22 |
"github.com/gohugoio/hugo/identity" |
| 23 |
"github.com/gohugoio/hugo/resources/page" |
| 24 |
"github.com/gohugoio/hugo/resources/resource" |
| 25 |
) |
| 26 |
|
| 27 |
var _ contentNode = (*contentNodeSeq)(nil) |
| 28 |
|
| 29 |
var ( |
| 30 |
_ contentNode = (*resourceSource)(nil) |
| 31 |
_ contentNodeContentWeightProvider = (*pageMetaSource)(nil) |
| 32 |
_ contentNodeContentWeightProvider = (*pageState)(nil) |
| 33 |
_ contentNodeForSites = (*pageState)(nil) |
| 34 |
_ contentNodePage = (*contentNodes)(nil) |
| 35 |
_ contentNodePage = (*pageMetaSource)(nil) |
| 36 |
_ contentNodePage = (*pageState)(nil) |
| 37 |
_ contentNodeSourceEntryIDProvider = (*pageState)(nil) |
| 38 |
_ contentNodeSourceEntryIDProvider = (*pageMetaSource)(nil) |
| 39 |
_ contentNodeSourceEntryIDProvider = (*resourceSource)(nil) |
| 40 |
_ contentNodeLookupContentNode = (*contentNodesMap)(nil) |
| 41 |
_ contentNodeLookupContentNode = (contentNodes)(nil) |
| 42 |
_ contentNodeSingle = (*pageMetaSource)(nil) |
| 43 |
_ contentNodeSingle = (*pageState)(nil) |
| 44 |
_ contentNodeSingle = (*resourceSource)(nil) |
| 45 |
) |
| 46 |
|
| 47 |
var ( |
| 48 |
_ contentNode = (*contentNodesMap)(nil) |
| 49 |
_ contentNodeMap = (*contentNodesMap)(nil) |
| 50 |
) |
| 51 |
|
| 52 |
func (n contentNodesMap) ForEeachInAllDimensions(f func(contentNode) bool) { |
| 53 |
for _, nn := range n { |
| 54 |
if !f(nn) { |
| 55 |
return |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func (n contentNodesMap) Path() string { |
| 61 |
return n.sample().Path() |
| 62 |
} |
| 63 |
|
| 64 |
type ( |
| 65 |
contentNode interface { |
| 66 |
Path() string |
| 67 |
contentNodeForEach |
| 68 |
} |
| 69 |
|
| 70 |
contentNodeBuildStateResetter interface { |
| 71 |
resetBuildState() |
| 72 |
} |
| 73 |
|
| 74 |
contentNodeSeq2 iter.Seq2[sitesmatrix.Vector, contentNode] |
| 75 |
|
| 76 |
contentNodeForSite interface { |
| 77 |
contentNode |
| 78 |
siteVector() sitesmatrix.Vector |
| 79 |
} |
| 80 |
|
| 81 |
contentNodeForEach interface { |
| 82 |
// forEeachContentNode iterates over all content nodes. |
| 83 |
// calling f with the site vector and the content node. |
| 84 |
// If f returns false, the iteration stops. |
| 85 |
// It returns false if the iteration was stopped early. |
| 86 |
forEeachContentNode(f func(sitesmatrix.Vector, contentNode) bool) bool |
| 87 |
} |
| 88 |
|
| 89 |
contentNodeForSites interface { |
| 90 |
contentNode |
| 91 |
siteVectors() sitesmatrix.VectorIterator |
| 92 |
} |
| 93 |
|
| 94 |
contentNodePage interface { |
| 95 |
contentNode |
| 96 |
nodeCategoryPage() // Marker interface. |
| 97 |
} |
| 98 |
|
| 99 |
contentNodeSingle interface { |
| 100 |
contentNode |
| 101 |
nodeCategorySingle() // Marker interface. |
| 102 |
} |
| 103 |
|
| 104 |
contentNodeLookupContentNode interface { |
| 105 |
contentNode |
| 106 |
lookupContentNode(v sitesmatrix.Vector) contentNode |
| 107 |
} |
| 108 |
|
| 109 |
contentNodeLookupContentNodes interface { |
| 110 |
lookupContentNodes(v sitesmatrix.Vector, fallback bool) iter.Seq[contentNodeForSite] |
| 111 |
} |
| 112 |
|
| 113 |
contentNodeSampleProvider interface { |
| 114 |
// sample is used to get a sample contentNode from a collection. |
| 115 |
sample() contentNode |
| 116 |
} |
| 117 |
|
| 118 |
contentNodeCascadeProvider interface { |
| 119 |
getCascade() *page.PageMatcherParamsConfigs |
| 120 |
} |
| 121 |
|
| 122 |
contentNodeContentWeightProvider interface { |
| 123 |
contentWeight() int |
| 124 |
} |
| 125 |
|
| 126 |
contentNodeIsEmptyProvider interface { |
| 127 |
isEmpty() bool |
| 128 |
} |
| 129 |
|
| 130 |
contentNodeSourceEntryIDProvider interface { |
| 131 |
nodeSourceEntryID() any |
| 132 |
} |
| 133 |
|
| 134 |
contentNodeMap interface { |
| 135 |
contentNode |
| 136 |
contentNodeLookupContentNode |
| 137 |
} |
| 138 |
) |
| 139 |
|
| 140 |
type contentNodeSeq iter.Seq[contentNode] |
| 141 |
|
| 142 |
func (n contentNodeSeq) Path() string { |
| 143 |
return n.sample().Path() |
| 144 |
} |
| 145 |
|
| 146 |
func (n contentNodeSeq) isEmpty() bool { |
| 147 |
for range n { |
| 148 |
return false |
| 149 |
} |
| 150 |
return true |
| 151 |
} |
| 152 |
|
| 153 |
func (n contentNodeSeq) forEeachContentNode(f func(sitesmatrix.Vector, contentNode) bool) bool { |
| 154 |
for nn := range n { |
| 155 |
if !nn.forEeachContentNode(f) { |
| 156 |
return false |
| 157 |
} |
| 158 |
} |
| 159 |
return true |
| 160 |
} |
| 161 |
|
| 162 |
func (n contentNodeSeq) sample() contentNode { |
| 163 |
for nn := range n { |
| 164 |
return nn |
| 165 |
} |
| 166 |
return nil |
| 167 |
} |
| 168 |
|
| 169 |
func (n contentNodeSeq2) forEeachContentNode(f func(sitesmatrix.Vector, contentNode) bool) bool { |
| 170 |
for _, nn := range n { |
| 171 |
if !nn.forEeachContentNode(f) { |
| 172 |
return false |
| 173 |
} |
| 174 |
} |
| 175 |
return true |
| 176 |
} |
| 177 |
|
| 178 |
type contentNodes []contentNode |
| 179 |
|
| 180 |
func (n contentNodes) Path() string { |
| 181 |
return n.sample().Path() |
| 182 |
} |
| 183 |
|
| 184 |
func (m contentNodes) isEmpty() bool { |
| 185 |
return len(m) == 0 |
| 186 |
} |
| 187 |
|
| 188 |
func (n contentNodes) forEeachContentNode(f func(v sitesmatrix.Vector, n contentNode) bool) bool { |
| 189 |
for _, nn := range n { |
| 190 |
if !nn.forEeachContentNode(f) { |
| 191 |
return false |
| 192 |
} |
| 193 |
} |
| 194 |
return true |
| 195 |
} |
| 196 |
|
| 197 |
func (n contentNodes) lookupContentNode(v sitesmatrix.Vector) contentNode { |
| 198 |
for _, nn := range n { |
| 199 |
if vv := nn.(contentNodeLookupContentNode).lookupContentNode(v); vv != nil { |
| 200 |
return vv |
| 201 |
} |
| 202 |
} |
| 203 |
return nil |
| 204 |
} |
| 205 |
|
| 206 |
func (m *contentNodes) nodeCategoryPage() { |
| 207 |
// Marker method. |
| 208 |
} |
| 209 |
|
| 210 |
func (n contentNodes) sample() contentNode { |
| 211 |
if len(n) == 0 { |
| 212 |
panic("pageMetaSourcesSlice is empty") |
| 213 |
} |
| 214 |
return n[0] |
| 215 |
} |
| 216 |
|
| 217 |
type contentNodesMap map[sitesmatrix.Vector]contentNode |
| 218 |
|
| 219 |
var cnh helperContentNode |
| 220 |
|
| 221 |
type helperContentNode struct{} |
| 222 |
|
| 223 |
func (h helperContentNode) findContentNodeForSiteVector(q sitesmatrix.Vector, fallback bool, candidates contentNodeSeq) contentNodeForSite { |
| 224 |
var ( |
| 225 |
best contentNodeForSite = nil |
| 226 |
bestDistance int |
| 227 |
bestWeight int |
| 228 |
) |
| 229 |
|
| 230 |
for n := range candidates { |
| 231 |
// The order of candidates is unstable, so we need to compare the matches to |
| 232 |
// get stable output. This compare will also make sure that we pick |
| 233 |
// language, version and role according to their individual sort order: |
| 234 |
// Closer is better, and matches above are better than matches below. |
| 235 |
if m := n.(contentNodeLookupContentNodes).lookupContentNodes(q, fallback); m != nil { |
| 236 |
for nn := range m { |
| 237 |
vec := nn.siteVector() |
| 238 |
var w int |
| 239 |
if wp, ok := nn.(contentNodeContentWeightProvider); ok { |
| 240 |
w = wp.contentWeight() |
| 241 |
} |
| 242 |
|
| 243 |
if q == vec && w > 0 { |
| 244 |
// Exact match backed by a file. |
| 245 |
return nn |
| 246 |
} |
| 247 |
|
| 248 |
distance := q.Distance(vec) |
| 249 |
distanceAbs := absint(distance) |
| 250 |
|
| 251 |
if best == nil { |
| 252 |
best = nn |
| 253 |
bestDistance = distance |
| 254 |
bestWeight = w |
| 255 |
} else { |
| 256 |
bestDistanceAbs := absint(bestDistance) |
| 257 |
if w > 0 && bestWeight == 0 { |
| 258 |
// Prefer file-backed pages over auto pages. |
| 259 |
best = nn |
| 260 |
bestDistance = distance |
| 261 |
bestWeight = w |
| 262 |
} else if (w > 0) == (bestWeight > 0) { |
| 263 |
// Both file-backed or both auto: use distance. |
| 264 |
if distanceAbs < bestDistanceAbs { |
| 265 |
best = nn |
| 266 |
bestDistance = distance |
| 267 |
bestWeight = w |
| 268 |
} else if distanceAbs == bestDistanceAbs && distance > 0 { |
| 269 |
best = nn |
| 270 |
bestDistance = distance |
| 271 |
bestWeight = w |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return best |
| 280 |
} |
| 281 |
|
| 282 |
func (h helperContentNode) markStale(n contentNode) { |
| 283 |
n.forEeachContentNode( |
| 284 |
func(_ sitesmatrix.Vector, nn contentNode) bool { |
| 285 |
resource.MarkStale(nn) |
| 286 |
return true |
| 287 |
}, |
| 288 |
) |
| 289 |
} |
| 290 |
|
| 291 |
func (h helperContentNode) resetBuildState(n contentNode) { |
| 292 |
n.forEeachContentNode( |
| 293 |
func(_ sitesmatrix.Vector, nn contentNode) bool { |
| 294 |
if nnn, ok := nn.(contentNodeBuildStateResetter); ok { |
| 295 |
nnn.resetBuildState() |
| 296 |
} |
| 297 |
return true |
| 298 |
}, |
| 299 |
) |
| 300 |
} |
| 301 |
|
| 302 |
func (h helperContentNode) isBranchNode(n contentNode) bool { |
| 303 |
switch nn := n.(type) { |
| 304 |
case *pageMetaSource: |
| 305 |
return nn.pathInfo.IsBranchBundle() |
| 306 |
case *pageState: |
| 307 |
return nn.IsBranch() |
| 308 |
case contentNodeSampleProvider: |
| 309 |
return h.isBranchNode(nn.sample()) |
| 310 |
default: |
| 311 |
return false |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func (h helperContentNode) isPageNode(n contentNode) bool { |
| 316 |
n, _ = h.one(n) |
| 317 |
switch n.(type) { |
| 318 |
case contentNodePage: |
| 319 |
return true |
| 320 |
default: |
| 321 |
return false |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func (helperContentNode) one(n contentNode) (nn contentNode, hasMore bool) { |
| 326 |
n.forEeachContentNode(func(_ sitesmatrix.Vector, n contentNode) bool { |
| 327 |
if nn == nil { |
| 328 |
nn = n |
| 329 |
return true |
| 330 |
} else { |
| 331 |
hasMore = true |
| 332 |
return false |
| 333 |
} |
| 334 |
}) |
| 335 |
return |
| 336 |
} |
| 337 |
|
| 338 |
func (h helperContentNode) GetIdentity(n contentNode) identity.Identity { |
| 339 |
switch nn := n.(type) { |
| 340 |
case identity.IdentityProvider: |
| 341 |
return nn.GetIdentity() |
| 342 |
case contentNodeSampleProvider: |
| 343 |
return h.GetIdentity(nn.sample()) |
| 344 |
default: |
| 345 |
return identity.Anonymous |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
func (h helperContentNode) PathInfo(n contentNode) *paths.Path { |
| 350 |
switch nn := n.(type) { |
| 351 |
case interface{ PathInfo() *paths.Path }: |
| 352 |
return nn.PathInfo() |
| 353 |
case contentNodeSampleProvider: |
| 354 |
return h.PathInfo(nn.sample()) |
| 355 |
default: |
| 356 |
return nil |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func (h helperContentNode) toForEachIdentityProvider(n contentNode) identity.ForEeachIdentityProvider { |
| 361 |
return identity.ForEeachIdentityProviderFunc( |
| 362 |
func(cb func(id identity.Identity) bool) bool { |
| 363 |
return n.forEeachContentNode( |
| 364 |
func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 365 |
return cb(h.GetIdentity(n)) |
| 366 |
}, |
| 367 |
) |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
type weightedContentNode struct { |
| 372 |
n contentNode |
| 373 |
weight int |
| 374 |
term *pageWithOrdinal |
| 375 |
} |
| 376 |
|
| 377 |
func (n contentNodesMap) isEmpty() bool { |
| 378 |
return len(n) == 0 |
| 379 |
} |
| 380 |
|
| 381 |
func absint(i int) int { |
| 382 |
if i < 0 { |
| 383 |
return -i |
| 384 |
} |
| 385 |
return i |
| 386 |
} |
| 387 |
|
| 388 |
func contentNodeToContentNodesPage(n contentNode) (contentNodesMap, bool) { |
| 389 |
switch v := n.(type) { |
| 390 |
case contentNodesMap: |
| 391 |
return v, false |
| 392 |
case *pageState: |
| 393 |
return contentNodesMap{v.s.siteVector: v}, true |
| 394 |
default: |
| 395 |
panic(fmt.Sprintf("contentNodeToContentNodesPage: unexpected type %T", n)) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
// hasAutoContentNode reports whether n holds at least one node that is not backed by a file. |
| 400 |
func (h helperContentNode) hasAutoContentNode(n contentNodeForEach) bool { |
| 401 |
return !n.forEeachContentNode(func(_ sitesmatrix.Vector, nn contentNode) bool { |
| 402 |
wp, ok := nn.(contentNodeContentWeightProvider) |
| 403 |
return ok && wp.contentWeight() > 0 |
| 404 |
}) |
| 405 |
} |
| 406 |
|
| 407 |
func (h helperContentNode) contentNodeToSeq(n contentNodeForEach) contentNodeSeq { |
| 408 |
if nn, ok := n.(contentNodeSeq); ok { |
| 409 |
return nn |
| 410 |
} |
| 411 |
return func(yield func(contentNode) bool) { |
| 412 |
n.forEeachContentNode(func(_ sitesmatrix.Vector, nn contentNode) bool { |
| 413 |
return yield(nn) |
| 414 |
}) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
func (h helperContentNode) contentNodeToSeq2(n contentNodeForEach) contentNodeSeq2 { |
| 419 |
if nn, ok := n.(contentNodeSeq2); ok { |
| 420 |
return nn |
| 421 |
} |
| 422 |
return func(yield func(sitesmatrix.Vector, contentNode) bool) { |
| 423 |
n.forEeachContentNode(func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 424 |
return yield(vec, nn) |
| 425 |
}) |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
func (ps contentNodesMap) forEeachContentNode(f func(v sitesmatrix.Vector, n contentNode) bool) bool { |
| 430 |
for vec, nn := range ps { |
| 431 |
if !f(vec, nn) { |
| 432 |
return false |
| 433 |
} |
| 434 |
} |
| 435 |
return true |
| 436 |
} |
| 437 |
|
| 438 |
func (n contentNodesMap) lookupContentNode(v sitesmatrix.Vector) contentNode { |
| 439 |
if vv, ok := n[v]; ok { |
| 440 |
return vv |
| 441 |
} |
| 442 |
return nil |
| 443 |
} |
| 444 |
|
| 445 |
func (n contentNodesMap) sample() contentNode { |
| 446 |
for _, nn := range n { |
| 447 |
return nn |
| 448 |
} |
| 449 |
return nil |
| 450 |
} |
| 451 |
|
| 452 |
func (n contentNodesMap) siteVectors() sitesmatrix.VectorIterator { |
| 453 |
return n |
| 454 |
} |
| 455 |
|
| 456 |
func (n contentNodesMap) ForEachVector(yield func(v sitesmatrix.Vector) bool) bool { |
| 457 |
for v := range n { |
| 458 |
if !yield(v) { |
| 459 |
return false |
| 460 |
} |
| 461 |
} |
| 462 |
return true |
| 463 |
} |
| 464 |
|
| 465 |
func (n contentNodesMap) LenVectors() int { |
| 466 |
return len(n) |
| 467 |
} |
| 468 |
|
| 469 |
func (n contentNodesMap) VectorSample() sitesmatrix.Vector { |
| 470 |
for v := range n { |
| 471 |
return v |
| 472 |
} |
| 473 |
panic("no vectors") |
| 474 |
} |