Alias Page.Scratch to Page.Store (note)

Fixes #13016
This commit is contained in:
Bjørn Erik Pedersen 2024-11-05 16:32:57 +01:00
parent df8bd4af4f
commit 2c3efc8106
6 changed files with 35 additions and 24 deletions

View file

@ -28,25 +28,6 @@ type Scratch struct {
mu sync.RWMutex mu sync.RWMutex
} }
// Scratcher provides a scratching service.
type Scratcher interface {
// Scratch returns a "scratch pad" that can be used to store state.
Scratch() *Scratch
}
type scratcher struct {
s *Scratch
}
func (s scratcher) Scratch() *Scratch {
return s.s
}
// NewScratcher creates a new Scratcher.
func NewScratcher() Scratcher {
return scratcher{s: NewScratch()}
}
// Add will, for single values, add (using the + operator) the addend to the existing addend (if found). // Add will, for single values, add (using the + operator) the addend to the existing addend (if found).
// Supports numeric values and strings. // Supports numeric values and strings.
// //

View file

@ -38,7 +38,6 @@ import (
"github.com/gohugoio/hugo/tpl" "github.com/gohugoio/hugo/tpl"
"github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/types" "github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/source" "github.com/gohugoio/hugo/source"
@ -149,7 +148,7 @@ func (p *pageState) Key() string {
} }
func (p *pageState) resetBuildState() { func (p *pageState) resetBuildState() {
p.Scratcher = maps.NewScratcher() // Nothing to do for now.
} }
func (p *pageState) reusePageOutputContent() bool { func (p *pageState) reusePageOutputContent() bool {

View file

@ -56,7 +56,6 @@ type pageCommon struct {
store *maps.Scratch store *maps.Scratch
// All of these represents the common parts of a page.Page // All of these represents the common parts of a page.Page
maps.Scratcher
navigation.PageMenusProvider navigation.PageMenusProvider
page.AuthorProvider page.AuthorProvider
page.AlternativeOutputFormatsProvider page.AlternativeOutputFormatsProvider
@ -113,3 +112,8 @@ type pageCommon struct {
func (p *pageCommon) Store() *maps.Scratch { func (p *pageCommon) Store() *maps.Scratch {
return p.store return p.store
} }
// See issue 13016.
func (p *pageCommon) Scratch() *maps.Scratch {
return p.Store()
}

View file

@ -184,7 +184,6 @@ func (h *HugoSites) doNewPage(m *pageMeta) (*pageState, *paths.Path, error) {
pageCommon: &pageCommon{ pageCommon: &pageCommon{
FileProvider: m, FileProvider: m,
AuthorProvider: m, AuthorProvider: m,
Scratcher: maps.NewScratcher(),
store: maps.NewScratch(), store: maps.NewScratch(),
Positioner: page.NopPage, Positioner: page.NopPage,
InSectionPositioner: page.NopPage, InSectionPositioner: page.NopPage,

View file

@ -1688,6 +1688,32 @@ title: Scratch Me!
b.AssertFileContent("public/scratchme/index.html", "C: cv") b.AssertFileContent("public/scratchme/index.html", "C: cv")
} }
// Issue 13016.
func TestScratchAliasToStore(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ["taxonomy", "term", "page", "section"]
disableLiveReload = true
-- layouts/index.html --
{{ .Scratch.Set "a" "b" }}
{{ .Store.Set "c" "d" }}
.Scratch eq .Store: {{ eq .Scratch .Store }}
a: {{ .Store.Get "a" }}
c: {{ .Scratch.Get "c" }}
`
b := Test(t, files)
b.AssertFileContent("public/index.html",
".Scratch eq .Store: true",
"a: b",
"c: d",
)
}
func TestPageParam(t *testing.T) { func TestPageParam(t *testing.T) {
t.Parallel() t.Parallel()

View file

@ -327,7 +327,9 @@ type PageWithoutContent interface {
// Scratch returns a Scratch that can be used to store temporary state. // 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. // Note that this Scratch gets reset on server rebuilds. See Store() for a variant that survives.
maps.Scratcher // Scratch returns a "scratch pad" that can be used to store state.
// Deprecated: From Hugo v0.138.0 this is just an alias for Store.
Scratch() *maps.Scratch
// Store returns a Scratch that can be used to store temporary state. // Store returns a Scratch that can be used to store temporary state.
// In contrast to Scratch(), this Scratch is not reset on server rebuilds. // In contrast to Scratch(), this Scratch is not reset on server rebuilds.