From 85d31f7bfb7a13c9ab7655829a315a820dc1b403 Mon Sep 17 00:00:00 2001 From: Paul Gottschling Date: Fri, 14 Jan 2022 16:08:11 -0500 Subject: [PATCH] Fix .RenderString issue in .Translations Fixes #9383 --- hugolib/page.go | 14 ++++- hugolib/page_test.go | 63 +++++++++++++++++++++ resources/page/page_lazy_contentprovider.go | 5 ++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/hugolib/page.go b/hugolib/page.go index 286d21075..d5055e7c1 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -635,7 +635,19 @@ func (p *pageState) RenderString(args ...interface{}) (template.HTML, error) { } } - c, err := p.pageOutput.cp.renderContentWithConverter(conv, []byte(s), false) + var cp *pageContentOutput + + // If the current content provider is not yet initialized, do so now. + if lcp, ok := p.pageOutput.ContentProvider.(*page.LazyContentProvider); ok { + c := lcp.Init() + if pco, ok := c.(*pageContentOutput); ok { + cp = pco + } + } else { + cp = p.pageOutput.cp + } + + c, err := cp.renderContentWithConverter(conv, []byte(s), false) if err != nil { return "", p.wrapError(err) } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index fc01bbf25..48a81ee4a 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -768,6 +768,69 @@ Here is the last report for commits in the year 2016. It covers hrev50718-hrev50 `) } +// Issue 9383 +func TestRenderStringForRegularPageTranslations(t *testing.T) { + c := qt.New(t) + b := newTestSitesBuilder(t) + b.WithLogger(loggers.NewBasicLoggerForWriter(jwalterweatherman.LevelDebug, os.Stderr)) + + b.WithConfigFile("toml", + `baseurl = "https://example.org/" +title = "My Site" + +defaultContentLanguage = "ru" +defaultContentLanguageInSubdir = true + +[languages.ru] +contentDir = 'content/ru' +weight = 1 + +[languages.en] +weight = 2 +contentDir = 'content/en' + +[outputs] +home = ["HTML", "JSON"]`) + + b.WithTemplates("index.html", ` +{{- range .Site.Home.Translations -}} +

{{- .RenderString "foo" -}}

+{{- end -}} +{{- range .Site.Home.AllTranslations -}} +

{{- .RenderString "bar" -}}

+{{- end -}} +`, "_default/single.html", + `{{ .Content }}`, + "index.json", + `{"Title": "My Site"}`, + ) + + b.WithContent( + "ru/a.md", + "", + "en/a.md", + "", + ) + + err := b.BuildE(BuildCfg{}) + c.Assert(err, qt.Equals, nil) + + b.AssertFileContent("public/ru/index.html", ` +

foo

+

foo

+

bar

+

bar

+`) + + b.AssertFileContent("public/en/index.html", ` +

foo

+

foo

+

bar

+

bar

+`) + +} + // Issue 8919 func TestContentProviderWithCustomOutputFormat(t *testing.T) { b := newTestSitesBuilder(t) diff --git a/resources/page/page_lazy_contentprovider.go b/resources/page/page_lazy_contentprovider.go index a513a063a..9979856f8 100644 --- a/resources/page/page_lazy_contentprovider.go +++ b/resources/page/page_lazy_contentprovider.go @@ -49,6 +49,11 @@ 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() }