| 1 |
// Copyright 2019 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 |
"fmt" |
| 19 |
"path" |
| 20 |
"strings" |
| 21 |
"sync" |
| 22 |
|
| 23 |
"github.com/bep/logg" |
| 24 |
"github.com/gohugoio/go-radix" |
| 25 |
"github.com/gohugoio/hugo/common/paths" |
| 26 |
"github.com/gohugoio/hugo/hugolib/doctree" |
| 27 |
"github.com/gohugoio/hugo/tpl/tplimpl" |
| 28 |
|
| 29 |
"github.com/gohugoio/hugo/config" |
| 30 |
|
| 31 |
"github.com/gohugoio/hugo/resources/kinds" |
| 32 |
"github.com/gohugoio/hugo/resources/page" |
| 33 |
) |
| 34 |
|
| 35 |
type siteRenderContext struct { |
| 36 |
cfg *BuildCfg |
| 37 |
|
| 38 |
infol logg.LevelLogger |
| 39 |
|
| 40 |
// languageIdx is the zero based index of the site. |
| 41 |
languageIdx int |
| 42 |
|
| 43 |
// Zero based index for all output formats combined. |
| 44 |
sitesOutIdx int |
| 45 |
|
| 46 |
// Zero based index of the output formats configured within a Site. |
| 47 |
// Note that these outputs are sorted. |
| 48 |
outIdx int |
| 49 |
|
| 50 |
multihost bool |
| 51 |
} |
| 52 |
|
| 53 |
// Whether to render 404.html, robotsTXT.txt and similar. |
| 54 |
// These are usually rendered once in the root of public. |
| 55 |
func (s siteRenderContext) shouldRenderStandalonePage(kind string) bool { |
| 56 |
if s.multihost || kind == kinds.KindSitemap { |
| 57 |
// 1 per site |
| 58 |
return s.outIdx == 0 |
| 59 |
} |
| 60 |
|
| 61 |
if kind == kinds.KindTemporary || kind == kinds.KindStatus404 { |
| 62 |
// 1 for all output formats |
| 63 |
return s.outIdx == 0 |
| 64 |
} |
| 65 |
|
| 66 |
// 1 for all sites and output formats. |
| 67 |
return s.languageIdx == 0 && s.outIdx == 0 |
| 68 |
} |
| 69 |
|
| 70 |
// renderPages renders pages concurrently. |
| 71 |
func (s *Site) renderPages(ctx *siteRenderContext) error { |
| 72 |
numWorkers := config.GetNumWorkerMultiplier() |
| 73 |
|
| 74 |
results := make(chan error) |
| 75 |
pages := make(chan *pageState, numWorkers) // buffered for performance |
| 76 |
errs := make(chan error) |
| 77 |
|
| 78 |
go s.errorCollator(results, errs) |
| 79 |
|
| 80 |
wg := &sync.WaitGroup{} |
| 81 |
|
| 82 |
for range numWorkers { |
| 83 |
wg.Add(1) |
| 84 |
go pageRenderer(ctx, s, pages, results, wg) |
| 85 |
} |
| 86 |
|
| 87 |
cfg := ctx.cfg |
| 88 |
|
| 89 |
w := &doctree.NodeShiftTreeWalker[contentNode]{ |
| 90 |
Tree: s.pageMap.treePages, |
| 91 |
Handle: func(key string, n contentNode) (radix.WalkFlag, error) { |
| 92 |
if p, ok := n.(*pageState); ok { |
| 93 |
if cfg.shouldRender(ctx.infol, p) { |
| 94 |
select { |
| 95 |
case <-s.h.Done(): |
| 96 |
return radix.WalkStop, nil |
| 97 |
default: |
| 98 |
pages <- p |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
return radix.WalkContinue, nil |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
if err := w.Walk(context.Background()); err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
close(pages) |
| 111 |
|
| 112 |
wg.Wait() |
| 113 |
|
| 114 |
close(results) |
| 115 |
|
| 116 |
err := <-errs |
| 117 |
if err != nil { |
| 118 |
return fmt.Errorf("%v failed to render pages: %w", s.resolveDimensionNames(), err) |
| 119 |
} |
| 120 |
return nil |
| 121 |
} |
| 122 |
|
| 123 |
func pageRenderer( |
| 124 |
ctx *siteRenderContext, |
| 125 |
s *Site, |
| 126 |
pages <-chan *pageState, |
| 127 |
results chan<- error, |
| 128 |
wg *sync.WaitGroup, |
| 129 |
) { |
| 130 |
defer wg.Done() |
| 131 |
|
| 132 |
sendErr := func(err error) bool { |
| 133 |
select { |
| 134 |
case results <- err: |
| 135 |
return true |
| 136 |
case <-s.h.Done(): |
| 137 |
return false |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
for p := range pages { |
| 142 |
|
| 143 |
if p.m.isStandalone() && !ctx.shouldRenderStandalonePage(p.Kind()) { |
| 144 |
continue |
| 145 |
} |
| 146 |
|
| 147 |
if p.m.pageConfig.Build.PublishResources { |
| 148 |
if err := p.renderResources(); err != nil { |
| 149 |
if sendErr(p.errorf(err, "failed to render resources")) { |
| 150 |
continue |
| 151 |
} else { |
| 152 |
return |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if !s.conf.DisableAliases && !s.h.BuildState.IsRebuild() && p.render { |
| 158 |
of := p.outputFormat() |
| 159 |
if of.IsHTML && of.Permalinkable { |
| 160 |
// Render any aliases for this page. |
| 161 |
if err := s.renderAliasesForPage(p); err != nil { |
| 162 |
if sendErr(err) { |
| 163 |
continue |
| 164 |
} else { |
| 165 |
return |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if !p.render { |
| 172 |
// Nothing more to do for this page. |
| 173 |
continue |
| 174 |
} |
| 175 |
|
| 176 |
templ, found, err := p.resolveTemplate() |
| 177 |
if err != nil { |
| 178 |
if sendErr(p.errorf(err, "failed to resolve template")) { |
| 179 |
continue |
| 180 |
} else { |
| 181 |
return |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if !found { |
| 186 |
s.Log.Trace( |
| 187 |
func() string { |
| 188 |
return fmt.Sprintf("no layout for kind %q found", p.Kind()) |
| 189 |
}, |
| 190 |
) |
| 191 |
// Don't emit warning for missing 404 etc. pages. |
| 192 |
if !p.m.isStandalone() { |
| 193 |
s.logMissingLayout("", p.Layout(), p.Kind(), p.f.Name) |
| 194 |
} |
| 195 |
continue |
| 196 |
} |
| 197 |
|
| 198 |
targetPath := p.targetPaths().TargetFilename |
| 199 |
|
| 200 |
s.Log.Trace( |
| 201 |
func() string { |
| 202 |
return fmt.Sprintf("rendering outputFormat %q kind %q using layout %q to %q", p.pageOutput.f.Name, p.Kind(), templ.Name(), targetPath) |
| 203 |
}, |
| 204 |
) |
| 205 |
|
| 206 |
var d any = p |
| 207 |
switch p.Kind() { |
| 208 |
case kinds.KindSitemapIndex: |
| 209 |
d = s.h.Sites |
| 210 |
} |
| 211 |
|
| 212 |
if err := s.renderAndWritePage(&s.PathSpec.ProcessingStats.Pages, targetPath, p, d, templ); err != nil { |
| 213 |
if sendErr(err) { |
| 214 |
continue |
| 215 |
} else { |
| 216 |
return |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if of := p.outputFormat(); p.IsHome() && of.IsHTML && s.isDefault() && (of.Path != "" || of.Name == "html") { |
| 221 |
if err = s.renderDefaultSiteRedirect(p); err != nil { |
| 222 |
if sendErr(err) { |
| 223 |
continue |
| 224 |
} else { |
| 225 |
return |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if p.paginator != nil && p.paginator.current != nil { |
| 231 |
if err := s.renderPaginator(p, templ); err != nil { |
| 232 |
if sendErr(err) { |
| 233 |
continue |
| 234 |
} else { |
| 235 |
return |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
func (s *Site) logMissingLayout(name, layout, kind, outputFormat string) { |
| 243 |
log := s.Log.Warn() |
| 244 |
if name != "" && infoOnMissingLayout[name] { |
| 245 |
log = s.Log.Info() |
| 246 |
} |
| 247 |
|
| 248 |
errMsg := "You should create a template file which matches Hugo Layouts Lookup Rules for this combination." |
| 249 |
var args []any |
| 250 |
msg := "found no layout file for" |
| 251 |
if outputFormat != "" { |
| 252 |
msg += " %q" |
| 253 |
args = append(args, outputFormat) |
| 254 |
} |
| 255 |
|
| 256 |
if layout != "" { |
| 257 |
msg += " for layout %q" |
| 258 |
args = append(args, layout) |
| 259 |
} |
| 260 |
|
| 261 |
if kind != "" { |
| 262 |
msg += " for kind %q" |
| 263 |
args = append(args, kind) |
| 264 |
} |
| 265 |
|
| 266 |
if name != "" { |
| 267 |
msg += " for %q" |
| 268 |
args = append(args, name) |
| 269 |
} |
| 270 |
|
| 271 |
msg += ": " + errMsg |
| 272 |
|
| 273 |
log.Logf(msg, args...) |
| 274 |
} |
| 275 |
|
| 276 |
// renderPaginator must be run after the owning Page has been rendered. |
| 277 |
func (s *Site) renderPaginator(p *pageState, templ *tplimpl.TemplInfo) error { |
| 278 |
paginatePath := s.Conf.Pagination().Path |
| 279 |
|
| 280 |
d := p.targetPathDescriptor |
| 281 |
f := p.outputFormat() |
| 282 |
d.Type = f |
| 283 |
|
| 284 |
if p.paginator.current == nil || p.paginator.current != p.paginator.current.First() { |
| 285 |
panic(fmt.Sprintf("invalid paginator state for %q", p.pathOrTitle())) |
| 286 |
} |
| 287 |
|
| 288 |
if f.IsHTML && !s.Conf.Pagination().DisableAliases { |
| 289 |
// Write alias for page 1 |
| 290 |
d.Addends = fmt.Sprintf("/%s/%d", paginatePath, 1) |
| 291 |
targetPaths := page.CreateTargetPaths(d) |
| 292 |
|
| 293 |
if err := s.writeDestAlias(targetPaths.TargetFilename, p.Permalink(), f, p); err != nil { |
| 294 |
return err |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
// Render pages for the rest |
| 300 |
for current := p.paginator.current.Next(); current != nil; current = current.Next() { |
| 301 |
|
| 302 |
p.paginator.current = current |
| 303 |
d.Addends = fmt.Sprintf("/%s/%d", paginatePath, current.PageNumber()) |
| 304 |
targetPaths := page.CreateTargetPaths(d) |
| 305 |
|
| 306 |
if err := s.renderAndWritePage( |
| 307 |
&s.PathSpec.ProcessingStats.PaginatorPages, |
| 308 |
targetPaths.TargetFilename, p, p, templ); err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
return nil |
| 315 |
} |
| 316 |
|
| 317 |
func (s *Site) renderAliasesForPage(p *pageState) error { |
| 318 |
po := p.pageOutput |
| 319 |
f := po.f |
| 320 |
plink := p.Permalink() |
| 321 |
for _, a := range p.Aliases() { |
| 322 |
err := s.writeDestAlias(a, plink, f, p) |
| 323 |
if err != nil { |
| 324 |
return err |
| 325 |
} |
| 326 |
} |
| 327 |
return nil |
| 328 |
} |
| 329 |
|
| 330 |
// renderDefaultSiteRedirect creates a redirect to the default site's home, |
| 331 |
// depending on if it lives in sub folder (e.g. /en) or not. |
| 332 |
// The default site is the site is the combination of defaultContentLanguage, |
| 333 |
// defaultContentVersion and defaultContentRole. |
| 334 |
func (s *Site) renderDefaultSiteRedirect(home *pageState) error { |
| 335 |
if s.conf.DisableDefaultLanguageRedirect || s.conf.DisableDefaultSiteRedirect { |
| 336 |
return nil |
| 337 |
} |
| 338 |
|
| 339 |
addRedirectInRoot := s.conf.DefaultContentLanguageInSubdir && !s.Conf.IsMultihost() |
| 340 |
addRedirectInRoot = addRedirectInRoot || s.conf.DefaultContentVersionInSubdir || s.conf.DefaultContentRoleInSubdir |
| 341 |
|
| 342 |
homeLink := home.pageOutput.targetPaths().Link // This doesn't have any baseURL paths in it. |
| 343 |
|
| 344 |
of := home.outputFormat() |
| 345 |
homePermalink := home.Permalink() |
| 346 |
|
| 347 |
var ps []string |
| 348 |
if of.Path != "" { |
| 349 |
if addRedirectInRoot { |
| 350 |
// For OutputFormats with a path, creating more than one alias will easily create path clashes without much value. |
| 351 |
ps = []string{paths.AddLeadingAndTrailingSlash(of.Path)} |
| 352 |
} |
| 353 |
} else { |
| 354 |
if addRedirectInRoot { |
| 355 |
ps = append(ps, "/") |
| 356 |
} |
| 357 |
|
| 358 |
if s.Conf.IsMultilingual() && !s.conf.DefaultContentLanguageInSubdir && !s.Conf.IsMultihost() { |
| 359 |
// Create redirect from e.g. /en => / |
| 360 |
ps = append(ps, homeLink+s.Lang()+"/") |
| 361 |
} |
| 362 |
|
| 363 |
// /guest/v1.0.0/en/ |
| 364 |
// /,/guest/,/guest/v1.0.0 = /guest/v1.0.0/en/ |
| 365 |
// /guest/v1.0.0/ |
| 366 |
// /,/guest/ = /guest/v1.0.0/ |
| 367 |
parts := strings.Split(strings.Trim(homeLink, "/"), "/") |
| 368 |
|
| 369 |
for i := 0; i < len(parts)-1; i++ { |
| 370 |
ps = append(ps, paths.AddLeadingAndTrailingSlash(strings.Join(parts[0:i+1], "/"))) |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if s.h.Configs.IsMultihost { |
| 375 |
prefix := "/" + s.LanguagePrefix() |
| 376 |
for i, p := range ps { |
| 377 |
ps[i] = path.Join(prefix, p) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
for _, p := range ps { |
| 382 |
if err := s.publishDestAlias(true, p, homePermalink, of, home); err != nil { |
| 383 |
return err |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
return nil |
| 388 |
} |