changeset 19:802b96e67bae draft

added global variables, added default date for markdown
author zaitsev.serge
date Sat, 29 Aug 2015 17:54:55 +0000
parents ae3116ea938b
children f8373c23a3ff
files zs.go zs_test.go
diffstat 2 files changed, 41 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/zs.go	Sat Aug 29 16:46:05 2015 +0000
+++ b/zs.go	Sat Aug 29 17:54:55 2015 +0000
@@ -38,7 +38,7 @@
 }
 
 // Parses markdown content. Returns parsed header variables and content
-func md(path string) (Vars, string, error) {
+func md(path string, globals Vars) (Vars, string, error) {
 	b, err := ioutil.ReadFile(path)
 	if err != nil {
 		return nil, "", err
@@ -51,8 +51,14 @@
 		"output": filepath.Join(PUBDIR, url),
 		"layout": "index.html",
 	}
+	if info, err := os.Stat(path); err == nil {
+		v["date"] = info.ModTime().Format("02-01-2006")
+	}
+	for name, value := range globals {
+		v[name] = value
+	}
 	if strings.Index(s, "\n\n") == -1 {
-		return Vars{}, s, nil
+		return v, s, nil
 	}
 	header, body := split2(s, "\n\n")
 	for _, line := range strings.Split(header, "\n") {
@@ -139,7 +145,7 @@
 
 // Renders markdown with the given layout into html expanding all the macros
 func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error {
-	v, body, err := md(path)
+	v, body, err := md(path, vars)
 	if err != nil {
 		return err
 	}
@@ -243,7 +249,16 @@
 
 func createFuncs() template.FuncMap {
 	// Builtin functions
-	funcs := template.FuncMap{}
+	funcs := template.FuncMap{
+		"exec": func(s ...string) string {
+			// Run external command with arguments
+			return ""
+		},
+		"zs": func(args ...string) string {
+			// Run zs with arguments
+			return ""
+		},
+	}
 	// Plugin functions
 	files, _ := ioutil.ReadDir(ZSDIR)
 	for _, f := range files {
@@ -257,17 +272,22 @@
 	return funcs
 }
 
+func globals() Vars {
+	vars := Vars{}
+	for _, e := range os.Environ() {
+		pair := strings.Split(e, "=")
+		if strings.HasPrefix(pair[0], "ZS_") {
+			vars[strings.ToLower(pair[0][3:])] = pair[1]
+		}
+	}
+	return vars
+}
+
 func buildAll(once bool) {
 	lastModified := time.Unix(0, 0)
 	modified := false
-	// Convert env variables into zs global variables
-	globals := Vars{}
-	for _, e := range os.Environ() {
-		pair := strings.Split(e, "=")
-		if strings.HasPrefix(pair[0], "ZS_") {
-			globals[strings.ToLower(pair[0][3:])] = pair[1]
-		}
-	}
+
+	vars := globals()
 	for {
 		os.Mkdir(PUBDIR, 0755)
 		funcs := createFuncs()
@@ -290,13 +310,13 @@
 				ext := filepath.Ext(path)
 				if ext == ".md" || ext == ".mkd" {
 					log.Println("md: ", path)
-					return buildMarkdown(path, funcs, globals)
+					return buildMarkdown(path, funcs, vars)
 				} else if ext == ".html" || ext == ".xml" {
 					log.Println("html: ", path)
-					return buildPlain(path, funcs, globals)
+					return buildPlain(path, funcs, vars)
 				} else if ext == ".amber" {
 					log.Println("html: ", path)
-					return buildAmber(path, funcs, globals)
+					return buildAmber(path, funcs, vars)
 				} else if ext == ".gcss" {
 					log.Println("css: ", path)
 					return buildGCSS(path)
@@ -341,7 +361,7 @@
 			log.Println("ERROR: filename expected")
 			return
 		}
-		if vars, _, err := md(args[0]); err == nil {
+		if vars, _, err := md(args[0], globals()); err == nil {
 			if len(args) > 1 {
 				for _, a := range args[1:] {
 					fmt.Println(vars[a])
--- a/zs_test.go	Sat Aug 29 16:46:05 2015 +0000
+++ b/zs_test.go	Sat Aug 29 17:54:55 2015 +0000
@@ -46,7 +46,7 @@
 	empty:
 	bayan: [:|||:]
 
-this: is a content`))
+this: is a content`), Vars{})
 	if v["title"] != "Hello, world!" {
 		t.Error()
 	}
@@ -64,14 +64,14 @@
 	}
 
 	// Test empty md
-	v, body, _ = md(tmpfile("foo.md", ""))
-	if len(v) != 0 || len(body) != 0 {
+	v, body, _ = md(tmpfile("foo.md", ""), Vars{})
+	if v["url"] != "foo.html" || len(body) != 0 {
 		t.Error(v, body)
 	}
 
 	// Test empty header
-	v, body, _ = md(tmpfile("foo.md", "Hello"))
-	if len(v) != 0 || body != "Hello" {
+	v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{})
+	if v["url"] != "foo.html" || body != "Hello" {
 		t.Error(v, body)
 	}
 }