Mercurial > yakumo_izuru > aya
comparison zs.go @ 39:806ca2102d6b draft
more obvious rules of variable overriding
author | zaitsev.serge |
---|---|
date | Fri, 04 Sep 2015 12:49:56 +0000 |
parents | a88ccfbd4103 |
children | 9c8d908163a9 |
comparison
equal
deleted
inserted
replaced
38:ea1bcd29a195 | 39:806ca2102d6b |
---|---|
102 // Pick some default values for content-dependent variables | 102 // Pick some default values for content-dependent variables |
103 v := Vars{} | 103 v := Vars{} |
104 title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1) | 104 title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1) |
105 v["title"] = strings.ToTitle(title) | 105 v["title"] = strings.ToTitle(title) |
106 v["description"] = "" | 106 v["description"] = "" |
107 | |
108 // Copy globals (will override title and description for markdown layouts | |
109 for name, value := range globals { | |
110 v[name] = value | |
111 } | |
112 | |
113 // Add default values extracted from file name/path | |
114 if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil { | |
115 v["layout"] = "layout.amber" | |
116 } else { | |
117 v["layout"] = "layout.html" | |
118 } | |
119 v["file"] = path | 107 v["file"] = path |
120 v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html" | 108 v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html" |
121 v["output"] = filepath.Join(PUBDIR, v["url"]) | 109 v["output"] = filepath.Join(PUBDIR, v["url"]) |
110 | |
111 // Override default values with globals | |
112 for name, value := range globals { | |
113 v[name] = value | |
114 } | |
115 | |
116 // Add layout if none is specified | |
117 if _, ok := v["layout"]; !ok { | |
118 if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil { | |
119 v["layout"] = "layout.amber" | |
120 } else { | |
121 v["layout"] = "layout.html" | |
122 } | |
123 } | |
122 | 124 |
123 delim := "\n---\n" | 125 delim := "\n---\n" |
124 if sep := strings.Index(s, delim); sep == -1 { | 126 if sep := strings.Index(s, delim); sep == -1 { |
125 return v, s, nil | 127 return v, s, nil |
126 } else { | 128 } else { |
130 vars := Vars{} | 132 vars := Vars{} |
131 if err := yaml.Unmarshal([]byte(header), &vars); err != nil { | 133 if err := yaml.Unmarshal([]byte(header), &vars); err != nil { |
132 fmt.Println("ERROR: failed to parse header", err) | 134 fmt.Println("ERROR: failed to parse header", err) |
133 return nil, "", err | 135 return nil, "", err |
134 } else { | 136 } else { |
137 // Override default values + globals with the ones defines in the file | |
135 for key, value := range vars { | 138 for key, value := range vars { |
136 v[key] = value | 139 v[key] = value |
137 } | 140 } |
138 } | 141 } |
139 if strings.HasPrefix(v["url"], "./") { | 142 if strings.HasPrefix(v["url"], "./") { |
396 case "var": | 399 case "var": |
397 if len(args) == 0 { | 400 if len(args) == 0 { |
398 fmt.Println("var: filename expected") | 401 fmt.Println("var: filename expected") |
399 } else { | 402 } else { |
400 s := "" | 403 s := "" |
401 if vars, _, err := getVars(args[0], globals()); err != nil { | 404 if vars, _, err := getVars(args[0], Vars{}); err != nil { |
402 fmt.Println("var: " + err.Error()) | 405 fmt.Println("var: " + err.Error()) |
403 } else { | 406 } else { |
404 if len(args) > 1 { | 407 if len(args) > 1 { |
405 for _, a := range args[1:] { | 408 for _, a := range args[1:] { |
406 s = s + vars[a] + "\n" | 409 s = s + vars[a] + "\n" |