| 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 |
"errors" |
| 19 |
"fmt" |
| 20 |
"html/template" |
| 21 |
"sync" |
| 22 |
"sync/atomic" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/hmaps" |
| 25 |
"github.com/gohugoio/hugo/common/text" |
| 26 |
"github.com/gohugoio/hugo/identity" |
| 27 |
"github.com/gohugoio/hugo/tpl/tplimpl" |
| 28 |
"github.com/spf13/cast" |
| 29 |
|
| 30 |
"github.com/gohugoio/hugo/markup/converter/hooks" |
| 31 |
gc "github.com/gohugoio/hugo/markup/goldmark/goldmark_config" |
| 32 |
"github.com/gohugoio/hugo/markup/tableofcontents" |
| 33 |
|
| 34 |
"github.com/gohugoio/hugo/markup/converter" |
| 35 |
|
| 36 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 37 |
|
| 38 |
"github.com/gohugoio/hugo/output" |
| 39 |
"github.com/gohugoio/hugo/resources/page" |
| 40 |
"github.com/gohugoio/hugo/resources/resource" |
| 41 |
) |
| 42 |
|
| 43 |
var ( |
| 44 |
nopTargetPath = targetPathsHolder{} |
| 45 |
nopPagePerOutput = struct { |
| 46 |
resource.ResourceLinksProvider |
| 47 |
page.ContentProvider |
| 48 |
page.PageRenderProvider |
| 49 |
page.PaginatorProvider |
| 50 |
page.TableOfContentsProvider |
| 51 |
page.AlternativeOutputFormatsProvider |
| 52 |
|
| 53 |
targetPather |
| 54 |
}{ |
| 55 |
page.NopPage, |
| 56 |
page.NopPage, |
| 57 |
page.NopPage, |
| 58 |
page.NopPage, |
| 59 |
page.NopPage, |
| 60 |
page.NopPage, |
| 61 |
nopTargetPath, |
| 62 |
} |
| 63 |
) |
| 64 |
|
| 65 |
func newPageContentOutput(po *pageOutput) (*pageContentOutput, error) { |
| 66 |
cp := &pageContentOutput{ |
| 67 |
po: po, |
| 68 |
renderHooks: &renderHooks{}, |
| 69 |
otherOutputs: hmaps.NewCache[uint64, *pageContentOutput](), |
| 70 |
} |
| 71 |
return cp, nil |
| 72 |
} |
| 73 |
|
| 74 |
type renderHooks struct { |
| 75 |
getRenderer hooks.GetRendererFunc |
| 76 |
init sync.Once |
| 77 |
} |
| 78 |
|
| 79 |
// pageContentOutput represents the Page content for a given output format. |
| 80 |
type pageContentOutput struct { |
| 81 |
po *pageOutput |
| 82 |
|
| 83 |
// Other pages involved in rendering of this page, |
| 84 |
// typically included with .RenderShortcodes. |
| 85 |
otherOutputs *hmaps.Cache[uint64, *pageContentOutput] |
| 86 |
|
| 87 |
contentRenderedVersion uint32 // Incremented on reset. |
| 88 |
contentRendered atomic.Bool // Set on content render. |
| 89 |
|
| 90 |
// Renders Markdown hooks. |
| 91 |
renderHooks *renderHooks |
| 92 |
} |
| 93 |
|
| 94 |
func (pco *pageContentOutput) trackDependency(idp identity.IdentityProvider) { |
| 95 |
pco.po.p.dependencyManagerOutput.AddIdentity(idp.GetIdentity()) |
| 96 |
} |
| 97 |
|
| 98 |
func (pco *pageContentOutput) Reset() { |
| 99 |
if pco == nil { |
| 100 |
return |
| 101 |
} |
| 102 |
pco.contentRenderedVersion++ |
| 103 |
pco.contentRendered.Store(false) |
| 104 |
pco.renderHooks = &renderHooks{} |
| 105 |
} |
| 106 |
|
| 107 |
func (pco *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) { |
| 108 |
if len(layout) == 0 { |
| 109 |
return "", errors.New("no layout given") |
| 110 |
} |
| 111 |
templ, found, err := pco.po.p.resolveTemplate(layout...) |
| 112 |
if err != nil { |
| 113 |
return "", pco.po.p.wrapError(err) |
| 114 |
} |
| 115 |
|
| 116 |
if !found { |
| 117 |
return "", fmt.Errorf("template %q not found", layout[0]) |
| 118 |
} |
| 119 |
|
| 120 |
// Make sure to send the *pageState and not the *pageContentOutput to the template. |
| 121 |
res, err := executeToString(ctx, pco.po.p.s.GetTemplateStore(), templ, pco.po.p) |
| 122 |
if err != nil { |
| 123 |
return "", pco.po.p.wrapError(fmt.Errorf("failed to execute template %s: %w", templ.Name(), err)) |
| 124 |
} |
| 125 |
return template.HTML(res), nil |
| 126 |
} |
| 127 |
|
| 128 |
func (pco *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Fragments { |
| 129 |
return pco.c().Fragments(ctx) |
| 130 |
} |
| 131 |
|
| 132 |
func (pco *pageContentOutput) RenderShortcodes(ctx context.Context) (template.HTML, error) { |
| 133 |
return pco.c().RenderShortcodes(ctx) |
| 134 |
} |
| 135 |
|
| 136 |
func (pco *pageContentOutput) Markup(opts ...any) page.Markup { |
| 137 |
if len(opts) > 1 { |
| 138 |
panic("too many arguments, expected 0 or 1") |
| 139 |
} |
| 140 |
var scope string |
| 141 |
if len(opts) == 1 { |
| 142 |
scope = cast.ToString(opts[0]) |
| 143 |
} |
| 144 |
return pco.po.p.m.content.getOrCreateScope(scope, pco) |
| 145 |
} |
| 146 |
|
| 147 |
func (pco *pageContentOutput) c() page.Markup { |
| 148 |
return pco.po.p.m.content.getOrCreateScope("", pco) |
| 149 |
} |
| 150 |
|
| 151 |
func (pco *pageContentOutput) Content(ctx context.Context) (any, error) { |
| 152 |
r, err := pco.c().Render(ctx) |
| 153 |
if err != nil { |
| 154 |
return nil, err |
| 155 |
} |
| 156 |
return r.Content(ctx) |
| 157 |
} |
| 158 |
|
| 159 |
func (pco *pageContentOutput) ContentWithoutSummary(ctx context.Context) (template.HTML, error) { |
| 160 |
r, err := pco.c().Render(ctx) |
| 161 |
if err != nil { |
| 162 |
return "", err |
| 163 |
} |
| 164 |
return r.ContentWithoutSummary(ctx) |
| 165 |
} |
| 166 |
|
| 167 |
func (pco *pageContentOutput) TableOfContents(ctx context.Context) template.HTML { |
| 168 |
return pco.c().(*cachedContentScope).fragmentsHTML(ctx) |
| 169 |
} |
| 170 |
|
| 171 |
func (pco *pageContentOutput) Len(ctx context.Context) int { |
| 172 |
return pco.mustRender(ctx).Len(ctx) |
| 173 |
} |
| 174 |
|
| 175 |
func (pco *pageContentOutput) mustRender(ctx context.Context) page.Content { |
| 176 |
c, err := pco.c().Render(ctx) |
| 177 |
if err != nil { |
| 178 |
pco.fail(err) |
| 179 |
} |
| 180 |
return c |
| 181 |
} |
| 182 |
|
| 183 |
func (pco *pageContentOutput) fail(err error) { |
| 184 |
pco.po.p.s.h.FatalError(pco.po.p.wrapError(err)) |
| 185 |
} |
| 186 |
|
| 187 |
func (pco *pageContentOutput) Plain(ctx context.Context) string { |
| 188 |
return pco.mustRender(ctx).Plain(ctx) |
| 189 |
} |
| 190 |
|
| 191 |
func (pco *pageContentOutput) PlainWords(ctx context.Context) []string { |
| 192 |
return pco.mustRender(ctx).PlainWords(ctx) |
| 193 |
} |
| 194 |
|
| 195 |
func (pco *pageContentOutput) ReadingTime(ctx context.Context) int { |
| 196 |
return pco.mustRender(ctx).ReadingTime(ctx) |
| 197 |
} |
| 198 |
|
| 199 |
func (pco *pageContentOutput) WordCount(ctx context.Context) int { |
| 200 |
return pco.mustRender(ctx).WordCount(ctx) |
| 201 |
} |
| 202 |
|
| 203 |
func (pco *pageContentOutput) FuzzyWordCount(ctx context.Context) int { |
| 204 |
return pco.mustRender(ctx).FuzzyWordCount(ctx) |
| 205 |
} |
| 206 |
|
| 207 |
func (pco *pageContentOutput) Summary(ctx context.Context) template.HTML { |
| 208 |
summary, err := pco.mustRender(ctx).Summary(ctx) |
| 209 |
if err != nil { |
| 210 |
pco.fail(err) |
| 211 |
} |
| 212 |
return summary.Text |
| 213 |
} |
| 214 |
|
| 215 |
func (pco *pageContentOutput) Truncated(ctx context.Context) bool { |
| 216 |
summary, err := pco.mustRender(ctx).Summary(ctx) |
| 217 |
if err != nil { |
| 218 |
pco.fail(err) |
| 219 |
} |
| 220 |
return summary.Truncated |
| 221 |
} |
| 222 |
|
| 223 |
func (pco *pageContentOutput) RenderString(ctx context.Context, args ...any) (template.HTML, error) { |
| 224 |
return pco.c().RenderString(ctx, args...) |
| 225 |
} |
| 226 |
|
| 227 |
func (pco *pageContentOutput) initRenderHooks() error { |
| 228 |
if pco == nil { |
| 229 |
return nil |
| 230 |
} |
| 231 |
|
| 232 |
pco.renderHooks.init.Do(func() { |
| 233 |
if pco.po.p.pageOutputTemplateVariationsState.Load() == 0 { |
| 234 |
pco.po.p.pageOutputTemplateVariationsState.Store(1) |
| 235 |
} |
| 236 |
|
| 237 |
type cacheKey struct { |
| 238 |
tp hooks.RendererType |
| 239 |
id any |
| 240 |
f output.Format |
| 241 |
} |
| 242 |
|
| 243 |
renderCache := make(map[cacheKey]any) |
| 244 |
var renderCacheMu sync.Mutex |
| 245 |
|
| 246 |
resolvePosition := func(renderContext any, pos int) text.Position { |
| 247 |
rc, ok := renderContext.(converter.RenderContext) |
| 248 |
var si sourceInfo |
| 249 |
if ok { |
| 250 |
si, ok = rc.SourceInfo.(sourceInfo) |
| 251 |
} |
| 252 |
|
| 253 |
if pos == -1 || !ok { |
| 254 |
return text.Position{ |
| 255 |
Filename: pco.po.p.pathOrTitle(), |
| 256 |
} |
| 257 |
} |
| 258 |
offset := resolveSourceOffset(si.sourceMap, pos) |
| 259 |
filename := si.filename |
| 260 |
if filename == "" { |
| 261 |
filename = pco.po.p.pathOrTitle() |
| 262 |
} |
| 263 |
|
| 264 |
return posFromInput(filename, si.source, offset) |
| 265 |
} |
| 266 |
|
| 267 |
pco.renderHooks.getRenderer = func(tp hooks.RendererType, id any) any { |
| 268 |
renderCacheMu.Lock() |
| 269 |
defer renderCacheMu.Unlock() |
| 270 |
|
| 271 |
key := cacheKey{tp: tp, id: id, f: pco.po.f} |
| 272 |
if r, ok := renderCache[key]; ok { |
| 273 |
return r |
| 274 |
} |
| 275 |
|
| 276 |
// Inherit the descriptor from the page/current output format. |
| 277 |
// This allows for fine-grained control of the template used for |
| 278 |
// rendering of e.g. links. |
| 279 |
base, layoutDescriptor := pco.po.p.GetInternalTemplateBasePathAndDescriptor() |
| 280 |
|
| 281 |
switch tp { |
| 282 |
case hooks.LinkRendererType: |
| 283 |
layoutDescriptor.Variant1 = "link" |
| 284 |
case hooks.ImageRendererType: |
| 285 |
layoutDescriptor.Variant1 = "image" |
| 286 |
case hooks.HeadingRendererType: |
| 287 |
layoutDescriptor.Variant1 = "heading" |
| 288 |
case hooks.PassthroughRendererType: |
| 289 |
layoutDescriptor.Variant1 = "passthrough" |
| 290 |
if id != nil { |
| 291 |
layoutDescriptor.Variant2 = id.(string) |
| 292 |
} |
| 293 |
case hooks.BlockquoteRendererType: |
| 294 |
layoutDescriptor.Variant1 = "blockquote" |
| 295 |
if id != nil { |
| 296 |
layoutDescriptor.Variant2 = id.(string) |
| 297 |
} |
| 298 |
case hooks.TableRendererType: |
| 299 |
layoutDescriptor.Variant1 = "table" |
| 300 |
case hooks.CodeBlockRendererType: |
| 301 |
layoutDescriptor.Variant1 = "codeblock" |
| 302 |
if id != nil { |
| 303 |
layoutDescriptor.Variant2 = id.(string) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
renderHookConfig := pco.po.p.s.conf.Markup.Goldmark.RenderHooks |
| 308 |
var ignoreEmbedded bool |
| 309 |
|
| 310 |
// For multilingual single-host sites, "auto" becomes "fallback" |
| 311 |
// earlier in the process. |
| 312 |
switch layoutDescriptor.Variant1 { |
| 313 |
case "link": |
| 314 |
ignoreEmbedded = renderHookConfig.Link.UseEmbedded == gc.RenderHookUseEmbeddedNever || |
| 315 |
renderHookConfig.Link.UseEmbedded == gc.RenderHookUseEmbeddedAuto |
| 316 |
case "image": |
| 317 |
ignoreEmbedded = renderHookConfig.Image.UseEmbedded == gc.RenderHookUseEmbeddedNever || |
| 318 |
renderHookConfig.Image.UseEmbedded == gc.RenderHookUseEmbeddedAuto |
| 319 |
} |
| 320 |
|
| 321 |
candidates := pco.po.p.s.renderFormats |
| 322 |
var numCandidatesFound int |
| 323 |
consider := func(candidate *tplimpl.TemplInfo) bool { |
| 324 |
if layoutDescriptor.Variant1 != candidate.D.Variant1 { |
| 325 |
return false |
| 326 |
} |
| 327 |
|
| 328 |
if layoutDescriptor.Variant2 != "" && candidate.D.Variant2 != "" && layoutDescriptor.Variant2 != candidate.D.Variant2 { |
| 329 |
return false |
| 330 |
} |
| 331 |
|
| 332 |
if ignoreEmbedded && candidate.SubCategory() == tplimpl.SubCategoryEmbedded { |
| 333 |
// Don't consider the embedded hook templates. |
| 334 |
return false |
| 335 |
} |
| 336 |
|
| 337 |
if pco.po.p.pageOutputTemplateVariationsState.Load() > 1 { |
| 338 |
return true |
| 339 |
} |
| 340 |
|
| 341 |
if candidate.D.OutputFormat == "" { |
| 342 |
numCandidatesFound++ |
| 343 |
} else if _, found := candidates.GetByName(candidate.D.OutputFormat); found { |
| 344 |
numCandidatesFound++ |
| 345 |
} |
| 346 |
|
| 347 |
return true |
| 348 |
} |
| 349 |
|
| 350 |
getHookTemplate := func() (*tplimpl.TemplInfo, bool) { |
| 351 |
q := tplimpl.TemplateQuery{ |
| 352 |
Path: base, |
| 353 |
Category: tplimpl.CategoryMarkup, |
| 354 |
Desc: layoutDescriptor, |
| 355 |
Sites: pco.po.p.s.siteVector, |
| 356 |
Consider: consider, |
| 357 |
} |
| 358 |
|
| 359 |
v := pco.po.p.s.TemplateStore.LookupPagesLayout(q) |
| 360 |
|
| 361 |
return v, v != nil |
| 362 |
} |
| 363 |
|
| 364 |
templ, found1 := getHookTemplate() |
| 365 |
if found1 && templ == nil { |
| 366 |
panic("found1 is true, but templ is nil") |
| 367 |
} |
| 368 |
|
| 369 |
if !found1 && layoutDescriptor.OutputFormat == pco.po.p.s.conf.DefaultOutputFormat { |
| 370 |
numCandidatesFound++ |
| 371 |
} |
| 372 |
|
| 373 |
if numCandidatesFound > 1 { |
| 374 |
// More than one output format candidate found for this hook temoplate, |
| 375 |
// so we cannot reuse the same rendered content. |
| 376 |
pco.po.p.incrPageOutputTemplateVariation() |
| 377 |
} |
| 378 |
|
| 379 |
if !found1 { |
| 380 |
if tp == hooks.CodeBlockRendererType { |
| 381 |
// No user provided template for code blocks, so we use the native Go version -- which is also faster. |
| 382 |
r := pco.po.p.s.ContentSpec.Converters.GetHighlighter() |
| 383 |
renderCache[key] = r |
| 384 |
return r |
| 385 |
} |
| 386 |
return nil |
| 387 |
} |
| 388 |
|
| 389 |
r := hookRendererTemplate{ |
| 390 |
templateHandler: pco.po.p.s.GetTemplateStore(), |
| 391 |
templ: templ, |
| 392 |
resolvePosition: resolvePosition, |
| 393 |
} |
| 394 |
renderCache[key] = r |
| 395 |
return r |
| 396 |
} |
| 397 |
}) |
| 398 |
|
| 399 |
return nil |
| 400 |
} |
| 401 |
|
| 402 |
func (pco *pageContentOutput) getContentConverter() (converter.Converter, error) { |
| 403 |
if err := pco.initRenderHooks(); err != nil { |
| 404 |
return nil, err |
| 405 |
} |
| 406 |
return pco.po.p.getContentConverter(), nil |
| 407 |
} |
| 408 |
|
| 409 |
func (cp *pageContentOutput) ParseAndRenderContent(ctx context.Context, content []byte, renderTOC bool) (converter.ResultRender, error) { |
| 410 |
c, err := cp.getContentConverter() |
| 411 |
if err != nil { |
| 412 |
return nil, err |
| 413 |
} |
| 414 |
si := sourceInfo{ |
| 415 |
source: content, |
| 416 |
} |
| 417 |
return cp.renderContentWithConverter(ctx, c, content, si, renderTOC) |
| 418 |
} |
| 419 |
|
| 420 |
func (pco *pageContentOutput) ParseContent(ctx context.Context, content []byte) (converter.ResultParse, bool, error) { |
| 421 |
c, err := pco.getContentConverter() |
| 422 |
if err != nil { |
| 423 |
return nil, false, err |
| 424 |
} |
| 425 |
p, ok := c.(converter.ParseRenderer) |
| 426 |
if !ok { |
| 427 |
return nil, ok, nil |
| 428 |
} |
| 429 |
rctx := converter.RenderContext{ |
| 430 |
Ctx: ctx, |
| 431 |
Src: content, |
| 432 |
RenderTOC: true, |
| 433 |
GetRenderer: pco.renderHooks.getRenderer, |
| 434 |
} |
| 435 |
r, err := p.Parse(rctx) |
| 436 |
return r, ok, err |
| 437 |
} |
| 438 |
|
| 439 |
func (pco *pageContentOutput) RenderContent(ctx context.Context, content []byte, sourceInfo, doc any) (converter.ResultRender, bool, error) { |
| 440 |
c, err := pco.getContentConverter() |
| 441 |
if err != nil { |
| 442 |
return nil, false, err |
| 443 |
} |
| 444 |
p, ok := c.(converter.ParseRenderer) |
| 445 |
if !ok { |
| 446 |
return nil, ok, nil |
| 447 |
} |
| 448 |
rctx := converter.RenderContext{ |
| 449 |
Ctx: ctx, |
| 450 |
Src: content, |
| 451 |
SourceInfo: sourceInfo, |
| 452 |
RenderTOC: true, |
| 453 |
GetRenderer: pco.renderHooks.getRenderer, |
| 454 |
} |
| 455 |
r, err := p.Render(rctx, doc) |
| 456 |
return r, ok, err |
| 457 |
} |
| 458 |
|
| 459 |
func (pco *pageContentOutput) renderContentWithConverter(ctx context.Context, c converter.Converter, content []byte, sourceInfo any, renderTOC bool) (converter.ResultRender, error) { |
| 460 |
r, err := c.Convert( |
| 461 |
converter.RenderContext{ |
| 462 |
Ctx: ctx, |
| 463 |
Src: content, |
| 464 |
SourceInfo: sourceInfo, |
| 465 |
RenderTOC: renderTOC, |
| 466 |
GetRenderer: pco.renderHooks.getRenderer, |
| 467 |
}) |
| 468 |
return r, err |
| 469 |
} |
| 470 |
|
| 471 |
// these will be shifted out when rendering a given output format. |
| 472 |
type pagePerOutputProviders interface { |
| 473 |
targetPather |
| 474 |
page.PaginatorProvider |
| 475 |
resource.ResourceLinksProvider |
| 476 |
} |
| 477 |
|
| 478 |
type targetPather interface { |
| 479 |
targetPaths() page.TargetPaths |
| 480 |
getRelURL() string |
| 481 |
} |
| 482 |
|
| 483 |
type targetPathsHolder struct { |
| 484 |
// relURL is usually the same as OutputFormat.RelPermalink, but can be different |
| 485 |
// for non-permalinkable output formats. These shares RelPermalink with the main (first) output format. |
| 486 |
relURL string |
| 487 |
paths page.TargetPaths |
| 488 |
page.OutputFormat |
| 489 |
} |
| 490 |
|
| 491 |
func (t targetPathsHolder) getRelURL() string { |
| 492 |
return t.relURL |
| 493 |
} |
| 494 |
|
| 495 |
func (t targetPathsHolder) targetPaths() page.TargetPaths { |
| 496 |
return t.paths |
| 497 |
} |
| 498 |
|
| 499 |
func executeToString(ctx context.Context, h *tplimpl.TemplateStore, templ *tplimpl.TemplInfo, data any) (string, error) { |
| 500 |
b := bp.GetBuffer() |
| 501 |
defer bp.PutBuffer(b) |
| 502 |
if err := h.ExecuteWithContext(ctx, templ, b, data); err != nil { |
| 503 |
return "", err |
| 504 |
} |
| 505 |
return b.String(), nil |
| 506 |
} |