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

@@ -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
import (
"github.com/spf13/hugo/helpers"
"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"
"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 @@ 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)
} 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,41 +329,41 @@ func (page *Page) update(f interface{}) error {
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)
}
}
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 @@ func (page *Page) update(f interface{}) error {
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 @@ func (page *Page) GetParam(key string) interface{} {
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
}

View File

@@ -31,6 +31,7 @@ import (
"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 @@ var DefaultTimer *nitro.B
//
// 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 @@ type SiteInfo struct {
Recent *Pages
LastChange time.Time
Title string
Config *Config
ConfigGet func(key string) interface{}
Permalinks PermalinkOverrides
Params map[string]interface{}
}
@@ -202,7 +202,7 @@ func (s *Site) initialize() (err error) {
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) initialize() (err error) {
}
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 @@ func (s *Site) CreatePages() (err error) {
return err
}
if s.Config.BuildDrafts || !page.Draft {
if viper.GetBool("BuildDrafts") || !page.Draft {
s.Pages = append(s.Pages, page)
}
@@ -293,7 +302,7 @@ func (s *Site) BuildSiteMeta() (err error) {
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) RenderPages() (err error) {
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) RenderIndexes() (err error) {
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) RenderHomePage() error {
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) setUrls(n *Node, in string) {
}
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) prepUrl(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 {
return helpers.PathPrep(s.Config.UglyUrls, in)
return helpers.PathPrep(viper.GetBool("UglyUrls"), in)
}
func (s *Site) NewNode() *Node {
@@ -578,8 +598,8 @@ func (s *Site) render(d interface{}, out string, layouts ...string) (err error)
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 @@ func (s *Site) initTarget() {
if s.Target == nil {
s.Target = &target.Filesystem{
PublishDir: s.absPublishDir(),
UglyUrls: s.Config.UglyUrls,
UglyUrls: viper.GetBool("UglyUrls"),
}
}
}