added global variables, added default date for markdown

This commit is contained in:
Serge A. Zaitsev 2015-08-29 19:54:55 +02:00
parent 85f08718a7
commit aafb2e9d82
2 changed files with 39 additions and 19 deletions

48
zs.go
View File

@ -38,7 +38,7 @@ func split2(s, delim string) (string, string) {
} }
// Parses markdown content. Returns parsed header variables and content // 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) b, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
@ -51,8 +51,14 @@ func md(path string) (Vars, string, error) {
"output": filepath.Join(PUBDIR, url), "output": filepath.Join(PUBDIR, url),
"layout": "index.html", "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 { if strings.Index(s, "\n\n") == -1 {
return Vars{}, s, nil return v, s, nil
} }
header, body := split2(s, "\n\n") header, body := split2(s, "\n\n")
for _, line := range strings.Split(header, "\n") { for _, line := range strings.Split(header, "\n") {
@ -139,7 +145,7 @@ func eval(cmd []string, vars Vars) (string, error) {
// Renders markdown with the given layout into html expanding all the macros // Renders markdown with the given layout into html expanding all the macros
func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error { func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error {
v, body, err := md(path) v, body, err := md(path, vars)
if err != nil { if err != nil {
return err return err
} }
@ -243,7 +249,16 @@ func pluginFunc(cmd string) func() string {
func createFuncs() template.FuncMap { func createFuncs() template.FuncMap {
// Builtin functions // 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 // Plugin functions
files, _ := ioutil.ReadDir(ZSDIR) files, _ := ioutil.ReadDir(ZSDIR)
for _, f := range files { for _, f := range files {
@ -257,17 +272,22 @@ func createFuncs() template.FuncMap {
return funcs return funcs
} }
func buildAll(once bool) { func globals() Vars {
lastModified := time.Unix(0, 0) vars := Vars{}
modified := false
// Convert env variables into zs global variables
globals := Vars{}
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.Split(e, "=") pair := strings.Split(e, "=")
if strings.HasPrefix(pair[0], "ZS_") { if strings.HasPrefix(pair[0], "ZS_") {
globals[strings.ToLower(pair[0][3:])] = pair[1] vars[strings.ToLower(pair[0][3:])] = pair[1]
} }
} }
return vars
}
func buildAll(once bool) {
lastModified := time.Unix(0, 0)
modified := false
vars := globals()
for { for {
os.Mkdir(PUBDIR, 0755) os.Mkdir(PUBDIR, 0755)
funcs := createFuncs() funcs := createFuncs()
@ -290,13 +310,13 @@ func buildAll(once bool) {
ext := filepath.Ext(path) ext := filepath.Ext(path)
if ext == ".md" || ext == ".mkd" { if ext == ".md" || ext == ".mkd" {
log.Println("md: ", path) log.Println("md: ", path)
return buildMarkdown(path, funcs, globals) return buildMarkdown(path, funcs, vars)
} else if ext == ".html" || ext == ".xml" { } else if ext == ".html" || ext == ".xml" {
log.Println("html: ", path) log.Println("html: ", path)
return buildPlain(path, funcs, globals) return buildPlain(path, funcs, vars)
} else if ext == ".amber" { } else if ext == ".amber" {
log.Println("html: ", path) log.Println("html: ", path)
return buildAmber(path, funcs, globals) return buildAmber(path, funcs, vars)
} else if ext == ".gcss" { } else if ext == ".gcss" {
log.Println("css: ", path) log.Println("css: ", path)
return buildGCSS(path) return buildGCSS(path)
@ -341,7 +361,7 @@ func main() {
log.Println("ERROR: filename expected") log.Println("ERROR: filename expected")
return return
} }
if vars, _, err := md(args[0]); err == nil { if vars, _, err := md(args[0], globals()); err == nil {
if len(args) > 1 { if len(args) > 1 {
for _, a := range args[1:] { for _, a := range args[1:] {
fmt.Println(vars[a]) fmt.Println(vars[a])

View File

@ -46,7 +46,7 @@ func TestMD(t *testing.T) {
empty: empty:
bayan: [:|||:] bayan: [:|||:]
this: is a content`)) this: is a content`), Vars{})
if v["title"] != "Hello, world!" { if v["title"] != "Hello, world!" {
t.Error() t.Error()
} }
@ -64,14 +64,14 @@ this: is a content`))
} }
// Test empty md // Test empty md
v, body, _ = md(tmpfile("foo.md", "")) v, body, _ = md(tmpfile("foo.md", ""), Vars{})
if len(v) != 0 || len(body) != 0 { if v["url"] != "foo.html" || len(body) != 0 {
t.Error(v, body) t.Error(v, body)
} }
// Test empty header // Test empty header
v, body, _ = md(tmpfile("foo.md", "Hello")) v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{})
if len(v) != 0 || body != "Hello" { if v["url"] != "foo.html" || body != "Hello" {
t.Error(v, body) t.Error(v, body)
} }
} }