From 6b2dbafa34ad1261800f17a1d747a8433660c31c Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Sun, 30 Aug 2015 14:22:00 +0200 Subject: [PATCH] fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions --- zs.go | 44 ++++++++++++++++++++++++++++---------------- zs_util.go | 14 +++++++++----- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/zs.go b/zs.go index 241a53a..6a3bc8f 100644 --- a/zs.go +++ b/zs.go @@ -132,15 +132,16 @@ func buildHTML(path string, w io.Writer, funcs Funcs, vars Vars) error { if err != nil { return err } - output := filepath.Join(PUBDIR, path) - if s, ok := vars["output"]; ok { - output = s - } - err = ioutil.WriteFile(output, []byte(content), 0666) - if err != nil { - return err + if w == nil { + f, err := os.Create(filepath.Join(PUBDIR, path)) + if err != nil { + return err + } + defer f.Close() + w = f } - return nil + _, err = io.WriteString(w, content) + return err } // Renders .amber file into .html @@ -150,6 +151,15 @@ func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error { if err != nil { return err } + + data := map[string]interface{}{} + for k, v := range vars { + data[k] = v + } + for k, v := range funcs { + data[k] = v + } + t, err := a.Compile() if err != nil { return err @@ -162,7 +172,7 @@ func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error { defer f.Close() w = f } - return t.Execute(w, vars) + return t.Execute(w, data) } // Compiles .gcss into .css @@ -275,15 +285,17 @@ func main() { args := os.Args[2:] switch cmd { case "build": - buildAll(false) - case "watch": - buildAll(true) - case "print": - if len(args) != 1 { - fmt.Println("ERROR: filename expected") + if len(args) == 0 { + buildAll(false) + } else if len(args) == 1 { + if err := build(args[0], os.Stdout, builtins(), globals()); err != nil { + fmt.Println("ERROR: " + err.Error()) + } } else { - build(args[0], os.Stdout, builtins(), globals()) + fmt.Println("ERROR: too many arguments") } + case "watch": + buildAll(true) case "var": fmt.Println(Var(args)) case "lorem": diff --git a/zs_util.go b/zs_util.go index f1c7a37..8b35caa 100644 --- a/zs_util.go +++ b/zs_util.go @@ -19,7 +19,7 @@ func varFunc(s string) func() string { func pluginFunc(cmd string, vars Vars) func(args ...string) string { return func(args ...string) string { out := bytes.NewBuffer(nil) - if err := run(cmd, args, vars, out); err != nil { + if err := run(filepath.Join(ZSDIR, cmd), args, vars, out); err != nil { return cmd + ":" + err.Error() } else { return string(out.Bytes()) @@ -28,15 +28,19 @@ func pluginFunc(cmd string, vars Vars) func(args ...string) string { } func builtins() Funcs { - exec := func(s ...string) string { + exec := func(cmd string, args ...string) string { + out := bytes.NewBuffer(nil) + if err := run(cmd, args, Vars{}, out); err != nil { + return cmd + ":" + err.Error() + } else { + return string(out.Bytes()) + } return "" } return Funcs{ "exec": exec, "zs": func(args ...string) string { - cmd := []string{"zs"} - cmd = append(cmd, args...) - return exec(cmd...) + return exec(os.Args[0], args...) }, } } -- 2.43.0