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