ref: 62dd1d45c12193ac5ad95a4f491dfcdbcad7be7a
parent: a01056b98a55b527d5a7cf2d973729eb9c25a1f4
author: spf13 <steve.francia@gmail.com>
date: Fri Apr 4 21:26:43 EDT 2014
Hugo config abstracted into a general purpose config library called "Viper". Hugo casting now in own library called "cast"
--- a/commands/check.go
+++ b/commands/check.go
@@ -25,7 +25,7 @@
content provided and will give feedback.`,
Run: func(cmd *cobra.Command, args []string) {InitializeConfig()
- site := hugolib.Site{Config: *Config}+ site := hugolib.Site{}site.Analyze()
},
}
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -24,14 +24,16 @@
"github.com/mostafah/fsync"
"github.com/spf13/cobra"
+ "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro"
+ "github.com/spf13/viper"
)
-var Config *hugolib.Config
+//var Config *hugolib.Config
var HugoCmd = &cobra.Command{Use: "hugo",
Short: "Hugo is a very fast static site generator",
@@ -78,34 +80,58 @@
}
func InitializeConfig() {- Config = hugolib.SetupConfig(&CfgFile, &Source)
+ viper.SetConfigName(CfgFile) // config
+ viper.AddConfigPath(Source)
+ viper.ReadInConfig()
+ viper.SetDefault("ContentDir", "content")+ viper.SetDefault("LayoutDir", "layouts")+ viper.SetDefault("StaticDir", "static")+ viper.SetDefault("PublishDir", "public")+ viper.SetDefault("DefaultLayout", "post")+ viper.SetDefault("BuildDrafts", false)+ viper.SetDefault("UglyUrls", false)+ viper.SetDefault("Verbose", false)+ viper.SetDefault("CanonifyUrls", false)+ viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})+ viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))+
if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {- Config.BuildDrafts = Draft
+ viper.Set("BuildDrafts", Draft)}
if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed {- Config.UglyUrls = UglyUrls
+ viper.Set("UglyUrls", UglyUrls)}
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {- Config.Verbose = Verbose
+ viper.Set("Verbose", Verbose)}
if hugoCmdV.PersistentFlags().Lookup("logfile").Changed {- Config.LogFile = LogFile
+ viper.Set("LogFile", LogFile)}
if BaseUrl != "" {- Config.BaseUrl = BaseUrl
+ if !strings.HasSuffix(BaseUrl, "/") {+ BaseUrl = BaseUrl + "/"
+ }
+ viper.Set("BaseUrl", BaseUrl)}
if Destination != "" {- Config.PublishDir = Destination
+ viper.Set("PublishDir", Destination)}
- if VerboseLog || Logging || Config.LogFile != "" {- if Config.LogFile != "" {- jww.SetLogFile(Config.LogFile)
+ if Source != "" {+ viper.Set("WorkingDir", Source)+ } else {+ dir, _ := helpers.FindCWD()
+ viper.Set("WorkingDir", dir)+ }
+
+ if VerboseLog || Logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {+ if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {+ jww.SetLogFile(viper.GetString("LogFile")) } else { jww.UseTempLogFile("hugo")}
@@ -113,7 +139,7 @@
jww.DiscardLogging()
}
- if Config.Verbose {+ if viper.GetBool("verbose") {jww.SetStdoutThreshold(jww.LevelDebug)
}
@@ -123,7 +149,7 @@
}
func build(watches ...bool) {- utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))+ utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))watch := false
if len(watches) > 0 && watches[0] {watch = true
@@ -131,7 +157,7 @@
utils.StopOnErr(buildSite(BuildWatch || watch))
if BuildWatch {- jww.FEEDBACK.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))+ jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("ContentDir"))) jww.FEEDBACK.Println("Press ctrl+c to stop")utils.CheckErr(NewWatcher(0))
}
@@ -138,13 +164,15 @@
}
func copyStatic() error {- staticDir := Config.GetAbsPath(Config.StaticDir + "/")
+ staticDir := helpers.AbsPathify(viper.GetString("StaticDir")) + "/" if _, err := os.Stat(staticDir); os.IsNotExist(err) {return nil
}
+ publishDir := helpers.AbsPathify(viper.GetString("PublishDir")) + "/"// Copy Static to Destination
- return fsync.Sync(Config.GetAbsPath(Config.PublishDir+"/"), Config.GetAbsPath(Config.StaticDir+"/"))
+ jww.INFO.Println("syncing from", staticDir, "to", publishDir)+ return fsync.Sync(publishDir, staticDir)
}
func getDirList() []string {@@ -161,9 +189,9 @@
return nil
}
- filepath.Walk(Config.GetAbsPath(Config.ContentDir), walker)
- filepath.Walk(Config.GetAbsPath(Config.LayoutDir), walker)
- filepath.Walk(Config.GetAbsPath(Config.StaticDir), walker)
+ filepath.Walk(helpers.AbsPathify(viper.GetString("ContentDir")), walker)+ filepath.Walk(helpers.AbsPathify(viper.GetString("LayoutDir")), walker)+ filepath.Walk(helpers.AbsPathify(viper.GetString("StaticDir")), walker)return a
}
@@ -170,7 +198,7 @@
func buildSite(watching ...bool) (err error) {startTime := time.Now()
- site := &hugolib.Site{Config: *Config}+ site := &hugolib.Site{} if len(watching) > 0 && watching[0] {site.RunMode.Watching = true
}
@@ -226,7 +254,7 @@
continue
}
- isstatic := strings.HasPrefix(ev.Name, Config.GetAbsPath(Config.StaticDir))
+ isstatic := strings.HasPrefix(ev.Name, helpers.AbsPathify(viper.GetString("StaticDir")))static_changed = static_changed || isstatic
dynamic_changed = dynamic_changed || !isstatic
@@ -240,7 +268,7 @@
if static_changed { fmt.Print("Static file changed, syncing\n\n")- utils.StopOnErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", Config.GetAbsPath(Config.PublishDir)))+ utils.StopOnErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))}
if dynamic_changed {--- a/commands/server.go
+++ b/commands/server.go
@@ -21,7 +21,9 @@
"strings"
"github.com/spf13/cobra"
+ "github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman"
+ "github.com/spf13/viper"
)
var serverPort int
@@ -55,9 +57,9 @@
}
if serverAppend {- Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") + ":" + strconv.Itoa(serverPort)
+ viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort)) } else {- Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/")
+ viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/"))}
build(serverWatch)
@@ -64,7 +66,7 @@
// Watch runs its own server as part of the routine
if serverWatch {- jww.FEEDBACK.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir))+ jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("ContentDir")))err := NewWatcher(serverPort)
if err != nil {fmt.Println(err)
@@ -75,10 +77,10 @@
}
func serve(port int) {- jww.FEEDBACK.Println("Serving pages from " + Config.GetAbsPath(Config.PublishDir))+ jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir"))) if BaseUrl == "" {- jww.FEEDBACK.Printf("Web Server is available at %s\n", Config.BaseUrl)+ jww.FEEDBACK.Printf("Web Server is available at %s\n", viper.GetString("BaseUrl")) } else { jww.FEEDBACK.Printf("Web Server is available at http://localhost:%v\n", port)}
@@ -85,7 +87,7 @@
fmt.Println("Press ctrl+c to stop")- err := http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir))))+ err := http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))) if err != nil { jww.ERROR.Printf("Error: %s\n", err.Error())os.Exit(1)
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -14,11 +14,14 @@
package helpers
import (
+ "fmt"
"os"
"path"
+ "path/filepath"
"regexp"
"strings"
"unicode"
+ "github.com/spf13/viper"
)
var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]")@@ -75,6 +78,14 @@
return false, err
}
+func AbsPathify(inPath string) string {+ if filepath.IsAbs(inPath) {+ return filepath.Clean(inPath)
+ }
+
+ return filepath.Clean(filepath.Join(viper.GetString("WorkingDir"), inPath))+}
+
func FileAndExt(in string) (name string, ext string) {ext = path.Ext(in)
base := path.Base(in)
@@ -116,4 +127,27 @@
return path.Join(path.Dir(in), name, "index"+ext)
}
}
+}
+
+func FindCWD() (string, error) {+ serverFile, err := filepath.Abs(os.Args[0])
+
+ if err != nil {+ return "", fmt.Errorf("Can't get absolute path for executable: %v", err)+ }
+
+ path := filepath.Dir(serverFile)
+ realFile, err := filepath.EvalSymlinks(serverFile)
+
+ if err != nil {+ if _, err = os.Stat(serverFile + ".exe"); err == nil {+ realFile = filepath.Clean(serverFile + ".exe")
+ }
+ }
+
+ if err == nil && realFile != serverFile {+ path = filepath.Dir(realFile)
+ }
+
+ return path, nil
}
--- a/hugolib/config.go
+++ /dev/null
@@ -1,203 +1,0 @@
-// Copyright © 2013 Steve Francia <spf@spf13.com>.
-//
-// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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 (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "os"
- "path"
- "path/filepath"
- "strings"
-
- "github.com/BurntSushi/toml"
- "github.com/spf13/hugo/helpers"
- jww "github.com/spf13/jwalterweatherman"
- "launchpad.net/goyaml"
-)
-
-// config file items
-type Config struct {- ContentDir, PublishDir, BaseUrl, StaticDir string
- Path, CacheDir, LayoutDir, DefaultLayout string
- ConfigFile, LogFile string
- Title string
- Indexes map[string]string // singular, plural
- ProcessFilters map[string][]string
- Params map[string]interface{}- Permalinks PermalinkOverrides
- BuildDrafts, UglyUrls, Verbose bool
- CanonifyUrls bool
-}
-
-var c Config
-
-// Read cfgfile or setup defaults.
-func SetupConfig(cfgfile *string, path *string) *Config {- c.setPath(*path)
-
- cfg, err := c.findConfigFile(*cfgfile)
- c.ConfigFile = cfg
-
- if err != nil {- jww.ERROR.Printf("%v", err)- jww.FEEDBACK.Println("using defaults instead")- }
-
- // set defaults
- c.ContentDir = "content"
- c.LayoutDir = "layouts"
- c.PublishDir = "public"
- c.StaticDir = "static"
- c.DefaultLayout = "post"
- c.BuildDrafts = false
- c.UglyUrls = false
- c.Verbose = false
- c.CanonifyUrls = true
-
- c.readInConfig()
-
- // set index defaults if none provided
- if c.Indexes == nil {- c.Indexes = make(map[string]string)
- c.Indexes["tag"] = "tags"
- c.Indexes["category"] = "categories"
- }
-
- // ensure map exists, albeit empty
- if c.Permalinks == nil {- c.Permalinks = make(PermalinkOverrides, 0)
- }
-
- if !strings.HasSuffix(c.BaseUrl, "/") {- c.BaseUrl = c.BaseUrl + "/"
- }
-
- return &c
-}
-
-func (c *Config) readInConfig() {- file, err := ioutil.ReadFile(c.ConfigFile)
- if err == nil {- switch path.Ext(c.ConfigFile) {- case ".yaml":
- if err := goyaml.Unmarshal(file, &c); err != nil {- jww.ERROR.Printf("Error parsing config: %s", err)- os.Exit(1)
- }
-
- case ".json":
- if err := json.Unmarshal(file, &c); err != nil {- jww.ERROR.Printf("Error parsing config: %s", err)- os.Exit(1)
- }
-
- case ".toml":
- if _, err := toml.Decode(string(file), &c); err != nil {- jww.ERROR.Printf("Error parsing config: %s", err)- os.Exit(1)
- }
- }
- }
-}
-
-func (c *Config) setPath(p string) {- if p == "" {- path, err := findPath()
- if err != nil {- jww.ERROR.Printf("Error finding path: %s", err)- }
- c.Path = path
- } else {- path, err := filepath.Abs(p)
- if err != nil {- jww.ERROR.Printf("Error finding path: %s", err)- }
- c.Path = path
- }
-}
-
-func (c *Config) GetPath() string {- if c.Path == "" {- c.setPath("")- }
- return c.Path
-}
-
-func findPath() (string, error) {- serverFile, err := filepath.Abs(os.Args[0])
-
- if err != nil {- return "", fmt.Errorf("Can't get absolute path for executable: %v", err)- }
-
- path := filepath.Dir(serverFile)
- realFile, err := filepath.EvalSymlinks(serverFile)
-
- if err != nil {- if _, err = os.Stat(serverFile + ".exe"); err == nil {- realFile = filepath.Clean(serverFile + ".exe")
- }
- }
-
- if err == nil && realFile != serverFile {- path = filepath.Dir(realFile)
- }
-
- return path, nil
-}
-
-// GetAbsPath return the absolute path for a given path with the internal slashes
-// properly converted.
-func (c *Config) GetAbsPath(name string) string {- if filepath.IsAbs(name) {- return name
- }
-
- return filepath.Join(c.GetPath(), name)
-}
-
-func (c *Config) findConfigFile(configFileName string) (string, error) {-
- if configFileName == "" { // config not specified, let's search- if b, _ := helpers.Exists(c.GetAbsPath("config.json")); b {- return c.GetAbsPath("config.json"), nil- }
-
- if b, _ := helpers.Exists(c.GetAbsPath("config.toml")); b {- return c.GetAbsPath("config.toml"), nil- }
-
- if b, _ := helpers.Exists(c.GetAbsPath("config.yaml")); b {- return c.GetAbsPath("config.yaml"), nil- }
-
- return "", fmt.Errorf("config file not found in: %s", c.GetPath())-
- } else {- // If the full path is given, just use that
- if path.IsAbs(configFileName) {- return configFileName, nil
- }
-
- // Else check the local directory
- t := c.GetAbsPath(configFileName)
- if b, _ := helpers.Exists(t); b {- return t, nil
- } else {- return "", fmt.Errorf("config file not found at: %s", t)- }
- }
-}
--- a/hugolib/index.go
+++ b/hugolib/index.go
@@ -14,8 +14,9 @@
package hugolib
import (
- "github.com/spf13/hugo/helpers"
"sort"
+
+ "github.com/spf13/hugo/helpers"
)
/*
--- a/hugolib/metadata.go
+++ /dev/null
@@ -1,157 +1,0 @@
-package hugolib
-
-import (
- "errors"
- "fmt"
- "strconv"
- "time"
-
- jww "github.com/spf13/jwalterweatherman"
-)
-
-func interfaceToTime(i interface{}) time.Time {- switch s := i.(type) {- case time.Time:
- return s
- case string:
- d, e := stringToDate(s)
- if e == nil {- return d
- }
- jww.ERROR.Println("Could not parse Date/Time format:", e)- default:
- jww.ERROR.Println("Only Time is supported for this key")- }
-
- return *new(time.Time)
-}
-
-func interfaceToStringToDate(i interface{}) time.Time {- s := interfaceToString(i)
-
- if d, e := stringToDate(s); e == nil {- return d
- }
-
- return time.Unix(0, 0)
-}
-
-func stringToDate(s string) (time.Time, error) {- return parseDateWith(s, []string{- time.RFC3339,
- "2006-01-02T15:04:05", // iso8601 without timezone
- time.RFC1123Z,
- time.RFC1123,
- time.RFC822Z,
- time.RFC822,
- time.ANSIC,
- time.UnixDate,
- time.RubyDate,
- "2006-01-02 15:04:05Z07:00",
- "02 Jan 06 15:04 MST",
- "2006-01-02",
- "02 Jan 2006",
- })
-}
-
-func parseDateWith(s string, dates []string) (d time.Time, e error) {- for _, dateType := range dates {- if d, e = time.Parse(dateType, s); e == nil {- return
- }
- }
- return d, errors.New(fmt.Sprintf("Unable to parse date: %s", s))-}
-
-func interfaceToBool(i interface{}) bool {- switch b := i.(type) {- case bool:
- return b
- case int:
- if i.(int) > 0 {- return true
- }
- return false
- default:
- jww.ERROR.Println("Only Boolean values are supported for this YAML key")- }
-
- return false
-
-}
-
-func interfaceArrayToStringArray(i interface{}) []string {- var a []string
-
- switch vv := i.(type) {- case []interface{}:- for _, u := range vv {- a = append(a, interfaceToString(u))
- }
- }
-
- return a
-}
-
-func interfaceToFloat64(i interface{}) float64 {- switch s := i.(type) {- case float64:
- return s
- case float32:
- return float64(s)
-
- case string:
- v, err := strconv.ParseFloat(s, 64)
- if err == nil {- return float64(v)
- } else {- jww.ERROR.Println("Only Floats are supported for this key\nErr:", err)- }
-
- default:
- jww.ERROR.Println("Only Floats are supported for this key")- }
-
- return 0.0
-}
-
-func interfaceToInt(i interface{}) int {- switch s := i.(type) {- case int:
- return s
- case int64:
- return int(s)
- case int32:
- return int(s)
- case int16:
- return int(s)
- case int8:
- return int(s)
- case string:
- v, err := strconv.ParseInt(s, 0, 0)
- if err == nil {- return int(v)
- } else {- jww.ERROR.Println("Only Ints are supported for this key\nErr:", err)- }
- default:
- jww.ERROR.Println("Only Ints are supported for this key")- }
-
- return 0
-}
-
-func interfaceToString(i interface{}) string {- switch s := i.(type) {- case string:
- return s
- case float64:
- return strconv.FormatFloat(i.(float64), 'f', -1, 64)
- case int:
- return strconv.FormatInt(int64(i.(int)), 10)
- default:
- jww.ERROR.Println(fmt.Sprintf("Only Strings are supported for this key (got type '%T'): %s", s, s))- }
-
- return ""
-}
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -25,10 +25,12 @@
"time"
"github.com/BurntSushi/toml"
+ "github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser"
"github.com/spf13/hugo/template/bundle"
jww "github.com/spf13/jwalterweatherman"
+ "github.com/spf13/viper"
"github.com/theplant/blackfriday"
"launchpad.net/goyaml"
json "launchpad.net/rjson"
@@ -252,10 +254,10 @@
// fmt.Printf("have a section override for %q in section %s → %s\n", p.Title, p.Section, permalink) } else { if len(pSlug) > 0 {- permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, p.Slug+"."+p.Extension))
+ permalink = helpers.UrlPrep(viper.GetBool("UglyUrls"), path.Join(dir, p.Slug+"."+p.Extension)) } else {_, t := path.Split(p.FileName)
- permalink = helpers.UrlPrep(p.Site.Config.UglyUrls, path.Join(dir, helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension)))
+ permalink = helpers.UrlPrep(viper.GetBool("UglyUrls"), path.Join(dir, helpers.ReplaceExtension(strings.TrimSpace(t), p.Extension)))}
}
@@ -327,34 +329,34 @@
loki := strings.ToLower(k)
switch loki {case "title":
- page.Title = interfaceToString(v)
+ page.Title = cast.ToString(v)
case "linktitle":
- page.linkTitle = interfaceToString(v)
+ page.linkTitle = cast.ToString(v)
case "description":
- page.Description = interfaceToString(v)
+ page.Description = cast.ToString(v)
case "slug":
- page.Slug = helpers.Urlize(interfaceToString(v))
+ page.Slug = helpers.Urlize(cast.ToString(v))
case "url":
- if url := interfaceToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {+ if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") { return fmt.Errorf("Only relative urls are supported, %v provided", url)}
- page.Url = helpers.Urlize(interfaceToString(v))
+ page.Url = helpers.Urlize(cast.ToString(v))
case "type":
- page.contentType = interfaceToString(v)
+ page.contentType = cast.ToString(v)
case "keywords":
- page.Keywords = interfaceArrayToStringArray(v)
+ page.Keywords = cast.ToStringSlice(v)
case "date", "pubdate":
- page.Date = interfaceToTime(v)
+ page.Date = cast.ToTime(v)
case "draft":
- page.Draft = interfaceToBool(v)
+ page.Draft = cast.ToBool(v)
case "layout":
- page.layout = interfaceToString(v)
+ page.layout = cast.ToString(v)
case "markup":
- page.Markup = interfaceToString(v)
+ page.Markup = cast.ToString(v)
case "weight":
- page.Weight = interfaceToInt(v)
+ page.Weight = cast.ToInt(v)
case "aliases":
- page.Aliases = interfaceArrayToStringArray(v)
+ page.Aliases = cast.ToStringSlice(v)
for _, alias := range page.Aliases { if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") { return fmt.Errorf("Only relative aliases are supported, %v provided", alias)@@ -361,7 +363,7 @@
}
}
case "status":
- page.Status = interfaceToString(v)
+ page.Status = cast.ToString(v)
default:
// If not one of the explicit values, store in Params
switch vv := v.(type) {@@ -380,7 +382,7 @@
case []interface{}:var a = make([]string, len(vvv))
for i, u := range vvv {- a[i] = interfaceToString(u)
+ a[i] = cast.ToString(u)
}
page.Params[loki] = a
}
@@ -400,15 +402,15 @@
switch v.(type) {case bool:
- return interfaceToBool(v)
+ return cast.ToBool(v)
case string:
- return interfaceToString(v)
+ return cast.ToString(v)
case int64, int32, int16, int8, int:
- return interfaceToInt(v)
+ return cast.ToInt(v)
case float64, float32:
- return interfaceToFloat64(v)
+ return cast.ToFloat64(v)
case time.Time:
- return interfaceToTime(v)
+ return cast.ToTime(v)
case []string:
return v
}
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -31,6 +31,7 @@
"github.com/spf13/hugo/transform"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro"
+ "github.com/spf13/viper"
)
var _ = transform.AbsURL
@@ -55,7 +56,6 @@
//
// 5. The entire collection of files is written to disk.
type Site struct {- Config Config
Pages Pages
Tmpl bundle.Template
Indexes IndexList
@@ -77,7 +77,7 @@
Recent *Pages
LastChange time.Time
Title string
- Config *Config
+ ConfigGet func(key string) interface{}Permalinks PermalinkOverrides
Params map[string]interface{}}
@@ -202,7 +202,7 @@
return err
}
- staticDir := s.Config.GetAbsPath(s.Config.StaticDir + "/")
+ staticDir := helpers.AbsPathify(viper.GetString("StaticDir") + "/") s.Source = &source.Filesystem{ AvoidPaths: []string{staticDir},@@ -216,26 +216,35 @@
}
func (s *Site) initializeSiteInfo() {+ params, ok := viper.Get("Params").(map[string]interface{})+ if !ok {+ params = make(map[string]interface{})+ }
+
+ permalinks, ok := viper.Get("Permalinks").(PermalinkOverrides)+ if !ok {+ permalinks = make(PermalinkOverrides)
+ }
+
s.Info = SiteInfo{- BaseUrl: template.URL(s.Config.BaseUrl),
- Title: s.Config.Title,
+ BaseUrl: template.URL(viper.GetString("BaseUrl")),+ Title: viper.GetString("Title"),Recent: &s.Pages,
- Config: &s.Config,
- Params: s.Config.Params,
- Permalinks: s.Config.Permalinks,
+ Params: params,
+ Permalinks: permalinks,
}
}
func (s *Site) absLayoutDir() string {- return s.Config.GetAbsPath(s.Config.LayoutDir)
+ return helpers.AbsPathify(viper.GetString("LayoutDir"))}
func (s *Site) absContentDir() string {- return s.Config.GetAbsPath(s.Config.ContentDir)
+ return helpers.AbsPathify(viper.GetString("ContentDir"))}
func (s *Site) absPublishDir() string {- return s.Config.GetAbsPath(s.Config.PublishDir)
+ return helpers.AbsPathify(viper.GetString("PublishDir"))}
func (s *Site) checkDirectories() (err error) {@@ -276,7 +285,7 @@
return err
}
- if s.Config.BuildDrafts || !page.Draft {+ if viper.GetBool("BuildDrafts") || !page.Draft {s.Pages = append(s.Pages, page)
}
@@ -293,7 +302,7 @@
s.Indexes = make(IndexList)
s.Sections = make(Index)
- for _, plural := range s.Config.Indexes {+ for _, plural := range viper.GetStringMapString("Indexes") {s.Indexes[plural] = make(Index)
for _, p := range s.Pages {vals := p.GetParam(plural)
@@ -411,34 +420,38 @@
func (s *Site) RenderIndexes() (err error) {var wg sync.WaitGroup
- for sing, pl := range s.Config.Indexes {- for key, oo := range s.Indexes[pl] {- wg.Add(1)
- go func(k string, o WeightedPages, singular string, plural string) (err error) {- defer wg.Done()
- base := plural + "/" + k
- n := s.NewNode()
- n.Title = strings.Title(k)
- s.setUrls(n, base)
- n.Date = o[0].Page.Date
- n.Data[singular] = o
- n.Data["Pages"] = o.Pages()
- layout := "indexes/" + singular + ".html"
- err = s.render(n, base+".html", layout)
- if err != nil {- return err
- }
- if a := s.Tmpl.Lookup("rss.xml"); a != nil {- // XML Feed
- s.setUrls(n, base+".xml")
- err := s.render(n, base+".xml", "rss.xml")
+ indexes, ok := viper.Get("Indexes").(map[string]string)+ if ok {+ for sing, pl := range indexes {+ for key, oo := range s.Indexes[pl] {+ wg.Add(1)
+ go func(k string, o WeightedPages, singular string, plural string) (err error) {+ defer wg.Done()
+ base := plural + "/" + k
+ n := s.NewNode()
+ n.Title = strings.Title(k)
+ s.setUrls(n, base)
+ n.Date = o[0].Page.Date
+ n.Data[singular] = o
+ n.Data["Pages"] = o.Pages()
+ layout := "indexes/" + singular + ".html"
+ err = s.render(n, base+".html", layout)
if err != nil {return err
}
- }
- return
- }(key, oo, sing, pl)
+
+ if a := s.Tmpl.Lookup("rss.xml"); a != nil {+ // XML Feed
+ s.setUrls(n, base+".xml")
+ err := s.render(n, base+".xml", "rss.xml")
+ if err != nil {+ return err
+ }
+ }
+ return
+ }(key, oo, sing, pl)
+ }
}
}
wg.Wait()
@@ -448,19 +461,23 @@
func (s *Site) RenderIndexesIndexes() (err error) {layout := "indexes/indexes.html"
if s.Tmpl.Lookup(layout) != nil {- for singular, plural := range s.Config.Indexes {- n := s.NewNode()
- n.Title = strings.Title(plural)
- s.setUrls(n, plural)
- n.Data["Singular"] = singular
- n.Data["Plural"] = plural
- n.Data["Index"] = s.Indexes[plural]
- // keep the following just for legacy reasons
- n.Data["OrderedIndex"] = s.Indexes[plural]
- err := s.render(n, plural+"/index.html", layout)
- if err != nil {- return err
+ indexes, ok := viper.Get("Indexes").(map[string]string)+ if ok {+ for singular, plural := range indexes {+ n := s.NewNode()
+ n.Title = strings.Title(plural)
+ s.setUrls(n, plural)
+ n.Data["Singular"] = singular
+ n.Data["Plural"] = plural
+ n.Data["Index"] = s.Indexes[plural]
+ // keep the following just for legacy reasons
+ n.Data["OrderedIndex"] = s.Indexes[plural]
+
+ err := s.render(n, plural+"/index.html", layout)
+ if err != nil {+ return err
+ }
}
}
}
@@ -534,7 +551,10 @@
func (s *Site) Stats() { jww.FEEDBACK.Printf("%d pages created \n", len(s.Pages))- for _, pl := range s.Config.Indexes {+
+ indexes := viper.GetStringMapString("Indexes")+
+ for _, pl := range indexes { jww.FEEDBACK.Printf("%d %s index created\n", len(s.Indexes[pl]), pl)}
}
@@ -546,7 +566,7 @@
}
func (s *Site) permalink(plink string) template.HTML {- return template.HTML(helpers.MakePermalink(string(s.Config.BaseUrl), s.prepUrl(plink)).String())
+ return template.HTML(helpers.MakePermalink(string(viper.GetString("BaseUrl")), s.prepUrl(plink)).String())}
func (s *Site) prepUrl(in string) string {@@ -554,11 +574,11 @@
}
func (s *Site) PrettifyUrl(in string) string {- return helpers.UrlPrep(s.Config.UglyUrls, in)
+ return helpers.UrlPrep(viper.GetBool("UglyUrls"), in)}
func (s *Site) PrettifyPath(in string) string {- return helpers.PathPrep(s.Config.UglyUrls, in)
+ return helpers.PathPrep(viper.GetBool("UglyUrls"), in)}
func (s *Site) NewNode() *Node {@@ -578,8 +598,8 @@
transformLinks := transform.NewEmptyTransforms()
- if s.Config.CanonifyUrls {- absURL, err := transform.AbsURL(s.Config.BaseUrl)
+ if viper.GetBool("CanonifyUrls") {+ absURL, err := transform.AbsURL(viper.GetString("BaseUrl")) if err != nil {return err
}
@@ -641,7 +661,7 @@
if s.Target == nil { s.Target = &target.Filesystem{PublishDir: s.absPublishDir(),
- UglyUrls: s.Config.UglyUrls,
+ UglyUrls: viper.GetBool("UglyUrls"),}
}
}
--
⑨