From 10ff2f31a6ff3c0de292c7810f74edebfc2c0fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 4 Apr 2017 09:12:33 +0200 Subject: [PATCH] hugolib: Read media types and output formats from site config Closes #3222 Closes #3223 --- hugolib/page.go | 2 +- hugolib/site.go | 66 ++++++++++++++++++++++++++++++++++-------- hugolib/site_output.go | 4 +-- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/hugolib/page.go b/hugolib/page.go index 9aa75a882..7b7d8d655 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -909,7 +909,7 @@ func (p *Page) update(f interface{}) error { o := cast.ToStringSlice(v) if len(o) > 0 { // 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 { p.s.Log.ERROR.Printf("Failed to resolve output formats: %s", err) diff --git a/hugolib/site.go b/hugolib/site.go index 40a7c44fb..722e14881 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -26,6 +26,10 @@ import ( "sync" "time" + "github.com/spf13/hugo/config" + + "github.com/spf13/hugo/media" + "github.com/bep/inflect" "sync/atomic" @@ -107,6 +111,12 @@ type Site struct { // Output formats defined in Page front matter will override these. 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. *deps.Deps `json:"-"` @@ -128,12 +138,14 @@ func (s *Site) isEnabled(kind string) bool { // reset returns a new Site prepared for rebuild. func (s *Site) reset() *Site { return &Site{Deps: s.Deps, - layoutHandler: output.NewLayoutHandler(s.PathSpec.ThemeSet()), - disabledKinds: s.disabledKinds, - outputFormats: s.outputFormats, - Language: s.Language, - owner: s.owner, - PageCollections: newPageCollections()} + layoutHandler: output.NewLayoutHandler(s.PathSpec.ThemeSet()), + disabledKinds: s.disabledKinds, + outputFormats: s.outputFormats, + outputFormatsConfig: s.outputFormatsConfig, + mediaTypesConfig: s.mediaTypesConfig, + Language: s.Language, + owner: s.owner, + PageCollections: newPageCollections()} } // newSite creates a new site with the given configuration. @@ -149,18 +161,48 @@ func newSite(cfg deps.DepsCfg) (*Site, error) { 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 { return nil, err } s := &Site{ - PageCollections: c, - layoutHandler: output.NewLayoutHandler(cfg.Cfg.GetString("themesDir") != ""), - Language: cfg.Language, - disabledKinds: disabledKinds, - outputFormats: outputFormats, + PageCollections: c, + layoutHandler: output.NewLayoutHandler(cfg.Cfg.GetString("themesDir") != ""), + Language: cfg.Language, + disabledKinds: disabledKinds, + outputFormats: outputFormats, + outputFormatsConfig: siteOutputFormatsConfig, + mediaTypesConfig: siteMediaTypesConfig, } s.Info = newSiteInfo(siteBuilderCfg{s: s, pageCollections: c, language: s.Language}) diff --git a/hugolib/site_output.go b/hugolib/site_output.go index 5ac5fe1f7..acd5d1cd1 100644 --- a/hugolib/site_output.go +++ b/hugolib/site_output.go @@ -23,7 +23,7 @@ import ( "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") { return createDefaultOutputFormats(cfg) } @@ -40,7 +40,7 @@ func createSiteOutputFormats(cfg config.Provider) (map[string]output.Formats, er var formats output.Formats vals := cast.ToStringSlice(v) for _, format := range vals { - f, found := output.DefaultFormats.GetByName(format) + f, found := allFormats.GetByName(format) if !found { return nil, fmt.Errorf("Failed to resolve output format %q from site config", format) }