diff options
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/alias.go | 5 | ||||
-rw-r--r-- | hugolib/hugo_sites.go | 5 | ||||
-rw-r--r-- | hugolib/page__per_output.go | 3 | ||||
-rw-r--r-- | hugolib/site.go | 25 | ||||
-rw-r--r-- | hugolib/site_render.go | 6 |
5 files changed, 28 insertions, 16 deletions
diff --git a/hugolib/alias.go b/hugolib/alias.go index 2609cd6bb..071f73d41 100644 --- a/hugolib/alias.go +++ b/hugolib/alias.go @@ -15,6 +15,7 @@ package hugolib import ( "bytes" + "context" "errors" "fmt" "io" @@ -64,8 +65,10 @@ func (a aliasHandler) renderAlias(permalink string, p page.Page) (io.Reader, err p, } + ctx := tpl.SetPageInContext(context.Background(), p) + buffer := new(bytes.Buffer) - err := a.t.Execute(templ, buffer, data) + err := a.t.ExecuteWithContext(ctx, templ, buffer, data) if err != nil { return nil, err } diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index cdc5d97fb..65cc23971 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -767,10 +767,11 @@ func (h *HugoSites) renderCrossSitesSitemap() error { } s := h.Sites[0] + // We don't have any page context to pass in here. + ctx := context.Background() templ := s.lookupLayouts("sitemapindex.xml", "_default/sitemapindex.xml", "_internal/_default/sitemapindex.xml") - - return s.renderAndWriteXML(&s.PathSpec.ProcessingStats.Sitemaps, "sitemapindex", + return s.renderAndWriteXML(ctx, &s.PathSpec.ProcessingStats.Sitemaps, "sitemapindex", s.siteCfg.sitemap.Filename, h.toSiteInfos(), templ) } diff --git a/hugolib/page__per_output.go b/hugolib/page__per_output.go index ce3498e0e..be65ad9e7 100644 --- a/hugolib/page__per_output.go +++ b/hugolib/page__per_output.go @@ -740,6 +740,7 @@ func (cp *pageContentOutput) ParseContent(ctx context.Context, content []byte) ( return nil, ok, nil } rctx := converter.RenderContext{ + Ctx: ctx, Src: content, RenderTOC: true, GetRenderer: cp.renderHooks.getRenderer, @@ -758,6 +759,7 @@ func (cp *pageContentOutput) RenderContent(ctx context.Context, content []byte, return nil, ok, nil } rctx := converter.RenderContext{ + Ctx: ctx, Src: content, RenderTOC: true, GetRenderer: cp.renderHooks.getRenderer, @@ -777,6 +779,7 @@ func (cp *pageContentOutput) RenderContent(ctx context.Context, content []byte, func (cp *pageContentOutput) renderContentWithConverter(ctx context.Context, c converter.Converter, content []byte, renderTOC bool) (converter.ResultRender, error) { r, err := c.Convert( converter.RenderContext{ + Ctx: ctx, Src: content, RenderTOC: renderTOC, GetRenderer: cp.renderHooks.getRenderer, diff --git a/hugolib/site.go b/hugolib/site.go index e90fa41ff..45b4643f0 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1710,12 +1710,12 @@ func (s *Site) lookupLayouts(layouts ...string) tpl.Template { return nil } -func (s *Site) renderAndWriteXML(statCounter *uint64, name string, targetPath string, d any, templ tpl.Template) error { +func (s *Site) renderAndWriteXML(ctx context.Context, statCounter *uint64, name string, targetPath string, d any, templ tpl.Template) error { s.Log.Debugf("Render XML for %q to %q", name, targetPath) renderBuffer := bp.GetBuffer() defer bp.PutBuffer(renderBuffer) - if err := s.renderForTemplate(name, "", d, renderBuffer, templ); err != nil { + if err := s.renderForTemplate(ctx, name, "", d, renderBuffer, templ); err != nil { return err } @@ -1739,8 +1739,9 @@ func (s *Site) renderAndWritePage(statCounter *uint64, name string, targetPath s defer bp.PutBuffer(renderBuffer) of := p.outputFormat() + ctx := tpl.SetPageInContext(context.Background(), p) - if err := s.renderForTemplate(p.Kind(), of.Name, p, renderBuffer, templ); err != nil { + if err := s.renderForTemplate(ctx, p.Kind(), of.Name, p, renderBuffer, templ); err != nil { return err } @@ -1797,16 +1798,16 @@ type hookRendererTemplate struct { resolvePosition func(ctx any) text.Position } -func (hr hookRendererTemplate) RenderLink(w io.Writer, ctx hooks.LinkContext) error { - return hr.templateHandler.Execute(hr.templ, w, ctx) +func (hr hookRendererTemplate) RenderLink(cctx context.Context, w io.Writer, ctx hooks.LinkContext) error { + return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) } -func (hr hookRendererTemplate) RenderHeading(w io.Writer, ctx hooks.HeadingContext) error { - return hr.templateHandler.Execute(hr.templ, w, ctx) +func (hr hookRendererTemplate) RenderHeading(cctx context.Context, w io.Writer, ctx hooks.HeadingContext) error { + return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) } -func (hr hookRendererTemplate) RenderCodeblock(w hugio.FlexiWriter, ctx hooks.CodeblockContext) error { - return hr.templateHandler.Execute(hr.templ, w, ctx) +func (hr hookRendererTemplate) RenderCodeblock(cctx context.Context, w hugio.FlexiWriter, ctx hooks.CodeblockContext) error { + return hr.templateHandler.ExecuteWithContext(cctx, hr.templ, w, ctx) } func (hr hookRendererTemplate) ResolvePosition(ctx any) text.Position { @@ -1817,13 +1818,15 @@ func (hr hookRendererTemplate) IsDefaultCodeBlockRenderer() bool { return false } -func (s *Site) renderForTemplate(name, outputFormat string, d any, w io.Writer, templ tpl.Template) (err error) { +func (s *Site) renderForTemplate(ctx context.Context, name, outputFormat string, d any, w io.Writer, templ tpl.Template) (err error) { if templ == nil { s.logMissingLayout(name, "", "", outputFormat) return nil } - ctx := context.Background() + if ctx == nil { + panic("nil context") + } if err = s.Tmpl().ExecuteWithContext(ctx, templ, w, d); err != nil { return fmt.Errorf("render of %q failed: %w", name, err) diff --git a/hugolib/site_render.go b/hugolib/site_render.go index 51d638dde..f105a1ae4 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -14,6 +14,7 @@ package hugolib import ( + "context" "fmt" "path" "strings" @@ -197,7 +198,7 @@ func (s *Site) renderPaginator(p *pageState, templ tpl.Template) error { d.Addends = fmt.Sprintf("/%s/%d", paginatePath, 1) targetPaths := page.CreateTargetPaths(d) - if err := s.writeDestAlias(targetPaths.TargetFilename, p.Permalink(), f, nil); err != nil { + if err := s.writeDestAlias(targetPaths.TargetFilename, p.Permalink(), f, p); err != nil { return err } } @@ -278,6 +279,7 @@ func (s *Site) renderSitemap() error { } targetPath := p.targetPaths().TargetFilename + ctx := tpl.SetPageInContext(context.Background(), p) if targetPath == "" { return errors.New("failed to create targetPath for sitemap") @@ -285,7 +287,7 @@ func (s *Site) renderSitemap() error { templ := s.lookupLayouts("sitemap.xml", "_default/sitemap.xml", "_internal/_default/sitemap.xml") - return s.renderAndWriteXML(&s.PathSpec.ProcessingStats.Sitemaps, "sitemap", targetPath, p, templ) + return s.renderAndWriteXML(ctx, &s.PathSpec.ProcessingStats.Sitemaps, "sitemap", targetPath, p, templ) } func (s *Site) renderRobotsTXT() error { |