more obvious rules of variable overriding

This commit is contained in:
Serge A. Zaitsev 2015-09-04 14:49:56 +02:00
parent 76175692a6
commit e21b87dfb6

15
zs.go
View File

@ -104,21 +104,23 @@ func getVars(path string, globals Vars) (Vars, string, error) {
title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1) title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1)
v["title"] = strings.ToTitle(title) v["title"] = strings.ToTitle(title)
v["description"] = "" v["description"] = ""
v["file"] = path
v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
v["output"] = filepath.Join(PUBDIR, v["url"])
// Copy globals (will override title and description for markdown layouts // Override default values with globals
for name, value := range globals { for name, value := range globals {
v[name] = value v[name] = value
} }
// Add default values extracted from file name/path // Add layout if none is specified
if _, ok := v["layout"]; !ok {
if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil { if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil {
v["layout"] = "layout.amber" v["layout"] = "layout.amber"
} else { } else {
v["layout"] = "layout.html" v["layout"] = "layout.html"
} }
v["file"] = path }
v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
v["output"] = filepath.Join(PUBDIR, v["url"])
delim := "\n---\n" delim := "\n---\n"
if sep := strings.Index(s, delim); sep == -1 { if sep := strings.Index(s, delim); sep == -1 {
@ -132,6 +134,7 @@ func getVars(path string, globals Vars) (Vars, string, error) {
fmt.Println("ERROR: failed to parse header", err) fmt.Println("ERROR: failed to parse header", err)
return nil, "", err return nil, "", err
} else { } else {
// Override default values + globals with the ones defines in the file
for key, value := range vars { for key, value := range vars {
v[key] = value v[key] = value
} }
@ -398,7 +401,7 @@ func main() {
fmt.Println("var: filename expected") fmt.Println("var: filename expected")
} else { } else {
s := "" s := ""
if vars, _, err := getVars(args[0], globals()); err != nil { if vars, _, err := getVars(args[0], Vars{}); err != nil {
fmt.Println("var: " + err.Error()) fmt.Println("var: " + err.Error())
} else { } else {
if len(args) > 1 { if len(args) > 1 {