Hugo config abstracted into a general purpose config library called "Viper".

Hugo casting now in own library called "cast"
This commit is contained in:
spf13 2014-04-05 01:26:43 -04:00
parent a01056b98a
commit 62dd1d45c1
9 changed files with 196 additions and 469 deletions

View file

@ -25,7 +25,7 @@ var check = &cobra.Command{
content provided and will give feedback.`, content provided and will give feedback.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
InitializeConfig() InitializeConfig()
site := hugolib.Site{Config: *Config} site := hugolib.Site{}
site.Analyze() site.Analyze()
}, },
} }

View file

@ -24,14 +24,16 @@ import (
"github.com/mostafah/fsync" "github.com/mostafah/fsync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/utils" "github.com/spf13/hugo/utils"
"github.com/spf13/hugo/watcher" "github.com/spf13/hugo/watcher"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro" "github.com/spf13/nitro"
"github.com/spf13/viper"
) )
var Config *hugolib.Config //var Config *hugolib.Config
var HugoCmd = &cobra.Command{ var HugoCmd = &cobra.Command{
Use: "hugo", Use: "hugo",
Short: "Hugo is a very fast static site generator", Short: "Hugo is a very fast static site generator",
@ -78,34 +80,58 @@ func init() {
} }
func InitializeConfig() { 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 { if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
Config.BuildDrafts = Draft viper.Set("BuildDrafts", Draft)
} }
if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed { if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed {
Config.UglyUrls = UglyUrls viper.Set("UglyUrls", UglyUrls)
} }
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed { if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
Config.Verbose = Verbose viper.Set("Verbose", Verbose)
} }
if hugoCmdV.PersistentFlags().Lookup("logfile").Changed { if hugoCmdV.PersistentFlags().Lookup("logfile").Changed {
Config.LogFile = LogFile viper.Set("LogFile", LogFile)
} }
if BaseUrl != "" { if BaseUrl != "" {
Config.BaseUrl = BaseUrl if !strings.HasSuffix(BaseUrl, "/") {
BaseUrl = BaseUrl + "/"
}
viper.Set("BaseUrl", BaseUrl)
} }
if Destination != "" { if Destination != "" {
Config.PublishDir = Destination viper.Set("PublishDir", Destination)
} }
if VerboseLog || Logging || Config.LogFile != "" { if Source != "" {
if Config.LogFile != "" { viper.Set("WorkingDir", Source)
jww.SetLogFile(Config.LogFile) } 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 { } else {
jww.UseTempLogFile("hugo") jww.UseTempLogFile("hugo")
} }
@ -113,7 +139,7 @@ func InitializeConfig() {
jww.DiscardLogging() jww.DiscardLogging()
} }
if Config.Verbose { if viper.GetBool("verbose") {
jww.SetStdoutThreshold(jww.LevelDebug) jww.SetStdoutThreshold(jww.LevelDebug)
} }
@ -123,7 +149,7 @@ func InitializeConfig() {
} }
func build(watches ...bool) { 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 watch := false
if len(watches) > 0 && watches[0] { if len(watches) > 0 && watches[0] {
watch = true watch = true
@ -131,20 +157,22 @@ func build(watches ...bool) {
utils.StopOnErr(buildSite(BuildWatch || watch)) utils.StopOnErr(buildSite(BuildWatch || watch))
if BuildWatch { 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") jww.FEEDBACK.Println("Press ctrl+c to stop")
utils.CheckErr(NewWatcher(0)) utils.CheckErr(NewWatcher(0))
} }
} }
func copyStatic() error { func copyStatic() error {
staticDir := Config.GetAbsPath(Config.StaticDir + "/") staticDir := helpers.AbsPathify(viper.GetString("StaticDir")) + "/"
if _, err := os.Stat(staticDir); os.IsNotExist(err) { if _, err := os.Stat(staticDir); os.IsNotExist(err) {
return nil return nil
} }
publishDir := helpers.AbsPathify(viper.GetString("PublishDir")) + "/"
// Copy Static to Destination // 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 { func getDirList() []string {
@ -161,16 +189,16 @@ func getDirList() []string {
return nil return nil
} }
filepath.Walk(Config.GetAbsPath(Config.ContentDir), walker) filepath.Walk(helpers.AbsPathify(viper.GetString("ContentDir")), walker)
filepath.Walk(Config.GetAbsPath(Config.LayoutDir), walker) filepath.Walk(helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
filepath.Walk(Config.GetAbsPath(Config.StaticDir), walker) filepath.Walk(helpers.AbsPathify(viper.GetString("StaticDir")), walker)
return a return a
} }
func buildSite(watching ...bool) (err error) { func buildSite(watching ...bool) (err error) {
startTime := time.Now() startTime := time.Now()
site := &hugolib.Site{Config: *Config} site := &hugolib.Site{}
if len(watching) > 0 && watching[0] { if len(watching) > 0 && watching[0] {
site.RunMode.Watching = true site.RunMode.Watching = true
} }
@ -226,7 +254,7 @@ func NewWatcher(port int) error {
continue 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 static_changed = static_changed || isstatic
dynamic_changed = dynamic_changed || !isstatic dynamic_changed = dynamic_changed || !isstatic
@ -240,7 +268,7 @@ func NewWatcher(port int) error {
if static_changed { if static_changed {
fmt.Print("Static file changed, syncing\n\n") 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 { if dynamic_changed {

View file

@ -21,7 +21,9 @@ import (
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/helpers"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
) )
var serverPort int var serverPort int
@ -55,16 +57,16 @@ func server(cmd *cobra.Command, args []string) {
} }
if serverAppend { if serverAppend {
Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") + ":" + strconv.Itoa(serverPort) viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort))
} else { } else {
Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/"))
} }
build(serverWatch) build(serverWatch)
// Watch runs its own server as part of the routine // Watch runs its own server as part of the routine
if serverWatch { 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) err := NewWatcher(serverPort)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -75,17 +77,17 @@ func server(cmd *cobra.Command, args []string) {
} }
func serve(port int) { 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 == "" { 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 { } else {
jww.FEEDBACK.Printf("Web Server is available at http://localhost:%v\n", port) jww.FEEDBACK.Printf("Web Server is available at http://localhost:%v\n", port)
} }
fmt.Println("Press ctrl+c to stop") 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 { if err != nil {
jww.ERROR.Printf("Error: %s\n", err.Error()) jww.ERROR.Printf("Error: %s\n", err.Error())
os.Exit(1) os.Exit(1)

View file

@ -14,11 +14,14 @@
package helpers package helpers
import ( import (
"fmt"
"os" "os"
"path" "path"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"unicode" "unicode"
"github.com/spf13/viper"
) )
var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]") var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]")
@ -75,6 +78,14 @@ func Exists(path string) (bool, error) {
return false, err 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) { func FileAndExt(in string) (name string, ext string) {
ext = path.Ext(in) ext = path.Ext(in)
base := path.Base(in) base := path.Base(in)
@ -117,3 +128,26 @@ func PrettifyPath(in string) string {
} }
} }
} }
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
}

View file

@ -1,203 +0,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)
}
}
}

View file

@ -14,8 +14,9 @@
package hugolib package hugolib
import ( import (
"github.com/spf13/hugo/helpers"
"sort" "sort"
"github.com/spf13/hugo/helpers"
) )
/* /*

View file

@ -1,157 +0,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 ""
}

View file

@ -25,10 +25,12 @@ import (
"time" "time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
"github.com/spf13/hugo/template/bundle" "github.com/spf13/hugo/template/bundle"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/theplant/blackfriday" "github.com/theplant/blackfriday"
"launchpad.net/goyaml" "launchpad.net/goyaml"
json "launchpad.net/rjson" json "launchpad.net/rjson"
@ -252,10 +254,10 @@ func (p *Page) permalink() (*url.URL, error) {
// fmt.Printf("have a section override for %q in section %s → %s\n", p.Title, p.Section, permalink) // fmt.Printf("have a section override for %q in section %s → %s\n", p.Title, p.Section, permalink)
} else { } else {
if len(pSlug) > 0 { 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 { } else {
_, t := path.Split(p.FileName) _, 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,41 +329,41 @@ func (page *Page) update(f interface{}) error {
loki := strings.ToLower(k) loki := strings.ToLower(k)
switch loki { switch loki {
case "title": case "title":
page.Title = interfaceToString(v) page.Title = cast.ToString(v)
case "linktitle": case "linktitle":
page.linkTitle = interfaceToString(v) page.linkTitle = cast.ToString(v)
case "description": case "description":
page.Description = interfaceToString(v) page.Description = cast.ToString(v)
case "slug": case "slug":
page.Slug = helpers.Urlize(interfaceToString(v)) page.Slug = helpers.Urlize(cast.ToString(v))
case "url": 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) 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": case "type":
page.contentType = interfaceToString(v) page.contentType = cast.ToString(v)
case "keywords": case "keywords":
page.Keywords = interfaceArrayToStringArray(v) page.Keywords = cast.ToStringSlice(v)
case "date", "pubdate": case "date", "pubdate":
page.Date = interfaceToTime(v) page.Date = cast.ToTime(v)
case "draft": case "draft":
page.Draft = interfaceToBool(v) page.Draft = cast.ToBool(v)
case "layout": case "layout":
page.layout = interfaceToString(v) page.layout = cast.ToString(v)
case "markup": case "markup":
page.Markup = interfaceToString(v) page.Markup = cast.ToString(v)
case "weight": case "weight":
page.Weight = interfaceToInt(v) page.Weight = cast.ToInt(v)
case "aliases": case "aliases":
page.Aliases = interfaceArrayToStringArray(v) page.Aliases = cast.ToStringSlice(v)
for _, alias := range page.Aliases { for _, alias := range page.Aliases {
if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") { if strings.HasPrefix(alias, "http://") || strings.HasPrefix(alias, "https://") {
return fmt.Errorf("Only relative aliases are supported, %v provided", alias) return fmt.Errorf("Only relative aliases are supported, %v provided", alias)
} }
} }
case "status": case "status":
page.Status = interfaceToString(v) page.Status = cast.ToString(v)
default: default:
// If not one of the explicit values, store in Params // If not one of the explicit values, store in Params
switch vv := v.(type) { switch vv := v.(type) {
@ -380,7 +382,7 @@ func (page *Page) update(f interface{}) error {
case []interface{}: case []interface{}:
var a = make([]string, len(vvv)) var a = make([]string, len(vvv))
for i, u := range vvv { for i, u := range vvv {
a[i] = interfaceToString(u) a[i] = cast.ToString(u)
} }
page.Params[loki] = a page.Params[loki] = a
} }
@ -400,15 +402,15 @@ func (page *Page) GetParam(key string) interface{} {
switch v.(type) { switch v.(type) {
case bool: case bool:
return interfaceToBool(v) return cast.ToBool(v)
case string: case string:
return interfaceToString(v) return cast.ToString(v)
case int64, int32, int16, int8, int: case int64, int32, int16, int8, int:
return interfaceToInt(v) return cast.ToInt(v)
case float64, float32: case float64, float32:
return interfaceToFloat64(v) return cast.ToFloat64(v)
case time.Time: case time.Time:
return interfaceToTime(v) return cast.ToTime(v)
case []string: case []string:
return v return v
} }

View file

@ -31,6 +31,7 @@ import (
"github.com/spf13/hugo/transform" "github.com/spf13/hugo/transform"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/nitro" "github.com/spf13/nitro"
"github.com/spf13/viper"
) )
var _ = transform.AbsURL var _ = transform.AbsURL
@ -55,7 +56,6 @@ var DefaultTimer *nitro.B
// //
// 5. The entire collection of files is written to disk. // 5. The entire collection of files is written to disk.
type Site struct { type Site struct {
Config Config
Pages Pages Pages Pages
Tmpl bundle.Template Tmpl bundle.Template
Indexes IndexList Indexes IndexList
@ -77,7 +77,7 @@ type SiteInfo struct {
Recent *Pages Recent *Pages
LastChange time.Time LastChange time.Time
Title string Title string
Config *Config ConfigGet func(key string) interface{}
Permalinks PermalinkOverrides Permalinks PermalinkOverrides
Params map[string]interface{} Params map[string]interface{}
} }
@ -202,7 +202,7 @@ func (s *Site) initialize() (err error) {
return err return err
} }
staticDir := s.Config.GetAbsPath(s.Config.StaticDir + "/") staticDir := helpers.AbsPathify(viper.GetString("StaticDir") + "/")
s.Source = &source.Filesystem{ s.Source = &source.Filesystem{
AvoidPaths: []string{staticDir}, AvoidPaths: []string{staticDir},
@ -216,26 +216,35 @@ func (s *Site) initialize() (err error) {
} }
func (s *Site) initializeSiteInfo() { 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{ s.Info = SiteInfo{
BaseUrl: template.URL(s.Config.BaseUrl), BaseUrl: template.URL(viper.GetString("BaseUrl")),
Title: s.Config.Title, Title: viper.GetString("Title"),
Recent: &s.Pages, Recent: &s.Pages,
Config: &s.Config, Params: params,
Params: s.Config.Params, Permalinks: permalinks,
Permalinks: s.Config.Permalinks,
} }
} }
func (s *Site) absLayoutDir() string { func (s *Site) absLayoutDir() string {
return s.Config.GetAbsPath(s.Config.LayoutDir) return helpers.AbsPathify(viper.GetString("LayoutDir"))
} }
func (s *Site) absContentDir() string { func (s *Site) absContentDir() string {
return s.Config.GetAbsPath(s.Config.ContentDir) return helpers.AbsPathify(viper.GetString("ContentDir"))
} }
func (s *Site) absPublishDir() string { func (s *Site) absPublishDir() string {
return s.Config.GetAbsPath(s.Config.PublishDir) return helpers.AbsPathify(viper.GetString("PublishDir"))
} }
func (s *Site) checkDirectories() (err error) { func (s *Site) checkDirectories() (err error) {
@ -276,7 +285,7 @@ func (s *Site) CreatePages() (err error) {
return err return err
} }
if s.Config.BuildDrafts || !page.Draft { if viper.GetBool("BuildDrafts") || !page.Draft {
s.Pages = append(s.Pages, page) s.Pages = append(s.Pages, page)
} }
@ -293,7 +302,7 @@ func (s *Site) BuildSiteMeta() (err error) {
s.Indexes = make(IndexList) s.Indexes = make(IndexList)
s.Sections = make(Index) s.Sections = make(Index)
for _, plural := range s.Config.Indexes { for _, plural := range viper.GetStringMapString("Indexes") {
s.Indexes[plural] = make(Index) s.Indexes[plural] = make(Index)
for _, p := range s.Pages { for _, p := range s.Pages {
vals := p.GetParam(plural) vals := p.GetParam(plural)
@ -411,34 +420,38 @@ func (s *Site) RenderPages() (err error) {
func (s *Site) RenderIndexes() (err error) { func (s *Site) RenderIndexes() (err error) {
var wg sync.WaitGroup 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 { indexes, ok := viper.Get("Indexes").(map[string]string)
// XML Feed if ok {
s.setUrls(n, base+".xml") for sing, pl := range indexes {
err := s.render(n, base+".xml", "rss.xml") 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 { if err != nil {
return err return err
} }
}
return if a := s.Tmpl.Lookup("rss.xml"); a != nil {
}(key, oo, sing, pl) // 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() wg.Wait()
@ -448,19 +461,23 @@ func (s *Site) RenderIndexes() (err error) {
func (s *Site) RenderIndexesIndexes() (err error) { func (s *Site) RenderIndexesIndexes() (err error) {
layout := "indexes/indexes.html" layout := "indexes/indexes.html"
if s.Tmpl.Lookup(layout) != nil { 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) indexes, ok := viper.Get("Indexes").(map[string]string)
if err != nil { if ok {
return err 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) RenderHomePage() error {
func (s *Site) Stats() { func (s *Site) Stats() {
jww.FEEDBACK.Printf("%d pages created \n", len(s.Pages)) 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) jww.FEEDBACK.Printf("%d %s index created\n", len(s.Indexes[pl]), pl)
} }
} }
@ -546,7 +566,7 @@ func (s *Site) setUrls(n *Node, in string) {
} }
func (s *Site) permalink(plink string) template.HTML { 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 { func (s *Site) prepUrl(in string) string {
@ -554,11 +574,11 @@ func (s *Site) prepUrl(in string) string {
} }
func (s *Site) PrettifyUrl(in string) string { 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 { 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 { func (s *Site) NewNode() *Node {
@ -578,8 +598,8 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
transformLinks := transform.NewEmptyTransforms() transformLinks := transform.NewEmptyTransforms()
if s.Config.CanonifyUrls { if viper.GetBool("CanonifyUrls") {
absURL, err := transform.AbsURL(s.Config.BaseUrl) absURL, err := transform.AbsURL(viper.GetString("BaseUrl"))
if err != nil { if err != nil {
return err return err
} }
@ -641,7 +661,7 @@ func (s *Site) initTarget() {
if s.Target == nil { if s.Target == nil {
s.Target = &target.Filesystem{ s.Target = &target.Filesystem{
PublishDir: s.absPublishDir(), PublishDir: s.absPublishDir(),
UglyUrls: s.Config.UglyUrls, UglyUrls: viper.GetBool("UglyUrls"),
} }
} }
} }