Skip to content

Commit 1e4d082

Browse files
committed
hubolib: Refactor site rendering with an "output format context"
Fixes #3397
1 parent 1d70aa9 commit 1e4d082

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

‎hugolib/site.go‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/url"
2323
"os"
2424
"path/filepath"
25+
"sort"
2526
"strconv"
2627
"strings"
2728
"sync"
@@ -118,12 +119,42 @@ type Site struct {
118119
outputFormatsConfig output.Formats
119120
mediaTypesConfig media.Types
120121

122+
// We render each site for all the relevant output formats in serial with
123+
// this rendering context pointing to the current one.
124+
rc *siteRenderingContext
125+
126+
// The output formats that we need to render this site in. This slice
127+
// will be fixed once set.
128+
// This will be the union of Site.Pages' outputFormats.
129+
// This slice will be sorted.
130+
renderFormats output.Formats
131+
121132
// Logger etc.
122133
*deps.Deps `json:"-"`
123134

124135
siteStats *siteStats
125136
}
126137

138+
type siteRenderingContext struct {
139+
output.Format
140+
}
141+
142+
func (s *Site) initRenderFormats() {
143+
formatSet := make(map[string]bool)
144+
formats := output.Formats{}
145+
for _, p := range s.Pages {
146+
for _, f := range p.outputFormats {
147+
if !formatSet[f.Name] {
148+
formats = append(formats, f)
149+
formatSet[f.Name] = true
150+
}
151+
}
152+
}
153+
154+
sort.Sort(formats)
155+
s.renderFormats = formats
156+
}
157+
127158
type siteStats struct {
128159
pageCount int
129160
pageCountRegular int
@@ -971,8 +1002,13 @@ func (s *Site) render() (err error) {
9711002
}
9721003
s.timerStep("render and write aliases")
9731004

974-
if err = s.renderPages(); err != nil {
975-
return
1005+
// TODO(bep) render consider this, ref. render404 etc.
1006+
s.initRenderFormats()
1007+
for _, rf := range s.renderFormats {
1008+
s.rc = &siteRenderingContext{Format: rf}
1009+
if err = s.renderPages(); err != nil {
1010+
return
1011+
}
9761012
}
9771013
s.timerStep("render and write pages")
9781014

‎hugolib/site_render.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa
8181
pageOutput, err = page.mainPageOutput.copyWithFormat(outFormat)
8282
}
8383

84+
if outFormat != page.s.rc.Format {
85+
// Will be rendered ... later.
86+
continue
87+
}
88+
8489
if err != nil {
8590
s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outFormat.Name, page, err)
8691
continue

0 commit comments

Comments
 (0)