Skip to content

Commit

Permalink
tpl/page: Add page template function
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 25, 2022
1 parent 08fdca9 commit 1cf823e
Show file tree
Hide file tree
Showing 56 changed files with 446 additions and 399 deletions.
2 changes: 1 addition & 1 deletion hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB
return nil, err
}

ps.init.Add(func() (interface{}, error) {
ps.init.Add(func(ctx context.Context) (interface{}, error) {
pp, err := newPagePaths(s, ps, metaProvider)
if err != nil {
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ func (h *hugoSitesInit) Reset() {
}

func (h *HugoSites) Data() map[string]interface{} {
if _, err := h.init.data.Do(); err != nil {
if _, err := h.init.data.Do(context.TODO()); err != nil {
h.SendError(errors.Wrap(err, "failed to load data"))
return nil
}
return h.data
}

func (h *HugoSites) gitInfoForPage(p page.Page) (*gitmap.GitInfo, error) {
if _, err := h.init.gitInfo.Do(); err != nil {
if _, err := h.init.gitInfo.Do(context.TODO()); err != nil {
return nil, err
}

Expand All @@ -210,7 +210,7 @@ func (h *HugoSites) gitInfoForPage(p page.Page) (*gitmap.GitInfo, error) {
}

func (h *HugoSites) codeownersForPage(p page.Page) ([]string, error) {
if _, err := h.init.gitInfo.Do(); err != nil {
if _, err := h.init.gitInfo.Do(context.TODO()); err != nil {
return nil, err
}

Expand Down Expand Up @@ -359,15 +359,15 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
donec: make(chan bool),
}

h.init.data.Add(func() (interface{}, error) {
h.init.data.Add(func(ctx context.Context) (interface{}, error) {
err := h.loadData(h.PathSpec.BaseFs.Data.Dirs)
if err != nil {
return nil, errors.Wrap(err, "failed to load data")
}
return nil, nil
})

h.init.layouts.Add(func() (interface{}, error) {
h.init.layouts.Add(func(ctx context.Context) (interface{}, error) {
for _, s := range h.Sites {
if err := s.Tmpl().(tpl.TemplateManager).MarkReady(); err != nil {
return nil, err
Expand All @@ -376,7 +376,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, nil
})

h.init.translations.Add(func() (interface{}, error) {
h.init.translations.Add(func(ctx context.Context) (interface{}, error) {
if len(h.Sites) > 1 {
allTranslations := pagesToTranslationsMap(h.Sites)
assignTranslationsToPages(allTranslations, h.Sites)
Expand All @@ -385,7 +385,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, nil
})

h.init.gitInfo.Add(func() (interface{}, error) {
h.init.gitInfo.Add(func(ctx context.Context) (interface{}, error) {
err := h.loadGitInfo()
if err != nil {
return nil, errors.Wrap(err, "failed to load Git info")
Expand Down
2 changes: 1 addition & 1 deletion hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (h *HugoSites) assemble(bcfg *BuildCfg) error {
}

func (h *HugoSites) render(config *BuildCfg) error {
if _, err := h.init.layouts.Do(); err != nil {
if _, err := h.init.layouts.Do(context.TODO()); err != nil {
return err
}

Expand Down
9 changes: 5 additions & 4 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package hugolib

import (
"bytes"
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -352,7 +353,7 @@ func (p *pageState) String() string {
// IsTranslated returns whether this content file is translated to
// other language(s).
func (p *pageState) IsTranslated() bool {
p.s.h.init.translations.Do()
p.s.h.init.translations.Do(context.TODO())
return len(p.translations) > 0
}

Expand All @@ -376,13 +377,13 @@ func (p *pageState) TranslationKey() string {

// AllTranslations returns all translations, including the current Page.
func (p *pageState) AllTranslations() page.Pages {
p.s.h.init.translations.Do()
p.s.h.init.translations.Do(context.TODO())
return p.allTranslations
}

// Translations returns the translations excluding the current Page.
func (p *pageState) Translations() page.Pages {
p.s.h.init.translations.Do()
p.s.h.init.translations.Do(context.TODO())
return p.translations
}

Expand Down Expand Up @@ -462,7 +463,7 @@ func (p *pageState) initOutputFormat(isRenderingSite bool, idx int) error {

// Must be run after the site section tree etc. is built and ready.
func (p *pageState) initPage() error {
if _, err := p.init.Do(); err != nil {
if _, err := p.init.Do(context.TODO()); err != nil {
return err
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions hugolib/page__menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"sync"

"github.com/gohugoio/hugo/navigation"
Expand All @@ -29,21 +30,21 @@ type pageMenus struct {
}

func (p *pageMenus) HasMenuCurrent(menuID string, me *navigation.MenuEntry) bool {
p.p.s.init.menus.Do()
p.p.s.init.menus.Do(context.TODO())
p.init()
return p.q.HasMenuCurrent(menuID, me)
}

func (p *pageMenus) IsMenuCurrent(menuID string, inme *navigation.MenuEntry) bool {
p.p.s.init.menus.Do()
p.p.s.init.menus.Do(context.TODO())
p.init()
return p.q.IsMenuCurrent(menuID, inme)
}

func (p *pageMenus) Menus() navigation.PageMenus {
// There is a reverse dependency here. initMenus will, once, build the
// site menus and update any relevant page.
p.p.s.init.menus.Do()
p.p.s.init.menus.Do(context.TODO())

return p.menus()
}
Expand Down
3 changes: 2 additions & 1 deletion hugolib/page__new.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"html/template"
"strings"

Expand Down Expand Up @@ -119,7 +120,7 @@ func newPageFromMeta(
return nil, err
}

ps.init.Add(func() (interface{}, error) {
ps.init.Add(func(ctx context.Context) (interface{}, error) {
pp, err := newPagePaths(metaProvider.s, ps, metaProvider)
if err != nil {
return nil, err
Expand Down
71 changes: 36 additions & 35 deletions hugolib/page__per_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
renderHooks: &renderHooks{},
}

initContent := func() (err error) {
initContent := func(ctx context.Context) (err error) {
p.s.h.IncrContentRender()

if p.cmap == nil {
Expand All @@ -105,7 +105,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
var hasShortcodeVariants bool

f := po.f
cp.contentPlaceholders, hasShortcodeVariants, err = p.shortcodeState.renderShortcodesForPage(p, f)
cp.contentPlaceholders, hasShortcodeVariants, err = p.shortcodeState.renderShortcodesForPage(ctx, p, f)
if err != nil {
return err
}
Expand All @@ -119,7 +119,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
isHTML := cp.p.m.markup == "html"

if !isHTML {
r, err := cp.renderContent(cp.workContent, true)
r, err := cp.renderContent(ctx, cp.workContent, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
}
}
} else if cp.p.m.summary != "" {
b, err := cp.renderContent([]byte(cp.p.m.summary), false)
b, err := cp.renderContent(ctx, []byte(cp.p.m.summary), false)
if err != nil {
return err
}
Expand All @@ -194,10 +194,10 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err

// There may be recursive loops in shortcodes and render hooks.
cp.initMain = parent.BranchWithTimeout(p.s.siteCfg.timeout, func(ctx context.Context) (interface{}, error) {
return nil, initContent()
return nil, initContent(ctx)
})

cp.initPlain = cp.initMain.Branch(func() (interface{}, error) {
cp.initPlain = cp.initMain.Branch(func(ctx context.Context) (interface{}, error) {
cp.plain = helpers.StripHTML(string(cp.content))
cp.plainWords = strings.Fields(cp.plain)
cp.setWordCounts(p.m.isCJKLanguage)
Expand Down Expand Up @@ -270,65 +270,65 @@ func (p *pageContentOutput) Reset() {
p.renderHooks = &renderHooks{}
}

func (p *pageContentOutput) Content() (interface{}, error) {
if p.p.s.initInit(p.initMain, p.p) {
func (p *pageContentOutput) Content(ctx context.Context) (interface{}, error) {
if p.p.s.initInit(ctx, p.initMain, p.p) {
return p.content, nil
}
return nil, nil
}

func (p *pageContentOutput) FuzzyWordCount() int {
p.p.s.initInit(p.initPlain, p.p)
func (p *pageContentOutput) FuzzyWordCount(ctx context.Context) int {
p.p.s.initInit(ctx, p.initPlain, p.p)
return p.fuzzyWordCount
}

func (p *pageContentOutput) Len() int {
p.p.s.initInit(p.initMain, p.p)
func (p *pageContentOutput) Len(ctx context.Context) int {
p.p.s.initInit(ctx, p.initMain, p.p)
return len(p.content)
}

func (p *pageContentOutput) Plain() string {
p.p.s.initInit(p.initPlain, p.p)
func (p *pageContentOutput) Plain(ctx context.Context) string {
p.p.s.initInit(ctx, p.initPlain, p.p)
return p.plain
}

func (p *pageContentOutput) PlainWords() []string {
p.p.s.initInit(p.initPlain, p.p)
func (p *pageContentOutput) PlainWords(ctx context.Context) []string {
p.p.s.initInit(ctx, p.initPlain, p.p)
return p.plainWords
}

func (p *pageContentOutput) ReadingTime() int {
p.p.s.initInit(p.initPlain, p.p)
func (p *pageContentOutput) ReadingTime(ctx context.Context) int {
p.p.s.initInit(ctx, p.initPlain, p.p)
return p.readingTime
}

func (p *pageContentOutput) Summary() template.HTML {
p.p.s.initInit(p.initMain, p.p)
func (p *pageContentOutput) Summary(ctx context.Context) template.HTML {
p.p.s.initInit(ctx, p.initMain, p.p)
if !p.p.source.hasSummaryDivider {
p.p.s.initInit(p.initPlain, p.p)
p.p.s.initInit(ctx, p.initPlain, p.p)
}
return p.summary
}

func (p *pageContentOutput) TableOfContents() template.HTML {
p.p.s.initInit(p.initMain, p.p)
func (p *pageContentOutput) TableOfContents(ctx context.Context) template.HTML {
p.p.s.initInit(ctx, p.initMain, p.p)
return p.tableOfContents
}

func (p *pageContentOutput) Truncated() bool {
func (p *pageContentOutput) Truncated(ctx context.Context) bool {
if p.p.truncated {
return true
}
p.p.s.initInit(p.initPlain, p.p)
p.p.s.initInit(ctx, p.initPlain, p.p)
return p.truncated
}

func (p *pageContentOutput) WordCount() int {
p.p.s.initInit(p.initPlain, p.p)
func (p *pageContentOutput) WordCount(ctx context.Context) int {
p.p.s.initInit(ctx, p.initPlain, p.p)
return p.wordCount
}

func (p *pageContentOutput) RenderString(args ...interface{}) (template.HTML, error) {
func (p *pageContentOutput) RenderString(ctx context.Context, args ...interface{}) (template.HTML, error) {
if len(args) < 1 || len(args) > 2 {
return "", errors.New("want 1 or 2 arguments")
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func (p *pageContentOutput) RenderString(args ...interface{}) (template.HTML, er
}
}

c, err := p.renderContentWithConverter(conv, []byte(s), false)
c, err := p.renderContentWithConverter(ctx, conv, []byte(s), false)
if err != nil {
return "", p.p.wrapError(err)
}
Expand All @@ -386,12 +386,12 @@ func (p *pageContentOutput) RenderString(args ...interface{}) (template.HTML, er
return template.HTML(string(b)), nil
}

func (p *pageContentOutput) RenderWithTemplateInfo(info tpl.Info, layout ...string) (template.HTML, error) {
func (p *pageContentOutput) RenderWithTemplateInfo(ctx context.Context, info tpl.Info, layout ...string) (template.HTML, error) {
p.p.addDependency(info)
return p.Render(layout...)
return p.Render(ctx, layout...)
}

func (p *pageContentOutput) Render(layout ...string) (template.HTML, error) {
func (p *pageContentOutput) Render(ctx context.Context, layout ...string) (template.HTML, error) {
templ, found, err := p.p.resolveTemplate(layout...)
if err != nil {
return "", p.p.wrapError(err)
Expand Down Expand Up @@ -539,17 +539,18 @@ func (p *pageContentOutput) setAutoSummary() error {
return nil
}

func (cp *pageContentOutput) renderContent(content []byte, renderTOC bool) (converter.Result, error) {
func (cp *pageContentOutput) renderContent(ctx context.Context, content []byte, renderTOC bool) (converter.Result, error) {
if err := cp.initRenderHooks(); err != nil {
return nil, err
}
c := cp.p.getContentConverter()
return cp.renderContentWithConverter(c, content, renderTOC)
return cp.renderContentWithConverter(ctx, c, content, renderTOC)
}

func (cp *pageContentOutput) renderContentWithConverter(c converter.Converter, content []byte, renderTOC bool) (converter.Result, error) {
func (cp *pageContentOutput) renderContentWithConverter(ctx context.Context, c converter.Converter, content []byte, renderTOC bool) (converter.Result, error) {
r, err := c.Convert(
converter.RenderContext{
Ctx: ctx,
Src: content,
RenderTOC: renderTOC,
GetRenderer: cp.renderHooks.getRenderer,
Expand Down
6 changes: 4 additions & 2 deletions hugolib/page__position.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package hugolib

import (
"context"

"github.com/gohugoio/hugo/lazy"
"github.com/gohugoio/hugo/resources/page"
)
Expand All @@ -33,12 +35,12 @@ type nextPrev struct {
}

func (n *nextPrev) next() page.Page {
n.init.Do()
n.init.Do(context.TODO())
return n.nextPage
}

func (n *nextPrev) prev() page.Page {
n.init.Do()
n.init.Do(context.TODO())
return n.prevPage
}

Expand Down
Loading

0 comments on commit 1cf823e

Please sign in to comment.