hugolib: Wrap pageOutput create in sync.Once

This commit is contained in:
Bjørn Erik Pedersen 2017-03-26 11:45:12 +02:00
parent 0aaf3c56a5
commit 9a0aa5fdbe
4 changed files with 32 additions and 6 deletions

View file

@ -244,6 +244,7 @@ type pageInit struct {
languageInit sync.Once
pageMenusInit sync.Once
pageMetaInit sync.Once
pageOutputInit sync.Once
plainInit sync.Once
plainWordsInit sync.Once
renderingConfigInit sync.Once

View file

@ -108,6 +108,21 @@ func (p *PageOutput) Render(layout ...string) template.HTML {
// TODO(bep) output
func (p *Page) Render(layout ...string) template.HTML {
p.pageOutputInit.Do(func() {
// If Render is called in a range loop, the page output isn't available.
// So, create one.
outFormat := p.outputFormats[0]
pageOutput, err := newPageOutput(p, true, outFormat)
if err != nil {
p.s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outFormat.Name, p, err)
return
}
p.mainPageOutput = pageOutput
})
return p.mainPageOutput.Render(layout...)
}

View file

@ -60,22 +60,31 @@ func (s *Site) renderPages() error {
func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.WaitGroup) {
defer wg.Done()
var mainPageOutput *PageOutput
for page := range pages {
for i, outFormat := range page.outputFormats {
pageOutput, err := newPageOutput(page, i > 0, outFormat)
var (
pageOutput *PageOutput
err error
)
if i == 0 {
page.pageOutputInit.Do(func() {
var po *PageOutput
po, err = newPageOutput(page, false, outFormat)
page.mainPageOutput = po
})
pageOutput = page.mainPageOutput
} else {
pageOutput, err = newPageOutput(page, true, outFormat)
}
if err != nil {
s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outFormat.Name, page, err)
continue
}
if i == 0 {
mainPageOutput = pageOutput
}
page.mainPageOutput = mainPageOutput
var layouts []string

View file

@ -180,6 +180,7 @@ func (t *GoHTMLTemplate) executeTemplate(context interface{}, w io.Writer, layou
for _, layout := range layouts {
templ := t.Lookup(layout)
if templ == nil {
// TODO(bep) output
layout += ".html"
templ = t.Lookup(layout)
}