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 {
return err
}
output := filepath.Join(PUBDIR, path)
if s, ok := vars["output"]; ok {
output = s
if w == nil {
f, err := os.Create(filepath.Join(PUBDIR, path))
if err != nil {
return err
}
defer f.Close()
w = f
}
err = ioutil.WriteFile(output, []byte(content), 0666)
if err != nil {
return err
}
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)
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":
buildAll(true)
case "print":
if len(args) != 1 {
fmt.Println("ERROR: filename expected")
} else {
build(args[0], os.Stdout, builtins(), globals())
}
case "var":
fmt.Println(Var(args))
case "lorem":

View File

@ -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...)
},
}
}