zs/zs.go

384 lines
8.5 KiB
Go
Raw Normal View History

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
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, globals Vars) (Vars, string, error) {
2015-08-29 18:47:16 +03:00
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",
}
if info, err := os.Stat(path); err == nil {
v["date"] = info.ModTime().Format("02-01-2006")
}
for name, value := range globals {
v[name] = value
}
2014-12-05 20:09:10 +03:00
if strings.Index(s, "\n\n") == -1 {
return v, 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 {
env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
env = append(env, os.Environ()...)
if vars != nil {
for k, v := range vars {
env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
}
2014-12-05 19:59:56 +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 {
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
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) {
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
}
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 {
v, body, err := md(path, vars)
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 19:46:05 +03:00
// Renders text file expanding all variable macros inside it
func buildPlain(path string, funcs template.FuncMap, vars Vars) error {
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
}
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
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()
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)
}
}
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{
"exec": func(s ...string) string {
// Run external command with arguments
return ""
},
"zs": func(args ...string) string {
// Run zs with arguments
return ""
},
}
2015-08-29 19:46:05 +03:00
// 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
}
func globals() Vars {
vars := Vars{}
2015-08-29 19:46:05 +03:00
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
if strings.HasPrefix(pair[0], "ZS_") {
vars[strings.ToLower(pair[0][3:])] = pair[1]
2015-08-29 19:46:05 +03:00
}
}
return vars
}
func buildAll(once bool) {
lastModified := time.Unix(0, 0)
modified := false
vars := globals()
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
}
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
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)
return buildMarkdown(path, funcs, vars)
} else if ext == ".html" || ext == ".xml" {
2015-08-29 18:47:16 +03:00
log.Println("html: ", path)
return buildPlain(path, funcs, vars)
2015-08-29 18:47:16 +03:00
} else if ext == ".amber" {
log.Println("html: ", path)
return buildAmber(path, funcs, vars)
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)
}
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
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 {
log.Println("ERROR: filename expected")
2014-12-05 19:59:56 +03:00
return
}
if vars, _, err := md(args[0], globals()); 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 {
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)
if err != nil {
2014-12-05 19:59:56 +03:00
log.Println(err)
}
}
}