hugolib: Read media types and output formats from site config
Closes #3222 Closes #3223
This commit is contained in:
parent
f8d555cca5
commit
10ff2f31a6
3 changed files with 57 additions and 15 deletions
|
@ -909,7 +909,7 @@ func (p *Page) update(f interface{}) error {
|
||||||
o := cast.ToStringSlice(v)
|
o := cast.ToStringSlice(v)
|
||||||
if len(o) > 0 {
|
if len(o) > 0 {
|
||||||
// Output formats are exlicitly set in front matter, use those.
|
// Output formats are exlicitly set in front matter, use those.
|
||||||
outFormats, err := output.DefaultFormats.GetByNames(o...)
|
outFormats, err := p.s.outputFormatsConfig.GetByNames(o...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.s.Log.ERROR.Printf("Failed to resolve output formats: %s", err)
|
p.s.Log.ERROR.Printf("Failed to resolve output formats: %s", err)
|
||||||
|
|
|
@ -26,6 +26,10 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/hugo/config"
|
||||||
|
|
||||||
|
"github.com/spf13/hugo/media"
|
||||||
|
|
||||||
"github.com/bep/inflect"
|
"github.com/bep/inflect"
|
||||||
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -107,6 +111,12 @@ type Site struct {
|
||||||
// Output formats defined in Page front matter will override these.
|
// Output formats defined in Page front matter will override these.
|
||||||
outputFormats map[string]output.Formats
|
outputFormats map[string]output.Formats
|
||||||
|
|
||||||
|
// All the output formats and media types available for this site.
|
||||||
|
// These values will be merged from the Hugo defaults, the site config and,
|
||||||
|
// finally, the language settings.
|
||||||
|
outputFormatsConfig output.Formats
|
||||||
|
mediaTypesConfig media.Types
|
||||||
|
|
||||||
// Logger etc.
|
// Logger etc.
|
||||||
*deps.Deps `json:"-"`
|
*deps.Deps `json:"-"`
|
||||||
|
|
||||||
|
@ -128,12 +138,14 @@ func (s *Site) isEnabled(kind string) bool {
|
||||||
// reset returns a new Site prepared for rebuild.
|
// reset returns a new Site prepared for rebuild.
|
||||||
func (s *Site) reset() *Site {
|
func (s *Site) reset() *Site {
|
||||||
return &Site{Deps: s.Deps,
|
return &Site{Deps: s.Deps,
|
||||||
layoutHandler: output.NewLayoutHandler(s.PathSpec.ThemeSet()),
|
layoutHandler: output.NewLayoutHandler(s.PathSpec.ThemeSet()),
|
||||||
disabledKinds: s.disabledKinds,
|
disabledKinds: s.disabledKinds,
|
||||||
outputFormats: s.outputFormats,
|
outputFormats: s.outputFormats,
|
||||||
Language: s.Language,
|
outputFormatsConfig: s.outputFormatsConfig,
|
||||||
owner: s.owner,
|
mediaTypesConfig: s.mediaTypesConfig,
|
||||||
PageCollections: newPageCollections()}
|
Language: s.Language,
|
||||||
|
owner: s.owner,
|
||||||
|
PageCollections: newPageCollections()}
|
||||||
}
|
}
|
||||||
|
|
||||||
// newSite creates a new site with the given configuration.
|
// newSite creates a new site with the given configuration.
|
||||||
|
@ -149,18 +161,48 @@ func newSite(cfg deps.DepsCfg) (*Site, error) {
|
||||||
disabledKinds[disabled] = true
|
disabledKinds[disabled] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
outputFormats, err := createSiteOutputFormats(cfg.Language)
|
var (
|
||||||
|
mediaTypesConfig []map[string]interface{}
|
||||||
|
outputFormatsConfig []map[string]interface{}
|
||||||
|
|
||||||
|
siteOutputFormatsConfig output.Formats
|
||||||
|
siteMediaTypesConfig media.Types
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add language last, if set, so it gets precedence.
|
||||||
|
for _, cfg := range []config.Provider{cfg.Cfg, cfg.Language} {
|
||||||
|
if cfg.IsSet("mediaTypes") {
|
||||||
|
mediaTypesConfig = append(mediaTypesConfig, cfg.GetStringMap("mediaTypes"))
|
||||||
|
}
|
||||||
|
if cfg.IsSet("outputFormats") {
|
||||||
|
outputFormatsConfig = append(outputFormatsConfig, cfg.GetStringMap("outputFormats"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
siteMediaTypesConfig, err = media.DecodeTypes(mediaTypesConfig...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
siteOutputFormatsConfig, err = output.DecodeFormats(siteMediaTypesConfig, outputFormatsConfig...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFormats, err := createSiteOutputFormats(siteOutputFormatsConfig, cfg.Language)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Site{
|
s := &Site{
|
||||||
PageCollections: c,
|
PageCollections: c,
|
||||||
layoutHandler: output.NewLayoutHandler(cfg.Cfg.GetString("themesDir") != ""),
|
layoutHandler: output.NewLayoutHandler(cfg.Cfg.GetString("themesDir") != ""),
|
||||||
Language: cfg.Language,
|
Language: cfg.Language,
|
||||||
disabledKinds: disabledKinds,
|
disabledKinds: disabledKinds,
|
||||||
outputFormats: outputFormats,
|
outputFormats: outputFormats,
|
||||||
|
outputFormatsConfig: siteOutputFormatsConfig,
|
||||||
|
mediaTypesConfig: siteMediaTypesConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Info = newSiteInfo(siteBuilderCfg{s: s, pageCollections: c, language: s.Language})
|
s.Info = newSiteInfo(siteBuilderCfg{s: s, pageCollections: c, language: s.Language})
|
||||||
|
|
|
@ -23,7 +23,7 @@ import (
|
||||||
"github.com/spf13/hugo/output"
|
"github.com/spf13/hugo/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createSiteOutputFormats(cfg config.Provider) (map[string]output.Formats, error) {
|
func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
|
||||||
if !cfg.IsSet("outputs") {
|
if !cfg.IsSet("outputs") {
|
||||||
return createDefaultOutputFormats(cfg)
|
return createDefaultOutputFormats(cfg)
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func createSiteOutputFormats(cfg config.Provider) (map[string]output.Formats, er
|
||||||
var formats output.Formats
|
var formats output.Formats
|
||||||
vals := cast.ToStringSlice(v)
|
vals := cast.ToStringSlice(v)
|
||||||
for _, format := range vals {
|
for _, format := range vals {
|
||||||
f, found := output.DefaultFormats.GetByName(format)
|
f, found := allFormats.GetByName(format)
|
||||||
if !found {
|
if !found {
|
||||||
return nil, fmt.Errorf("Failed to resolve output format %q from site config", format)
|
return nil, fmt.Errorf("Failed to resolve output format %q from site config", format)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue