Fix panic on no output formats

A page needs its output formats even if it should not be rendered or its resources should not be published.

Fixes #6924
This commit is contained in:
Bjørn Erik Pedersen 2020-02-21 09:02:07 +01:00
parent 4c2a0de412
commit f460530347
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
5 changed files with 84 additions and 47 deletions

View file

@ -177,10 +177,13 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB
outputFormatsForPage := ps.m.outputFormats()
if !ps.m.noRender() {
// Prepare output formats for all sites.
// We do this even if this page does not get rendered on
// its own. It may be referenced via .Site.GetPage and
// it will then need an output format.
ps.pageOutputs = make([]*pageOutput, len(ps.s.h.renderFormats))
created := make(map[string]*pageOutput)
shouldRenderPage := !ps.m.noRender()
for i, f := range ps.s.h.renderFormats {
if po, found := created[f.Name]; found {
@ -188,7 +191,11 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB
continue
}
_, render := outputFormatsForPage.GetByName(f.Name)
render := shouldRenderPage
if render {
_, render = outputFormatsForPage.GetByName(f.Name)
}
po := newPageOutput(ps, pp, f, render)
// Create a content provider for the first,
@ -203,16 +210,7 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB
ps.pageOutputs[i] = po
created[f.Name] = po
}
} else if ps.m.buildConfig.PublishResources {
// We need one output format for potential resources to publish.
po := newPageOutput(ps, pp, outputFormatsForPage[0], false)
contentProvider, err := newPageContentOutput(ps, po)
if err != nil {
return nil, err
}
po.initContentProvider(contentProvider)
ps.pageOutputs = []*pageOutput{po}
}
if err := ps.initCommonProviders(pp); err != nil {

View file

@ -311,3 +311,37 @@ _build:
b.Assert(b.CheckExists("public/section/bundle-false/data2.json"), qt.Equals, false)
b.AssertFileContent("public/section/bundle-true/data3.json", `Some data 3`)
}
func TestNoRenderAndNoPublishResources(t *testing.T) {
noRenderPage := `
---
title: %s
_build:
render: false
publishResources: false
---
`
b := newTestSitesBuilder(t)
b.WithTemplatesAdded("index.html", `
{{ $page := site.GetPage "sect/no-render" }}
{{ $sect := site.GetPage "sect-no-render" }}
Page: {{ $page.Title }}|RelPermalink: {{ $page.RelPermalink }}|Outputs: {{ len $page.OutputFormats }}
Section: {{ $sect.Title }}|RelPermalink: {{ $sect.RelPermalink }}|Outputs: {{ len $sect.OutputFormats }}
`)
b.WithContent("sect-no-render/_index.md", fmt.Sprintf(noRenderPage, "MySection"))
b.WithContent("sect/no-render.md", fmt.Sprintf(noRenderPage, "MyPage"))
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Page: MyPage|RelPermalink: |Outputs: 0
Section: MySection|RelPermalink: |Outputs: 0
`)
b.Assert(b.CheckExists("public/sect/no-render/index.html"), qt.Equals, false)
b.Assert(b.CheckExists("public/sect-no-render/index.html"), qt.Equals, false)
}

View file

@ -723,6 +723,7 @@ Loop:
if err := meta.setMetadata(bucket, p, m); err != nil {
return err
}
frontMatterSet = true
next := iter.Peek()

View file

@ -137,27 +137,31 @@ func newPageFromMeta(
return newPageOutput(ps, pp, f, render)
}
shouldRenderPage := !ps.m.noRender()
if ps.m.standalone {
ps.pageOutput = makeOut(ps.m.outputFormats()[0], !ps.m.noRender())
ps.pageOutput = makeOut(ps.m.outputFormats()[0], shouldRenderPage)
} else {
outputFormatsForPage := ps.m.outputFormats()
if !ps.m.noRender() {
// Prepare output formats for all sites.
// We do this even if this page does not get rendered on
// its own. It may be referenced via .Site.GetPage and
// it will then need an output format.
ps.pageOutputs = make([]*pageOutput, len(ps.s.h.renderFormats))
created := make(map[string]*pageOutput)
for i, f := range ps.s.h.renderFormats {
po, found := created[f.Name]
if !found {
_, shouldRender := outputFormatsForPage.GetByName(f.Name)
po = makeOut(f, shouldRender)
render := shouldRenderPage
if render {
_, render = outputFormatsForPage.GetByName(f.Name)
}
po = makeOut(f, render)
created[f.Name] = po
}
ps.pageOutputs[i] = po
}
} else {
// We need one output format for potential resources to publish.
ps.pageOutputs = []*pageOutput{makeOut(outputFormatsForPage[0], false)}
}
}
if err := ps.initCommonProviders(pp); err != nil {