Skip to content

Commit

Permalink
Allow themes to define output formats, media types and params
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 18, 2018
1 parent 3cc3b2c commit 8310bbc
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 8 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,7 @@
name = "github.com/muesli/smartcrop"
branch = "master"


[[constraint]]
name = "github.com/sanity-io/litter"
version = "1.1.0"
110 changes: 109 additions & 1 deletion hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package hugolib
import (
"errors"
"fmt"
"path/filepath"

"io"
"strings"
Expand Down Expand Up @@ -85,7 +86,8 @@ func LoadConfig(d ConfigSourceDescriptor) (*viper.Viper, error) {
return v, err
}

return v, nil
return v, loadThemeConfig(fs, v)

}

func loadLanguageSettings(cfg config.Provider, oldLangs helpers.Languages) error {
Expand Down Expand Up @@ -201,6 +203,111 @@ func loadLanguageSettings(cfg config.Provider, oldLangs helpers.Languages) error
return nil
}

func loadThemeConfig(fs afero.Fs, v1 *viper.Viper) error {

theme := v1.GetString("theme")
if theme == "" {
return nil
}

themesDir := v1.GetString("themesDir")
configDir := filepath.Join(themesDir, theme)

var configPath string
var exists bool
var err error

for _, exensionsToCheck := range []string{"toml", "yaml", "yml", "json"} {
configPath = filepath.Join(configDir, "config."+exensionsToCheck)
exists, err = helpers.Exists(configPath, fs)
if err != nil {
return err
}
if exists {
break
}
}

if !exists {
return nil
}

v2 := viper.New()
v2.SetFs(fs)
v2.AutomaticEnv()
v2.SetEnvPrefix("hugo")
v2.SetConfigFile(configPath)

err = v2.ReadInConfig()
if err != nil {
return err
}

for _, key := range []string{"params", "outputformats", "mediatypes"} {
mergeStringMapKeepLeft(key, v1, v2)
}

themeLower := strings.ToLower(theme)
themeParamsNamespace := "params." + themeLower
// Set namespaced params
if v2.IsSet("params") && !v1.IsSet(themeParamsNamespace) {
v1.Set(themeParamsNamespace, v2.Get("params"))
}

// Only add params, we do not add language definitions.
if v1.IsSet("languages") && v2.IsSet("languages") {
v1Langs := v1.GetStringMap("languages")
for k, _ := range v1Langs {
mergeStringMapKeepLeft("languages."+k+".params", v1, v2)
}
v2Langs := v2.GetStringMap("languages")
for k, _ := range v2Langs {
langParamsKey := "languages." + k + ".params"
langParamsThemeNamespace := langParamsKey + "." + themeLower
// Set namespaced params
if v2.IsSet(langParamsKey) && !v1.IsSet(langParamsThemeNamespace) {
v1.Set(langParamsThemeNamespace, v2.Get(langParamsKey))
}
}
}

// Add menu definitions from theme not found in project
if v2.IsSet("menu") {
v2menus := v2.GetStringMap("menu")
for k, v := range v2menus {
if !v1.IsSet("menu." + k) {
v1.Set("menu."+k, v)
}
}
}

return nil

}

func mergeStringMapKeepLeft(key string, v1, v2 *viper.Viper) {
if !v1.IsSet(key) {
if v2.IsSet(key) {
v1.Set(key, v2.Get(key))
}
return
}

if !v2.IsSet(key) {
return
}

m1 := v1.GetStringMap(key)
m2 := v2.GetStringMap(key)

for k, v := range m2 {
if _, found := m1[k]; !found {
m1[k] = v
}
}

}

func loadDefaultSettingsFor(v *viper.Viper) error {

c, err := helpers.NewContentSpec(v)
Expand Down Expand Up @@ -282,4 +389,5 @@ lastmod = ["lastmod" ,":fileModTime", ":default"]
}

return loadLanguageSettings(v, nil)

}
Loading

0 comments on commit 8310bbc

Please sign in to comment.