summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-01-15 15:59:56 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-01-22 09:39:49 +0100
commitc6d650c8c8b22fdc7ddedc1e42a3ca698e1390d6 (patch)
tree5a189224439d654a9ef7517b0c702e781874b45a /hugolib
parent8585b388d27abde1ab6b6c63ad6addf4066ec8dd (diff)
tpl/tplimpl: Rework template management to get rid of concurrency issues
This more or less completes the simplification of the template handling code in Hugo started in v0.62. The main motivation was to fix a long lasting issue about a crash in HTML content files without front matter. But this commit also comes with a big functional improvement. As we now have moved the base template evaluation to the build stage we now use the same lookup rules for `baseof` as for `list` etc. type of templates. This means that in this simple example you can have a `baseof` template for the `blog` section without having to duplicate the others: ``` layouts ├── _default │   ├── baseof.html │   ├── list.html │   └── single.html └── blog └── baseof.html ``` Also, when simplifying code, you often get rid of some double work, as shown in the "site building" benchmarks below. These benchmarks looks suspiciously good, but I have repeated the below with ca. the same result. Compared to master: ``` name old time/op new time/op delta SiteNew/Bundle_with_image-16 13.1ms ± 1% 10.5ms ± 1% -19.34% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 13.0ms ± 0% 10.7ms ± 1% -18.05% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 46.4ms ± 2% 43.1ms ± 1% -7.15% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 52.2ms ± 2% 47.8ms ± 1% -8.30% (p=0.029 n=4+4) SiteNew/Deep_content_tree-16 77.9ms ± 1% 70.9ms ± 1% -9.01% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 43.0ms ± 0% 37.2ms ± 1% -13.54% (p=0.029 n=4+4) SiteNew/Page_collections-16 58.2ms ± 1% 52.4ms ± 1% -9.95% (p=0.029 n=4+4) name old alloc/op new alloc/op delta SiteNew/Bundle_with_image-16 3.81MB ± 0% 2.22MB ± 0% -41.70% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 3.60MB ± 0% 2.01MB ± 0% -44.20% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 19.3MB ± 1% 14.1MB ± 0% -26.91% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 70.7MB ± 0% 69.0MB ± 0% -2.40% (p=0.029 n=4+4) SiteNew/Deep_content_tree-16 37.1MB ± 0% 31.2MB ± 0% -15.94% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 17.6MB ± 0% 10.6MB ± 0% -39.92% (p=0.029 n=4+4) SiteNew/Page_collections-16 25.9MB ± 0% 21.2MB ± 0% -17.99% (p=0.029 n=4+4) name old allocs/op new allocs/op delta SiteNew/Bundle_with_image-16 52.3k ± 0% 26.1k ± 0% -50.18% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 52.3k ± 0% 26.1k ± 0% -50.16% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 336k ± 1% 269k ± 0% -19.90% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 422k ± 0% 395k ± 0% -6.43% (p=0.029 n=4+4) SiteNew/Deep_content_tree-16 401k ± 0% 313k ± 0% -21.79% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 247k ± 0% 143k ± 0% -42.17% (p=0.029 n=4+4) SiteNew/Page_collections-16 282k ± 0% 207k ± 0% -26.55% (p=0.029 n=4+4) ``` Fixes #6716 Fixes #6760 Fixes #6768 Fixes #6778
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/alias.go2
-rw-r--r--hugolib/cascade_test.go6
-rw-r--r--hugolib/content_render_hooks_test.go4
-rw-r--r--hugolib/hugo_modules_test.go4
-rw-r--r--hugolib/hugo_sites.go26
-rw-r--r--hugolib/hugo_sites_build.go9
-rw-r--r--hugolib/hugo_sites_build_errors_test.go14
-rw-r--r--hugolib/hugo_sites_rebuild_test.go140
-rw-r--r--hugolib/page.go68
-rw-r--r--hugolib/page__meta.go6
-rw-r--r--hugolib/page__per_output.go7
-rw-r--r--hugolib/page_test.go13
-rw-r--r--hugolib/shortcode.go10
-rw-r--r--hugolib/site.go77
-rw-r--r--hugolib/site_render.go64
-rw-r--r--hugolib/template_test.go172
-rw-r--r--hugolib/testhelpers_test.go5
17 files changed, 456 insertions, 171 deletions
diff --git a/hugolib/alias.go b/hugolib/alias.go
index c80e7d0d2..9eba8b335 100644
--- a/hugolib/alias.go
+++ b/hugolib/alias.go
@@ -81,7 +81,7 @@ func (s *Site) writeDestAlias(path, permalink string, outputFormat output.Format
}
func (s *Site) publishDestAlias(allowRoot bool, path, permalink string, outputFormat output.Format, p page.Page) (err error) {
- handler := newAliasHandler(s.Tmpl, s.Log, allowRoot)
+ handler := newAliasHandler(s.Tmpl(), s.Log, allowRoot)
s.Log.DEBUG.Println("creating alias:", path, "redirecting to", permalink)
diff --git a/hugolib/cascade_test.go b/hugolib/cascade_test.go
index 6b176ad64..be243e39b 100644
--- a/hugolib/cascade_test.go
+++ b/hugolib/cascade_test.go
@@ -68,12 +68,12 @@ func TestCascade(t *testing.T) {
42|taxonomy|tags/blue|blue|home.png|tags|HTML-|
42|section|sect3|Cascade Home|home.png|sect3|HTML-|
42|taxonomyTerm|tags|Cascade Home|home.png|tags|HTML-|
- 42|page|p2.md|Cascade Home|home.png|page|HTML-|
+ 42|page|p2.md|Cascade Home|home.png||HTML-|
42|page|sect2/p2.md|Cascade Home|home.png|sect2|HTML-|
42|page|sect3/p1.md|Cascade Home|home.png|sect3|HTML-|
42|taxonomy|tags/green|green|home.png|tags|HTML-|
- 42|home|_index.md|Home|home.png|page|HTML-|
- 42|page|p1.md|p1|home.png|page|HTML-|
+ 42|home|_index.md|Home|home.png||HTML-|
+ 42|page|p1.md|p1|home.png||HTML-|
42|section|sect1/_index.md|Sect1|sect1.png|stype|HTML-|
42|section|sect1/s1_2/_index.md|Sect1_2|sect1.png|stype|HTML-|
42|page|sect1/s1_2/p1.md|Sect1_2_p1|sect1.png|stype|HTML-|
diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go
index ee7a02074..8aba1dd8c 100644
--- a/hugolib/content_render_hooks_test.go
+++ b/hugolib/content_render_hooks_test.go
@@ -13,7 +13,9 @@
package hugolib
-import "testing"
+import (
+ "testing"
+)
func TestRenderHooks(t *testing.T) {
config := `
diff --git a/hugolib/hugo_modules_test.go b/hugolib/hugo_modules_test.go
index 14085e2c0..5c2b46b30 100644
--- a/hugolib/hugo_modules_test.go
+++ b/hugolib/hugo_modules_test.go
@@ -340,6 +340,8 @@ b = "B param"
}
func TestModulesIncompatible(t *testing.T) {
+ t.Parallel()
+
b := newTestSitesBuilder(t).WithWorkingDir("/site").WithConfigFile("toml", `
baseURL="https://example.org"
@@ -518,6 +520,7 @@ weight = 2
}
func TestMountsProject(t *testing.T) {
+ t.Parallel()
config := `
@@ -547,6 +550,7 @@ title: "My Page"
// https://github.com/gohugoio/hugo/issues/6684
func TestMountsContentFile(t *testing.T) {
+ t.Parallel()
c := qt.New(t)
workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-modules-content-file")
c.Assert(err, qt.IsNil)
diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go
index 4e1623b2e..a0c62f01e 100644
--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -121,6 +121,9 @@ type hugoSitesInit struct {
// Loads the data from all of the /data folders.
data *lazy.Init
+ // Performs late initialization (before render) of the templates.
+ layouts *lazy.Init
+
// Loads the Git info for all the pages if enabled.
gitInfo *lazy.Init
@@ -130,6 +133,7 @@ type hugoSitesInit struct {
func (h *hugoSitesInit) Reset() {
h.data.Reset()
+ h.layouts.Reset()
h.gitInfo.Reset()
h.translations.Reset()
}
@@ -271,6 +275,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
Sites: sites,
init: &hugoSitesInit{
data: lazy.New(),
+ layouts: lazy.New(),
gitInfo: lazy.New(),
translations: lazy.New(),
},
@@ -289,6 +294,15 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, nil
})
+ h.init.layouts.Add(func() (interface{}, error) {
+ for _, s := range h.Sites {
+ if err := s.Tmpl().(tpl.TemplateManager).MarkReady(); err != nil {
+ return nil, err
+ }
+ }
+ return nil, nil
+ })
+
h.init.translations.Add(func() (interface{}, error) {
if len(h.Sites) > 1 {
allTranslations := pagesToTranslationsMap(h.Sites)
@@ -429,10 +443,6 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
func (s *Site) withSiteTemplates(withTemplates ...func(templ tpl.TemplateManager) error) func(templ tpl.TemplateManager) error {
return func(templ tpl.TemplateManager) error {
- if err := templ.LoadTemplates(""); err != nil {
- return err
- }
-
for _, wt := range withTemplates {
if wt == nil {
continue
@@ -619,10 +629,10 @@ func (h *HugoSites) renderCrossSitesArtifacts() error {
s := h.Sites[0]
- smLayouts := []string{"sitemapindex.xml", "_default/sitemapindex.xml", "_internal/_default/sitemapindex.xml"}
+ templ := s.lookupLayouts("sitemapindex.xml", "_default/sitemapindex.xml", "_internal/_default/sitemapindex.xml")
return s.renderAndWriteXML(&s.PathSpec.ProcessingStats.Sitemaps, "sitemapindex",
- s.siteCfg.sitemap.Filename, h.toSiteInfos(), smLayouts...)
+ s.siteCfg.sitemap.Filename, h.toSiteInfos(), templ)
}
func (h *HugoSites) removePageByFilename(filename string) {
@@ -832,7 +842,7 @@ func (h *HugoSites) resetPageStateFromEvents(idset identity.Identities) {
if po.cp == nil {
continue
}
- for id, _ := range idset {
+ for id := range idset {
if po.cp.dependencyTracker.Search(id) != nil {
po.cp.Reset()
continue OUTPUTS
@@ -841,7 +851,7 @@ func (h *HugoSites) resetPageStateFromEvents(idset identity.Identities) {
}
for _, s := range p.shortcodeState.shortcodes {
- for id, _ := range idset {
+ for id := range idset {
if idm, ok := s.info.(identity.Manager); ok && idm.Search(id) != nil {
for _, po := range p.pageOutputs {
if po.cp != nil {
diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go
index d749ff581..901941bda 100644
--- a/hugolib/hugo_sites_build.go
+++ b/hugolib/hugo_sites_build.go
@@ -291,6 +291,10 @@ func (h *HugoSites) assemble(bcfg *BuildCfg) error {
}
func (h *HugoSites) render(config *BuildCfg) error {
+ if _, err := h.init.layouts.Do(); err != nil {
+ return err
+ }
+
siteRenderContext := &siteRenderContext{cfg: config, multihost: h.multihost}
if !config.PartialReRender {
@@ -312,11 +316,6 @@ func (h *HugoSites) render(config *BuildCfg) error {
case <-h.Done():
return nil
default:
- // For the non-renderable pages, we use the content iself as
- // template and we may have to re-parse and execute it for
- // each output format.
- h.TemplateHandler().RebuildClone()
-
for _, s2 := range h.Sites {
// We render site by site, but since the content is lazily rendered
// and a site can "borrow" content from other sites, every site
diff --git a/hugolib/hugo_sites_build_errors_test.go b/hugolib/hugo_sites_build_errors_test.go
index 21b745ccd..d90a8b364 100644
--- a/hugolib/hugo_sites_build_errors_test.go
+++ b/hugolib/hugo_sites_build_errors_test.go
@@ -27,7 +27,7 @@ func (t testSiteBuildErrorAsserter) getFileError(err error) *herrors.ErrorWithFi
func (t testSiteBuildErrorAsserter) assertLineNumber(lineNumber int, err error) {
fe := t.getFileError(err)
- t.c.Assert(fe.Position().LineNumber, qt.Equals, lineNumber)
+ t.c.Assert(fe.Position().LineNumber, qt.Equals, lineNumber, qt.Commentf(err.Error()))
}
func (t testSiteBuildErrorAsserter) assertErrorMessage(e1, e2 string) {
@@ -65,7 +65,8 @@ func TestSiteBuildErrors(t *testing.T) {
fileFixer: func(content string) string {
return strings.Replace(content, ".Title }}", ".Title }", 1)
},
- assertCreateError: func(a testSiteBuildErrorAsserter, err error) {
+ // Base templates gets parsed at build time.
+ assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
a.assertLineNumber(4, err)
},
},
@@ -90,7 +91,7 @@ func TestSiteBuildErrors(t *testing.T) {
a.c.Assert(fe.Position().LineNumber, qt.Equals, 5)
a.c.Assert(fe.Position().ColumnNumber, qt.Equals, 1)
a.c.Assert(fe.ChromaLexer, qt.Equals, "go-html-template")
- a.assertErrorMessage("\"layouts/_default/single.html:5:1\": parse failed: template: _default/single.html:5: unexpected \"}\" in operand", fe.Error())
+ a.assertErrorMessage("\"layouts/foo/single.html:5:1\": parse failed: template: foo/single.html:5: unexpected \"}\" in operand", fe.Error())
},
},
@@ -258,6 +259,13 @@ SINGLE L5: {{ .Title }} {{ .Content }}
{{ end }}
`))
+ b.WithTemplatesAdded("layouts/foo/single.html", f(single, `
+SINGLE L2:
+SINGLE L3:
+SINGLE L4:
+SINGLE L5: {{ .Title }} {{ .Content }}
+`))
+
b.WithContent("myyaml.md", f(yamlcontent, `---
title: "The YAML"
---
diff --git a/hugolib/hugo_sites_rebuild_test.go b/hugolib/hugo_sites_rebuild_test.go
index e36c1a1d4..eca6c730b 100644
--- a/hugolib/hugo_sites_rebuild_test.go
+++ b/hugolib/hugo_sites_rebuild_test.go
@@ -23,23 +23,28 @@ func TestSitesRebuild(t *testing.T) {
baseURL = "https://example.com"
title = "Rebuild this"
contentDir = "content"
+enableInlineShortcodes = true
`
- contentFilename := "content/blog/page1.md"
+ var (
+ contentFilename = "content/blog/page1.md"
+ dataFilename = "data/mydata.toml"
+ )
- b := newTestSitesBuilder(t).WithConfigFile("toml", configFile)
+ createSiteBuilder := func(t testing.TB) *sitesBuilder {
+ b := newTestSitesBuilder(t).WithConfigFile("toml", configFile).Running()
- // To simulate https://github.com/gohugoio/hugo/issues/5838, the home page
- // needs a content page.
- b.WithContent("content/_index.md", `---
+ b.WithSourceFile(dataFilename, `hugo = "Rocks!"`)
+
+ b.WithContent("content/_index.md", `---
title: Home, Sweet Home!
---
`)
- b.WithContent(contentFilename, `
+ b.WithContent(contentFilename, `
---
title: "Page 1"
summary: "Initial summary"
@@ -48,22 +53,58 @@ paginate: 3
Content.
+{{< badge.inline >}}
+Data Inline: {{ site.Data.mydata.hugo }}
+{{< /badge.inline >}}
`)
- b.WithTemplatesAdded("index.html", `
+ // For .Page.Render tests
+ b.WithContent("prender.md", `---
+title: Page 1
+---
+
+Content for Page 1.
+
+{{< dorender >}}
+
+`)
+
+ b.WithTemplatesAdded(
+ "layouts/shortcodes/dorender.html", `
+{{ $p := .Page }}
+Render {{ $p.RelPermalink }}: {{ $p.Render "single" }}
+
+`)
+
+ b.WithTemplatesAdded("index.html", `
{{ range (.Paginate .Site.RegularPages).Pages }}
* Page Paginate: {{ .Title }}|Summary: {{ .Summary }}|Content: {{ .Content }}
{{ end }}
{{ range .Site.RegularPages }}
* Page Pages: {{ .Title }}|Summary: {{ .Summary }}|Content: {{ .Content }}
{{ end }}
+Content: {{ .Content }}
+Data: {{ site.Data.mydata.hugo }}
`)
- b.Running().Build(BuildCfg{})
+ b.WithTemplatesAdded("layouts/partials/mypartial1.html", `Mypartial1`)
+ b.WithTemplatesAdded("layouts/partials/mypartial2.html", `Mypartial2`)
+ b.WithTemplatesAdded("layouts/partials/mypartial3.html", `Mypartial3`)
+ b.WithTemplatesAdded("_default/single.html", `{{ define "main" }}Single Main: {{ .Title }}|Mypartial1: {{ partial "mypartial1.html" }}{{ end }}`)
+ b.WithTemplatesAdded("_default/list.html", `{{ define "main" }}List Main: {{ .Title }}{{ end }}`)
+ b.WithTemplatesAdded("_default/baseof.html", `Baseof:{{ block "main" . }}Baseof Main{{ end }}|Mypartial3: {{ partial "mypartial3.html" }}:END`)
+
+ return b
+ }
- b.AssertFileContent("public/index.html", "* Page Paginate: Page 1|Summary: Initial summary|Content: <p>Content.</p>")
+ t.Run("Refresh paginator on edit", func(t *testing.T) {
+ b := createSiteBuilder(t)
- b.EditFiles(contentFilename, `
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", "* Page Paginate: Page 1|Summary: Initial summary|Content: <p>Content.</p>")
+
+ b.EditFiles(contentFilename, `
---
title: "Page 1 edit"
summary: "Edited summary"
@@ -73,11 +114,82 @@ Edited content.
`)
- b.Build(BuildCfg{})
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", "* Page Paginate: Page 1 edit|Summary: Edited summary|Content: <p>Edited content.</p>")
+ // https://github.com/gohugoio/hugo/issues/5833
+ b.AssertFileContent("public/index.html", "* Page Pages: Page 1 edit|Summary: Edited summary|Content: <p>Edited content.</p>")
+ })
+
+ // https://github.com/gohugoio/hugo/issues/6768
+ t.Run("Edit data", func(t *testing.T) {
+ b := createSiteBuilder(t)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", `
+Data: Rocks!
+Data Inline: Rocks!
+`)
+
+ b.EditFiles(dataFilename, `hugo = "Rules!"`)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", `
+Data: Rules!
+Data Inline: Rules!`)
+
+ })
+
+ t.Run("Page.Render, edit baseof", func(t *testing.T) {
+ b := createSiteBuilder(t)
+
+ b.WithTemplatesAdded("index.html", `
+{{ $p := site.GetPage "prender.md" }}
+prender: {{ $p.Title }}|{{ $p.Content }}
+
+`)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", `
+ Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3:END
+`)
+
+ b.EditFiles("layouts/_default/baseof.html", `Baseof Edited:{{ block "main" . }}Baseof Main{{ end }}:END`)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", `
+Render /prender/: Baseof Edited:Single Main: Page 1|Mypartial1: Mypartial1:END
+`)
+
+ })
+
+ t.Run("Page.Render, edit partial in baseof", func(t *testing.T) {
+ b := createSiteBuilder(t)
+
+ b.WithTemplatesAdded("index.html", `
+{{ $p := site.GetPage "prender.md" }}
+prender: {{ $p.Title }}|{{ $p.Content }}
+
+`)
+
+ b.Build(BuildCfg{})
- b.AssertFileContent("public/index.html", "* Page Paginate: Page 1 edit|Summary: Edited summary|Content: <p>Edited content.</p>")
+ b.AssertFileContent("public/index.html", `
+ Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3:END
+`)
+
+ b.EditFiles("layouts/partials/mypartial3.html", `Mypartial3 Edited`)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", `
+Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3 Edited:END
+`)
- // https://github.com/gohugoio/hugo/issues/5833
- b.AssertFileContent("public/index.html", "* Page Pages: Page 1 edit|Summary: Edited summary|Content: <p>Edited content.</p>")
+ })
}
diff --git a/hugolib/page.go b/hugolib/page.go
index 8aad8dcc7..bb5107747 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -337,37 +337,33 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Render, error) {
layoutDescriptor.Layout = ""
layoutDescriptor.Kind = "render-link"
- linkLayouts, err := p.s.layoutHandler.For(layoutDescriptor, f)
+ linkTempl, linkTemplFound, err := p.s.Tmpl().LookupLayout(layoutDescriptor, f)
if err != nil {
return nil, err
}
layoutDescriptor.Kind = "render-image"
- imageLayouts, err := p.s.layoutHandler.For(layoutDescriptor, f)
+ imgTempl, imgTemplFound, err := p.s.Tmpl().LookupLayout(layoutDescriptor, f)
if err != nil {
return nil, err
}
- if linkLayouts == nil && imageLayouts == nil {
- return nil, nil
- }
-
var linkRenderer hooks.LinkRenderer
var imageRenderer hooks.LinkRenderer
- if templ, found := p.s.lookupTemplate(linkLayouts...); found {
+ if linkTemplFound {
linkRenderer = contentLinkRenderer{
- templateHandler: p.s.Tmpl,
- Provider: templ.(tpl.Info),
- templ: templ,
+ templateHandler: p.s.Tmpl(),
+ Provider: linkTempl.(tpl.Info),
+ templ: linkTempl,
}
}
- if templ, found := p.s.lookupTemplate(imageLayouts...); found {
+ if imgTemplFound {
imageRenderer = contentLinkRenderer{
- templateHandler: p.s.Tmpl,
- Provider: templ.(tpl.Info),
- templ: templ,
+ templateHandler: p.s.Tmpl(),
+ Provider: imgTempl.(tpl.Info),
+ templ: imgTempl,
}
}
@@ -406,24 +402,25 @@ func (p *pageState) getLayoutDescriptor() output.LayoutDescriptor {
}
-func (p *pageState) getLayouts(layouts ...string) ([]string, error) {
+func (p *pageState) resolveTemplate(layouts ...string) (tpl.Template, bool, error) {
f := p.outputFormat()
if len(layouts) == 0 {
selfLayout := p.selfLayoutForOutput(f)
if selfLayout != "" {
- return []string{selfLayout}, nil
+ templ, found := p.s.Tmpl().Lookup(selfLayout)
+ return templ, found, nil
}
}
- layoutDescriptor := p.getLayoutDescriptor()
+ d := p.getLayoutDescriptor()
if len(layouts) > 0 {
- layoutDescriptor.Layout = layouts[0]
- layoutDescriptor.LayoutOverride = true
+ d.Layout = layouts[0]
+ d.LayoutOverride = true
}
- return p.s.layoutHandler.For(layoutDescriptor, f)
+ return p.s.Tmpl().LookupLayout(d, f)
}
// This is serialized
@@ -601,31 +598,21 @@ func (p *pageState) RenderWithTemplateInfo(info tpl.Info, layout ...string) (tem
}
func (p *pageState) Render(layout ...string) (template.HTML, error) {
- l, err := p.getLayouts(layout...)
+ templ, found, err := p.resolveTemplate(layout...)
if err != nil {
- return "", p.wrapError(errors.Errorf("failed to resolve layout %v", layout))
+ return "", p.wrapError(err)
}
- for _, layout := range l {
- templ, found := p.s.Tmpl.Lookup(layout)
- if !found {
- // This is legacy from when we had only one output format and
- // HTML templates only. Some have references to layouts without suffix.
- // We default to good old HTML.
- templ, _ = p.s.Tmpl.Lookup(layout + ".html")
- }
-
- if templ != nil {
- p.addDependency(templ.(tpl.Info))
- res, err := executeToString(p.s.Tmpl, templ, p)
- if err != nil {
- return "", p.wrapError(errors.Wrapf(err, "failed to execute template %q v", layout))
- }
- return template.HTML(res), nil
- }
+ if !found {
+ return "", nil
}
- return "", nil
+ p.addDependency(templ.(tpl.Info))
+ res, err := executeToString(p.s.Tmpl(), templ, p)
+ if err != nil {
+ return "", p.wrapError(errors.Wrapf(err, "failed to execute template %q v", layout))
+ }
+ return template.HTML(res), nil
}
@@ -689,6 +676,7 @@ Loop:
// This is HTML without front matter. It can still have shortcodes.
p.selfLayout = "__" + p.File().Filename()
p.renderable = false
+ p.s.BuildFlags.HasLateTemplate.CAS(false, true)
rn.AddBytes(it)
case it.IsFrontMatter():
f := metadecoders.FormatFromFrontMatterType(it.Type)
diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go
index 9f3e1687a..caffbe736 100644
--- a/hugolib/page__meta.go
+++ b/hugolib/page__meta.go
@@ -296,11 +296,7 @@ func (p *pageMeta) Type() string {
return p.contentType
}
- if x := p.Section(); x != "" {
- return x
- }
-
- return "page"
+ return p.Section()
}
func (p *pageMeta) Weight() int {
diff --git a/hugolib/page__per_output.go b/hugolib/page__per_output.go
index 03448ba80..59440c7cb 100644
--- a/hugolib/page__per_output.go
+++ b/hugolib/page__per_output.go
@@ -189,9 +189,10 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
html := cp.p.s.ContentSpec.TrimShortHTML(b.Bytes())
cp.summary = helpers.BytesToHTML(html)
}
- }
- cp.content = helpers.BytesToHTML(cp.workContent)
+ cp.content = helpers.BytesToHTML(cp.workContent)
+
+ }
if !p.renderable {
err := cp.addSelfTemplate()
@@ -427,7 +428,7 @@ func (p *pageContentOutput) setWordCounts(isCJKLanguage bool) {
func (p *pageContentOutput) addSelfTemplate() error {
self := p.p.selfLayoutForOutput(p.f)
- err := p.p.s.TemplateHandler().AddLateTemplate(self, string(p.content))
+ err := p.p.s.Tmpl().(tpl.TemplateManager).AddLateTemplate(self, string(p.workContent))
if err != nil {
return err
}
diff --git a/hugolib/page_test.go b/hugolib/page_test.go
index 9bcfc1fc8..d7cbc0fca 100644
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -333,12 +333,6 @@ func checkPageSummary(t *testing.T, page page.Page, summary string, msg ...inter
}
}
-func checkPageType(t *testing.T, page page.Page, pageType string) {
- if page.Type() != pageType {
- t.Fatalf("Page type is: %s. Expected: %s", page.Type(), pageType)
- }
-}
-
func checkPageDate(t *testing.T, page page.Page, time time.Time) {
if page.Date() != time {
t.Fatalf("Page date is: %s. Expected: %s", page.Date(), time)
@@ -542,7 +536,6 @@ func TestCreateNewPage(t *testing.T) {
checkPageTitle(t, p, "Simple")
checkPageContent(t, p, normalizeExpected(ext, "<p>Simple Page</p>\n"))
checkPageSummary(t, p, "Simple Page")
- checkPageType(t, p, "page")
}
settings := map[string]interface{}{
@@ -562,7 +555,6 @@ func TestPageSummary(t *testing.T) {
checkPageContent(t, p, normalizeExpected(ext, "<p><a href=\"https://lipsum.com/\">Lorem ipsum</a> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n\n<p>Additional text.</p>\n\n<p>Further text.</p>\n"), ext)
checkPageSummary(t, p, normalizeExpected(ext, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Additional text."), ext)
}
- checkPageType(t, p, "page")
}
testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithoutSummaryDelimiter)
@@ -575,7 +567,6 @@ func TestPageWithDelimiter(t *testing.T) {
checkPageTitle(t, p, "Simple")
checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n"), ext)
checkPageSummary(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>"), ext)
- checkPageType(t, p, "page")
}
testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiter)
@@ -591,7 +582,6 @@ func TestPageWithSummaryParameter(t *testing.T) {
if ext != "ad" && ext != "rst" {
checkPageSummary(t, p, normalizeExpected(ext, "Page with summary parameter and <a href=\"http://www.example.com/\">a link</a>"), ext)
}
- checkPageType(t, p, "page")
}
testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryParameter)
@@ -663,7 +653,6 @@ func TestPageWithShortCodeInSummary(t *testing.T) {
checkPageTitle(t, p, "Simple")
checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Next Line. <figure> <img src=\"/not/real\"/> </figure> . More text here.</p><p>Some more text</p>"))
checkPageSummary(t, p, "Summary Next Line. . More text here. Some more text")
- checkPageType(t, p, "page")