2014-12-05 19:59:56 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-08-29 19:46:05 +03:00
|
|
|
"text/template"
|
2014-12-05 19:59:56 +03:00
|
|
|
"time"
|
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
"github.com/eknkc/amber"
|
2014-12-05 19:59:56 +03:00
|
|
|
"github.com/russross/blackfriday"
|
2015-08-29 18:47:16 +03:00
|
|
|
"github.com/yosssi/gcss"
|
2014-12-05 19:59:56 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ZSDIR = ".zs"
|
|
|
|
PUBDIR = ".pub"
|
|
|
|
)
|
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
type Vars map[string]string
|
2014-12-05 20:26:59 +03:00
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
// Splits a string in exactly two parts by delimiter
|
|
|
|
// If no delimiter is found - the second string is be empty
|
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], ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
// Parses markdown content. Returns parsed header variables and content
|
|
|
|
func md(path string) (Vars, string, error) {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
s := string(b)
|
2014-12-05 21:26:15 +03:00
|
|
|
url := path[:len(path)-len(filepath.Ext(path))] + ".html"
|
2015-08-29 18:47:16 +03:00
|
|
|
v := Vars{
|
2014-12-05 21:26:15 +03:00
|
|
|
"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 {
|
2015-08-29 18:47:16 +03:00
|
|
|
return Vars{}, s, nil
|
2014-12-05 20:09:10 +03:00
|
|
|
}
|
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:]
|
|
|
|
}
|
2015-08-29 18:47:16 +03:00
|
|
|
return v, body, nil
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Use standard Go templates
|
|
|
|
func render(s string, funcs template.FuncMap, vars Vars) (string, error) {
|
|
|
|
f := template.FuncMap{}
|
|
|
|
for k, v := range funcs {
|
|
|
|
f[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range vars {
|
|
|
|
f[k] = varFunc(v)
|
|
|
|
}
|
|
|
|
tmpl, err := template.New("").Funcs(f).Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
2015-08-29 19:46:05 +03:00
|
|
|
out := &bytes.Buffer{}
|
|
|
|
if err := tmpl.Execute(out, vars); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(out.Bytes()), nil
|
2014-12-05 19:59:56 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
// Converts zs markdown variables into environment variables
|
|
|
|
func env(vars Vars) []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
|
|
|
|
}
|
|
|
|
|
2015-08-29 18:47:16 +03:00
|
|
|
// Runs command with given arguments and variables, intercepts stderr and
|
|
|
|
// redirects stdout into the given writer
|
|
|
|
func run(cmd string, args []string, vars Vars, output io.Writer) error {
|
2014-12-05 21:09:03 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Expands macro: either replacing it with the variable value, or
|
|
|
|
// running the plugin command and replacing it with the command's output
|
2015-08-29 18:47:16 +03:00
|
|
|
func eval(cmd []string, vars Vars) (string, error) {
|
2014-12-05 21:09:03 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Renders markdown with the given layout into html expanding all the macros
|
|
|
|
func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error {
|
2015-08-29 18:47:16 +03:00
|
|
|
v, body, err := md(path)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-29 19:46:05 +03:00
|
|
|
content, err := render(body, funcs, v)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v["content"] = string(blackfriday.MarkdownBasic([]byte(content)))
|
2015-08-29 19:46:05 +03:00
|
|
|
if strings.HasSuffix(v["layout"], ".amber") {
|
|
|
|
return buildAmber(filepath.Join(ZSDIR, v["layout"]), funcs, v)
|
|
|
|
} else {
|
|
|
|
return buildPlain(filepath.Join(ZSDIR, v["layout"]), funcs, v)
|
|
|
|
}
|
2015-08-29 16:07:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Renders text file expanding all variable macros inside it
|
|
|
|
func buildPlain(path string, funcs template.FuncMap, vars Vars) error {
|
2015-08-29 16:07:18 +03:00
|
|
|
b, err := ioutil.ReadFile(path)
|
2014-12-05 19:59:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-29 19:46:05 +03:00
|
|
|
content, err := render(string(b), funcs, vars)
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Renders .amber file into .html
|
|
|
|
func buildAmber(path string, funcs template.FuncMap, vars Vars) error {
|
2015-08-29 18:47:16 +03:00
|
|
|
a := amber.New()
|
|
|
|
err := a.ParseFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t, err := a.Compile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//amber.FuncMap = amber.FuncMap
|
|
|
|
s := strings.TrimSuffix(path, ".amber") + ".html"
|
|
|
|
f, err := os.Create(filepath.Join(PUBDIR, s))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
return t.Execute(f, vars)
|
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
// Compiles .gcss into .css
|
|
|
|
func buildGCSS(path string) error {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := strings.TrimSuffix(path, ".gcss") + ".css"
|
|
|
|
css, err := os.Create(filepath.Join(PUBDIR, s))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
defer css.Close()
|
|
|
|
|
|
|
|
_, err = gcss.Compile(css, f)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies file from working directory into public directory
|
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
|
|
|
}
|
|
|
|
|
2015-08-29 19:46:05 +03:00
|
|
|
func varFunc(s string) func() string {
|
|
|
|
return func() string {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pluginFunc(cmd string) func() string {
|
|
|
|
return func() string {
|
|
|
|
return "Not implemented yet"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFuncs() template.FuncMap {
|
|
|
|
// Builtin functions
|
|
|
|
funcs := template.FuncMap{}
|
|
|
|
// Plugin functions
|
|
|
|
files, _ := ioutil.ReadDir(ZSDIR)
|
|
|
|
for _, f := range files {
|
|
|
|
if !f.IsDir() {
|
|
|
|
name := f.Name()
|
|
|
|
if !strings.HasSuffix(name, ".html") && !strings.HasSuffix(name, ".amber") {
|
|
|
|
funcs[strings.TrimSuffix(name, filepath.Ext(name))] = pluginFunc(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return funcs
|
|
|
|
}
|
|
|
|
|
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
|
2015-08-29 19:46:05 +03:00
|
|
|
// Convert env variables into zs global variables
|
|
|
|
globals := Vars{}
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
pair := strings.Split(e, "=")
|
|
|
|
if strings.HasPrefix(pair[0], "ZS_") {
|
|
|
|
globals[strings.ToLower(pair[0][3:])] = pair[1]
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 19:59:56 +03:00
|
|
|
for {
|
|
|
|
os.Mkdir(PUBDIR, 0755)
|
2015-08-29 19:46:05 +03:00
|
|
|
funcs := createFuncs()
|
2014-12-05 19:59:56 +03:00
|
|
|
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
|
2015-08-29 19:46:05 +03:00
|
|
|
// FIXME on windows it might not work well
|
2014-12-05 22:03:33 +03:00
|
|
|
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" {
|
2015-08-29 18:47:16 +03:00
|
|
|
log.Println("md: ", path)
|
2015-08-29 19:46:05 +03:00
|
|
|
return buildMarkdown(path, funcs, globals)
|
2015-08-29 16:17:13 +03:00
|
|
|
} else if ext == ".html" || ext == ".xml" {
|
2015-08-29 18:47:16 +03:00
|
|
|
log.Println("html: ", path)
|
2015-08-29 19:46:05 +03:00
|
|
|
return buildPlain(path, funcs, globals)
|
2015-08-29 18:47:16 +03:00
|
|
|
} else if ext == ".amber" {
|
|
|
|
log.Println("html: ", path)
|
2015-08-29 19:46:05 +03:00
|
|
|
return buildAmber(path, funcs, globals)
|
2015-08-29 18:47:16 +03:00
|
|
|
} else if ext == ".gcss" {
|
|
|
|
log.Println("css: ", path)
|
|
|
|
return buildGCSS(path)
|
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
|
2015-08-29 19:46:05 +03:00
|
|
|
// FIXME on windows it might not work well
|
2014-12-05 22:03:33 +03:00
|
|
|
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
|
|
|
|
}
|
2015-08-29 18:47:16 +03:00
|
|
|
if vars, _, err := md(args[0]); err == nil {
|
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:
|
2015-08-29 18:47:16 +03:00
|
|
|
err := run(path.Join(ZSDIR, cmd), args, Vars{}, os.Stdout)
|
2014-12-05 21:09:03 +03:00
|
|
|
if err != nil {
|
2014-12-05 19:59:56 +03:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|