| 1 |
// Copyright 2024 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 |
"errors" |
| 19 |
"fmt" |
| 20 |
"io" |
| 21 |
"maps" |
| 22 |
"mime" |
| 23 |
"net/url" |
| 24 |
"os" |
| 25 |
"path/filepath" |
| 26 |
"runtime" |
| 27 |
"slices" |
| 28 |
"sort" |
| 29 |
"strings" |
| 30 |
"sync" |
| 31 |
"time" |
| 32 |
|
| 33 |
"github.com/bep/logg" |
| 34 |
"github.com/gohugoio/go-radix" |
| 35 |
"github.com/gohugoio/hugo/cache/dynacache" |
| 36 |
"github.com/gohugoio/hugo/common/hmaps" |
| 37 |
"github.com/gohugoio/hugo/common/hstore" |
| 38 |
"github.com/gohugoio/hugo/common/hsync" |
| 39 |
"github.com/gohugoio/hugo/common/htime" |
| 40 |
"github.com/gohugoio/hugo/common/hugio" |
| 41 |
"github.com/gohugoio/hugo/common/hugo" |
| 42 |
"github.com/gohugoio/hugo/common/loggers" |
| 43 |
"github.com/gohugoio/hugo/common/para" |
| 44 |
"github.com/gohugoio/hugo/common/types" |
| 45 |
"github.com/gohugoio/hugo/config" |
| 46 |
"github.com/gohugoio/hugo/config/allconfig" |
| 47 |
"github.com/gohugoio/hugo/deps" |
| 48 |
"github.com/gohugoio/hugo/hugolib/doctree" |
| 49 |
"github.com/gohugoio/hugo/hugolib/pagesfromdata" |
| 50 |
"github.com/gohugoio/hugo/hugolib/roles" |
| 51 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 52 |
"github.com/gohugoio/hugo/hugolib/versions" |
| 53 |
"github.com/gohugoio/hugo/internal/js/esbuild" |
| 54 |
"github.com/gohugoio/hugo/internal/warpc" |
| 55 |
"github.com/gohugoio/hugo/langs/i18n" |
| 56 |
"github.com/gohugoio/hugo/modules" |
| 57 |
"github.com/gohugoio/hugo/resources" |
| 58 |
|
| 59 |
"github.com/gohugoio/hugo/tpl/tplimpl" |
| 60 |
"github.com/gohugoio/hugo/tpl/tplimplinit" |
| 61 |
|
| 62 |
// Loads the template funcs namespaces. |
| 63 |
|
| 64 |
"golang.org/x/text/unicode/norm" |
| 65 |
|
| 66 |
"github.com/gohugoio/hugo/common/paths" |
| 67 |
|
| 68 |
"github.com/gohugoio/hugo/identity" |
| 69 |
|
| 70 |
"github.com/gohugoio/hugo/markup/converter/hooks" |
| 71 |
|
| 72 |
"github.com/gohugoio/hugo/markup/converter" |
| 73 |
|
| 74 |
"github.com/gohugoio/hugo/common/text" |
| 75 |
|
| 76 |
"github.com/gohugoio/hugo/publisher" |
| 77 |
|
| 78 |
"github.com/gohugoio/hugo/langs" |
| 79 |
|
| 80 |
"github.com/gohugoio/hugo/resources/kinds" |
| 81 |
"github.com/gohugoio/hugo/resources/page" |
| 82 |
"github.com/gohugoio/hugo/resources/page/pagemeta" |
| 83 |
"github.com/gohugoio/hugo/resources/page/siteidentities" |
| 84 |
"github.com/gohugoio/hugo/resources/resource" |
| 85 |
|
| 86 |
"github.com/fsnotify/fsnotify" |
| 87 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 88 |
"github.com/gohugoio/hugo/helpers" |
| 89 |
"github.com/gohugoio/hugo/navigation" |
| 90 |
"github.com/gohugoio/hugo/output" |
| 91 |
"github.com/gohugoio/hugo/tpl" |
| 92 |
) |
| 93 |
|
| 94 |
var _ page.Site = (*Site)(nil) |
| 95 |
|
| 96 |
type siteState int |
| 97 |
|
| 98 |
const ( |
| 99 |
siteStateInit siteState = iota |
| 100 |
siteStateReady |
| 101 |
) |
| 102 |
|
| 103 |
type Site struct { |
| 104 |
state siteState |
| 105 |
conf *allconfig.Config |
| 106 |
language *langs.Language |
| 107 |
store *hstore.Scratch |
| 108 |
siteWrapped page.Site |
| 109 |
|
| 110 |
// The owning container. |
| 111 |
h *HugoSites |
| 112 |
|
| 113 |
*deps.Deps |
| 114 |
*siteLanguageVersionRole |
| 115 |
|
| 116 |
relatedDocsHandler *page.RelatedDocsHandler |
| 117 |
|
| 118 |
publisher publisher.Publisher |
| 119 |
frontmatterHandler pagemeta.FrontMatterHandler |
| 120 |
} |
| 121 |
|
| 122 |
func (s Site) cloneForVersionAndRole(version, role int) (*Site, error) { |
| 123 |
s.siteLanguageVersionRole = s.siteLanguageVersionRole.cloneForVersionAndRole(version, role) |
| 124 |
d, err := s.Deps.Clone(&s, s.Conf) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
s.Deps = d |
| 129 |
ss := &s |
| 130 |
ss.siteWrapped = page.WrapSite(ss) |
| 131 |
return ss, nil |
| 132 |
} |
| 133 |
|
| 134 |
// For debugging purposes only. |
| 135 |
func (s *Site) resolveDimensionNames() types.Strings3 { |
| 136 |
return s.Conf.ConfiguredDimensions().ResolveNames(s.siteVector) |
| 137 |
} |
| 138 |
|
| 139 |
type siteLanguageVersionRole struct { |
| 140 |
siteVector sitesmatrix.Vector |
| 141 |
isDefaultLanguage bool |
| 142 |
|
| 143 |
roleInternal roles.RoleInternal |
| 144 |
role roles.Role |
| 145 |
|
| 146 |
versionInternal versions.VersionInternal |
| 147 |
version versions.Version |
| 148 |
|
| 149 |
pageMap *pageMap |
| 150 |
|
| 151 |
// Page navigation. |
| 152 |
*pageFinder |
| 153 |
siteRefLinker siteRefLinker |
| 154 |
|
| 155 |
// Shortcut to the home page. Note that this may be nil if |
| 156 |
// home page, for some odd reason, is disabled. |
| 157 |
home *pageState |
| 158 |
|
| 159 |
// The last modification date of this site. |
| 160 |
lastmod time.Time |
| 161 |
|
| 162 |
// Lazily loaded site dependencies |
| 163 |
init *siteInit |
| 164 |
|
| 165 |
// The output formats that we need to render this site in. This slice |
| 166 |
// will be fixed once set. |
| 167 |
// This will be the union of Site.Pages' outputFormats. |
| 168 |
// This slice will be sorted. |
| 169 |
renderFormats output.Formats |
| 170 |
} |
| 171 |
|
| 172 |
func (s siteLanguageVersionRole) cloneForVersionAndRole(version, role int) *siteLanguageVersionRole { |
| 173 |
s.siteVector[sitesmatrix.Version] = version |
| 174 |
s.siteVector[sitesmatrix.Role] = role |
| 175 |
s.home = nil |
| 176 |
s.lastmod = time.Time{} |
| 177 |
s.init = &siteInit{} |
| 178 |
return &s |
| 179 |
} |
| 180 |
|
| 181 |
func (s siteLanguageVersionRole) Role() roles.Role { |
| 182 |
return s.role |
| 183 |
} |
| 184 |
|
| 185 |
func (s siteLanguageVersionRole) Version() versions.Version { |
| 186 |
return s.version |
| 187 |
} |
| 188 |
|
| 189 |
func (s siteLanguageVersionRole) isDefault() bool { |
| 190 |
return s.isDefaultLanguage && s.roleInternal.Default && s.versionInternal.Default |
| 191 |
} |
| 192 |
|
| 193 |
func (s *Site) Debug() { |
| 194 |
fmt.Println("Debugging site", s.Lang(), "=>") |
| 195 |
// fmt.Println(s.pageMap.testDump()) |
| 196 |
} |
| 197 |
|
| 198 |
// NewHugoSites creates HugoSites from the given config. |
| 199 |
func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) { |
| 200 |
if !cfg.TestCfg.IsZero() && !cfg.IsIntegrationTest { |
| 201 |
panic("DepsCfg.TestCfg must only be set in integration tests") |
| 202 |
} |
| 203 |
|
| 204 |
conf := cfg.Configs.GetFirstLanguageConfig() |
| 205 |
rolesSorted := cfg.Configs.Base.Roles.Config.Sorted |
| 206 |
versionsSorted := cfg.Configs.Base.Versions.Config.Sorted |
| 207 |
|
| 208 |
var ( |
| 209 |
poolSizeKatex = 2 |
| 210 |
poolSizeWebP = 1 |
| 211 |
poolSizeAvif = 1 |
| 212 |
) |
| 213 |
if n := config.GetNumWorkerMultiplier(); n > 1 { |
| 214 |
poolSizeKatex = min(n, 8) |
| 215 |
poolSizeWebP = max(2, n/2) |
| 216 |
poolSizeAvif = max(2, n/2) |
| 217 |
} |
| 218 |
|
| 219 |
var logger loggers.Logger |
| 220 |
if cfg.TestLogger != nil { |
| 221 |
logger = cfg.TestLogger |
| 222 |
} else { |
| 223 |
var logHookLast func(e *logg.Entry) error |
| 224 |
if cfg.Configs.Base.PanicOnWarning { |
| 225 |
logHookLast = loggers.PanicOnWarningHook |
| 226 |
} |
| 227 |
if cfg.StdOut == nil { |
| 228 |
cfg.StdOut = os.Stdout |
| 229 |
} |
| 230 |
if cfg.StdErr == nil { |
| 231 |
cfg.StdErr = os.Stderr |
| 232 |
} |
| 233 |
if cfg.LogLevel == 0 { |
| 234 |
cfg.LogLevel = logg.LevelWarn |
| 235 |
} |
| 236 |
|
| 237 |
logOpts := loggers.Options{ |
| 238 |
Level: cfg.LogLevel, |
| 239 |
DistinctLevel: logg.LevelWarn, // This will drop duplicate log warning and errors. |
| 240 |
HandlerPost: logHookLast, |
| 241 |
StdOut: cfg.StdOut, |
| 242 |
StdErr: cfg.StdErr, |
| 243 |
StoreErrors: conf.Watching(), |
| 244 |
SuppressStatements: conf.IgnoredLogs(), |
| 245 |
} |
| 246 |
logger = loggers.New(logOpts) |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
memCache := dynacache.New(dynacache.Options{Watching: conf.Watching(), Log: logger}) |
| 251 |
|
| 252 |
var h *HugoSites |
| 253 |
onSignalRebuild := func(ids ...identity.Identity) { |
| 254 |
// This channel is buffered, but make sure we do this in a non-blocking way. |
| 255 |
if cfg.ChangesFromBuild != nil { |
| 256 |
go func() { |
| 257 |
cfg.ChangesFromBuild <- ids |
| 258 |
}() |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
compilationCacheDir := filepath.Join(conf.Dirs().CacheDir, "_warpc") |
| 263 |
|
| 264 |
imageWasmMemory := 384 // 384 MiB (4096 MiB Max) |
| 265 |
if m := cfg.TestCfg.WarpcMemory; m > 0 { |
| 266 |
imageWasmMemory = m |
| 267 |
} |
| 268 |
|
| 269 |
firstSiteDeps := &deps.Deps{ |
| 270 |
Fs: cfg.Fs, |
| 271 |
Log: logger, |
| 272 |
Conf: conf, |
| 273 |
BuildState: &deps.BuildState{ |
| 274 |
OnSignalRebuild: onSignalRebuild, |
| 275 |
}, |
| 276 |
Counters: &deps.Counters{}, |
| 277 |
MemCache: memCache, |
| 278 |
TranslationProvider: i18n.NewTranslationProvider(), |
| 279 |
WasmDispatchers: warpc.AllDispatchers( |
| 280 |
// Katex options. |
| 281 |
warpc.Options{ |
| 282 |
CompilationCacheDir: compilationCacheDir, |
| 283 |
|
| 284 |
PoolSize: poolSizeKatex, |
| 285 |
Infof: logger.InfoCommand("katex").Logf, |
| 286 |
Warnf: logger.WarnCommand("katex").Logf, |
| 287 |
}, |
| 288 |
// WebP options. |
| 289 |
warpc.Options{ |
| 290 |
CompilationCacheDir: compilationCacheDir, |
| 291 |
PoolSize: poolSizeWebP, |
| 292 |
Memory: imageWasmMemory, |
| 293 |
Infof: logger.InfoCommand("webp").Logf, |
| 294 |
Warnf: logger.WarnCommand("webp").Logf, |
| 295 |
}, |
| 296 |
// Avif options. |
| 297 |
warpc.Options{ |
| 298 |
CompilationCacheDir: compilationCacheDir, |
| 299 |
PoolSize: poolSizeAvif, |
| 300 |
Memory: imageWasmMemory, |
| 301 |
Infof: logger.InfoCommand("avif").Logf, |
| 302 |
Warnf: logger.WarnCommand("avif").Logf, |
| 303 |
}, |
| 304 |
), |
| 305 |
} |
| 306 |
|
| 307 |
if err := firstSiteDeps.Init(); err != nil { |
| 308 |
return nil, err |
| 309 |
} |
| 310 |
|
| 311 |
// Prevent leaking goroutines in tests. |
| 312 |
if cfg.IsIntegrationTest && cfg.ChangesFromBuild != nil { |
| 313 |
firstSiteDeps.BuildClosers.Add(types.CloserFunc(func() error { |
| 314 |
close(cfg.ChangesFromBuild) |
| 315 |
return nil |
| 316 |
})) |
| 317 |
} |
| 318 |
|
| 319 |
batcherClient, err := esbuild.NewBatcherClient(firstSiteDeps) |
| 320 |
if err != nil { |
| 321 |
return nil, err |
| 322 |
} |
| 323 |
firstSiteDeps.JSBatcherClient = batcherClient |
| 324 |
|
| 325 |
confm := cfg.Configs |
| 326 |
if err := confm.Validate(logger); err != nil { |
| 327 |
return nil, err |
| 328 |
} |
| 329 |
var sites []*Site |
| 330 |
|
| 331 |
ns := &contentNodeShifter{ |
| 332 |
conf: conf, |
| 333 |
} |
| 334 |
|
| 335 |
treeConfig := doctree.Config[contentNode]{ |
| 336 |
Shifter: ns, |
| 337 |
TransformerRaw: &contentNodeTransformerRaw{}, |
| 338 |
} |
| 339 |
|
| 340 |
dimensionLengths := sitesmatrix.Vector{len(confm.Languages), len(versionsSorted), len(rolesSorted)} |
| 341 |
|
| 342 |
pageTrees := &pageTrees{ |
| 343 |
treePages: doctree.New( |
| 344 |
treeConfig, |
| 345 |
), |
| 346 |
treeResources: doctree.New( |
| 347 |
treeConfig, |
| 348 |
), |
| 349 |
treeTaxonomyEntries: doctree.NewTreeShiftTree[*weightedContentNode](dimensionLengths), |
| 350 |
treePagesFromTemplateAdapters: doctree.NewTreeShiftTree[*pagesfromdata.PagesFromTemplate](dimensionLengths), |
| 351 |
} |
| 352 |
|
| 353 |
pageTrees.createMutableTrees() |
| 354 |
|
| 355 |
for i, confp := range confm.ConfigLangs() { |
| 356 |
language := confp.Language().(*langs.Language) |
| 357 |
if language.Disabled { |
| 358 |
panic("cannot create site for disabled language: " + language.Lang) |
| 359 |
} |
| 360 |
k := language.Lang |
| 361 |
conf := confm.LanguageConfigMap[k] |
| 362 |
frontmatterHandler, err := pagemeta.NewFrontmatterHandler(firstSiteDeps.Log, conf.Frontmatter) |
| 363 |
if err != nil { |
| 364 |
return nil, err |
| 365 |
} |
| 366 |
|
| 367 |
langs.SetParams(language, conf.Params) |
| 368 |
|
| 369 |
s := &Site{ |
| 370 |
conf: conf, |
| 371 |
language: language, |
| 372 |
frontmatterHandler: frontmatterHandler, |
| 373 |
store: hstore.NewScratch(), |
| 374 |
siteLanguageVersionRole: &siteLanguageVersionRole{ |
| 375 |
isDefaultLanguage: language.IsDefault(), |
| 376 |
siteVector: sitesmatrix.Vector{i, 0, 0}, |
| 377 |
}, |
| 378 |
} |
| 379 |
|
| 380 |
s.siteWrapped = page.WrapSite(s) |
| 381 |
|
| 382 |
if i == 0 { |
| 383 |
firstSiteDeps.Site = s |
| 384 |
s.Deps = firstSiteDeps |
| 385 |
} else { |
| 386 |
d, err := firstSiteDeps.Clone(s, confp) |
| 387 |
if err != nil { |
| 388 |
return nil, err |
| 389 |
} |
| 390 |
s.Deps = d |
| 391 |
} |
| 392 |
|
| 393 |
// Set up the main publishing chain. |
| 394 |
pub, err := publisher.NewDestinationPublisher( |
| 395 |
firstSiteDeps.ResourceSpec, |
| 396 |
s.conf.OutputFormats.Config, |
| 397 |
s.conf.MediaTypes.Config, |
| 398 |
) |
| 399 |
if err != nil { |
| 400 |
return nil, err |
| 401 |
} |
| 402 |
|
| 403 |
s.publisher = pub |
| 404 |
s.relatedDocsHandler = page.NewRelatedDocsHandler(s.conf.Related) |
| 405 |
// Site deps end. |
| 406 |
|
| 407 |
sites = append(sites, s) |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
if len(sites) == 0 { |
| 412 |
return nil, errors.New("no sites to build") |
| 413 |
} |
| 414 |
|
| 415 |
sitesVersionsRoles := make([][][]*Site, len(sites)) |
| 416 |
for i := 0; i < len(sites); i++ { |
| 417 |
sitesVersionsRoles[i] = make([][]*Site, len(versionsSorted)) |
| 418 |
for j := range versionsSorted { |
| 419 |
sitesVersionsRoles[i][j] = make([]*Site, len(rolesSorted)) |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
siteVersionRoles := map[types.Ints2][]roles.Role{} |
| 424 |
siteRoleVersions := map[types.Ints2][]versions.Version{} |
| 425 |
// i = site, j = version, k = role |
| 426 |
for i, v1 := range sitesVersionsRoles { |
| 427 |
for j, v2 := range v1 { |
| 428 |
for k := range v2 { |
| 429 |
var vrs *Site |
| 430 |
if j == 0 && k == 0 { |
| 431 |
vrs = sites[i] |
| 432 |
} else { |
| 433 |
prototype := sites[i] |
| 434 |
vrs, err = prototype.cloneForVersionAndRole(j, k) |
| 435 |
if err != nil { |
| 436 |
return nil, err |
| 437 |
} |
| 438 |
} |
| 439 |
vrs.roleInternal = rolesSorted[k] |
| 440 |
vrs.role = roles.NewRole(rolesSorted[k]) |
| 441 |
vrs.versionInternal = versionsSorted[j] |
| 442 |
vrs.version = versions.NewVersion(versionsSorted[j]) |
| 443 |
siteRoleVersions[types.Ints2{i, k}] = append(siteRoleVersions[types.Ints2{i, k}], vrs.version) |
| 444 |
siteVersionRoles[types.Ints2{i, j}] = append(siteVersionRoles[types.Ints2{i, j}], vrs.role) |
| 445 |
vrs.pageMap = newPageMap(vrs, memCache, pageTrees) |
| 446 |
vrs.pageFinder = newPageFinder(vrs.pageMap) |
| 447 |
vrs.siteRefLinker = newSiteRefLinker(vrs) |
| 448 |
vrs.prepareInits() |
| 449 |
v2[k] = vrs |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
h, err = newHugoSites(cfg, firstSiteDeps, pageTrees, sitesVersionsRoles) |
| 455 |
if err == nil && h == nil { |
| 456 |
panic("hugo: newHugoSitesNew returned nil error and nil HugoSites") |
| 457 |
} |
| 458 |
|
| 459 |
return h, err |
| 460 |
} |
| 461 |
|
| 462 |
func newHugoSites( |
| 463 |
cfg deps.DepsCfg, |
| 464 |
d *deps.Deps, |
| 465 |
pageTrees *pageTrees, |
| 466 |
sitesVersionsRoles [][][]*Site, |
| 467 |
) (*HugoSites, error) { |
| 468 |
first := make([]*Site, len(sitesVersionsRoles)) |
| 469 |
for i, v := range sitesVersionsRoles { |
| 470 |
first[i] = v[0][0] |
| 471 |
} |
| 472 |
|
| 473 |
sitesVersionsRolesMap := map[sitesmatrix.Vector]*Site{} |
| 474 |
for _, v1 := range sitesVersionsRoles { |
| 475 |
for _, v2 := range v1 { |
| 476 |
for _, s := range v2 { |
| 477 |
sitesVersionsRolesMap[s.siteVector] = s |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
numWorkers := config.GetNumWorkerMultiplier() |
| 483 |
numWorkersSite := min(numWorkers, len(sitesVersionsRolesMap)) |
| 484 |
workersSite := para.New(numWorkersSite) |
| 485 |
|
| 486 |
var sitesLanguages []*Site |
| 487 |
for _, v := range sitesVersionsRoles { |
| 488 |
sitesLanguages = append(sitesLanguages, v[0][0]) |
| 489 |
} |
| 490 |
|
| 491 |
h := &HugoSites{ |
| 492 |
Sites: first, |
| 493 |
sitesVersionsRoles: sitesVersionsRoles, |
| 494 |
sitesVersionsRolesMap: sitesVersionsRolesMap, |
| 495 |
sitesLanguages: sitesLanguages, |
| 496 |
Deps: first[0].Deps, |
| 497 |
Configs: cfg.Configs, |
| 498 |
workersSite: workersSite, |
| 499 |
numWorkersSites: numWorkers, |
| 500 |
numWorkers: numWorkers, |
| 501 |
pageTrees: pageTrees, |
| 502 |
cachePages: dynacache.GetOrCreatePartition[string, |
| 503 |
page.Pages](d.MemCache, "/pags/all", |
| 504 |
dynacache.OptionsPartition{Weight: 10, ClearWhen: dynacache.ClearOnRebuild}, |
| 505 |
), |
| 506 |
cacheContentSource: dynacache.GetOrCreatePartition[uint64, *resources.StaleValue[[]byte]](d.MemCache, "/cont/src", dynacache.OptionsPartition{Weight: 70, ClearWhen: dynacache.ClearOnChange}), |
| 507 |
translationKeyPages: hmaps.NewSliceCache[page.Page](), |
| 508 |
currentSite: first[0], |
| 509 |
skipRebuildForFilenames: make(map[string]bool), |
| 510 |
init: &hugoSitesInit{}, |
| 511 |
progressReporter: &progressReporter{}, |
| 512 |
} |
| 513 |
|
| 514 |
// Assemble dependencies to be used in hugo.Deps. |
| 515 |
var dependencies []*hugo.Dependency |
| 516 |
var depFromMod func(m modules.Module) *hugo.Dependency |
| 517 |
depFromMod = func(m modules.Module) *hugo.Dependency { |
| 518 |
dep := &hugo.Dependency{ |
| 519 |
Path: m.Path(), |
| 520 |
Version: m.Version(), |
| 521 |
Time: m.Time(), |
| 522 |
Vendor: m.Vendor(), |
| 523 |
} |
| 524 |
|
| 525 |
// These are pointers, but this all came from JSON so there's no recursive navigation, |
| 526 |
// so just create new values. |
| 527 |
if m.Replace() != nil { |
| 528 |
dep.Replace = depFromMod(m.Replace()) |
| 529 |
} |
| 530 |
if m.Owner() != nil { |
| 531 |
dep.Owner = depFromMod(m.Owner()) |
| 532 |
} |
| 533 |
return dep |
| 534 |
} |
| 535 |
for _, m := range d.Paths.AllModules() { |
| 536 |
dependencies = append(dependencies, depFromMod(m)) |
| 537 |
} |
| 538 |
|
| 539 |
// Create providers that avoid naming conflicts with HugoSites fields. |
| 540 |
sp := hugoSitesSitesProvider{h: h} |
| 541 |
|
| 542 |
opts := page.HugoInfoOptions{ |
| 543 |
Conf: h.Configs.GetFirstLanguageConfig(), |
| 544 |
HugoInfoHugoSitesProvider: sp, |
| 545 |
Deps: dependencies, |
| 546 |
} |
| 547 |
|
| 548 |
if bi := hugo.GetBuildInfo(); bi != nil { |
| 549 |
opts.CommitHash = bi.Revision |
| 550 |
opts.BuildDate = bi.RevisionTime |
| 551 |
opts.GoVersion = bi.GoVersion |
| 552 |
} |
| 553 |
|
| 554 |
if opts.BuildDate == "" { |
| 555 |
opts.BuildDate = hugo.GetBuildDate() |
| 556 |
} |
| 557 |
|
| 558 |
h.hugoInfo = page.NewHugoInfo(opts) |
| 559 |
|
| 560 |
var prototype *deps.Deps |
| 561 |
|
| 562 |
var i int |
| 563 |
for _, v := range sitesVersionsRoles { |
| 564 |
for _, r := range v { |
| 565 |
for _, s := range r { |
| 566 |
s.h = h |
| 567 |
// The template store needs to be initialized after the h container is set on s. |
| 568 |
if i == 0 { |
| 569 |
templateStore, err := tplimpl.NewStore( |
| 570 |
tplimpl.StoreOptions{ |
| 571 |
Fs: s.BaseFs.Layouts.Fs, |
| 572 |
Log: s.Log, |
| 573 |
Watching: s.Conf.Watching(), |
| 574 |
PathParser: s.Conf.PathParser(), |
| 575 |
Metrics: d.Metrics, |
| 576 |
OutputFormats: s.conf.OutputFormats.Config, |
| 577 |
MediaTypes: s.conf.MediaTypes.Config, |
| 578 |
DefaultOutputFormat: s.conf.DefaultOutputFormat, |
| 579 |
TaxonomySingularPlural: s.conf.Taxonomies, |
| 580 |
RenderHooks: s.conf.Markup.Goldmark.RenderHooks, |
| 581 |
}, tplimpl.SiteOptions{ |
| 582 |
Site: s, |
| 583 |
TemplateFuncs: tplimplinit.CreateFuncMap(s.Deps), |
| 584 |
}) |
| 585 |
if err != nil { |
| 586 |
return nil, err |
| 587 |
} |
| 588 |
s.Deps.TemplateStore = templateStore |
| 589 |
} else { |
| 590 |
s.Deps.TemplateStore = prototype.TemplateStore.WithSiteOpts( |
| 591 |
tplimpl.SiteOptions{ |
| 592 |
Site: s, |
| 593 |
TemplateFuncs: tplimplinit.CreateFuncMap(s.Deps), |
| 594 |
}) |
| 595 |
} |
| 596 |
if err := s.Deps.Compile(prototype); err != nil { |
| 597 |
return nil, err |
| 598 |
} |
| 599 |
if i == 0 { |
| 600 |
prototype = s.Deps |
| 601 |
} |
| 602 |
|
| 603 |
i++ |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
h.fatalErrorHandler = &fatalErrorHandler{ |
| 609 |
h: h, |
| 610 |
donec: make(chan bool), |
| 611 |
} |
| 612 |
|
| 613 |
h.init.data = hsync.OnceMoreFunc(func(ctx context.Context) error { |
| 614 |
return h.loadData() |
| 615 |
}) |
| 616 |
|
| 617 |
h.init.gitInfo = hsync.OnceMoreFunc(func(ctx context.Context) error { |
| 618 |
return h.loadGitInfo() |
| 619 |
}) |
| 620 |
|
| 621 |
return h, nil |
| 622 |
} |
| 623 |
|
| 624 |
// GetSiteDimension returns the value of the specified site dimension (such as language, version, or role) |
| 625 |
// based on the provided dimension name string. If the dimension name is unknown, it panics. |
| 626 |
func (s *Site) Dimension(d string) page.SiteDimension { |
| 627 |
switch d { |
| 628 |
case sitesmatrix.DimensionName(sitesmatrix.Language): |
| 629 |
return s.language |
| 630 |
case sitesmatrix.DimensionName(sitesmatrix.Version): |
| 631 |
return s.version |
| 632 |
case sitesmatrix.DimensionName(sitesmatrix.Role): |
| 633 |
return s.role |
| 634 |
default: |
| 635 |
panic(fmt.Sprintf("unknown dimension %q", d)) |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
// Returns the server port. |
| 640 |
func (s *Site) ServerPort() int { |
| 641 |
return s.conf.C.BaseURL.Port() |
| 642 |
} |
| 643 |
|
| 644 |
// Returns the configured title for this Site. |
| 645 |
func (s *Site) Title() string { |
| 646 |
return s.conf.Title |
| 647 |
} |
| 648 |
|
| 649 |
func (s *Site) Copyright() string { |
| 650 |
return s.conf.Copyright |
| 651 |
} |
| 652 |
|
| 653 |
func (s *Site) Lang() string { |
| 654 |
return s.language.Lang |
| 655 |
} |
| 656 |
|
| 657 |
func (s *Site) Config() page.SiteConfig { |
| 658 |
return page.SiteConfig{ |
| 659 |
Privacy: s.conf.Privacy, |
| 660 |
Services: s.conf.Services, |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
// Deprecated: Use .Language.Locale instead. |
| 665 |
func (s *Site) LanguageCode() string { |
| 666 |
hugo.DeprecateWithLogger(".Site.LanguageCode", "Use .Site.Language.Locale instead.", "v0.158.0", s.Language().Logger()) |
| 667 |
return s.Language().Locale() |
| 668 |
} |
| 669 |
|
| 670 |
// Returns all sites for all dimensions. |
| 671 |
// Deprecated: Use hugo.Sites instead. |
| 672 |
func (s *Site) Sites() page.Sites { |
| 673 |
s.h.printSiteSitesDeprecationInit.Do(func() { |
| 674 |
hugo.Deprecate(".Site.Sites and .Page.Sites", "Use hugo.Sites instead.", "v0.156.0") |
| 675 |
}) |
| 676 |
return slices.Collect(s.h.allSitesInterface(nil)) |
| 677 |
} |
| 678 |
|
| 679 |
// IsDefault reports whether this site is the default across all dimensions. |
| 680 |
func (s *Site) IsDefault() bool { |
| 681 |
return s.siteLanguageVersionRole.isDefault() |
| 682 |
} |
| 683 |
|
| 684 |
// Returns Site currently rendering. |
| 685 |
func (s *Site) Current() page.Site { |
| 686 |
return s.h.currentSite |
| 687 |
} |
| 688 |
|
| 689 |
// MainSections returns the list of main sections. |
| 690 |
func (s *Site) MainSections() []string { |
| 691 |
s.CheckReady() |
| 692 |
return s.conf.C.MainSections |
| 693 |
} |
| 694 |
|
| 695 |
// Returns a struct with some information about the build. |
| 696 |
func (s *Site) Hugo() page.HugoInfo { |
| 697 |
return s.h.hugoInfo |
| 698 |
} |
| 699 |
|
| 700 |
// Returns the BaseURL for this Site. |
| 701 |
func (s *Site) BaseURL() string { |
| 702 |
return s.conf.C.BaseURL.WithPath |
| 703 |
} |
| 704 |
|
| 705 |
// Returns the last modification date of the content. |
| 706 |
func (s *Site) Lastmod() time.Time { |
| 707 |
return s.lastmod |
| 708 |
} |
| 709 |
|
| 710 |
// Returns the Params configured for this site. |
| 711 |
func (s *Site) Params() hmaps.Params { |
| 712 |
return s.conf.Params |
| 713 |
} |
| 714 |
|
| 715 |
func (s *Site) Param(key any) (any, error) { |
| 716 |
return resource.Param(s, nil, key) |
| 717 |
} |
| 718 |
|
| 719 |
// Returns a map of all the data inside /data. |
| 720 |
// Deprecated: Use hugo.Data instead. |
| 721 |
func (s *Site) Data() map[string]any { |
| 722 |
s.h.printSiteDataDeprecationInit.Do(func() { |
| 723 |
hugo.Deprecate(".Site.Data", "Use hugo.Data instead.", "v0.156.0") |
| 724 |
}) |
| 725 |
return s.h.Data() |
| 726 |
} |
| 727 |
|
| 728 |
// Deprecated: See https://discourse.gohugo.io/t/56732. |
| 729 |
func (s *Site) BuildDrafts() bool { |
| 730 |
s.h.printSiteBuildDraftsDeprecationInit.Do(func() { |
| 731 |
hugo.Deprecate(".Site.BuildDrafts", "See https://discourse.gohugo.io/t/56732.", "v0.156.0") |
| 732 |
}) |
| 733 |
return s.conf.BuildDrafts |
| 734 |
} |
| 735 |
|
| 736 |
func (s *Site) LanguagePrefix() string { |
| 737 |
prefix := s.GetLanguagePrefix() |
| 738 |
if prefix == "" { |
| 739 |
return "" |
| 740 |
} |
| 741 |
return "/" + prefix |
| 742 |
} |
| 743 |
|
| 744 |
func (s *Site) Site() page.Site { |
| 745 |
return page.WrapSite(s) |
| 746 |
} |
| 747 |
|
| 748 |
func (s *Site) ForEeachIdentityByName(name string, f func(identity.Identity) bool) { |
| 749 |
if id, found := siteidentities.FromString(name); found { |
| 750 |
if f(id) { |
| 751 |
return |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
// Pages returns all pages. |
| 757 |
// This is for the current language only. |
| 758 |
func (s *Site) Pages() page.Pages { |
| 759 |
s.CheckReady() |
| 760 |
return s.pageMap.getPagesInSection( |
| 761 |
pageMapQueryPagesInSection{ |
| 762 |
Path: "", |
| 763 |
KeyPart: "global", |
| 764 |
Include: pagePredicates.ShouldListGlobal.BoolFunc(), |
| 765 |
Recursive: true, |
| 766 |
IncludeSelf: true, |
| 767 |
}, |
| 768 |
) |
| 769 |
} |
| 770 |
|
| 771 |
// RegularPages returns all the regular pages. |
| 772 |
// This is for the current language only. |
| 773 |
func (s *Site) RegularPages() page.Pages { |
| 774 |
s.CheckReady() |
| 775 |
return s.pageMap.getPagesInSection( |
| 776 |
pageMapQueryPagesInSection{ |
| 777 |
Path: "", |
| 778 |
KeyPart: "global", |
| 779 |
Include: pagePredicates.ShouldListGlobal.And(pagePredicates.KindPage).BoolFunc(), |
| 780 |
Recursive: true, |
| 781 |
}, |
| 782 |
) |
| 783 |
} |
| 784 |
|
| 785 |
// AllPages returns all pages for all sites. |
| 786 |
// Deprecated: See https://discourse.gohugo.io/t/56732. |
| 787 |
func (s *Site) AllPages() page.Pages { |
| 788 |
s.h.printSiteAllPagesDeprecationInit.Do(func() { |
| 789 |
hugo.Deprecate(".Site.AllPages", "See https://discourse.gohugo.io/t/56732.", "v0.156.0") |
| 790 |
}) |
| 791 |
s.CheckReady() |
| 792 |
return s.h.Pages() |
| 793 |
} |
| 794 |
|
| 795 |
// AllRegularPages returns all regular pages for all sites. |
| 796 |
func (s *Site) AllRegularPages() page.Pages { |
| 797 |
s.CheckReady() |
| 798 |
return s.h.RegularPages() |
| 799 |
} |
| 800 |
|
| 801 |
func (s *Site) Store() *hstore.Scratch { |
| 802 |
return s.store |
| 803 |
} |
| 804 |
|
| 805 |
func (s *Site) CheckReady() { |
| 806 |
if s.state != siteStateReady { |
| 807 |
panic("this method cannot be called before the site is fully initialized") |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
func (s *Site) Taxonomies() page.TaxonomyList { |
| 812 |
s.CheckReady() |
| 813 |
return s.init.taxonomies.Value(context.Background()) |
| 814 |
} |
| 815 |
|
| 816 |
type ( |
| 817 |
taxonomiesConfig map[string]string |
| 818 |
taxonomiesConfigValues struct { |
| 819 |
views []viewName |
| 820 |
viewsByTreeKey map[string]viewName |
| 821 |
} |
| 822 |
) |
| 823 |
|
| 824 |
func (t taxonomiesConfig) Values() taxonomiesConfigValues { |
| 825 |
var views []viewName |
| 826 |
for k, v := range t { |
| 827 |
views = append(views, viewName{singular: k, plural: v, pluralTreeKey: cleanTreeKey(v)}) |
| 828 |
} |
| 829 |
sort.Slice(views, func(i, j int) bool { |
| 830 |
return views[i].plural < views[j].plural |
| 831 |
}) |
| 832 |
|
| 833 |
viewsByTreeKey := make(map[string]viewName) |
| 834 |
for _, v := range views { |
| 835 |
viewsByTreeKey[v.pluralTreeKey] = v |
| 836 |
} |
| 837 |
|
| 838 |
return taxonomiesConfigValues{ |
| 839 |
views: views, |
| 840 |
viewsByTreeKey: viewsByTreeKey, |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
// Lazily loaded site dependencies. |
| 845 |
type siteInit struct { |
| 846 |
prevNext hsync.FuncResetter |
| 847 |
prevNextInSection hsync.FuncResetter |
| 848 |
|
| 849 |
menus hsync.ValueResetter[navigation.Menus] |
| 850 |
taxonomies hsync.ValueResetter[page.TaxonomyList] |
| 851 |
} |
| 852 |
|
| 853 |
func (init *siteInit) Reset() { |
| 854 |
init.prevNext.Reset() |
| 855 |
init.prevNextInSection.Reset() |
| 856 |
init.menus.Reset() |
| 857 |
init.taxonomies.Reset() |
| 858 |
} |
| 859 |
|
| 860 |
func (s *Site) prepareInits() { |
| 861 |
s.init = &siteInit{} |
| 862 |
|
| 863 |
s.init.prevNext = hsync.OnceMoreFunc(func(ctx context.Context) error { |
| 864 |
regularPages := s.RegularPages() |
| 865 |
if s.conf.Page.NextPrevSortOrder == "asc" { |
| 866 |
regularPages = regularPages.Reverse() |
| 867 |
} |
| 868 |
for i, p := range regularPages { |
| 869 |
np, ok := p.(nextPrevProvider) |
| 870 |
if !ok { |
| 871 |
continue |
| 872 |
} |
| 873 |
|
| 874 |
pos := np.getNextPrev() |
| 875 |
if pos == nil { |
| 876 |
continue |
| 877 |
} |
| 878 |
|
| 879 |
pos.nextPage = nil |
| 880 |
pos.prevPage = nil |
| 881 |
|
| 882 |
if i > 0 { |
| 883 |
pos.nextPage = regularPages[i-1] |
| 884 |
} |
| 885 |
|
| 886 |
if i < len(regularPages)-1 { |
| 887 |
pos.prevPage = regularPages[i+1] |
| 888 |
} |
| 889 |
} |
| 890 |
return nil |
| 891 |
}) |
| 892 |
|
| 893 |
s.init.prevNextInSection = hsync.OnceMoreFunc(func(ctx context.Context) error { |
| 894 |
setNextPrev := func(pas page.Pages) { |
| 895 |
for i, p := range pas { |
| 896 |
np, ok := p.(nextPrevInSectionProvider) |
| 897 |
if !ok { |
| 898 |
continue |
| 899 |
} |
| 900 |
|
| 901 |
pos := np.getNextPrevInSection() |
| 902 |
if pos == nil { |
| 903 |
continue |
| 904 |
} |
| 905 |
|
| 906 |
pos.nextPage = nil |
| 907 |
pos.prevPage = nil |
| 908 |
|
| 909 |
if i > 0 { |
| 910 |
pos.nextPage = pas[i-1] |
| 911 |
} |
| 912 |
|
| 913 |
if i < len(pas)-1 { |
| 914 |
pos.prevPage = pas[i+1] |
| 915 |
} |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
sections := s.pageMap.getPagesInSection( |
| 920 |
pageMapQueryPagesInSection{ |
| 921 |
Path: "", |
| 922 |
KeyPart: "sectionorhome", |
| 923 |
Include: pagePredicates.KindSection.Or(pagePredicates.KindHome).BoolFunc(), |
| 924 |
IncludeSelf: true, |
| 925 |
Recursive: true, |
| 926 |
}, |
| 927 |
) |
| 928 |
|
| 929 |
for _, section := range sections { |
| 930 |
ps := section.RegularPages() |
| 931 |
if s.conf.Page.NextPrevInSectionSortOrder == "asc" { |
| 932 |
ps = ps.Reverse() |
| 933 |
} |
| 934 |
setNextPrev(ps) |
| 935 |
} |
| 936 |
|
| 937 |
return nil |
| 938 |
}) |
| 939 |
|
| 940 |
s.init.menus = hsync.OnceMoreValue(func(ctx context.Context) navigation.Menus { |
| 941 |
m, err := s.assembleMenus() |
| 942 |
if err != nil { |
| 943 |
panic(err) |
| 944 |
} |
| 945 |
return m |
| 946 |
}) |
| 947 |
|
| 948 |
s.init.taxonomies = hsync.OnceMoreValue(func(ctx context.Context) page.TaxonomyList { |
| 949 |
taxonomies, err := s.pageMap.CreateSiteTaxonomies(ctx) |
| 950 |
if err != nil { |
| 951 |
panic(err) |
| 952 |
} |
| 953 |
return taxonomies |
| 954 |
}) |
| 955 |
} |
| 956 |
|
| 957 |
func (s *Site) Menus() navigation.Menus { |
| 958 |
s.CheckReady() |
| 959 |
return s.init.menus.Value(context.Background()) |
| 960 |
} |
| 961 |
|
| 962 |
func (s *Site) initRenderFormats() { |
| 963 |
formatSet := make(map[string]bool) |
| 964 |
formats := output.Formats{} |
| 965 |
|
| 966 |
w := &doctree.NodeShiftTreeWalker[contentNode]{ |
| 967 |
Tree: s.pageMap.treePages, |
| 968 |
Handle: func(key string, n contentNode) (radix.WalkFlag, error) { |
| 969 |
if p, ok := n.(*pageState); ok { |
| 970 |
for _, f := range p.m.pageConfig.ConfiguredOutputFormats { |
| 971 |
if !formatSet[f.Name] { |
| 972 |
formats = append(formats, f) |
| 973 |
formatSet[f.Name] = true |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
return radix.WalkContinue, nil |
| 978 |
}, |
| 979 |
} |
| 980 |
|
| 981 |
if err := w.Walk(context.TODO()); err != nil { |
| 982 |
panic(err) |
| 983 |
} |
| 984 |
|
| 985 |
// Add the per kind configured output formats |
| 986 |
for _, kind := range kinds.AllKindsInPages { |
| 987 |
if siteFormats, found := s.conf.C.KindOutputFormats[kind]; found { |
| 988 |
for _, f := range siteFormats { |
| 989 |
if !formatSet[f.Name] { |
| 990 |
formats = append(formats, f) |
| 991 |
formatSet[f.Name] = true |
| 992 |
} |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
sort.Sort(formats) |
| 998 |
s.renderFormats = formats |
| 999 |
} |
| 1000 |
|
| 1001 |
func (s *Site) GetInternalRelatedDocsHandler() *page.RelatedDocsHandler { |
| 1002 |
return s.relatedDocsHandler |
| 1003 |
} |
| 1004 |
|
| 1005 |
func (s *Site) Language() *langs.Language { |
| 1006 |
return s.language |
| 1007 |
} |
| 1008 |
|
| 1009 |
// Deprecated: See https://discourse.gohugo.io/t/56732. |
| 1010 |
func (s *Site) Languages() langs.Languages { |
| 1011 |
s.h.printSiteLanguagesDeprecationInit.Do(func() { |
| 1012 |
hugo.Deprecate(".Site.Languages", "See https://discourse.gohugo.io/t/56732.", "v0.156.0") |
| 1013 |
}) |
| 1014 |
return s.h.Configs.Languages |
| 1015 |
} |
| 1016 |
|
| 1017 |
type siteRefLinker struct { |
| 1018 |
s *Site |
| 1019 |
|
| 1020 |
errorLogger logg.LevelLogger |
| 1021 |
notFoundURL string |
| 1022 |
} |
| 1023 |
|
| 1024 |
func newSiteRefLinker(s *Site) siteRefLinker { |
| 1025 |
logger := s.Log.Error() |
| 1026 |
|
| 1027 |
notFoundURL := s.conf.RefLinksNotFoundURL |
| 1028 |
errLevel := s.conf.RefLinksErrorLevel |
| 1029 |
if strings.EqualFold(errLevel, "warning") { |
| 1030 |
logger = s.Log.Warn() |
| 1031 |
} |
| 1032 |
return siteRefLinker{s: s, errorLogger: logger, notFoundURL: notFoundURL} |
| 1033 |
} |
| 1034 |
|
| 1035 |
func (s siteRefLinker) logNotFound(ref, what string, p page.Page, position text.Position) { |
| 1036 |
if position.IsValid() { |
| 1037 |
s.errorLogger.Logf("[%s] REF_NOT_FOUND: Ref %q: %s: %s", s.s.Lang(), ref, position.String(), what) |
| 1038 |
} else if p == nil { |
| 1039 |
s.errorLogger.Logf("[%s] REF_NOT_FOUND: Ref %q: %s", s.s.Lang(), ref, what) |
| 1040 |
} else { |
| 1041 |
s.errorLogger.Logf("[%s] REF_NOT_FOUND: Ref %q from page %q: %s", s.s.Lang(), ref, p.Path(), what) |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
func (s *siteRefLinker) refLink(ref string, source any, relative bool, outputFormat string) (string, error) { |
| 1046 |
p, err := unwrapPage(source) |
| 1047 |
if err != nil { |
| 1048 |
return "", err |
| 1049 |
} |
| 1050 |
|
| 1051 |
var refURL *url.URL |
| 1052 |
|
| 1053 |
ref = filepath.ToSlash(ref) |
| 1054 |
|
| 1055 |
refURL, err = url.Parse(ref) |
| 1056 |
if err != nil { |
| 1057 |
return s.notFoundURL, err |
| 1058 |
} |
| 1059 |
|
| 1060 |
var target page.Page |
| 1061 |
var link string |
| 1062 |
|
| 1063 |
if refURL.Path != "" { |
| 1064 |
var err error |
| 1065 |
target, err = s.s.getPageRef(p, refURL.Path) |
| 1066 |
var pos text.Position |
| 1067 |
if err != nil || target == nil { |
| 1068 |
if p, ok := source.(text.Positioner); ok { |
| 1069 |
pos = p.Position() |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
if err != nil { |
| 1074 |
s.logNotFound(refURL.Path, err.Error(), p, pos) |
| 1075 |
return s.notFoundURL, nil |
| 1076 |
} |
| 1077 |
|
| 1078 |
if target == nil { |
| 1079 |
s.logNotFound(refURL.Path, "page not found", p, pos) |
| 1080 |
return s.notFoundURL, nil |
| 1081 |
} |
| 1082 |
|
| 1083 |
var permalinker Permalinker = target |
| 1084 |
|
| 1085 |
if outputFormat != "" { |
| 1086 |
o := target.OutputFormats().Get(outputFormat) |
| 1087 |
|
| 1088 |
if o.IsZero() { |
| 1089 |
s.logNotFound(refURL.Path, fmt.Sprintf("output format %q", outputFormat), p, pos) |
| 1090 |
return s.notFoundURL, nil |
| 1091 |
} |
| 1092 |
permalinker = o |
| 1093 |
} |
| 1094 |
|
| 1095 |
if relative { |
| 1096 |
link = permalinker.RelPermalink() |
| 1097 |
} else { |
| 1098 |
link = permalinker.Permalink() |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
if refURL.Fragment != "" { |
| 1103 |
_ = target |
| 1104 |
link = link + "#" + refURL.Fragment |
| 1105 |
|
| 1106 |
if pctx, ok := target.(pageContext); ok { |
| 1107 |
if refURL.Path != "" { |
| 1108 |
if di, ok := pctx.getContentConverter().(converter.DocumentInfo); ok { |
| 1109 |
link = link + di.AnchorSuffix() |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} else if pctx, ok := p.(pageContext); ok { |
| 1113 |
if di, ok := pctx.getContentConverter().(converter.DocumentInfo); ok { |
| 1114 |
link = link + di.AnchorSuffix() |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
} |
| 1119 |
|
| 1120 |
return link, nil |
| 1121 |
} |
| 1122 |
|
| 1123 |
func (s *Site) watching() bool { |
| 1124 |
return s.h != nil && s.h.Configs.Base.Internal.Watch |
| 1125 |
} |
| 1126 |
|
| 1127 |
type WhatChanged struct { |
| 1128 |
mu sync.Mutex |
| 1129 |
|
| 1130 |
needsPagesAssembly bool |
| 1131 |
|
| 1132 |
ids map[identity.Identity]bool |
| 1133 |
} |
| 1134 |
|
| 1135 |
func (w *WhatChanged) init() { |
| 1136 |
if w.ids == nil { |
| 1137 |
w.ids = make(map[identity.Identity]bool) |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
func (w *WhatChanged) Add(ids ...identity.Identity) { |
| 1142 |
w.mu.Lock() |
| 1143 |
defer w.mu.Unlock() |
| 1144 |
|
| 1145 |
w.init() |
| 1146 |
|
| 1147 |
for _, id := range ids { |
| 1148 |
w.ids[id] = true |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
func (w *WhatChanged) Clear() { |
| 1153 |
w.mu.Lock() |
| 1154 |
defer w.mu.Unlock() |
| 1155 |
w.clear() |
| 1156 |
} |
| 1157 |
|
| 1158 |
func (w *WhatChanged) clear() { |
| 1159 |
w.ids = nil |
| 1160 |
} |
| 1161 |
|
| 1162 |
func (w *WhatChanged) Changes() []identity.Identity { |
| 1163 |
if w == nil || w.ids == nil { |
| 1164 |
return nil |
| 1165 |
} |
| 1166 |
return slices.Collect(maps.Keys(w.ids)) |
| 1167 |
} |
| 1168 |
|
| 1169 |
func (w *WhatChanged) Drain() []identity.Identity { |
| 1170 |
w.mu.Lock() |
| 1171 |
defer w.mu.Unlock() |
| 1172 |
ids := w.Changes() |
| 1173 |
w.clear() |
| 1174 |
return ids |
| 1175 |
} |
| 1176 |
|
| 1177 |
// RegisterMediaTypes will register the Site's media types in the mime |
| 1178 |
// package, so it will behave correctly with Hugo's built-in server. |
| 1179 |
func (s *Site) RegisterMediaTypes() { |
| 1180 |
for _, mt := range s.conf.MediaTypes.Config { |
| 1181 |
for _, suffix := range mt.Suffixes() { |
| 1182 |
_ = mime.AddExtensionType(mt.Delimiter+suffix, mt.Type) |
| 1183 |
} |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
func (h *HugoSites) fileEventsFilter(events []fsnotify.Event) []fsnotify.Event { |
| 1188 |
seen := make(map[fsnotify.Event]bool) |
| 1189 |
|
| 1190 |
n := 0 |
| 1191 |
for _, ev := range events { |
| 1192 |
// Avoid processing the same event twice. |
| 1193 |
if seen[ev] { |
| 1194 |
continue |
| 1195 |
} |
| 1196 |
seen[ev] = true |
| 1197 |
|
| 1198 |
if h.SourceSpec.IgnoreFile(ev.Name) { |
| 1199 |
continue |
| 1200 |
} |
| 1201 |
|
| 1202 |
if runtime.GOOS == "darwin" { // When a file system is HFS+, its filepath is in NFD form. |
| 1203 |
ev.Name = norm.NFC.String(ev.Name) |
| 1204 |
} |
| 1205 |
|
| 1206 |
events[n] = ev |
| 1207 |
n++ |
| 1208 |
} |
| 1209 |
events = events[:n] |
| 1210 |
|
| 1211 |
eventOrdinal := func(e fsnotify.Event) int { |
| 1212 |
// Pull the structural changes to the top. |
| 1213 |
if e.Op.Has(fsnotify.Create) { |
| 1214 |
return 1 |
| 1215 |
} |
| 1216 |
if e.Op.Has(fsnotify.Remove) { |
| 1217 |
return 2 |
| 1218 |
} |
| 1219 |
if e.Op.Has(fsnotify.Rename) { |
| 1220 |
return 3 |
| 1221 |
} |
| 1222 |
if e.Op.Has(fsnotify.Write) { |
| 1223 |
return 4 |
| 1224 |
} |
| 1225 |
return 5 |
| 1226 |
} |
| 1227 |
|
| 1228 |
sort.Slice(events, func(i, j int) bool { |
| 1229 |
// First sort by event type. |
| 1230 |
if eventOrdinal(events[i]) != eventOrdinal(events[j]) { |
| 1231 |
return eventOrdinal(events[i]) < eventOrdinal(events[j]) |
| 1232 |
} |
| 1233 |
// Then sort by name. |
| 1234 |
return events[i].Name < events[j].Name |
| 1235 |
}) |
| 1236 |
|
| 1237 |
return events |
| 1238 |
} |
| 1239 |
|
| 1240 |
type fileEventInfo struct { |
| 1241 |
fsnotify.Event |
| 1242 |
fi os.FileInfo |
| 1243 |
added bool |
| 1244 |
removed bool |
| 1245 |
isChangedDir bool |
| 1246 |
} |
| 1247 |
|
| 1248 |
func (h *HugoSites) fileEventsApplyInfo(events []fsnotify.Event) []fileEventInfo { |
| 1249 |
var infos []fileEventInfo |
| 1250 |
for _, ev := range events { |
| 1251 |
removed := false |
| 1252 |
added := false |
| 1253 |
|
| 1254 |
fi, statErr := h.Fs.Source.Stat(ev.Name) |
| 1255 |
|
| 1256 |
// Some editors (Vim) sometimes issue only a Rename operation when writing an existing file, |
| 1257 |
// and an atomic save (write temp file, then rename it into place) makes the watcher |
| 1258 |
// report the replaced file as removed (kqueue/macOS). |
| 1259 |
// So, if the file is still on disk, it's only been updated, if it's not, it's gone. |
| 1260 |
if ev.Op.Has(fsnotify.Remove) || ev.Op.Has(fsnotify.Rename) { |
| 1261 |
removed = statErr != nil |
| 1262 |
} |
| 1263 |
if ev.Op.Has(fsnotify.Create) { |
| 1264 |
added = true |
| 1265 |
} |
| 1266 |
|
| 1267 |
isChangedDir := statErr == nil && fi.IsDir() |
| 1268 |
|
| 1269 |
infos = append(infos, fileEventInfo{ |
| 1270 |
Event: ev, |
| 1271 |
fi: fi, |
| 1272 |
added: added, |
| 1273 |
removed: removed, |
| 1274 |
isChangedDir: isChangedDir, |
| 1275 |
}) |
| 1276 |
} |
| 1277 |
|
| 1278 |
n := 0 |
| 1279 |
|
| 1280 |
for _, ev := range infos { |
| 1281 |
// Remove any directories that's also represented by a file. |
| 1282 |
keep := true |
| 1283 |
if ev.isChangedDir { |
| 1284 |
for _, ev2 := range infos { |
| 1285 |
if ev2.fi != nil && !ev2.fi.IsDir() && filepath.Dir(ev2.Name) == ev.Name { |
| 1286 |
keep = false |
| 1287 |
break |
| 1288 |
} |
| 1289 |
} |
| 1290 |
} |
| 1291 |
if keep { |
| 1292 |
infos[n] = ev |
| 1293 |
n++ |
| 1294 |
} |
| 1295 |
} |
| 1296 |
infos = infos[:n] |
| 1297 |
|
| 1298 |
return infos |
| 1299 |
} |
| 1300 |
|
| 1301 |
func (h *HugoSites) fileEventsTrim(events []fsnotify.Event) []fsnotify.Event { |
| 1302 |
seen := make(map[string]bool) |
| 1303 |
n := 0 |
| 1304 |
for _, ev := range events { |
| 1305 |
if seen[ev.Name] { |
| 1306 |
continue |
| 1307 |
} |
| 1308 |
seen[ev.Name] = true |
| 1309 |
events[n] = ev |
| 1310 |
n++ |
| 1311 |
} |
| 1312 |
return events |
| 1313 |
} |
| 1314 |
|
| 1315 |
func (h *HugoSites) fileEventsContentPaths(p []pathChange) []pathChange { |
| 1316 |
var bundles []pathChange |
| 1317 |
var dirs []pathChange |
| 1318 |
var regular []pathChange |
| 1319 |
|
| 1320 |
var others []pathChange |
| 1321 |
for _, p := range p { |
| 1322 |
if p.isDir { |
| 1323 |
dirs = append(dirs, p) |
| 1324 |
} else { |
| 1325 |
others = append(others, p) |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
// Remove all files below dir. |
| 1330 |
if len(dirs) > 0 { |
| 1331 |
n := 0 |
| 1332 |
for _, o := range others { |
| 1333 |
keep := true |
| 1334 |
for _, d := range dirs { |
| 1335 |
if strings.HasPrefix(o.p.Path(), d.p.Path()+"/") { |
| 1336 |
keep = false |
| 1337 |
break |
| 1338 |
} |
| 1339 |
} |
| 1340 |
if keep { |
| 1341 |
others[n] = o |
| 1342 |
n++ |
| 1343 |
} |
| 1344 |
} |
| 1345 |
others = others[:n] |
| 1346 |
} |
| 1347 |
|
| 1348 |
for _, p := range others { |
| 1349 |
if p.p.IsBundle() { |
| 1350 |
bundles = append(bundles, p) |
| 1351 |
} else { |
| 1352 |
regular = append(regular, p) |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
// Remove any files below leaf bundles. |
| 1357 |
// Remove any files in the same folder as branch bundles. |
| 1358 |
var keepers []pathChange |
| 1359 |
|
| 1360 |
for _, o := range regular { |
| 1361 |
keep := true |
| 1362 |
for _, b := range bundles { |
| 1363 |
prefix := b.p.Base() + "/" |
| 1364 |
if b.p.IsLeafBundle() && strings.HasPrefix(o.p.Path(), prefix) { |
| 1365 |
keep = false |
| 1366 |
break |
| 1367 |
} else if b.p.IsBranchBundle() && o.p.Dir() == b.p.Dir() { |
| 1368 |
keep = false |
| 1369 |
break |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
if keep { |
| 1374 |
keepers = append(keepers, o) |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
keepers = append(dirs, keepers...) |
| 1379 |
keepers = append(bundles, keepers...) |
| 1380 |
|
| 1381 |
return keepers |
| 1382 |
} |
| 1383 |
|
| 1384 |
// SitemapAbsURL is a convenience method giving the absolute URL to the sitemap. |
| 1385 |
func (s *Site) SitemapAbsURL() string { |
| 1386 |
base := "" |
| 1387 |
if s.Conf.IsMultilingual() || s.Conf.DefaultContentLanguageInSubdir() { |
| 1388 |
base = s.Language().Lang |
| 1389 |
} |
| 1390 |
p := s.AbsURL(base, false) |
| 1391 |
if !strings.HasSuffix(p, "/") { |
| 1392 |
p += "/" |
| 1393 |
} |
| 1394 |
p += s.conf.Sitemap.Filename |
| 1395 |
return p |
| 1396 |
} |
| 1397 |
|
| 1398 |
func (s *Site) createNodeMenuEntryURL(in string) string { |
| 1399 |
if !strings.HasPrefix(in, "/") { |
| 1400 |
return in |
| 1401 |
} |
| 1402 |
// make it match the nodes |
| 1403 |
menuEntryURL := in |
| 1404 |
menuEntryURL = s.PathSpec.URLize(menuEntryURL) |
| 1405 |
if !s.conf.CanonifyURLs { |
| 1406 |
menuEntryURL = paths.AddContextRoot(s.PathSpec.Cfg.BaseURL().String(), menuEntryURL) |
| 1407 |
} |
| 1408 |
return menuEntryURL |
| 1409 |
} |
| 1410 |
|
| 1411 |
func (s *Site) assembleMenus() (navigation.Menus, error) { |
| 1412 |
menus := make(navigation.Menus) |
| 1413 |
|
| 1414 |
type twoD struct { |
| 1415 |
MenuName, EntryName string |
| 1416 |
} |
| 1417 |
flat := map[twoD]*navigation.MenuEntry{} |
| 1418 |
children := map[twoD]navigation.Menu{} |
| 1419 |
|
| 1420 |
// add menu entries from config to flat hash |
| 1421 |
for name, menu := range s.conf.Menus.Config { |
| 1422 |
for _, x := range menu { |
| 1423 |
// Shallow copy the menu entry, so we can set the URL and page without modifying the config. |
| 1424 |
c := *x |
| 1425 |
me := &c |
| 1426 |
|
| 1427 |
if me.PageRef != "" { |
| 1428 |
// Try to resolve the page. |
| 1429 |
me.Page, _ = s.getPage(nil, me.PageRef) |
| 1430 |
} |
| 1431 |
|
| 1432 |
// If page is still nill, we must make sure that we have a URL that considers baseURL etc. |
| 1433 |
if types.IsNil(me.Page) { |
| 1434 |
me.ConfiguredURL = s.createNodeMenuEntryURL(me.MenuConfig.URL) |
| 1435 |
} else { |
| 1436 |
navigation.SetPageValues(me, me.Page) |
| 1437 |
} |
| 1438 |
|
| 1439 |
flat[twoD{name, me.KeyName()}] = me |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
sectionPagesMenu := s.conf.SectionPagesMenu |
| 1444 |
|
| 1445 |
if sectionPagesMenu != "" { |
| 1446 |
if err := s.pageMap.forEachPage(pagePredicates.ShouldListGlobal, func(p *pageState) (bool, error) { |
| 1447 |
if p.Kind() != kinds.KindSection || !p.m.shouldBeCheckedForMenuDefinitions() { |
| 1448 |
return false, nil |
| 1449 |
} |
| 1450 |
|
| 1451 |
// The section pages menus are attached to the top level section. |
| 1452 |
id := p.Section() |
| 1453 |
if id == "" { |
| 1454 |
id = "/" |
| 1455 |
} |
| 1456 |
|
| 1457 |
if _, ok := flat[twoD{sectionPagesMenu, id}]; ok { |
| 1458 |
return false, nil |
| 1459 |
} |
| 1460 |
me := navigation.MenuEntry{ |
| 1461 |
Identifier: id, |
| 1462 |
Name: p.LinkTitle(), |
| 1463 |
Weight: p.Weight(), |
| 1464 |
Page: p, |
| 1465 |
} |
| 1466 |
|
| 1467 |
navigation.SetPageValues(&me, p) |
| 1468 |
flat[twoD{sectionPagesMenu, me.KeyName()}] = &me |
| 1469 |
return false, nil |
| 1470 |
}); err != nil { |
| 1471 |
return nil, err |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
// Add menu entries provided by pages |
| 1476 |
if err := s.pageMap.forEachPage(pagePredicates.ShouldListGlobal, func(p *pageState) (bool, error) { |
| 1477 |
for name, me := range p.pageMenus.menus() { |
| 1478 |
if _, ok := flat[twoD{name, me.KeyName()}]; ok { |
| 1479 |
err := p.wrapError(fmt.Errorf("duplicate menu entry with identifier %q in menu %q", me.KeyName(), name)) |
| 1480 |
s.Log.Warnln(err) |
| 1481 |
continue |
| 1482 |
} |
| 1483 |
flat[twoD{name, me.KeyName()}] = me |
| 1484 |
} |
| 1485 |
return false, nil |
| 1486 |
}); err != nil { |
| 1487 |
return nil, err |
| 1488 |
} |
| 1489 |
|
| 1490 |
// Create Children Menus First |
| 1491 |
for _, e := range flat { |
| 1492 |
if e.Parent != "" { |
| 1493 |
children[twoD{e.Menu, e.Parent}] = children[twoD{e.Menu, e.Parent}].Add(e) |
| 1494 |
} |
| 1495 |
} |
| 1496 |
|
| 1497 |
// Placing Children in Parents (in flat) |
| 1498 |
for p, childmenu := range children { |
| 1499 |
_, ok := flat[twoD{p.MenuName, p.EntryName}] |
| 1500 |
if !ok { |
| 1501 |
// if parent does not exist, create one without a URL |
| 1502 |
flat[twoD{p.MenuName, p.EntryName}] = &navigation.MenuEntry{ |
| 1503 |
Name: p.EntryName, |
| 1504 |
} |
| 1505 |
} |
| 1506 |
flat[twoD{p.MenuName, p.EntryName}].Children = childmenu |
| 1507 |
} |
| 1508 |
|
| 1509 |
// Assembling Top Level of Tree |
| 1510 |
for menu, e := range flat { |
| 1511 |
if e.Parent == "" { |
| 1512 |
_, ok := menus[menu.MenuName] |
| 1513 |
if !ok { |
| 1514 |
menus[menu.MenuName] = navigation.Menu{} |
| 1515 |
} |
| 1516 |
menus[menu.MenuName] = menus[menu.MenuName].Add(e) |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
return menus, nil |
| 1521 |
} |
| 1522 |
|
| 1523 |
// get any language code to prefix the target file path with. |
| 1524 |
func (s *Site) getLanguageTargetPathLang(alwaysInSubDir bool) string { |
| 1525 |
if s.h.Conf.IsMultihost() { |
| 1526 |
return s.Language().Lang |
| 1527 |
} |
| 1528 |
|
| 1529 |
return s.getLanguagePermalinkLang(alwaysInSubDir) |
| 1530 |
} |
| 1531 |
|
| 1532 |
// get any language code to prefix the relative permalink with. |
| 1533 |
func (s *Site) getLanguagePermalinkLang(alwaysInSubDir bool) string { |
| 1534 |
if s.h.Conf.IsMultihost() { |
| 1535 |
return "" |
| 1536 |
} |
| 1537 |
|
| 1538 |
if s.h.Conf.IsMultilingual() && alwaysInSubDir { |
| 1539 |
return s.Language().Lang |
| 1540 |
} |
| 1541 |
|
| 1542 |
return s.GetLanguagePrefix() |
| 1543 |
} |
| 1544 |
|
| 1545 |
func (s *Site) getPrefixRole() string { |
| 1546 |
role := s.roleInternal |
| 1547 |
if role.Default { |
| 1548 |
if s.conf.DefaultContentRoleInSubdir { |
| 1549 |
return role.Name |
| 1550 |
} |
| 1551 |
return "" |
| 1552 |
} |
| 1553 |
return role.Name |
| 1554 |
} |
| 1555 |
|
| 1556 |
func (s *Site) getPrefixVersion() string { |
| 1557 |
version := s.versionInternal |
| 1558 |
if version.Default { |
| 1559 |
if s.conf.DefaultContentVersionInSubdir { |
| 1560 |
return version.Name |
| 1561 |
} |
| 1562 |
return "" |
| 1563 |
} |
| 1564 |
return version.Name |
| 1565 |
} |
| 1566 |
|
| 1567 |
// Prepare site for a new full build. |
| 1568 |
func (s *Site) resetBuildState(sourceChanged bool) { |
| 1569 |
s.relatedDocsHandler = s.relatedDocsHandler.Clone() |
| 1570 |
s.init.Reset() |
| 1571 |
s.pageMap.Reset() |
| 1572 |
} |
| 1573 |
|
| 1574 |
func (s *Site) errorCollator(results <-chan error, errs chan<- error) { |
| 1575 |
var errors []error |
| 1576 |
defer func() { |
| 1577 |
errs <- s.h.filterAndJoinErrors(errors) |
| 1578 |
close(errs) |
| 1579 |
}() |
| 1580 |
const maxErrors = 10 |
| 1581 |
for e := range results { |
| 1582 |
errors = append(errors, e) |
| 1583 |
if len(errors) >= maxErrors { |
| 1584 |
s.h.Stop() |
| 1585 |
break |
| 1586 |
} |
| 1587 |
} |
| 1588 |
} |
| 1589 |
|
| 1590 |
// GetPage looks up a page of a given type for the given ref. |
| 1591 |
// In Hugo <= 0.44 you had to add Page Kind (section, home) etc. as the first |
| 1592 |
// argument and then either a unix styled path (with or without a leading slash)) |
| 1593 |
// or path elements separated. |
| 1594 |
// When we now remove the Kind from this API, we need to make the transition as painless |
| 1595 |
// as possible for existing sites. Most sites will use {{ .Site.GetPage "section" "my/section" }}, |
| 1596 |
// i.e. 2 arguments, so we test for that. |
| 1597 |
func (s *Site) GetPage(ref ...string) (page.Page, error) { |
| 1598 |
s.CheckReady() |
| 1599 |
p, err := s.getPageForRefs(ref...) |
| 1600 |
|
| 1601 |
if p == nil { |
| 1602 |
// The nil struct has meaning in some situations, mostly to avoid breaking |
| 1603 |
// existing sites doing $nilpage.IsDescendant($p), which will always return |
| 1604 |
// false. |
| 1605 |
p = page.NilPage |
| 1606 |
} |
| 1607 |
|
| 1608 |
return p, err |
| 1609 |
} |
| 1610 |
|
| 1611 |
func (s *Site) absURLPath(targetPath string) string { |
| 1612 |
var path string |
| 1613 |
if s.conf.RelativeURLs { |
| 1614 |
path = helpers.GetDottedRelativePath(targetPath) |
| 1615 |
} else { |
| 1616 |
url := s.PathSpec.Cfg.BaseURL().String() |
| 1617 |
if !strings.HasSuffix(url, "/") { |
| 1618 |
url += "/" |
| 1619 |
} |
| 1620 |
path = url |
| 1621 |
} |
| 1622 |
|
| 1623 |
return path |
| 1624 |
} |
| 1625 |
|
| 1626 |
const ( |
| 1627 |
pageDependencyScopeDefault int = iota |
| 1628 |
pageDependencyScopeGlobal |
| 1629 |
) |
| 1630 |
|
| 1631 |
func (s *Site) renderAndWritePage(statCounter *uint64, targetPath string, p *pageState, d any, templ *tplimpl.TemplInfo) error { |
| 1632 |
s.h.onPageRender() |
| 1633 |
renderBuffer := bp.GetBuffer() |
| 1634 |
defer bp.PutBuffer(renderBuffer) |
| 1635 |
|
| 1636 |
of := p.outputFormat() |
| 1637 |
p.incrRenderState() |
| 1638 |
|
| 1639 |
ctx := s.TemplateStore.PrepareTopLevelRenderCtx(context.Background(), p) |
| 1640 |
ctx = tpl.Context.DependencyManagerScopedProvider.Set(ctx, p) |
| 1641 |
|
| 1642 |
if err := s.renderForTemplate(ctx, p.Kind(), of.Name, d, renderBuffer, templ); err != nil { |
| 1643 |
return err |
| 1644 |
} |
| 1645 |
|
| 1646 |
if renderBuffer.Len() == 0 { |
| 1647 |
return nil |
| 1648 |
} |
| 1649 |
|
| 1650 |
isHTML := of.IsHTML |
| 1651 |
isRSS := of.Name == "rss" |
| 1652 |
|
| 1653 |
pd := publisher.Descriptor{ |
| 1654 |
Src: renderBuffer, |
| 1655 |
TargetPath: targetPath, |
| 1656 |
StatCounter: statCounter, |
| 1657 |
OutputFormat: p.outputFormat(), |
| 1658 |
} |
| 1659 |
|
| 1660 |
if isRSS { |
| 1661 |
// Always canonify URLs in RSS |
| 1662 |
pd.AbsURLPath = s.absURLPath(targetPath) |
| 1663 |
} else if isHTML { |
| 1664 |
if s.conf.RelativeURLs || s.conf.CanonifyURLs { |
| 1665 |
pd.AbsURLPath = s.absURLPath(targetPath) |
| 1666 |
} |
| 1667 |
|
| 1668 |
if s.watching() && s.conf.Internal.Running && !s.conf.DisableLiveReload { |
| 1669 |
pd.LiveReloadBaseURL = s.Conf.BaseURLLiveReload().URL() |
| 1670 |
} |
| 1671 |
|
| 1672 |
// For performance reasons we only inject the Hugo generator tag on the home page. |
| 1673 |
if p.IsHome() { |
| 1674 |
pd.AddHugoGeneratorTag = !s.conf.DisableHugoGeneratorInject |
| 1675 |
} |
| 1676 |
|
| 1677 |
} |
| 1678 |
|
| 1679 |
return s.publisher.Publish(pd) |
| 1680 |
} |
| 1681 |
|
| 1682 |
var infoOnMissingLayout = map[string]bool{ |
| 1683 |
// The 404 layout is very much optional in Hugo, but we do look for it. |
| 1684 |
"404": true, |
| 1685 |
} |
| 1686 |
|
| 1687 |
// hookRendererTemplate is the canonical implementation of all hooks.ITEMRenderer, |
| 1688 |
// where ITEM is the thing being hooked. |
| 1689 |
type hookRendererTemplate struct { |
| 1690 |
templateHandler *tplimpl.TemplateStore |
| 1691 |
templ *tplimpl.TemplInfo |
| 1692 |
resolvePosition func(renderContext any, pos int) text.Position |
| 1693 |
} |
| 1694 |
|
| 1695 |
func (hr hookRendererTemplate) RenderLink(cctx context.Context, w io.Writer, ctx hooks.LinkContext) error { |
| 1696 |
return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) |
| 1697 |
} |
| 1698 |
|
| 1699 |
func (hr hookRendererTemplate) RenderHeading(cctx context.Context, w io.Writer, ctx hooks.HeadingContext) error { |
| 1700 |
return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) |
| 1701 |
} |
| 1702 |
|
| 1703 |
func (hr hookRendererTemplate) RenderCodeblock(cctx context.Context, w hugio.FlexiWriter, ctx hooks.CodeblockContext) error { |
| 1704 |
return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) |
| 1705 |
} |
| 1706 |
|
| 1707 |
func (hr hookRendererTemplate) RenderPassthrough(cctx context.Context, w io.Writer, ctx hooks.PassthroughContext) error { |
| 1708 |
return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) |
| 1709 |
} |
| 1710 |
|
| 1711 |
func (hr hookRendererTemplate) RenderBlockquote(cctx context.Context, w hugio.FlexiWriter, ctx hooks.BlockquoteContext) error { |
| 1712 |
return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) |
| 1713 |
} |
| 1714 |
|
| 1715 |
func (hr hookRendererTemplate) RenderTable(cctx context.Context, w hugio.FlexiWriter, ctx hooks.TableContext) error { |
| 1716 |
return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) |
| 1717 |
} |
| 1718 |
|
| 1719 |
func (hr hookRendererTemplate) ResolvePosition(renderContext any, pos int) text.Position { |
| 1720 |
return hr.resolvePosition(renderContext, pos) |
| 1721 |
} |
| 1722 |
|
| 1723 |
func (hr hookRendererTemplate) IsDefaultCodeBlockRenderer() bool { |
| 1724 |
return false |
| 1725 |
} |
| 1726 |
|
| 1727 |
func (s *Site) renderForTemplate(ctx context.Context, name, outputFormat string, d any, w io.Writer, templ *tplimpl.TemplInfo) (err error) { |
| 1728 |
if templ == nil { |
| 1729 |
s.logMissingLayout(name, "", "", outputFormat) |
| 1730 |
return nil |
| 1731 |
} |
| 1732 |
|
| 1733 |
if ctx == nil { |
| 1734 |
panic("nil context") |
| 1735 |
} |
| 1736 |
|
| 1737 |
if err = s.GetTemplateStore().ExecuteWithContext(ctx, templ, w, d); err != nil { |
| 1738 |
filename := name |
| 1739 |
if p, ok := d.(*pageState); ok { |
| 1740 |
filename = p.String() |
| 1741 |
} |
| 1742 |
return fmt.Errorf("render of %q failed: %w", filename, err) |
| 1743 |
} |
| 1744 |
return |
| 1745 |
} |
| 1746 |
|
| 1747 |
func (s *Site) shouldBuild(p page.Page) bool { |
| 1748 |
if !s.conf.IsKindEnabled(p.Kind()) { |
| 1749 |
return false |
| 1750 |
} |
| 1751 |
return shouldBuild(s.Conf.BuildFuture(), s.Conf.BuildExpired(), |
| 1752 |
s.Conf.BuildDrafts(), p.Draft(), p.PublishDate(), p.ExpiryDate()) |
| 1753 |
} |
| 1754 |
|
| 1755 |
func shouldBuild(buildFuture bool, buildExpired bool, buildDrafts bool, Draft bool, |
| 1756 |
publishDate time.Time, expiryDate time.Time, |
| 1757 |
) bool { |
| 1758 |
if !(buildDrafts || !Draft) { |
| 1759 |
return false |
| 1760 |
} |
| 1761 |
hnow := htime.Now() |
| 1762 |
if !buildFuture && !publishDate.IsZero() && publishDate.After(hnow) { |
| 1763 |
return false |
| 1764 |
} |
| 1765 |
if !buildExpired && !expiryDate.IsZero() && expiryDate.Before(hnow) { |
| 1766 |
return false |
| 1767 |
} |
| 1768 |
return true |
| 1769 |
} |
| 1770 |
|
| 1771 |
func (s *Site) render(ctx *siteRenderContext) (err error) { |
| 1772 |
if err := page.Clear(); err != nil { |
| 1773 |
return err |
| 1774 |
} |
| 1775 |
|
| 1776 |
if err = s.renderPages(ctx); err != nil { |
| 1777 |
return |
| 1778 |
} |
| 1779 |
|
| 1780 |
if !ctx.shouldRenderStandalonePage("") { |
| 1781 |
return |
| 1782 |
} |
| 1783 |
|
| 1784 |
return |
| 1785 |
} |
| 1786 |
|
| 1787 |
func (s *Site) String() string { |
| 1788 |
if s == nil { |
| 1789 |
return "Site (nil)" |
| 1790 |
} |
| 1791 |
|
| 1792 |
return fmt.Sprintf("Site %v: %s", s.siteVector, s.resolveDimensionNames()) |
| 1793 |
} |