Make the RenderString content provider fix more general

Updates #9383
This commit is contained in:
Bjørn Erik Pedersen
2022-01-27 09:46:51 +01:00
parent 85d31f7bfb
commit f22c4aba04
7 changed files with 178 additions and 167 deletions

View File

@@ -19,6 +19,16 @@ import (
"github.com/gohugoio/hugo/lazy"
)
// OutputFormatContentProvider represents the method set that is "outputFormat aware" and that we
// provide lazy initialization for in case they get invoked outside of their normal rendering context, e.g. via .Translations.
// Note that this set is currently not complete, but should cover the most common use cases.
// For the others, the implementation will be from the page.NoopPage.
type OutputFormatContentProvider interface {
ContentProvider
TableOfContentsProvider
PageRenderProvider
}
// LazyContentProvider initializes itself when read. Each method of the
// ContentProvider interface initializes a content provider and shares it
// with other methods.
@@ -27,13 +37,13 @@ import (
// will be needed. Must create via NewLazyContentProvider.
type LazyContentProvider struct {
init *lazy.Init
cp ContentProvider
cp OutputFormatContentProvider
}
// NewLazyContentProvider returns a LazyContentProvider initialized with
// function f. The resulting LazyContentProvider calls f in order to
// retrieve a ContentProvider
func NewLazyContentProvider(f func() (ContentProvider, error)) *LazyContentProvider {
func NewLazyContentProvider(f func() (OutputFormatContentProvider, error)) *LazyContentProvider {
lcp := LazyContentProvider{
init: lazy.New(),
cp: NopPage,
@@ -49,11 +59,6 @@ func NewLazyContentProvider(f func() (ContentProvider, error)) *LazyContentProvi
return &lcp
}
func (lcp *LazyContentProvider) Init() ContentProvider {
lcp.init.Do()
return lcp.cp
}
func (lcp *LazyContentProvider) Reset() {
lcp.init.Reset()
}
@@ -102,3 +107,18 @@ func (lcp *LazyContentProvider) Len() int {
lcp.init.Do()
return lcp.cp.Len()
}
func (lcp *LazyContentProvider) Render(layout ...string) (template.HTML, error) {
lcp.init.Do()
return lcp.cp.Render(layout...)
}
func (lcp *LazyContentProvider) RenderString(args ...interface{}) (template.HTML, error) {
lcp.init.Do()
return lcp.cp.RenderString(args...)
}
func (lcp *LazyContentProvider) TableOfContents() template.HTML {
lcp.init.Do()
return lcp.cp.TableOfContents()
}