mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-20 01:24:37 +00:00
hubolib: Refactor site rendering with an "output format context"
Fixes #3397
This commit is contained in:
parent
1d70aa9826
commit
1e4d082cf5
2 changed files with 43 additions and 2 deletions
|
@ -22,6 +22,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -118,12 +119,42 @@ type Site struct {
|
||||||
outputFormatsConfig output.Formats
|
outputFormatsConfig output.Formats
|
||||||
mediaTypesConfig media.Types
|
mediaTypesConfig media.Types
|
||||||
|
|
||||||
|
// We render each site for all the relevant output formats in serial with
|
||||||
|
// this rendering context pointing to the current one.
|
||||||
|
rc *siteRenderingContext
|
||||||
|
|
||||||
|
// The output formats that we need to render this site in. This slice
|
||||||
|
// will be fixed once set.
|
||||||
|
// This will be the union of Site.Pages' outputFormats.
|
||||||
|
// This slice will be sorted.
|
||||||
|
renderFormats output.Formats
|
||||||
|
|
||||||
// Logger etc.
|
// Logger etc.
|
||||||
*deps.Deps `json:"-"`
|
*deps.Deps `json:"-"`
|
||||||
|
|
||||||
siteStats *siteStats
|
siteStats *siteStats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type siteRenderingContext struct {
|
||||||
|
output.Format
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Site) initRenderFormats() {
|
||||||
|
formatSet := make(map[string]bool)
|
||||||
|
formats := output.Formats{}
|
||||||
|
for _, p := range s.Pages {
|
||||||
|
for _, f := range p.outputFormats {
|
||||||
|
if !formatSet[f.Name] {
|
||||||
|
formats = append(formats, f)
|
||||||
|
formatSet[f.Name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(formats)
|
||||||
|
s.renderFormats = formats
|
||||||
|
}
|
||||||
|
|
||||||
type siteStats struct {
|
type siteStats struct {
|
||||||
pageCount int
|
pageCount int
|
||||||
pageCountRegular int
|
pageCountRegular int
|
||||||
|
@ -971,9 +1002,14 @@ func (s *Site) render() (err error) {
|
||||||
}
|
}
|
||||||
s.timerStep("render and write aliases")
|
s.timerStep("render and write aliases")
|
||||||
|
|
||||||
|
// TODO(bep) render consider this, ref. render404 etc.
|
||||||
|
s.initRenderFormats()
|
||||||
|
for _, rf := range s.renderFormats {
|
||||||
|
s.rc = &siteRenderingContext{Format: rf}
|
||||||
if err = s.renderPages(); err != nil {
|
if err = s.renderPages(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
s.timerStep("render and write pages")
|
s.timerStep("render and write pages")
|
||||||
|
|
||||||
if err = s.renderSitemap(); err != nil {
|
if err = s.renderSitemap(); err != nil {
|
||||||
|
|
|
@ -81,6 +81,11 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa
|
||||||
pageOutput, err = page.mainPageOutput.copyWithFormat(outFormat)
|
pageOutput, err = page.mainPageOutput.copyWithFormat(outFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if outFormat != page.s.rc.Format {
|
||||||
|
// Will be rendered ... later.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue