Changeset 20 in code
- Timestamp:
- Aug 29, 2015, 5:54:55 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zs.go
r19 r20 39 39 40 40 // Parses markdown content. Returns parsed header variables and content 41 func md(path string ) (Vars, string, error) {41 func md(path string, globals Vars) (Vars, string, error) { 42 42 b, err := ioutil.ReadFile(path) 43 43 if err != nil { … … 52 52 "layout": "index.html", 53 53 } 54 if info, err := os.Stat(path); err == nil { 55 v["date"] = info.ModTime().Format("02-01-2006") 56 } 57 for name, value := range globals { 58 v[name] = value 59 } 54 60 if strings.Index(s, "\n\n") == -1 { 55 return Vars{}, s, nil61 return v, s, nil 56 62 } 57 63 header, body := split2(s, "\n\n") … … 140 146 // Renders markdown with the given layout into html expanding all the macros 141 147 func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error { 142 v, body, err := md(path )148 v, body, err := md(path, vars) 143 149 if err != nil { 144 150 return err … … 244 250 func createFuncs() template.FuncMap { 245 251 // Builtin functions 246 funcs := template.FuncMap{} 252 funcs := template.FuncMap{ 253 "exec": func(s ...string) string { 254 // Run external command with arguments 255 return "" 256 }, 257 "zs": func(args ...string) string { 258 // Run zs with arguments 259 return "" 260 }, 261 } 247 262 // Plugin functions 248 263 files, _ := ioutil.ReadDir(ZSDIR) … … 258 273 } 259 274 275 func globals() Vars { 276 vars := Vars{} 277 for _, e := range os.Environ() { 278 pair := strings.Split(e, "=") 279 if strings.HasPrefix(pair[0], "ZS_") { 280 vars[strings.ToLower(pair[0][3:])] = pair[1] 281 } 282 } 283 return vars 284 } 285 260 286 func buildAll(once bool) { 261 287 lastModified := time.Unix(0, 0) 262 288 modified := false 263 // Convert env variables into zs global variables 264 globals := Vars{} 265 for _, e := range os.Environ() { 266 pair := strings.Split(e, "=") 267 if strings.HasPrefix(pair[0], "ZS_") { 268 globals[strings.ToLower(pair[0][3:])] = pair[1] 269 } 270 } 289 290 vars := globals() 271 291 for { 272 292 os.Mkdir(PUBDIR, 0755) … … 291 311 if ext == ".md" || ext == ".mkd" { 292 312 log.Println("md: ", path) 293 return buildMarkdown(path, funcs, globals)313 return buildMarkdown(path, funcs, vars) 294 314 } else if ext == ".html" || ext == ".xml" { 295 315 log.Println("html: ", path) 296 return buildPlain(path, funcs, globals)316 return buildPlain(path, funcs, vars) 297 317 } else if ext == ".amber" { 298 318 log.Println("html: ", path) 299 return buildAmber(path, funcs, globals)319 return buildAmber(path, funcs, vars) 300 320 } else if ext == ".gcss" { 301 321 log.Println("css: ", path) … … 342 362 return 343 363 } 344 if vars, _, err := md(args[0] ); err == nil {364 if vars, _, err := md(args[0], globals()); err == nil { 345 365 if len(args) > 1 { 346 366 for _, a := range args[1:] { -
trunk/zs_test.go
r19 r20 47 47 bayan: [:|||:] 48 48 49 this: is a content`) )49 this: is a content`), Vars{}) 50 50 if v["title"] != "Hello, world!" { 51 51 t.Error() … … 65 65 66 66 // Test empty md 67 v, body, _ = md(tmpfile("foo.md", "") )68 if len(v) != 0|| len(body) != 0 {67 v, body, _ = md(tmpfile("foo.md", ""), Vars{}) 68 if v["url"] != "foo.html" || len(body) != 0 { 69 69 t.Error(v, body) 70 70 } 71 71 72 72 // Test empty header 73 v, body, _ = md(tmpfile("foo.md", "Hello") )74 if len(v) != 0|| body != "Hello" {73 v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{}) 74 if v["url"] != "foo.html" || body != "Hello" { 75 75 t.Error(v, body) 76 76 }
Note:
See TracChangeset
for help on using the changeset viewer.