2014-12-05 19:59:56 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ZSDIR = ".zs"
|
|
|
|
PUBDIR = ".pub"
|
|
|
|
)
|
|
|
|
|
2014-12-05 20:26:59 +03:00
|
|
|
type EvalFn func(args []string, vars map[string]string) (string, error)
|
|
|
|
|
2014-12-05 19:59:56 +03:00
|
|
|
func split2(s, delim string) (string, string) {
|
|
|
|
parts := strings.SplitN(s, delim, 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return parts[0], parts[1]
|
|
|
|
} else {
|
|
|
|
return parts[0], ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 21:26:15 +03:00
|
|
|
func md(path, s string) (map[string]string, string) {
|
|
|
|
url := path[:len(path)-len(filepath.Ext(path))] + ".html"
|
|
|
|
v := map[string]string{
|
|
|
|
"file": path,
|
|
|
|
"url": url,
|
|
|
|
"output": filepath.Join(PUBDIR, url),
|
|
|
|
"layout": "index.html",
|
|
|
|
}
|
2014-12-05 20:09:10 +03:00
|
|
|
if strings.Index(s, "\n\n") == -1 {
|
|
|
|
return map[string]string{}, s
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
header, body := split2(s, "\n\n")
|
|
|
|
for _, line := range strings.Split(header, "\n") {
|
|
|
|
key, value := split2(line, ":")
|
|
|
|
v[strings.ToLower(strings.TrimSpace(key))] = strings.TrimSpace(value)
|
|
|
|
}
|
2014-12-05 21:26:15 +03:00
|
|
|
if strings.HasPrefix(v["url"], "./") {
|
|
|
|
v["url"] = v["url"][2:]
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
return v, body
|
|
|
|
}
|
|
|
|
|
|
|
|
func render(s string, vars map[string]string, eval EvalFn) (string, error) {
|
2014-12-05 21:09:03 +03:00
|
|
|
delim_open := "{{"
|
|
|
|
delim_close := "}}"
|
2014-12-05 19:59:56 +03:00
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
for {
|
2014-12-05 21:09:03 +03:00
|
|
|
if from := strings.Index(s, delim_open); from == -1 {
|
|
|
|
out.WriteString(s)
|
2014-12-05 19:59:56 +03:00
|
|
|
return out.String(), nil
|
|
|
|
} else {
|
2014-12-05 21:09:03 +03:00
|
|
|
if to := strings.Index(s, delim_close); to == -1 {
|
2014-12-05 19:59:56 +03:00
|
|
|
return "", fmt.Errorf("Close delim not found")
|
|
|
|
} else {
|
2014-12-05 21:09:03 +03:00
|
|
|
out.WriteString(s[:from])
|
|
|
|
cmd := s[from+len(delim_open) : to]
|
|
|
|
s = s[to+len(delim_close):]
|
|
|
|
m := strings.Fields(cmd)
|
2014-12-05 19:59:56 +03:00
|
|
|
if len(m) == 1 {
|
|
|
|
if v, ok := vars[m[0]]; ok {
|
2014-12-05 21:09:03 +03:00
|
|
|
out.WriteString(v)
|
2014-12-05 19:59:56 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res, err := eval(m, vars); err == nil {
|
2014-12-05 21:09:03 +03:00
|
|
|
out.WriteString(res)
|
2014-12-05 19:59:56 +03:00
|
|
|
} else {
|
|
|
|
log.Println(err) // silent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 21:09:03 +03:00
|
|
|
func env(vars map[string]string) []string {
|
2014-12-05 22:03:33 +03:00
|
|
|
env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
|
2014-12-05 21:09:03 +03:00
|
|
|
env = append(env, os.Environ()...)
|
2014-12-05 22:03:33 +03:00
|
|
|
if vars != nil {
|
|
|
|
for k, v := range vars {
|
|
|
|
env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
2014-12-05 21:09:03 +03:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cmd string, args []string, vars map[string]string, output io.Writer) error {
|
|
|
|
var errbuf bytes.Buffer
|
|
|
|
c := exec.Command(cmd, args...)
|
|
|
|
c.Env = env(vars)
|
|
|
|
c.Stdout = output
|
2014-12-05 19:59:56 +03:00
|
|
|
c.Stderr = &errbuf
|
2014-12-05 21:09:03 +03:00
|
|
|
|
|
|
|
err := c.Run()
|
|
|
|
|
|
|
|
if errbuf.Len() > 0 {
|
|
|
|
log.Println(errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func eval(cmd []string, vars map[string]string) (string, error) {
|
|
|
|
outbuf := bytes.NewBuffer(nil)
|
|
|
|
err := run(path.Join(ZSDIR, cmd[0]), cmd[1:], vars, outbuf)
|
|
|
|
if err != nil {
|
2014-12-05 21:21:42 +03:00
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-05 21:09:03 +03:00
|
|
|
outbuf = bytes.NewBuffer(nil)
|
|
|
|
err := run(cmd[0], cmd[1:], vars, outbuf)
|
2014-12-05 21:21:42 +03:00
|
|
|
// Return exit errors, but ignore if the command was not found
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
2014-12-05 19:59:56 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outbuf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildMarkdown(path string) error {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-05 21:26:15 +03:00
|
|
|
v, body := md(path, string(b))
|
2014-12-05 19:59:56 +03:00
|
|
|
content, err := render(body, v, eval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v["content"] = string(blackfriday.MarkdownBasic([]byte(content)))
|
2015-08-29 16:07:18 +03:00
|
|
|
return buildPlain(filepath.Join(ZSDIR, v["layout"]), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPlain(path string, vars map[string]string) error {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-29 16:07:18 +03:00
|
|
|
content, err := render(string(b), vars, eval)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-29 16:07:18 +03:00
|
|
|
output := filepath.Join(PUBDIR, path)
|
|
|
|
if s, ok := vars["output"]; ok {
|
|
|
|
output = s
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(output, []byte(content), 0666)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-05 22:03:33 +03:00
|
|
|
func copyFile(path string) (err error) {
|
|
|
|
var in, out *os.File
|
|
|
|
if in, err = os.Open(path); err == nil {
|
2014-12-05 19:59:56 +03:00
|
|
|
defer in.Close()
|
2014-12-05 22:03:33 +03:00
|
|
|
if out, err = os.Create(filepath.Join(PUBDIR, path)); err == nil {
|
2014-12-05 19:59:56 +03:00
|
|
|
defer out.Close()
|
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 22:03:33 +03:00
|
|
|
return err
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildAll(once bool) {
|
|
|
|
lastModified := time.Unix(0, 0)
|
2014-12-05 22:03:33 +03:00
|
|
|
modified := false
|
2014-12-05 19:59:56 +03:00
|
|
|
for {
|
|
|
|
os.Mkdir(PUBDIR, 0755)
|
|
|
|
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
|
|
|
// ignore hidden files and directories
|
|
|
|
if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-05 22:03:33 +03:00
|
|
|
if info.IsDir() {
|
|
|
|
os.Mkdir(filepath.Join(PUBDIR, path), 0755)
|
|
|
|
return nil
|
|
|
|
} else if info.ModTime().After(lastModified) {
|
|
|
|
if !modified {
|
|
|
|
// About to be modified, so run pre-build hook
|
|
|
|
run(filepath.Join(ZSDIR, "pre"), []string{}, nil, nil)
|
|
|
|
modified = true
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
ext := filepath.Ext(path)
|
2015-08-29 14:47:05 +03:00
|
|
|
if ext == ".md" || ext == ".mkd" {
|
2014-12-05 19:59:56 +03:00
|
|
|
log.Println("mkd: ", path)
|
|
|
|
return buildMarkdown(path)
|
2015-08-29 16:17:13 +03:00
|
|
|
} else if ext == ".html" || ext == ".xml" {
|
|
|
|
return buildPlain(path, map[string]string{})
|
2014-12-05 19:59:56 +03:00
|
|
|
} else {
|
|
|
|
log.Println("raw: ", path)
|
|
|
|
return copyFile(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
}
|
2014-12-05 22:03:33 +03:00
|
|
|
if modified {
|
|
|
|
// Something was modified, so post-build hook
|
|
|
|
run(filepath.Join(ZSDIR, "post"), []string{}, nil, nil)
|
|
|
|
modified = false
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
lastModified = time.Now()
|
|
|
|
if once {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
fmt.Println(os.Args[0], "<command> [args]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd := os.Args[1]
|
|
|
|
args := os.Args[2:]
|
|
|
|
switch cmd {
|
|
|
|
case "build":
|
|
|
|
buildAll(true)
|
|
|
|
case "watch":
|
|
|
|
buildAll(false) // pass duration
|
|
|
|
case "var":
|
|
|
|
if len(args) == 0 {
|
2014-12-05 20:26:59 +03:00
|
|
|
log.Println("ERROR: filename expected")
|
2014-12-05 19:59:56 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if b, err := ioutil.ReadFile(args[0]); err == nil {
|
2014-12-05 21:26:15 +03:00
|
|
|
vars, _ := md(args[0], string(b))
|
2014-12-05 19:59:56 +03:00
|
|
|
if len(args) > 1 {
|
|
|
|
for _, a := range args[1:] {
|
|
|
|
fmt.Println(vars[a])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := range vars {
|
|
|
|
fmt.Println(k + ":" + v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-05 20:26:59 +03:00
|
|
|
log.Println(err)
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
default:
|
2014-12-05 21:09:03 +03:00
|
|
|
err := run(path.Join(ZSDIR, cmd), args, map[string]string{}, os.Stdout)
|
|
|
|
if err != nil {
|
2014-12-05 19:59:56 +03:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|