Skip to content

Commit

Permalink
Add page.Store
Browse files Browse the repository at this point in the history
Fixes #9546
  • Loading branch information
bep committed Feb 23, 2022
1 parent 7732da9 commit e97d3c6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
7 changes: 7 additions & 0 deletions hugolib/page__common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type pageCommon struct {
// Lazily initialized dependencies.
init *lazy.Init

// Store holds state that survives server rebuilds.
store *maps.Scratch

// All of these represents the common parts of a page.Page
maps.Scratcher
navigation.PageMenusProvider
Expand Down Expand Up @@ -134,6 +137,10 @@ type pageCommon struct {
forceRender bool
}

func (p *pageCommon) Store() *maps.Scratch {
return p.store
}

type pagePages struct {
pagesInit sync.Once
pages page.Pages
Expand Down
1 change: 1 addition & 0 deletions hugolib/page__new.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func newPageBase(metaProvider *pageMeta) (*pageState, error) {
FileProvider: metaProvider,
AuthorProvider: metaProvider,
Scratcher: maps.NewScratcher(),
store: maps.NewScratch(),
Positioner: page.NopPage,
InSectionPositioner: page.NopPage,
ResourceMetaProvider: metaProvider,
Expand Down
46 changes: 45 additions & 1 deletion hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ Summary: In Chinese, 好 means good.
b.AssertFileContent("public/p6/index.html", "WordCount: 7\nFuzzyWordCount: 100\nReadingTime: 1\nLen Plain: 638\nLen PlainWords: 7\nTruncated: false\nLen Summary: 637\nLen Content: 652")
}

func TestScratchSite(t *testing.T) {
func TestScratch(t *testing.T) {
t.Parallel()

b := newTestSitesBuilder(t)
Expand All @@ -1796,6 +1796,50 @@ title: Scratch Me!
b.AssertFileContent("public/scratchme/index.html", "C: cv")
}

func TestScratchRebuild(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
-- content/p1.md --
---
title: "p1"
---
{{< scratchme >}}
-- layouts/shortcodes/foo.html --
notused
-- layouts/shortcodes/scratchme.html --
{{ .Page.Scratch.Set "scratch" "foo" }}
{{ .Page.Store.Set "scratch" "bar" }}
-- layouts/_default/single.html --
{{ .Content }}
Scratch: {{ .Scratch.Get "scratch" }}|
Store: {{ .Store.Get "scratch" }}|
`

b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
Running: true,
},
).Build()

b.AssertFileContent("public/p1/index.html", `
Scratch: foo|
Store: bar|
`)

b.EditFiles("layouts/shortcodes/foo.html", "edit")

b.Build()

b.AssertFileContent("public/p1/index.html", `
Scratch: |
Store: bar|
`)
}

func TestPageParam(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 8 additions & 0 deletions resources/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,15 @@ type PageWithoutContent interface {
// Helper methods
ShortcodeInfoProvider
compare.Eqer

// Scratch returns a Scratch that can be used to store temporary state.
// Note that this Scratch gets reset on server rebuilds. See Store() for a variant that survives.
maps.Scratcher

// Store returns a Scratch that can be used to store temporary state.
// In contrast to Scratch(), this Scratch is not reset on server rebuilds.
Store() *maps.Scratch

RelatedKeywordsProvider

// GetTerms gets the terms of a given taxonomy,
Expand Down
4 changes: 4 additions & 0 deletions resources/page/page_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ func (p *nopPage) Scratch() *maps.Scratch {
return nil
}

func (p *nopPage) Store() *maps.Scratch {
return nil
}

func (p *nopPage) RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {
return nil, nil
}
Expand Down
4 changes: 4 additions & 0 deletions resources/page/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ func (p *testPage) Scratch() *maps.Scratch {
panic("not implemented")
}

func (p *testPage) Store() *maps.Scratch {
panic("not implemented")
}

func (p *testPage) RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {
v, err := p.Param(cfg.Name)
if err != nil {
Expand Down

0 comments on commit e97d3c6

Please sign in to comment.