fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions

This commit is contained in:
Serge A. Zaitsev 2015-08-30 14:22:00 +02:00
parent 9d486cc767
commit 6b2dbafa34
2 changed files with 37 additions and 21 deletions

44
zs.go
View File

@ -132,15 +132,16 @@ func buildHTML(path string, w io.Writer, funcs Funcs, vars Vars) error {
if err != nil { if err != nil {
return err return err
} }
output := filepath.Join(PUBDIR, path) if w == nil {
if s, ok := vars["output"]; ok { f, err := os.Create(filepath.Join(PUBDIR, path))
output = s if err != nil {
return err
}
defer f.Close()
w = f
} }
err = ioutil.WriteFile(output, []byte(content), 0666) _, err = io.WriteString(w, content)
if err != nil { return err
return err
}
return nil
} }
// Renders .amber file into .html // Renders .amber file into .html
@ -150,6 +151,15 @@ func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error {
if err != nil { if err != nil {
return err 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() t, err := a.Compile()
if err != nil { if err != nil {
return err return err
@ -162,7 +172,7 @@ func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error {
defer f.Close() defer f.Close()
w = f w = f
} }
return t.Execute(w, vars) return t.Execute(w, data)
} }
// Compiles .gcss into .css // Compiles .gcss into .css
@ -275,15 +285,17 @@ func main() {
args := os.Args[2:] args := os.Args[2:]
switch cmd { switch cmd {
case "build": case "build":
buildAll(false) 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 {
fmt.Println("ERROR: too many arguments")
}
case "watch": case "watch":
buildAll(true) buildAll(true)
case "print":
if len(args) != 1 {
fmt.Println("ERROR: filename expected")
} else {
build(args[0], os.Stdout, builtins(), globals())
}
case "var": case "var":
fmt.Println(Var(args)) fmt.Println(Var(args))
case "lorem": case "lorem":

View File

@ -19,7 +19,7 @@ func varFunc(s string) func() string {
func pluginFunc(cmd string, vars Vars) func(args ...string) string { func pluginFunc(cmd string, vars Vars) func(args ...string) string {
return func(args ...string) string { return func(args ...string) string {
out := bytes.NewBuffer(nil) 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() return cmd + ":" + err.Error()
} else { } else {
return string(out.Bytes()) return string(out.Bytes())
@ -28,15 +28,19 @@ func pluginFunc(cmd string, vars Vars) func(args ...string) string {
} }
func builtins() Funcs { 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 ""
} }
return Funcs{ return Funcs{
"exec": exec, "exec": exec,
"zs": func(args ...string) string { "zs": func(args ...string) string {
cmd := []string{"zs"} return exec(os.Args[0], args...)
cmd = append(cmd, args...)
return exec(cmd...)
}, },
} }
} }