Add support for multiple staticDirs

This commit adds support for multiple statDirs both on the global and language level.

A simple `config.toml` example:

```bash
staticDir = ["static1", "static2"]
[languages]
[languages.no]
staticDir = ["staticDir_override", "static_no"]
baseURL = "https://example.no"
languageName = "Norsk"
weight = 1
title = "På norsk"

[languages.en]
staticDir2 = "static_en"
baseURL = "https://example.com"
languageName = "English"
weight = 2
title = "In English"
```

In the above, with no theme used:

the English site will get its static files as a union of "static1", "static2" and "static_en". On file duplicates, the right-most version will win.
the Norwegian site will get its static files as a union of "staticDir_override" and "static_no".

This commit also concludes the Multihost support in #4027.

Fixes #36
Closes #4027
This commit is contained in:
Bjørn Erik Pedersen
2017-11-12 10:03:56 +01:00
parent 2e0465764b
commit 60dfb9a6e0
25 changed files with 825 additions and 273 deletions

View File

@@ -14,6 +14,7 @@
package hugolib
import (
"errors"
"fmt"
"io"
@@ -88,7 +89,7 @@ func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.
return v, nil
}
func loadLanguageSettings(cfg config.Provider) error {
func loadLanguageSettings(cfg config.Provider, oldLangs helpers.Languages) error {
multilingual := cfg.GetStringMap("languages")
var (
langs helpers.Languages
@@ -104,7 +105,56 @@ func loadLanguageSettings(cfg config.Provider) error {
}
}
if oldLangs != nil {
// When in multihost mode, the languages are mapped to a server, so
// some structural language changes will need a restart of the dev server.
// The validation below isn't complete, but should cover the most
// important cases.
var invalid bool
if langs.IsMultihost() != oldLangs.IsMultihost() {
invalid = true
} else {
if langs.IsMultihost() && len(langs) != len(oldLangs) {
invalid = true
}
}
if invalid {
return errors.New("language change needing a server restart detected")
}
if langs.IsMultihost() {
// We need to transfer any server baseURL to the new language
for i, ol := range oldLangs {
nl := langs[i]
nl.Set("baseURL", ol.GetString("baseURL"))
}
}
}
cfg.Set("languagesSorted", langs)
cfg.Set("multilingual", len(langs) > 1)
// The baseURL may be provided at the language level. If that is true,
// then every language must have a baseURL. In this case we always render
// to a language sub folder, which is then stripped from all the Permalink URLs etc.
var baseURLFromLang bool
for _, l := range langs {
burl := l.GetLocal("baseURL")
if baseURLFromLang && burl == nil {
return errors.New("baseURL must be set on all or none of the languages")
}
if burl != nil {
baseURLFromLang = true
}
}
if baseURLFromLang {
cfg.Set("defaultContentLanguageInSubdir", true)
cfg.Set("multihost", true)
}
return nil
}
@@ -178,5 +228,5 @@ func loadDefaultSettingsFor(v *viper.Viper) error {
v.SetDefault("debug", false)
v.SetDefault("disableFastRender", false)
return loadLanguageSettings(v)
return loadLanguageSettings(v, nil)
}