| 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 |
"cmp" |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"path" |
| 21 |
"strings" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/bep/helpers/maphelpers" |
| 25 |
"github.com/gohugoio/go-radix" |
| 26 |
"github.com/gohugoio/hugo/common/paths" |
| 27 |
"github.com/gohugoio/hugo/common/types" |
| 28 |
"github.com/gohugoio/hugo/hugofs/files" |
| 29 |
"github.com/gohugoio/hugo/hugolib/doctree" |
| 30 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 31 |
"github.com/gohugoio/hugo/media" |
| 32 |
"github.com/gohugoio/hugo/output" |
| 33 |
"github.com/gohugoio/hugo/resources" |
| 34 |
"github.com/gohugoio/hugo/resources/kinds" |
| 35 |
"github.com/gohugoio/hugo/resources/page" |
| 36 |
"github.com/gohugoio/hugo/resources/page/pagemeta" |
| 37 |
"github.com/spf13/cast" |
| 38 |
"golang.org/x/sync/errgroup" |
| 39 |
) |
| 40 |
|
| 41 |
// Handles the flow of creating all the pages and resources for all sites. |
| 42 |
// This includes applying any cascade configuration passed down the tree. |
| 43 |
type allPagesAssembler struct { |
| 44 |
// Dependencies. |
| 45 |
ctx context.Context |
| 46 |
h *HugoSites |
| 47 |
m *pageMap |
| 48 |
g errgroup.Group |
| 49 |
|
| 50 |
assembleChanges *WhatChanged |
| 51 |
assembleSectionsInParallel bool |
| 52 |
|
| 53 |
pwRoot *doctree.NodeShiftTreeWalker[contentNode] // walks pages. |
| 54 |
rwRoot *doctree.NodeShiftTreeWalker[contentNode] // walks resources. |
| 55 |
|
| 56 |
// Walking state. |
| 57 |
seenTerms *maphelpers.ConcurrentMap[term, sitesmatrix.Vectors] |
| 58 |
droppedPages *maphelpers.ConcurrentMap[*Site, []string] // e.g. drafts, expired, future. |
| 59 |
seenRootSections *maphelpers.ConcurrentMap[string, bool] |
| 60 |
seenHome bool // set before we fan out to multiple goroutines. |
| 61 |
} |
| 62 |
|
| 63 |
func newAllPagesAssembler( |
| 64 |
ctx context.Context, |
| 65 |
h *HugoSites, |
| 66 |
m *pageMap, |
| 67 |
assembleChanges *WhatChanged, |
| 68 |
) *allPagesAssembler { |
| 69 |
rw := &doctree.NodeShiftTreeWalker[contentNode]{ |
| 70 |
Tree: m.treeResources, |
| 71 |
LockType: doctree.LockTypeNone, |
| 72 |
NoShift: true, |
| 73 |
WalkContext: &doctree.WalkContext[contentNode]{}, |
| 74 |
} |
| 75 |
pw := rw.Extend() |
| 76 |
pw.Tree = m.treePages |
| 77 |
|
| 78 |
seenRootSections := maphelpers.NewConcurrentMap[string, bool]() |
| 79 |
seenRootSections.Set("", true) // home. |
| 80 |
|
| 81 |
return &allPagesAssembler{ |
| 82 |
ctx: ctx, |
| 83 |
h: h, |
| 84 |
m: m, |
| 85 |
assembleChanges: assembleChanges, |
| 86 |
seenTerms: maphelpers.NewConcurrentMap[term, sitesmatrix.Vectors](), |
| 87 |
droppedPages: maphelpers.NewConcurrentMap[*Site, []string](), |
| 88 |
seenRootSections: seenRootSections, |
| 89 |
assembleSectionsInParallel: !h.isRebuild(), // On partial rebuilds, there's potential data races with parallel section assembly. |
| 90 |
pwRoot: pw, |
| 91 |
rwRoot: rw, |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
type sitePagesAssembler struct { |
| 96 |
s *Site |
| 97 |
assembleChanges *WhatChanged |
| 98 |
a *allPagesAssembler |
| 99 |
ctx context.Context |
| 100 |
} |
| 101 |
|
| 102 |
func (a *allPagesAssembler) createAllPages() error { |
| 103 |
defer func() { |
| 104 |
for site, dropped := range a.droppedPages.All() { |
| 105 |
for _, s := range dropped { |
| 106 |
site.pageMap.treePages.Delete(s) |
| 107 |
site.pageMap.resourceTrees.DeletePrefix(paths.AddTrailingSlash(s)) |
| 108 |
} |
| 109 |
} |
| 110 |
}() |
| 111 |
|
| 112 |
if a.h.Conf.Watching() { |
| 113 |
defer func() { |
| 114 |
if a.h.isRebuild() && a.h.previousSeenTerms != nil { |
| 115 |
// Find removed terms. |
| 116 |
for t := range a.h.previousSeenTerms.All() { |
| 117 |
if _, found := a.seenTerms.Lookup(t); !found { |
| 118 |
// This term has been removed. |
| 119 |
a.pwRoot.Tree.Delete(t.view.pluralTreeKey) |
| 120 |
a.rwRoot.Tree.DeletePrefix(t.view.pluralTreeKey + "/") |
| 121 |
} |
| 122 |
} |
| 123 |
// Find new terms. |
| 124 |
for t := range a.seenTerms.All() { |
| 125 |
if _, found := a.h.previousSeenTerms.Lookup(t); !found { |
| 126 |
// This term is new. |
| 127 |
if n, ok := a.pwRoot.Tree.GetRaw(t.view.pluralTreeKey); ok { |
| 128 |
a.assembleChanges.Add(cnh.GetIdentity(n)) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
a.h.previousSeenTerms = a.seenTerms |
| 134 |
}() |
| 135 |
} |
| 136 |
|
| 137 |
if err := cmp.Or(a.doCreatePages("", 0), a.g.Wait()); err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
if err := a.pwRoot.WalkContext.HandleEventsAndHooks(); err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
return nil |
| 144 |
} |
| 145 |
|
| 146 |
func (a *allPagesAssembler) doCreatePages(prefix string, depth int) error { |
| 147 |
var ( |
| 148 |
sites = a.h.sitesVersionsRolesMap |
| 149 |
h = a.h |
| 150 |
treePages = a.m.treePages |
| 151 |
|
| 152 |
getViews = func(vec sitesmatrix.Vector) []viewName { |
| 153 |
return h.languageSiteForSiteVector(vec).pageMap.cfg.taxonomyConfig.views |
| 154 |
} |
| 155 |
|
| 156 |
pw *doctree.NodeShiftTreeWalker[contentNode] // walks pages. |
| 157 |
rw *doctree.NodeShiftTreeWalker[contentNode] // walks resources. |
| 158 |
|
| 159 |
isRootWalk = depth == 0 |
| 160 |
) |
| 161 |
|
| 162 |
if isRootWalk { |
| 163 |
pw = a.pwRoot |
| 164 |
rw = a.rwRoot |
| 165 |
} else { |
| 166 |
// Sub-walkers for a specific prefix. |
| 167 |
pw = a.pwRoot.Extend() |
| 168 |
pw.Prefix = prefix + "/" |
| 169 |
|
| 170 |
rw = a.rwRoot.Extend() // rw will get its prefix(es) set later. |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
resourceOwnerInfo := struct { |
| 175 |
n contentNode |
| 176 |
s string |
| 177 |
}{} |
| 178 |
|
| 179 |
newHomePageMetaSource := func() *pageMetaSource { |
| 180 |
pi := a.h.Conf.PathParser().Parse(files.ComponentFolderContent, "/_index.md") |
| 181 |
return &pageMetaSource{ |
| 182 |
pathInfo: pi, |
| 183 |
sitesMatrixBase: a.h.Conf.AllSitesMatrix(), |
| 184 |
sitesMatrixBaseOnly: true, |
| 185 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 186 |
Kind: kinds.KindHome, |
| 187 |
}, |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if isRootWalk { |
| 192 |
if err := a.createMissingPages(); err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
|
| 196 |
if treePages.Len() == 0 { |
| 197 |
// No pages, insert a home page to get something to walk on. |
| 198 |
p := newHomePageMetaSource() |
| 199 |
treePages.InsertRaw(p.pathInfo.Base(), p) |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
getCascades := func(wctx *doctree.WalkContext[contentNode], s string) *page.PageMatcherParamsConfigs { |
| 205 |
if wctx == nil { |
| 206 |
panic("nil walk context") |
| 207 |
} |
| 208 |
var cascades *page.PageMatcherParamsConfigs |
| 209 |
data := wctx.Data() |
| 210 |
if s != "" { |
| 211 |
_, data := data.LongestPrefix(s) |
| 212 |
|
| 213 |
if data != nil { |
| 214 |
cascades = data.(*page.PageMatcherParamsConfigs) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if cascades == nil { |
| 219 |
for s := range a.h.allSiteLanguages(nil) { |
| 220 |
cascades = cascades.Append(s.conf.Cascade) |
| 221 |
} |
| 222 |
} |
| 223 |
return cascades |
| 224 |
} |
| 225 |
|
| 226 |
doTransformPages := func(s string, n contentNode, cascades *page.PageMatcherParamsConfigs) (n2 contentNode, ns doctree.NodeTransformState, err error) { |
| 227 |
defer func() { |
| 228 |
cascadesLen := cascades.Len() |
| 229 |
if n2 == nil { |
| 230 |
if ns < doctree.NodeTransformStateSkip { |
| 231 |
ns = doctree.NodeTransformStateSkip |
| 232 |
} |
| 233 |
} else { |
| 234 |
n2.forEeachContentNode( |
| 235 |
func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 236 |
if pms, ok := nn.(contentNodeCascadeProvider); ok { |
| 237 |
cascades = cascades.Prepend(pms.getCascade()) |
| 238 |
} |
| 239 |
return true |
| 240 |
}, |
| 241 |
) |
| 242 |
} |
| 243 |
|
| 244 |
if s == "" || cascades.Len() > cascadesLen { |
| 245 |
// New cascade values added, pass them downwards. |
| 246 |
rw.WalkContext.Data().Insert(paths.AddTrailingSlash(s), cascades) |
| 247 |
} |
| 248 |
}() |
| 249 |
|
| 250 |
var handlePageMetaSource func(v contentNode, n contentNodesMap, replaceVector bool) (n2 contentNode, err error) |
| 251 |
handlePageMetaSource = func(v contentNode, n contentNodesMap, replaceVector bool) (n2 contentNode, err error) { |
| 252 |
if n != nil { |
| 253 |
n2 = n |
| 254 |
} |
| 255 |
|
| 256 |
switch ms := v.(type) { |
| 257 |
case *pageMetaSource: |
| 258 |
if err := ms.initEarly(a.h, cascades); err != nil { |
| 259 |
return nil, err |
| 260 |
} |
| 261 |
|
| 262 |
sitesMatrix := ms.pageConfigSource.SitesMatrix |
| 263 |
|
| 264 |
sitesMatrix.ForEachVector(func(vec sitesmatrix.Vector) bool { |
| 265 |
site, found := sites[vec] |
| 266 |
if !found { |
| 267 |
panic(fmt.Sprintf("site not found for %v", vec)) |
| 268 |
} |
| 269 |
|
| 270 |
var p *pageState |
| 271 |
p, err = site.newPageFromPageMetasource(ms, cascades) |
| 272 |
if err != nil { |
| 273 |
return false |
| 274 |
} |
| 275 |
|
| 276 |
if !site.shouldBuild(p) { |
| 277 |
(&p.m.pageConfig.Build).Disable() |
| 278 |
switch p.Kind() { |
| 279 |
case kinds.KindHome, kinds.KindSection, kinds.KindTaxonomy: |
| 280 |
// We need to keep these for the structure. |
| 281 |
default: |
| 282 |
// Drop this page. |
| 283 |
a.droppedPages.WithWriteLock( |
| 284 |
func(m map[*Site][]string) error { |
| 285 |
m[site] = append(m[site], s) |
| 286 |
return nil |
| 287 |
}, |
| 288 |
) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if n == nil { |
| 293 |
if n2 == nil { |
| 294 |
// Avoid creating a map for one node. |
| 295 |
n2 = p |
| 296 |
} else { |
| 297 |
// Upgrade to a map. |
| 298 |
n = make(contentNodesMap) |
| 299 |
ps := n2.(*pageState) |
| 300 |
n[ps.s.siteVector] = ps |
| 301 |
n2 = n |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if n == nil { |
| 306 |
return true |
| 307 |
} |
| 308 |
|
| 309 |
pp, found := n[vec] |
| 310 |
|
| 311 |
var w1, w2 int |
| 312 |
if wp, ok := pp.(contentNodeContentWeightProvider); ok { |
| 313 |
w1 = wp.contentWeight() |
| 314 |
} |
| 315 |
w2 = p.contentWeight() |
| 316 |
|
| 317 |
if found && !replaceVector && w1 > w2 { |
| 318 |
return true |
| 319 |
} |
| 320 |
|
| 321 |
n[vec] = p |
| 322 |
return true |
| 323 |
}) |
| 324 |
case *pageState: |
| 325 |
if n == nil { |
| 326 |
n2 = ms |
| 327 |
return |
| 328 |
} |
| 329 |
n[ms.s.siteVector] = ms |
| 330 |
case contentNodesMap: |
| 331 |
for _, vv := range ms { |
| 332 |
var err error |
| 333 |
n2, err = handlePageMetaSource(vv, n, replaceVector) |
| 334 |
if err != nil { |
| 335 |
return nil, err |
| 336 |
} |
| 337 |
if m, ok := n2.(contentNodesMap); ok { |
| 338 |
n = m |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
default: |
| 343 |
panic(fmt.Sprintf("unexpected type %T", v)) |
| 344 |
} |
| 345 |
|
| 346 |
return |
| 347 |
} |
| 348 |
|
| 349 |
// The common case. |
| 350 |
ns = doctree.NodeTransformStateReplaced |
| 351 |
|
| 352 |
handleContentNodeSeq := func(v contentNodeSeq) (contentNode, doctree.NodeTransformState, error) { |
| 353 |
is := make(contentNodesMap) |
| 354 |
for ms := range v { |
| 355 |
_, err := handlePageMetaSource(ms, is, false) |
| 356 |
if err != nil { |
| 357 |
return nil, 0, fmt.Errorf("failed to create page from pageMetaSource %s: %w", s, err) |
| 358 |
} |
| 359 |
} |
| 360 |
return is, ns, nil |
| 361 |
} |
| 362 |
|
| 363 |
switch v := n.(type) { |
| 364 |
case contentNodeSeq, contentNodes: |
| 365 |
return handleContentNodeSeq(cnh.contentNodeToSeq(v)) |
| 366 |
case *pageMetaSource: |
| 367 |
n2, err = handlePageMetaSource(v, nil, false) |
| 368 |
if err != nil { |
| 369 |
return nil, 0, fmt.Errorf("failed to create page from pageMetaSource %s: %w", s, err) |
| 370 |
} |
| 371 |
return |
| 372 |
case *pageState: |
| 373 |
// Nothing to do. |
| 374 |
ns = doctree.NodeTransformStateNone |
| 375 |
return v, ns, nil |
| 376 |
case contentNodesMap: |
| 377 |
ns = doctree.NodeTransformStateNone |
| 378 |
for _, vv := range v { |
| 379 |
switch m := vv.(type) { |
| 380 |
case *pageMetaSource: |
| 381 |
ns = doctree.NodeTransformStateUpdated |
| 382 |
_, err := handlePageMetaSource(m, v, true) |
| 383 |
if err != nil { |
| 384 |
return nil, 0, fmt.Errorf("failed to create page from pageMetaSource %s: %w", s, err) |
| 385 |
} |
| 386 |
default: |
| 387 |
// Nothing to do. |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
return v, ns, nil |
| 392 |
|
| 393 |
default: |
| 394 |
panic(fmt.Sprintf("unexpected type %T", n)) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
var unpackPageMetaSources func(n contentNode) contentNode |
| 399 |
unpackPageMetaSources = func(n contentNode) contentNode { |
| 400 |
switch nn := n.(type) { |
| 401 |
case *pageState: |
| 402 |
return nn.m.pageMetaSource |
| 403 |
case contentNodesMap: |
| 404 |
if len(nn) == 0 { |
| 405 |
return nil |
| 406 |
} |
| 407 |
var iter contentNodeSeq = func(yield func(contentNode) bool) { |
| 408 |
seen := map[*pageMetaSource]struct{}{} |
| 409 |
for _, v := range nn { |
| 410 |
vv := unpackPageMetaSources(v) |
| 411 |
pms := vv.(*pageMetaSource) |
| 412 |
if _, found := seen[pms]; !found { |
| 413 |
if !yield(pms) { |
| 414 |
return |
| 415 |
} |
| 416 |
seen[pms] = struct{}{} |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
return iter |
| 421 |
case contentNodes: |
| 422 |
if len(nn) == 0 { |
| 423 |
return nil |
| 424 |
} |
| 425 |
var iter contentNodeSeq = func(yield func(contentNode) bool) { |
| 426 |
seen := map[*pageMetaSource]struct{}{} |
| 427 |
for _, v := range nn { |
| 428 |
vv := unpackPageMetaSources(v) |
| 429 |
pms := vv.(*pageMetaSource) |
| 430 |
if _, found := seen[pms]; !found { |
| 431 |
if !yield(pms) { |
| 432 |
return |
| 433 |
} |
| 434 |
seen[pms] = struct{}{} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
return iter |
| 439 |
case *pageMetaSource: |
| 440 |
return nn |
| 441 |
default: |
| 442 |
panic(fmt.Sprintf("unexpected type %T", n)) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
transformPages := func(s string, n contentNode, cascades *page.PageMatcherParamsConfigs) (n2 contentNode, ns doctree.NodeTransformState, err error) { |
| 447 |
if a.h.isRebuild() { |
| 448 |
cascadesPrevious := getCascades(h.previousPageTreesWalkContext, s) |
| 449 |
h1, h2 := cascadesPrevious.SourceHash(), cascades.SourceHash() |
| 450 |
if h1 != h2 { |
| 451 |
// Force rebuild from the source. |
| 452 |
n = unpackPageMetaSources(n) |
| 453 |
} |
| 454 |
} |
| 455 |
if n == nil { |
| 456 |
panic("nil node") |
| 457 |
} |
| 458 |
n2, ns, err = doTransformPages(s, n, cascades) |
| 459 |
|
| 460 |
return |
| 461 |
} |
| 462 |
|
| 463 |
transformPagesAndCreateMissingHome := func(s string, n contentNode, isResource bool, cascades *page.PageMatcherParamsConfigs) (n2 contentNode, ns doctree.NodeTransformState, err error) { |
| 464 |
if n == nil { |
| 465 |
panic("nil node " + s) |
| 466 |
} |
| 467 |
level := strings.Count(s, "/") |
| 468 |
|
| 469 |
if s == "" { |
| 470 |
a.seenHome = true |
| 471 |
} |
| 472 |
|
| 473 |
if !isResource && s != "" && !a.seenHome { |
| 474 |
a.seenHome = true |
| 475 |
var homePages contentNode |
| 476 |
homePages, ns, err = transformPages("", newHomePageMetaSource(), cascades) |
| 477 |
if err != nil { |
| 478 |
return |
| 479 |
} |
| 480 |
treePages.InsertRaw("", homePages) |
| 481 |
} |
| 482 |
|
| 483 |
n2, ns, err = transformPages(s, n, cascades) |
| 484 |
if err != nil || ns >= doctree.NodeTransformStateSkip { |
| 485 |
return |
| 486 |
} |
| 487 |
|
| 488 |
if n2 == nil { |
| 489 |
ns = doctree.NodeTransformStateSkip |
| 490 |
return |
| 491 |
} |
| 492 |
|
| 493 |
if isResource { |
| 494 |
// Done. |
| 495 |
return |
| 496 |
} |
| 497 |
|
| 498 |
isTaxonomy := !a.h.getFirstTaxonomyConfig(s).IsZero() |
| 499 |
isRootSection := !isTaxonomy && level == 1 && cnh.isBranchNode(n) |
| 500 |
if !isTaxonomy { |
| 501 |
p := cnh.PathInfo(n) |
| 502 |
rootSection := p.Section() |
| 503 |
_, err := a.seenRootSections.GetOrCreate(rootSection, func() (bool, error) { |
| 504 |
if isRootSection { |
| 505 |
return true, nil |
| 506 |
} |
| 507 |
|
| 508 |
// Try to preserve the original casing if possible. |
| 509 |
sectionUnnormalized := p.Unnormalized().Section() |
| 510 |
rootSectionPath := a.h.Conf.PathParser().Parse(files.ComponentFolderContent, "/"+sectionUnnormalized+"/_index.md") |
| 511 |
|
| 512 |
// A regular page (e.g. content/s1.md) may share the same tree |
| 513 |
// key as its section (both map to /s1). In that case the page |
| 514 |
// itself already occupies the section root; creating a synthetic |
| 515 |
// section on top of it would overwrite the real content. |
| 516 |
if _, exists := treePages.GetRaw(rootSectionPath.Base()); exists { |
| 517 |
return true, nil |
| 518 |
} |
| 519 |
|
| 520 |
var rootSectionPages contentNode |
| 521 |
rootSectionPages, _, err = transformPages(rootSectionPath.Base(), &pageMetaSource{ |
| 522 |
pathInfo: rootSectionPath, |
| 523 |
sitesMatrixBase: n2.(contentNodeForSites).siteVectors(), |
| 524 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 525 |
Kind: kinds.KindSection, |
| 526 |
}, |
| 527 |
}, cascades) |
| 528 |
if err != nil { |
| 529 |
return true, err |
| 530 |
} |
| 531 |
treePages.InsertRaw(rootSectionPath.Base(), rootSectionPages) |
| 532 |
|
| 533 |
// Collect vectors from descendant pages to create section |
| 534 |
// pages for all languages that have content in this section. |
| 535 |
nm, replaced := contentNodeToContentNodesPage(rootSectionPages) |
| 536 |
if replaced { |
| 537 |
treePages.InsertRaw(rootSectionPath.Base(), nm) |
| 538 |
} |
| 539 |
missingVectors := sitesmatrix.Vectors{} |
| 540 |
pw.WalkContext.AddEventListener("sitesmatrix", rootSectionPath.Base(), |
| 541 |
func(e *doctree.Event[contentNode]) { |
| 542 |
if cnh.isBranchNode(e.Source) && !a.h.getFirstTaxonomyConfig(e.Path).IsZero() { |
| 543 |
return |
| 544 |
} |
| 545 |
n := e.Source |
| 546 |
e.StopPropagation() |
| 547 |
n.forEeachContentNode( |
| 548 |
func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 549 |
if _, found := nm[vec]; !found { |
| 550 |
missingVectors[vec] = struct{}{} |
| 551 |
} |
| 552 |
return true |
| 553 |
}) |
| 554 |
}, |
| 555 |
) |
| 556 |
pw.WalkContext.HooksPost().Push( |
| 557 |
func() error { |
| 558 |
if len(missingVectors) > 0 { |
| 559 |
vec := missingVectors.VectorSample() |
| 560 |
nm[vec] = &pageMetaSource{ |
| 561 |
pathInfo: rootSectionPath, |
| 562 |
sitesMatrixBase: missingVectors, |
| 563 |
sitesMatrixBaseOnly: true, |
| 564 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 565 |
Kind: kinds.KindSection, |
| 566 |
}, |
| 567 |
} |
| 568 |
_, _, err := transformPages(rootSectionPath.Base(), nm, cascades) |
| 569 |
if err != nil { |
| 570 |
return err |
| 571 |
} |
| 572 |
} |
| 573 |
return nil |
| 574 |
}, |
| 575 |
) |
| 576 |
|
| 577 |
return true, nil |
| 578 |
}) |
| 579 |
if err != nil { |
| 580 |
return nil, 0, err |
| 581 |
} |
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
const eventNameSitesMatrix = "sitesmatrix" |
| 586 |
|
| 587 |
if s == "" || isRootSection { |
| 588 |
|
| 589 |
// Every page needs a home and a root section (.FirstSection). |
| 590 |
// We don't know yet what language, version, role combination that will |
| 591 |
// be created below, so collect that information and create the missing pages |
| 592 |
// on demand. |
| 593 |
nm, replaced := contentNodeToContentNodesPage(n2) |
| 594 |
|
| 595 |
missingVectorsForHomeOrRootSection := sitesmatrix.Vectors{} |
| 596 |
|
| 597 |
if s == "" { |
| 598 |
// We need a complete set of home pages. |
| 599 |
a.h.Conf.AllSitesMatrix().ForEachVector(func(vec sitesmatrix.Vector) bool { |
| 600 |
if _, found := nm[vec]; !found { |
| 601 |
missingVectorsForHomeOrRootSection[vec] = struct{}{} |
| 602 |
} |
| 603 |
return true |
| 604 |
}) |
| 605 |
} else { |
| 606 |
pw.WalkContext.AddEventListener(eventNameSitesMatrix, s, |
| 607 |
func(e *doctree.Event[contentNode]) { |
| 608 |
n := e.Source |
| 609 |
e.StopPropagation() |
| 610 |
n.forEeachContentNode( |
| 611 |
func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 612 |
if _, found := nm[vec]; !found { |
| 613 |
missingVectorsForHomeOrRootSection[vec] = struct{}{} |
| 614 |
} |
| 615 |
return true |
| 616 |
}) |
| 617 |
}, |
| 618 |
) |
| 619 |
} |
| 620 |
|
| 621 |
// We need to wait until after the walk to have a complete set. |
| 622 |
pw.WalkContext.HooksPost().Push( |
| 623 |
func() error { |
| 624 |
if i := len(missingVectorsForHomeOrRootSection); i > 0 { |
| 625 |
// Pick one, the rest will be created later. |
| 626 |
vec := missingVectorsForHomeOrRootSection.VectorSample() |
| 627 |
|
| 628 |
kind := kinds.KindSection |
| 629 |
if s == "" { |
| 630 |
kind = kinds.KindHome |
| 631 |
} |
| 632 |
|
| 633 |
pms := &pageMetaSource{ |
| 634 |
pathInfo: cnh.PathInfo(n), |
| 635 |
sitesMatrixBase: missingVectorsForHomeOrRootSection, |
| 636 |
sitesMatrixBaseOnly: true, |
| 637 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 638 |
Kind: kind, |
| 639 |
}, |
| 640 |
} |
| 641 |
nm[vec] = pms |
| 642 |
|
| 643 |
_, ns, err := transformPages(s, nm, cascades) |
| 644 |
if err != nil { |
| 645 |
return err |
| 646 |
} |
| 647 |
if ns == doctree.NodeTransformStateReplaced { |
| 648 |
// Should not happen. |
| 649 |
panic(fmt.Sprintf("expected no replacement for %q", s)) |
| 650 |
} |
| 651 |
|
| 652 |
if replaced { |
| 653 |
pw.Tree.InsertRaw(s, nm) |
| 654 |
} |
| 655 |
} |
| 656 |
return nil |
| 657 |
}, |
| 658 |
) |
| 659 |
} |
| 660 |
|
| 661 |
if s != "" { |
| 662 |
rw.WalkContext.SendEvent(&doctree.Event[contentNode]{Source: n2, Path: s, Name: eventNameSitesMatrix}) |
| 663 |
} |
| 664 |
|
| 665 |
return |
| 666 |
} |
| 667 |
|
| 668 |
transformPagesAndCreateMissingStructuralNodes := func(s string, n contentNode, isResource bool) (n2 contentNode, ns doctree.NodeTransformState, err error) { |
| 669 |
cascades := getCascades(pw.WalkContext, s) |
| 670 |
n2, ns, err = transformPagesAndCreateMissingHome(s, n, isResource, cascades) |
| 671 |
if err != nil || ns >= doctree.NodeTransformStateSkip { |
| 672 |
return |
| 673 |
} |
| 674 |
n2.forEeachContentNode( |
| 675 |
func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 676 |
if ps, ok := nn.(*pageState); ok { |
| 677 |
if ps.m.noLink() { |
| 678 |
return true |
| 679 |
} |
| 680 |
for _, viewName := range getViews(vec) { |
| 681 |
vals := types.ToStringSlicePreserveString(getParam(ps, viewName.plural, false)) |
| 682 |
if vals == nil { |
| 683 |
continue |
| 684 |
} |
| 685 |
for _, v := range vals { |
| 686 |
if v == "" { |
| 687 |
continue |
| 688 |
} |
| 689 |
t := term{view: viewName, term: v} |
| 690 |
a.seenTerms.WithWriteLock(func(m map[term]sitesmatrix.Vectors) error { |
| 691 |
vectors, found := m[t] |
| 692 |
if !found { |
| 693 |
m[t] = sitesmatrix.Vectors{ |
| 694 |
vec: struct{}{}, |
| 695 |
} |
| 696 |
return nil |
| 697 |
} |
| 698 |
vectors[vec] = struct{}{} |
| 699 |
return nil |
| 700 |
}) |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
return true |
| 705 |
}, |
| 706 |
) |
| 707 |
|
| 708 |
return |
| 709 |
} |
| 710 |
|
| 711 |
shouldSkipOrTerminate := func(s string) (ns doctree.NodeTransformState) { |
| 712 |
owner := resourceOwnerInfo.n |
| 713 |
if owner == nil { |
| 714 |
return doctree.NodeTransformStateTerminate |
| 715 |
} |
| 716 |
if !cnh.isBranchNode(owner) { |
| 717 |
return |
| 718 |
} |
| 719 |
|
| 720 |
// A resourceKey always represents a filename with extension. |
| 721 |
// A page key points to the logical path of a page, which when sourced from the filesystem |
| 722 |
// may represent a directory (bundles) or a single content file (e.g. p1.md). |
| 723 |
// So, to avoid any overlapping ambiguity, we start looking from the owning directory. |
| 724 |
for { |
| 725 |
s = path.Dir(s) |
| 726 |
ownerKey, found := treePages.LongestPrefixRaw(s) |
| 727 |
if !found { |
| 728 |
return doctree.NodeTransformStateTerminate |
| 729 |
} |
| 730 |
if ownerKey == resourceOwnerInfo.s { |
| 731 |
break |
| 732 |
} |
| 733 |
|
| 734 |
if s != ownerKey && strings.HasPrefix(s, ownerKey) { |
| 735 |
// Keep looking |
| 736 |
continue |
| 737 |
} |
| 738 |
|
| 739 |
// Stop walking downwards, someone else owns this resource. |
| 740 |
rw.SkipPrefix(ownerKey + "/") |
| 741 |
return doctree.NodeTransformStateSkip |
| 742 |
} |
| 743 |
return |
| 744 |
} |
| 745 |
|
| 746 |
forEeachResourceOwnerPage := func(fn func(p *pageState) bool) bool { |
| 747 |
switch nn := resourceOwnerInfo.n.(type) { |
| 748 |
case *pageState: |
| 749 |
return fn(nn) |
| 750 |
case contentNodesMap: |
| 751 |
for _, p := range nn { |
| 752 |
if !fn(p.(*pageState)) { |
| 753 |
return false |
| 754 |
} |
| 755 |
} |
| 756 |
default: |
| 757 |
panic(fmt.Sprintf("unknown type %T", nn)) |
| 758 |
} |
| 759 |
return true |
| 760 |
} |
| 761 |
|
| 762 |
rw.Transform = func(s string, n contentNode) (n2 contentNode, ns doctree.NodeTransformState, err error) { |
| 763 |
if ns = shouldSkipOrTerminate(s); ns >= doctree.NodeTransformStateSkip { |
| 764 |
return |
| 765 |
} |
| 766 |
|
| 767 |
if cnh.isPageNode(n) { |
| 768 |
return transformPagesAndCreateMissingStructuralNodes(s, n, true) |
| 769 |
} |
| 770 |
|
| 771 |
nodes := make(contentNodesMap) |
| 772 |
ns = doctree.NodeTransformStateReplaced |
| 773 |
n2 = nodes |
| 774 |
|
| 775 |
forEeachResourceOwnerPage( |
| 776 |
func(p *pageState) bool { |
| 777 |
duplicateResourceFiles := a.h.Cfg.IsMultihost() |
| 778 |
if !duplicateResourceFiles && p.m.pageConfigSource.ContentMediaType.IsMarkdown() { |
| 779 |
duplicateResourceFiles = p.s.ContentSpec.Converters.GetMarkupConfig().Goldmark.DuplicateResourceFiles |
| 780 |
} |
| 781 |
|
| 782 |
if _, found := nodes[p.s.siteVector]; !found { |
| 783 |
var rs *resourceSource |
| 784 |
match := cnh.findContentNodeForSiteVector(p.s.siteVector, duplicateResourceFiles, cnh.contentNodeToSeq(n)) |
| 785 |
if match == nil { |
| 786 |
return true |
| 787 |
} |
| 788 |
|
| 789 |
rs = match.(*resourceSource) |
| 790 |
|
| 791 |
if rs != nil { |
| 792 |
if rs.state == resourceStateNew { |
| 793 |
nodes[p.s.siteVector] = rs.assignSiteVector(p.s.siteVector) |
| 794 |
} else { |
| 795 |
nodes[p.s.siteVector] = rs.clone().assignSiteVector(p.s.siteVector) |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
return true |
| 800 |
}, |
| 801 |
) |
| 802 |
|
| 803 |
// Duplicate resources across roles when owner pages share the same source. |
| 804 |
// This ensures resources in a page bundle are available to all roles |
| 805 |
// that the page spans, avoiding 404 errors in the rendered output. |
| 806 |
if len(nodes) > 0 { |
| 807 |
forEeachResourceOwnerPage(func(p *pageState) bool { |
| 808 |
if _, found := nodes[p.s.siteVector]; found { |
| 809 |
return true |
| 810 |
} |
| 811 |
// Find a resource from another role whose page shares |
| 812 |
// the same pageMetaSource (i.e. same mount/content file). |
| 813 |
forEeachResourceOwnerPage(func(donor *pageState) bool { |
| 814 |
if donor.s.siteVector == p.s.siteVector { |
| 815 |
return true |
| 816 |
} |
| 817 |
if donor.m.pageMetaSource != p.m.pageMetaSource { |
| 818 |
return true |
| 819 |
} |
| 820 |
// Only clone across role differences, not language or version. |
| 821 |
if donor.s.siteVector[sitesmatrix.Language] != p.s.siteVector[sitesmatrix.Language] || |
| 822 |
donor.s.siteVector[sitesmatrix.Version] != p.s.siteVector[sitesmatrix.Version] { |
| 823 |
return true |
| 824 |
} |
| 825 |
if rs, ok := nodes[donor.s.siteVector]; ok { |
| 826 |
nodes[p.s.siteVector] = rs.(*resourceSource).clone().assignSiteVector(p.s.siteVector) |
| 827 |
return false |
| 828 |
} |
| 829 |
return true |
| 830 |
}) |
| 831 |
return true |
| 832 |
}) |
| 833 |
} |
| 834 |
|
| 835 |
return |
| 836 |
} |
| 837 |
|
| 838 |
// Create missing term pages. |
| 839 |
pw.WalkContext.HooksPost().Push( |
| 840 |
func() error { |
| 841 |
for k, v := range a.seenTerms.All() { |
| 842 |
viewTermKey := "/" + k.view.plural + "/" + k.term |
| 843 |
|
| 844 |
pi := a.h.Conf.PathParser().Parse(files.ComponentFolderContent, viewTermKey+"/_index.md") |
| 845 |
termKey := pi.Base() |
| 846 |
|
| 847 |
n, found := pw.Tree.GetRaw(termKey) |
| 848 |
|
| 849 |
if found { |
| 850 |
// Merge. |
| 851 |
n.forEeachContentNode( |
| 852 |
func(vec sitesmatrix.Vector, nn contentNode) bool { |
| 853 |
delete(v, vec) |
| 854 |
return true |
| 855 |
}, |
| 856 |
) |
| 857 |
} |
| 858 |
|
| 859 |
if len(v) > 0 { |
| 860 |
p := &pageMetaSource{ |
| 861 |
pathInfo: pi, |
| 862 |
sitesMatrixBase: v, |
| 863 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 864 |
Kind: kinds.KindTerm, |
| 865 |
}, |
| 866 |
} |
| 867 |
var n2 contentNode = p |
| 868 |
if found { |
| 869 |
n2 = contentNodes{n, p} |
| 870 |
} |
| 871 |
n2, ns, err := transformPages(termKey, n2, getCascades(pw.WalkContext, termKey)) |
| 872 |
if err != nil { |
| 873 |
return fmt.Errorf("failed to create term page %q: %w", termKey, err) |
| 874 |
} |
| 875 |
|
| 876 |
switch ns { |
| 877 |
case doctree.NodeTransformStateReplaced: |
| 878 |
pw.Tree.InsertRaw(termKey, n2) |
| 879 |
} |
| 880 |
|
| 881 |
} |
| 882 |
} |
| 883 |
return nil |
| 884 |
}, |
| 885 |
) |
| 886 |
|
| 887 |
pw.Transform = func(s string, n contentNode) (n2 contentNode, ns doctree.NodeTransformState, err error) { |
| 888 |
n2, ns, err = transformPagesAndCreateMissingStructuralNodes(s, n, false) |
| 889 |
|
| 890 |
if err != nil || ns >= doctree.NodeTransformStateSkip { |
| 891 |
return |
| 892 |
} |
| 893 |
|
| 894 |
if iep, ok := n2.(contentNodeIsEmptyProvider); ok && iep.isEmpty() { |
| 895 |
ns = doctree.NodeTransformStateDeleted |
| 896 |
} |
| 897 |
|
| 898 |
if ns == doctree.NodeTransformStateDeleted { |
| 899 |
return |
| 900 |
} |
| 901 |
|
| 902 |
// Walk nested resources. |
| 903 |
resourceOwnerInfo.s = s |
| 904 |
resourceOwnerInfo.n = n2 |
| 905 |
rw = rw.WithPrefix(s + "/") |
| 906 |
if err := rw.Walk(a.ctx); err != nil { |
| 907 |
return nil, 0, err |
| 908 |
} |
| 909 |
const maxDepth = 3 |
| 910 |
if a.assembleSectionsInParallel && s != "" && depth < maxDepth && cnh.isBranchNode(n) && a.h.getFirstTaxonomyConfig(s).IsZero() { |
| 911 |
// Handle this branch in its own goroutine. |
| 912 |
pw.SkipPrefix(s + "/") |
| 913 |
a.g.Go(func() error { |
| 914 |
return a.doCreatePages(s, depth+1) |
| 915 |
}) |
| 916 |
|
| 917 |
} |
| 918 |
|
| 919 |
return |
| 920 |
} |
| 921 |
|
| 922 |
pw.Handle = nil |
| 923 |
|
| 924 |
if err := pw.Walk(a.ctx); err != nil { |
| 925 |
return err |
| 926 |
} |
| 927 |
|
| 928 |
return nil |
| 929 |
} |
| 930 |
|
| 931 |
// Calculate and apply aggregate values to the page tree (e.g. dates). |
| 932 |
func (sa *sitePagesAssembler) applyAggregates() error { |
| 933 |
sectionPageCount := map[string]int{} |
| 934 |
|
| 935 |
pw := &doctree.NodeShiftTreeWalker[contentNode]{ |
| 936 |
Tree: sa.s.pageMap.treePages, |
| 937 |
LockType: doctree.LockTypeRead, |
| 938 |
WalkContext: &doctree.WalkContext[contentNode]{}, |
| 939 |
} |
| 940 |
rw := pw.Extend() |
| 941 |
rw.Tree = sa.s.pageMap.treeResources |
| 942 |
sa.s.lastmod = time.Time{} |
| 943 |
rebuild := sa.s.h.isRebuild() |
| 944 |
|
| 945 |
pw.Handle = func(keyPage string, n contentNode) (radix.WalkFlag, error) { |
| 946 |
pageBundle := n.(*pageState) |
| 947 |
|
| 948 |
if pageBundle.Kind() == kinds.KindTerm { |
| 949 |
// Delay this until they're created. |
| 950 |
return radix.WalkContinue, nil |
| 951 |
} |
| 952 |
|
| 953 |
if pageBundle.IsPage() { |
| 954 |
rootSection := pageBundle.Section() |
| 955 |
sectionPageCount[rootSection]++ |
| 956 |
} |
| 957 |
|
| 958 |
if rebuild { |
| 959 |
if pageBundle.IsHome() || pageBundle.IsSection() { |
| 960 |
oldDates := pageBundle.m.pageConfig.Dates |
| 961 |
|
| 962 |
// We need to wait until after the walk to determine if any of the dates have changed. |
| 963 |
pw.WalkContext.HooksPost().Push( |
| 964 |
func() error { |
| 965 |
if oldDates != pageBundle.m.pageConfig.Dates { |
| 966 |
sa.assembleChanges.Add(pageBundle) |
| 967 |
} |
| 968 |
return nil |
| 969 |
}, |
| 970 |
) |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
const eventName = "dates" |
| 975 |
if cnh.isBranchNode(n) { |
| 976 |
wasZeroDates := pageBundle.m.pageConfig.Dates.IsAllDatesZero() |
| 977 |
if wasZeroDates || pageBundle.IsHome() { |
| 978 |
pw.WalkContext.AddEventListener(eventName, keyPage, func(e *doctree.Event[contentNode]) { |
| 979 |
sp, ok := e.Source.(*pageState) |
| 980 |
if !ok { |
| 981 |
return |
| 982 |
} |
| 983 |
|
| 984 |
if wasZeroDates { |
| 985 |
pageBundle.m.pageConfig.Dates.UpdateDateAndLastmodAndPublishDateIfAfter(sp.m.pageConfig.Dates) |
| 986 |
} |
| 987 |
|
| 988 |
if pageBundle.IsHome() { |
| 989 |
if pageBundle.m.pageConfig.Dates.Lastmod.After(pageBundle.s.lastmod) { |
| 990 |
pageBundle.s.lastmod = pageBundle.m.pageConfig.Dates.Lastmod |
| 991 |
} |
| 992 |
if sp.m.pageConfig.Dates.Lastmod.After(pageBundle.s.lastmod) { |
| 993 |
pageBundle.s.lastmod = sp.m.pageConfig.Dates.Lastmod |
| 994 |
} |
| 995 |
} |
| 996 |
}) |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
// Send the date info up the tree. |
| 1001 |
pw.WalkContext.SendEvent(&doctree.Event[contentNode]{Source: n, Path: keyPage, Name: eventName}) |
| 1002 |
|
| 1003 |
isBranch := cnh.isBranchNode(n) |
| 1004 |
rw.Prefix = keyPage + "/" |
| 1005 |
rw.IncludeRawFilter = func(s string, n contentNode) bool { |
| 1006 |
// Only page nodes. |
| 1007 |
return cnh.isPageNode(n) |
| 1008 |
} |
| 1009 |
|
| 1010 |
rw.IncludeFilter = func(s string, n contentNode) bool { |
| 1011 |
switch n.(type) { |
| 1012 |
case *pageState: |
| 1013 |
return true |
| 1014 |
default: |
| 1015 |
// We only want to handle page nodes here. |
| 1016 |
return false |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
rw.Handle = func(resourceKey string, n contentNode) (radix.WalkFlag, error) { |
| 1021 |
if isBranch { |
| 1022 |
ownerKey, _ := pw.Tree.LongestPrefix(resourceKey, false, nil) |
| 1023 |
if ownerKey != keyPage { |
| 1024 |
// Stop walking downwards, someone else owns this resource. |
| 1025 |
rw.SkipPrefix(ownerKey + "/") |
| 1026 |
return radix.WalkContinue, nil |
| 1027 |
} |
| 1028 |
} |
| 1029 |
switch rs := n.(type) { |
| 1030 |
case *pageState: |
| 1031 |
relPath := rs.m.pathInfo.BaseRel(pageBundle.m.pathInfo) |
| 1032 |
rs.m.resourcePath = relPath |
| 1033 |
} |
| 1034 |
|
| 1035 |
return radix.WalkContinue, nil |
| 1036 |
} |
| 1037 |
if err := rw.Walk(sa.ctx); err != nil { |
| 1038 |
return radix.WalkStop, err |
| 1039 |
} |
| 1040 |
return radix.WalkContinue, nil |
| 1041 |
} |
| 1042 |
|
| 1043 |
if err := pw.Walk(sa.ctx); err != nil { |
| 1044 |
return err |
| 1045 |
} |
| 1046 |
|
| 1047 |
if err := pw.WalkContext.HandleEventsAndHooks(); err != nil { |
| 1048 |
return err |
| 1049 |
} |
| 1050 |
|
| 1051 |
if !sa.s.conf.C.IsMainSectionsSet() { |
| 1052 |
var mainSection string |
| 1053 |
var maxcount int |
| 1054 |
for section, counter := range sectionPageCount { |
| 1055 |
if section != "" && counter > maxcount { |
| 1056 |
mainSection = section |
| 1057 |
maxcount = counter |
| 1058 |
} |
| 1059 |
} |
| 1060 |
sa.s.conf.C.SetMainSections([]string{mainSection}) |
| 1061 |
|
| 1062 |
} |
| 1063 |
|
| 1064 |
return nil |
| 1065 |
} |
| 1066 |
|
| 1067 |
func (sa *sitePagesAssembler) applyAggregatesToTaxonomiesAndTerms() error { |
| 1068 |
walkContext := &doctree.WalkContext[contentNode]{} |
| 1069 |
|
| 1070 |
handlePlural := func(key string) error { |
| 1071 |
var pw *doctree.NodeShiftTreeWalker[contentNode] |
| 1072 |
pw = &doctree.NodeShiftTreeWalker[contentNode]{ |
| 1073 |
Tree: sa.s.pageMap.treePages, |
| 1074 |
Prefix: key, // We also want to include the root taxonomy nodes, so no trailing slash. |
| 1075 |
LockType: doctree.LockTypeRead, |
| 1076 |
WalkContext: walkContext, |
| 1077 |
Handle: func(s string, n contentNode) (radix.WalkFlag, error) { |
| 1078 |
p := n.(*pageState) |
| 1079 |
|
| 1080 |
if p.Kind() != kinds.KindTerm && p.Kind() != kinds.KindTaxonomy { |
| 1081 |
// Already handled. |
| 1082 |
return radix.WalkContinue, nil |
| 1083 |
} |
| 1084 |
|
| 1085 |
const eventName = "dates" |
| 1086 |
|
| 1087 |
if p.Kind() == kinds.KindTerm { |
| 1088 |
if !p.s.shouldBuild(p) { |
| 1089 |
sa.s.pageMap.treePages.Delete(s) |
| 1090 |
sa.s.pageMap.treeTaxonomyEntries.DeletePrefix(paths.AddTrailingSlash(s)) |
| 1091 |
} else if err := sa.s.pageMap.treeTaxonomyEntries.WalkPrefix( |
| 1092 |
doctree.LockTypeRead, |
| 1093 |
paths.AddTrailingSlash(s), |
| 1094 |
func(ss string, wn *weightedContentNode) (bool, error) { |
| 1095 |
// Send the date info up the tree. |
| 1096 |
pw.WalkContext.SendEvent(&doctree.Event[contentNode]{Source: wn.n, Path: ss, Name: eventName}) |
| 1097 |
return false, nil |
| 1098 |
}, |
| 1099 |
); err != nil { |
| 1100 |
return radix.WalkStop, err |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
// Send the date info up the tree. |
| 1105 |
pw.WalkContext.SendEvent(&doctree.Event[contentNode]{Source: n, Path: s, Name: eventName}) |
| 1106 |
|
| 1107 |
if p.m.pageConfig.Dates.IsAllDatesZero() { |
| 1108 |
pw.WalkContext.AddEventListener(eventName, s, func(e *doctree.Event[contentNode]) { |
| 1109 |
sp, ok := e.Source.(*pageState) |
| 1110 |
if !ok { |
| 1111 |
return |
| 1112 |
} |
| 1113 |
|
| 1114 |
p.m.pageConfig.Dates.UpdateDateAndLastmodAndPublishDateIfAfter(sp.m.pageConfig.Dates) |
| 1115 |
}) |
| 1116 |
} |
| 1117 |
|
| 1118 |
return radix.WalkContinue, nil |
| 1119 |
}, |
| 1120 |
} |
| 1121 |
|
| 1122 |
if err := pw.Walk(sa.ctx); err != nil { |
| 1123 |
return err |
| 1124 |
} |
| 1125 |
return nil |
| 1126 |
} |
| 1127 |
|
| 1128 |
for _, viewName := range sa.s.pageMap.cfg.taxonomyConfig.views { |
| 1129 |
if err := handlePlural(viewName.pluralTreeKey); err != nil { |
| 1130 |
return err |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
if err := walkContext.HandleEventsAndHooks(); err != nil { |
| 1135 |
return err |
| 1136 |
} |
| 1137 |
|
| 1138 |
return nil |
| 1139 |
} |
| 1140 |
|
| 1141 |
func (sa *sitePagesAssembler) assembleTerms() error { |
| 1142 |
if sa.s.pageMap.cfg.taxonomyTermDisabled { |
| 1143 |
return nil |
| 1144 |
} |
| 1145 |
|
| 1146 |
var ( |
| 1147 |
pages = sa.s.pageMap.treePages |
| 1148 |
entries = sa.s.pageMap.treeTaxonomyEntries |
| 1149 |
views = sa.s.pageMap.cfg.taxonomyConfig.views |
| 1150 |
) |
| 1151 |
|
| 1152 |
lockType := doctree.LockTypeWrite |
| 1153 |
w := &doctree.NodeShiftTreeWalker[contentNode]{ |
| 1154 |
Tree: pages, |
| 1155 |
LockType: lockType, |
| 1156 |
Handle: func(s string, n contentNode) (radix.WalkFlag, error) { |
| 1157 |
ps := n.(*pageState) |
| 1158 |
|
| 1159 |
if ps.m.noLink() { |
| 1160 |
return radix.WalkContinue, nil |
| 1161 |
} |
| 1162 |
|
| 1163 |
for _, viewName := range views { |
| 1164 |
vals := types.ToStringSlicePreserveString(getParam(ps, viewName.plural, false)) |
| 1165 |
if vals == nil { |
| 1166 |
continue |
| 1167 |
} |
| 1168 |
|
| 1169 |
w := getParamToLower(ps, viewName.plural+"_weight") |
| 1170 |
weight, err := cast.ToIntE(w) |
| 1171 |
if err != nil { |
| 1172 |
sa.s.Log.Warnf("Unable to convert taxonomy weight %#v to int for %q", w, n.Path()) |
| 1173 |
// weight will equal zero, so let the flow continue |
| 1174 |
} |
| 1175 |
|
| 1176 |
for i, v := range vals { |
| 1177 |
if v == "" { |
| 1178 |
continue |
| 1179 |
} |
| 1180 |
viewTermKey := "/" + viewName.plural + "/" + v |
| 1181 |
pi := sa.s.Conf.PathParser().Parse(files.ComponentFolderContent, viewTermKey+"/_index.md") |
| 1182 |
termNode := pages.Get(pi.Base()) |
| 1183 |
if termNode == nil { |
| 1184 |
// This means that the term page has been disabled (e.g. a draft). |
| 1185 |
continue |
| 1186 |
} |
| 1187 |
|
| 1188 |
m := termNode.(*pageState).m |
| 1189 |
m.term = v |
| 1190 |
m.singular = viewName.singular |
| 1191 |
|
| 1192 |
if s == "" { |
| 1193 |
s = "/" |
| 1194 |
} |
| 1195 |
|
| 1196 |
key := pi.Base() + s |
| 1197 |
|
| 1198 |
entries.Insert(key, &weightedContentNode{ |
| 1199 |
weight: weight, |
| 1200 |
n: n, |
| 1201 |
term: &pageWithOrdinal{pageState: termNode.(*pageState), ordinal: i}, |
| 1202 |
}) |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
return radix.WalkContinue, nil |
| 1207 |
}, |
| 1208 |
} |
| 1209 |
|
| 1210 |
if err := w.Walk(sa.ctx); err != nil { |
| 1211 |
return err |
| 1212 |
} |
| 1213 |
|
| 1214 |
return nil |
| 1215 |
} |
| 1216 |
|
| 1217 |
func (sa *sitePagesAssembler) assemblePagesStepFinal() error { |
| 1218 |
if err := sa.assembleResourcesAndSetHome(); err != nil { |
| 1219 |
return err |
| 1220 |
} |
| 1221 |
return nil |
| 1222 |
} |
| 1223 |
|
| 1224 |
func (sa *sitePagesAssembler) assembleResourcesAndSetHome() error { |
| 1225 |
pagesTree := sa.s.pageMap.treePages |
| 1226 |
|
| 1227 |
lockType := doctree.LockTypeWrite |
| 1228 |
w := &doctree.NodeShiftTreeWalker[contentNode]{ |
| 1229 |
Tree: pagesTree, |
| 1230 |
LockType: lockType, |
| 1231 |
Handle: func(s string, n contentNode) (radix.WalkFlag, error) { |
| 1232 |
ps := n.(*pageState) |
| 1233 |
|
| 1234 |
if s == "" { |
| 1235 |
sa.s.home = ps |
| 1236 |
} else if ps.s.home == nil { |
| 1237 |
panic(fmt.Sprintf("[%v] expected home page to be set for %q", sa.s.siteVector, s)) |
| 1238 |
} |
| 1239 |
|
| 1240 |
// This is a little out of place, but is conveniently put here. |
| 1241 |
// Check if translationKey is set by user. |
| 1242 |
// This is to support the manual way of setting the translationKey in front matter. |
| 1243 |
if ps.m.pageConfig.TranslationKey != "" { |
| 1244 |
sa.s.h.translationKeyPages.Append(ps.m.pageConfig.TranslationKey, ps) |
| 1245 |
} |
| 1246 |
|
| 1247 |
if !sa.s.h.isRebuild() { |
| 1248 |
if ps.hasRenderableOutput() { |
| 1249 |
// For multi output pages this will not be complete, but will have to do for now. |
| 1250 |
sa.s.h.progressReporter.numPagesToRender.Add(1) |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
// Prepare resources for this page. |
| 1255 |
ps.shiftToOutputFormat(true, 0) |
| 1256 |
targetPaths := ps.targetPaths() |
| 1257 |
baseTarget := targetPaths.SubResourceBaseTarget |
| 1258 |
|
| 1259 |
err := sa.s.pageMap.forEachResourceInPage( |
| 1260 |
ps, lockType, |
| 1261 |
false, |
| 1262 |
nil, |
| 1263 |
func(resourceKey string, n contentNode) (bool, error) { |
| 1264 |
if _, ok := n.(*pageState); ok { |
| 1265 |
return false, nil |
| 1266 |
} |
| 1267 |
rs := n.(*resourceSource) |
| 1268 |
|
| 1269 |
relPathOriginal := rs.path.Unnormalized().PathRel(ps.m.pathInfo.Unnormalized()) |
| 1270 |
relPath := rs.path.BaseRel(ps.m.pathInfo) |
| 1271 |
|
| 1272 |
var targetBasePaths []string |
| 1273 |
if ps.s.Conf.IsMultihost() { |
| 1274 |
baseTarget = targetPaths.SubResourceBaseLink |
| 1275 |
// In multihost we need to publish to the lang sub folder. |
| 1276 |
targetBasePaths = []string{ps.s.GetTargetLanguageBasePath()} // TODO(bep) we don't need this as a slice anymore. |
| 1277 |
|
| 1278 |
} |
| 1279 |
|
| 1280 |
if rs.rc != nil && rs.rc.Content.IsResourceValue() { |
| 1281 |
if rs.rc.Name == "" { |
| 1282 |
rs.rc.Name = relPathOriginal |
| 1283 |
} |
| 1284 |
r, err := ps.s.ResourceSpec.NewResourceWrapperFromResourceConfig(rs.rc) |
| 1285 |
if err != nil { |
| 1286 |
return false, err |
| 1287 |
} |
| 1288 |
rs.r = r |
| 1289 |
return false, nil |
| 1290 |
} |
| 1291 |
|
| 1292 |
var mt media.Type |
| 1293 |
if rs.rc != nil { |
| 1294 |
mt = rs.rc.ContentMediaType |
| 1295 |
} |
| 1296 |
|
| 1297 |
var filename string |
| 1298 |
if rs.fi != nil { |
| 1299 |
filename = rs.fi.Meta().Filename |
| 1300 |
} |
| 1301 |
|
| 1302 |
rd := resources.ResourceSourceDescriptor{ |
| 1303 |
OpenReadSeekCloser: rs.opener, |
| 1304 |
Path: rs.path, |
| 1305 |
GroupIdentity: rs.path, |
| 1306 |
TargetPath: relPathOriginal, // Use the original path for the target path, so the links can be guessed. |
| 1307 |
TargetBasePaths: targetBasePaths, |
| 1308 |
BasePathRelPermalink: targetPaths.SubResourceBaseLink, |
| 1309 |
BasePathTargetPath: baseTarget, |
| 1310 |
SourceFilenameOrPath: filename, |
| 1311 |
NameNormalized: relPath, |
| 1312 |
NameOriginal: relPathOriginal, |
| 1313 |
MediaType: mt, |
| 1314 |
LazyPublish: !ps.m.pageConfig.Build.PublishResources, |
| 1315 |
} |
| 1316 |
|
| 1317 |
if rs.rc != nil { |
| 1318 |
rc := rs.rc |
| 1319 |
rd.OpenReadSeekCloser = rc.Content.ValueAsOpenReadSeekCloser() |
| 1320 |
if rc.Name != "" { |
| 1321 |
rd.NameNormalized = rc.Name |
| 1322 |
rd.NameOriginal = rc.Name |
| 1323 |
} |
| 1324 |
if rc.Title != "" { |
| 1325 |
rd.Title = rc.Title |
| 1326 |
} |
| 1327 |
rd.Params = rc.Params |
| 1328 |
} |
| 1329 |
|
| 1330 |
r, err := ps.s.ResourceSpec.NewResource(rd) |
| 1331 |
if err != nil { |
| 1332 |
return false, err |
| 1333 |
} |
| 1334 |
|
| 1335 |
rs.r = r |
| 1336 |
|
| 1337 |
return false, nil |
| 1338 |
}, |
| 1339 |
) |
| 1340 |
|
| 1341 |
return radix.WalkContinue, err |
| 1342 |
}, |
| 1343 |
} |
| 1344 |
|
| 1345 |
return w.Walk(sa.ctx) |
| 1346 |
} |
| 1347 |
|
| 1348 |
func (sa *sitePagesAssembler) assemblePagesStep1() error { |
| 1349 |
if err := sa.applyAggregates(); err != nil { |
| 1350 |
return err |
| 1351 |
} |
| 1352 |
return nil |
| 1353 |
} |
| 1354 |
|
| 1355 |
func (sa *sitePagesAssembler) assemblePagesStep2() error { |
| 1356 |
if err := sa.assembleTerms(); err != nil { |
| 1357 |
return err |
| 1358 |
} |
| 1359 |
|
| 1360 |
if err := sa.applyAggregatesToTaxonomiesAndTerms(); err != nil { |
| 1361 |
return err |
| 1362 |
} |
| 1363 |
|
| 1364 |
return nil |
| 1365 |
} |
| 1366 |
|
| 1367 |
// No locking. |
| 1368 |
func (a *allPagesAssembler) createMissingTaxonomies() error { |
| 1369 |
if a.m.cfg.taxonomyDisabled && a.m.cfg.taxonomyTermDisabled { |
| 1370 |
return nil |
| 1371 |
} |
| 1372 |
|
| 1373 |
tree := a.m.treePages |
| 1374 |
|
| 1375 |
viewLanguages := map[viewName][]int{} |
| 1376 |
for _, s := range a.h.sitesLanguages { |
| 1377 |
if s.pageMap.cfg.taxonomyDisabled && s.pageMap.cfg.taxonomyTermDisabled { |
| 1378 |
continue |
| 1379 |
} |
| 1380 |
for _, viewName := range s.pageMap.cfg.taxonomyConfig.views { |
| 1381 |
viewLanguages[viewName] = append(viewLanguages[viewName], s.siteVector.Language()) |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
for viewName, languages := range viewLanguages { |
| 1386 |
key := viewName.pluralTreeKey |
| 1387 |
if a.h.isRebuild() { |
| 1388 |
// Note that we cannot use tree.Get here, as the tree at this point |
| 1389 |
// may hold not yet assembled *pageMetaSource nodes. |
| 1390 |
// We're only interested in whether the auto created (not file backed) |
| 1391 |
// taxonomy node is still there. |
| 1392 |
if n, found := tree.GetRaw(key); found && cnh.hasAutoContentNode(n) { |
| 1393 |
continue |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
matrixAllForLanguages := sitesmatrix.NewIntSetsBuilder(a.h.Conf.ConfiguredDimensions()).WithLanguageIndices(languages...).WithAllIfNotSet().Build() |
| 1398 |
|
| 1399 |
pi := a.h.Conf.PathParser().Parse(files.ComponentFolderContent, key+"/_index.md") |
| 1400 |
p := &pageMetaSource{ |
| 1401 |
pathInfo: pi, |
| 1402 |
sitesMatrixBase: matrixAllForLanguages, |
| 1403 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 1404 |
Kind: kinds.KindTaxonomy, |
| 1405 |
}, |
| 1406 |
} |
| 1407 |
tree.AppendRaw(key, p) |
| 1408 |
} |
| 1409 |
|
| 1410 |
return nil |
| 1411 |
} |
| 1412 |
|
| 1413 |
// Create the fixed output pages, e.g. sitemap.xml, if not already there. |
| 1414 |
// TODO2 revise, especially around disabled and config per language/site. |
| 1415 |
// No locking. |
| 1416 |
func (a *allPagesAssembler) createMissingStandalonePages() error { |
| 1417 |
m := a.m |
| 1418 |
tree := m.treePages |
| 1419 |
oneSiteStore := (sitesmatrix.Vectors{sitesmatrix.Vector{0, 0, 0}: struct{}{}}).ToVectorStore() |
| 1420 |
|
| 1421 |
addStandalone := func(key, kind string, f output.Format) { |
| 1422 |
if !a.h.Conf.IsKindEnabled(kind) || tree.Has(key) { |
| 1423 |
return |
| 1424 |
} |
| 1425 |
|
| 1426 |
var sitesMatrixBase sitesmatrix.VectorIterator |
| 1427 |
|
| 1428 |
sitesMatrixBase = a.h.Conf.AllSitesMatrix() |
| 1429 |
if !a.h.Conf.IsMultihost() { |
| 1430 |
switch kind { |
| 1431 |
case kinds.KindSitemapIndex, kinds.KindRobotsTXT: |
| 1432 |
// First site only. |
| 1433 |
sitesMatrixBase = oneSiteStore |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
p := &pageMetaSource{ |
| 1438 |
pathInfo: a.h.Conf.PathParser().Parse(files.ComponentFolderContent, key+f.MediaType.FirstSuffix.FullSuffix), |
| 1439 |
standaloneOutputFormat: f, |
| 1440 |
sitesMatrixBase: sitesMatrixBase, |
| 1441 |
sitesMatrixBaseOnly: true, |
| 1442 |
pageConfigSource: &pagemeta.PageConfigEarly{ |
| 1443 |
Kind: kind, |
| 1444 |
}, |
| 1445 |
} |
| 1446 |
|
| 1447 |
tree.InsertRaw(key, p) |
| 1448 |
} |
| 1449 |
|
| 1450 |
addStandalone("/404", kinds.KindStatus404, output.HTTPStatus404HTMLFormat) |
| 1451 |
|
| 1452 |
if a.h.Configs.Base.EnableRobotsTXT { |
| 1453 |
if m.i == 0 || a.h.Conf.IsMultihost() { |
| 1454 |
addStandalone("/_robots", kinds.KindRobotsTXT, output.RobotsTxtFormat) |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
sitemapEnabled := false |
| 1459 |
for _, s := range a.h.Sites { |
| 1460 |
if s.conf.IsKindEnabled(kinds.KindSitemap) { |
| 1461 |
sitemapEnabled = true |
| 1462 |
break |
| 1463 |
} |
| 1464 |
} |
| 1465 |
|
| 1466 |
if sitemapEnabled { |
| 1467 |
of := output.SitemapFormat |
| 1468 |
if a.h.Configs.Base.Sitemap.Filename != "" { |
| 1469 |
of.BaseName = paths.Filename(a.h.Configs.Base.Sitemap.Filename) |
| 1470 |
} |
| 1471 |
addStandalone("/_sitemap", kinds.KindSitemap, of) |
| 1472 |
|
| 1473 |
skipSitemapIndex := a.h.Conf.IsMultihost() || !(a.h.Conf.DefaultContentLanguageInSubdir() || a.h.Conf.IsMultilingual()) |
| 1474 |
if !skipSitemapIndex { |
| 1475 |
of = output.SitemapIndexFormat |
| 1476 |
if a.h.Configs.Base.Sitemap.Filename != "" { |
| 1477 |
of.BaseName = paths.Filename(a.h.Configs.Base.Sitemap.Filename) |
| 1478 |
} |
| 1479 |
addStandalone("/_sitemapindex", kinds.KindSitemapIndex, of) |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
return nil |
| 1484 |
} |
| 1485 |
|
| 1486 |
// No locking. |
| 1487 |
func (a *allPagesAssembler) createMissingPages() error { |
| 1488 |
if err := a.createMissingTaxonomies(); err != nil { |
| 1489 |
return err |
| 1490 |
} |
| 1491 |
|
| 1492 |
if !a.h.isRebuild() { |
| 1493 |
if err := a.createMissingStandalonePages(); err != nil { |
| 1494 |
return err |
| 1495 |
} |
| 1496 |
} |
| 1497 |
return nil |
| 1498 |
} |