2016-08-05 05:04:52 -04:00
|
|
|
// Copyright 2016-present The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2017-11-12 04:03:56 -05:00
|
|
|
"errors"
|
2016-08-05 05:04:52 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-09-25 02:59:02 -04:00
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2017-11-02 03:25:20 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/afero"
|
2016-08-05 05:04:52 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// LoadConfig loads Hugo configuration into a new Viper and then adds
|
|
|
|
// a set of defaults.
|
|
|
|
func LoadConfig(fs afero.Fs, relativeSourcePath, configFilename string) (*viper.Viper, error) {
|
|
|
|
v := viper.New()
|
|
|
|
v.SetFs(fs)
|
2016-08-05 05:04:52 -04:00
|
|
|
if relativeSourcePath == "" {
|
|
|
|
relativeSourcePath = "."
|
|
|
|
}
|
2017-08-09 14:13:02 -04:00
|
|
|
configFilenames := strings.Split(configFilename, ",")
|
2017-02-04 22:20:06 -05:00
|
|
|
v.AutomaticEnv()
|
|
|
|
v.SetEnvPrefix("hugo")
|
2017-08-09 14:13:02 -04:00
|
|
|
v.SetConfigFile(configFilenames[0])
|
2016-08-05 05:04:52 -04:00
|
|
|
// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
|
|
|
|
if relativeSourcePath == "" {
|
2017-02-04 22:20:06 -05:00
|
|
|
v.AddConfigPath(".")
|
2016-08-05 05:04:52 -04:00
|
|
|
} else {
|
2017-02-04 22:20:06 -05:00
|
|
|
v.AddConfigPath(relativeSourcePath)
|
2016-08-05 05:04:52 -04:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
err := v.ReadInConfig()
|
2016-08-05 05:04:52 -04:00
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigParseError); ok {
|
2017-02-04 22:20:06 -05:00
|
|
|
return nil, err
|
2016-08-05 05:04:52 -04:00
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
return nil, fmt.Errorf("Unable to locate Config file. Perhaps you need to create a new site.\n Run `hugo help new` for details. (%s)\n", err)
|
2016-08-05 05:04:52 -04:00
|
|
|
}
|
2017-08-09 14:13:02 -04:00
|
|
|
for _, configFile := range configFilenames[1:] {
|
|
|
|
var r io.Reader
|
|
|
|
var err error
|
|
|
|
if r, err = fs.Open(configFile); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to open Config file.\n (%s)\n", err)
|
|
|
|
}
|
|
|
|
if err = v.MergeConfig(r); err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to parse/merge Config file (%s).\n (%s)\n", configFile, err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 05:04:52 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
v.RegisterAlias("indexes", "taxonomies")
|
2016-08-05 05:04:52 -04:00
|
|
|
|
2017-05-10 14:00:08 -04:00
|
|
|
// Remove these in Hugo 0.23.
|
|
|
|
if v.IsSet("disable404") {
|
|
|
|
helpers.Deprecated("site config", "disable404", "Use disableKinds=[\"404\"]", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet("disableRSS") {
|
|
|
|
helpers.Deprecated("site config", "disableRSS", "Use disableKinds=[\"RSS\"]", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet("disableSitemap") {
|
2017-05-31 03:57:19 -04:00
|
|
|
// NOTE: Do not remove this until Hugo 0.24, ERROR in 0.23.
|
2017-05-10 14:00:08 -04:00
|
|
|
helpers.Deprecated("site config", "disableSitemap", "Use disableKinds= [\"sitemap\"]", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet("disableRobotsTXT") {
|
|
|
|
helpers.Deprecated("site config", "disableRobotsTXT", "Use disableKinds= [\"robotsTXT\"]", false)
|
|
|
|
}
|
|
|
|
|
2017-11-02 03:25:20 -04:00
|
|
|
if err := loadDefaultSettingsFor(v); err != nil {
|
|
|
|
return v, err
|
|
|
|
}
|
2016-08-05 05:04:52 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
return v, nil
|
2016-08-05 05:04:52 -04:00
|
|
|
}
|
|
|
|
|
2017-11-12 04:03:56 -05:00
|
|
|
func loadLanguageSettings(cfg config.Provider, oldLangs helpers.Languages) error {
|
2017-11-02 03:25:20 -04:00
|
|
|
multilingual := cfg.GetStringMap("languages")
|
|
|
|
var (
|
|
|
|
langs helpers.Languages
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(multilingual) == 0 {
|
|
|
|
langs = append(langs, helpers.NewDefaultLanguage(cfg))
|
|
|
|
} else {
|
|
|
|
langs, err = toSortedLanguages(cfg, multilingual)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse multilingual config: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 04:03:56 -05:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 03:25:20 -04:00
|
|
|
cfg.Set("languagesSorted", langs)
|
2017-11-12 04:03:56 -05:00
|
|
|
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)
|
|
|
|
}
|
2017-11-02 03:25:20 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:59:02 -04:00
|
|
|
func loadDefaultSettingsFor(v *viper.Viper) error {
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2017-09-25 02:59:02 -04:00
|
|
|
c, err := helpers.NewContentSpec(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
v.SetDefault("cleanDestinationDir", false)
|
|
|
|
v.SetDefault("watch", false)
|
|
|
|
v.SetDefault("metaDataFormat", "toml")
|
|
|
|
v.SetDefault("disable404", false)
|
|
|
|
v.SetDefault("disableRSS", false)
|
|
|
|
v.SetDefault("disableSitemap", false)
|
|
|
|
v.SetDefault("disableRobotsTXT", false)
|
|
|
|
v.SetDefault("contentDir", "content")
|
|
|
|
v.SetDefault("layoutDir", "layouts")
|
|
|
|
v.SetDefault("staticDir", "static")
|
|
|
|
v.SetDefault("archetypeDir", "archetypes")
|
|
|
|
v.SetDefault("publishDir", "public")
|
|
|
|
v.SetDefault("dataDir", "data")
|
|
|
|
v.SetDefault("i18nDir", "i18n")
|
|
|
|
v.SetDefault("themesDir", "themes")
|
|
|
|
v.SetDefault("defaultLayout", "post")
|
|
|
|
v.SetDefault("buildDrafts", false)
|
|
|
|
v.SetDefault("buildFuture", false)
|
|
|
|
v.SetDefault("buildExpired", false)
|
|
|
|
v.SetDefault("uglyURLs", false)
|
|
|
|
v.SetDefault("verbose", false)
|
|
|
|
v.SetDefault("ignoreCache", false)
|
|
|
|
v.SetDefault("canonifyURLs", false)
|
|
|
|
v.SetDefault("relativeURLs", false)
|
|
|
|
v.SetDefault("removePathAccents", false)
|
2017-07-30 11:46:04 -04:00
|
|
|
v.SetDefault("titleCaseStyle", "AP")
|
2017-02-04 22:20:06 -05:00
|
|
|
v.SetDefault("taxonomies", map[string]string{"tag": "tags", "category": "categories"})
|
|
|
|
v.SetDefault("permalinks", make(PermalinkOverrides, 0))
|
|
|
|
v.SetDefault("sitemap", Sitemap{Priority: -1, Filename: "sitemap.xml"})
|
|
|
|
v.SetDefault("pygmentsStyle", "monokai")
|
|
|
|
v.SetDefault("pygmentsUseClasses", false)
|
|
|
|
v.SetDefault("pygmentsCodeFences", false)
|
2017-09-25 02:59:02 -04:00
|
|
|
v.SetDefault("pygmentsUseClassic", false)
|
2017-02-04 22:20:06 -05:00
|
|
|
v.SetDefault("pygmentsOptions", "")
|
|
|
|
v.SetDefault("disableLiveReload", false)
|
|
|
|
v.SetDefault("pluralizeListTitles", true)
|
|
|
|
v.SetDefault("preserveTaxonomyNames", false)
|
|
|
|
v.SetDefault("forceSyncStatic", false)
|
|
|
|
v.SetDefault("footnoteAnchorPrefix", "")
|
|
|
|
v.SetDefault("footnoteReturnLinkContents", "")
|
|
|
|
v.SetDefault("newContentEditor", "")
|
|
|
|
v.SetDefault("paginate", 10)
|
|
|
|
v.SetDefault("paginatePath", "page")
|
2017-09-29 03:04:55 -04:00
|
|
|
v.SetDefault("summaryLength", 70)
|
2017-02-04 22:20:06 -05:00
|
|
|
v.SetDefault("blackfriday", c.NewBlackfriday())
|
|
|
|
v.SetDefault("rSSUri", "index.xml")
|
2017-03-09 19:41:59 -05:00
|
|
|
v.SetDefault("rssLimit", -1)
|
2017-02-04 22:20:06 -05:00
|
|
|
v.SetDefault("sectionPagesMenu", "")
|
|
|
|
v.SetDefault("disablePathToLower", false)
|
|
|
|
v.SetDefault("hasCJKLanguage", false)
|
|
|
|
v.SetDefault("enableEmoji", false)
|
|
|
|
v.SetDefault("pygmentsCodeFencesGuessSyntax", false)
|
|
|
|
v.SetDefault("useModTimeAsFallback", false)
|
|
|
|
v.SetDefault("defaultContentLanguage", "en")
|
|
|
|
v.SetDefault("defaultContentLanguageInSubdir", false)
|
|
|
|
v.SetDefault("enableMissingTranslationPlaceholders", false)
|
|
|
|
v.SetDefault("enableGitInfo", false)
|
2017-05-06 03:41:24 -04:00
|
|
|
v.SetDefault("ignoreFiles", make([]string, 0))
|
2017-06-20 04:30:40 -04:00
|
|
|
v.SetDefault("disableAliases", false)
|
2017-07-27 16:36:22 -04:00
|
|
|
v.SetDefault("debug", false)
|
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
|
|
|
v.SetDefault("disableFastRender", false)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
2017-11-12 04:03:56 -05:00
|
|
|
return loadLanguageSettings(v, nil)
|
2016-08-05 05:04:52 -04:00
|
|
|
}
|