diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-04 09:12:33 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-04 15:12:30 +0200 |
commit | 10ff2f31a6ff3c0de292c7810f74edebfc2c0fb4 (patch) | |
tree | 3cbb6dc222452351f1c7f65e3a3ca201105a9714 /hugolib | |
parent | f8d555cca59a48df9cde2e7323ff2d500e0590a3 (diff) |
hugolib: Read media types and output formats from site config
Closes #3222
Closes #3223
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/page.go | 2 | ||||
-rw-r--r-- | hugolib/site.go | 66 | ||||
-rw-r--r-- | 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) } |