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 languageInit sync.Once
pageMenusInit sync.Once pageMenusInit sync.Once
pageMetaInit sync.Once pageMetaInit sync.Once
pageOutputInit sync.Once
plainInit sync.Once plainInit sync.Once
plainWordsInit sync.Once plainWordsInit sync.Once
renderingConfigInit sync.Once renderingConfigInit sync.Once

View file

@ -108,6 +108,21 @@ func (p *PageOutput) Render(layout ...string) template.HTML {
// TODO(bep) output // TODO(bep) output
func (p *Page) Render(layout ...string) template.HTML { 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...) 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) { func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
var mainPageOutput *PageOutput
for page := range pages { for page := range pages {
for i, outFormat := range page.outputFormats { 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 { if err != nil {
s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outFormat.Name, page, err) s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outFormat.Name, page, err)
continue continue
} }
if i == 0 {
mainPageOutput = pageOutput
}
page.mainPageOutput = mainPageOutput
var layouts []string var layouts []string

View file

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