2014-12-05 11:59:56 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-09-02 11:00:14 -04:00
|
|
|
"os/exec"
|
2014-12-05 11:59:56 -05:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-08-29 12:46:05 -04:00
|
|
|
"text/template"
|
2014-12-05 11:59:56 -05:00
|
|
|
"time"
|
|
|
|
|
2015-08-29 17:58:48 -04:00
|
|
|
"github.com/eknkc/amber"
|
2014-12-05 11:59:56 -05:00
|
|
|
"github.com/russross/blackfriday"
|
2015-08-29 11:47:16 -04:00
|
|
|
"github.com/yosssi/gcss"
|
2014-12-05 11:59:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ZSDIR = ".zs"
|
|
|
|
PUBDIR = ".pub"
|
|
|
|
)
|
|
|
|
|
2015-08-29 11:47:16 -04:00
|
|
|
type Vars map[string]string
|
2015-09-02 11:00:14 -04:00
|
|
|
|
|
|
|
func renameExt(path, from, to string) string {
|
|
|
|
if from == "" {
|
|
|
|
from = filepath.Ext(path)
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(path, from) {
|
|
|
|
return strings.TrimSuffix(path, from) + to
|
|
|
|
} else {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func globals() Vars {
|
|
|
|
vars := Vars{}
|
|
|
|
for _, e := range os.Environ() {
|
|
|
|
pair := strings.Split(e, "=")
|
|
|
|
if strings.HasPrefix(pair[0], "ZS_") {
|
|
|
|
vars[strings.ToLower(pair[0][3:])] = pair[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vars
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
c.Stderr = &errbuf
|
|
|
|
|
|
|
|
err := c.Run()
|
|
|
|
|
|
|
|
if errbuf.Len() > 0 {
|
|
|
|
log.Println("ERROR:", errbuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splits a string in exactly two parts by delimiter
|
|
|
|
// If no delimiter is found - the second string is be empty
|
|
|
|
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 11:59:56 -05:00
|
|
|
|
2015-08-29 11:47:16 -04:00
|
|
|
// Parses markdown content. Returns parsed header variables and content
|
2015-08-29 13:54:55 -04:00
|
|
|
func md(path string, globals Vars) (Vars, string, error) {
|
2015-08-29 11:47:16 -04:00
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
s := string(b)
|
2014-12-05 13:26:15 -05:00
|
|
|
url := path[:len(path)-len(filepath.Ext(path))] + ".html"
|
2015-08-29 11:47:16 -04:00
|
|
|
v := Vars{
|
2015-08-30 09:50:03 -04:00
|
|
|
"title": "",
|
|
|
|
"description": "",
|
|
|
|
"keywords": "",
|
2015-08-30 10:01:05 -04:00
|
|
|
}
|
|
|
|
for name, value := range globals {
|
|
|
|
v[name] = value
|
2014-12-05 13:26:15 -05:00
|
|
|
}
|
2015-08-29 17:43:59 -04:00
|
|
|
if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil {
|
|
|
|
v["layout"] = "layout.amber"
|
|
|
|
} else {
|
|
|
|
v["layout"] = "layout.html"
|
|
|
|
}
|
2015-08-30 10:01:05 -04:00
|
|
|
v["file"] = path
|
|
|
|
v["url"] = url
|
|
|
|
v["output"] = filepath.Join(PUBDIR, url)
|
2015-08-29 17:43:59 -04:00
|
|
|
|
2014-12-05 12:09:10 -05:00
|
|
|
if strings.Index(s, "\n\n") == -1 {
|
2015-08-29 13:54:55 -04:00
|
|
|
return v, s, nil
|
2014-12-05 12:09:10 -05:00
|
|
|
}
|
2014-12-05 11:59:56 -05: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 13:26:15 -05:00
|
|
|
if strings.HasPrefix(v["url"], "./") {
|
|
|
|
v["url"] = v["url"][2:]
|
|
|
|
}
|
2015-08-29 11:47:16 -04:00
|
|
|
return v, body, nil
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
|
2015-08-29 12:46:05 -04:00
|
|
|
// Use standard Go templates
|
2015-09-02 11:00:14 -04:00
|
|
|
func render(s string, vars Vars) (string, error) {
|
|
|
|
tmpl, err := template.New("").Parse(s)
|
2015-08-29 12:46:05 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-29 12:46:05 -04:00
|
|
|
out := &bytes.Buffer{}
|
|
|
|
if err := tmpl.Execute(out, vars); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(out.Bytes()), nil
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
|
2015-08-29 12:46:05 -04:00
|
|
|
// Renders markdown with the given layout into html expanding all the macros
|
2015-09-02 11:00:14 -04:00
|
|
|
func buildMarkdown(path string, w io.Writer, vars Vars) error {
|
2015-08-29 13:54:55 -04:00
|
|
|
v, body, err := md(path, vars)
|
2014-12-05 11:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-02 11:00:14 -04:00
|
|
|
content, err := render(body, v)
|
2014-12-05 11:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-02 10:54:16 -04:00
|
|
|
v["content"] = string(blackfriday.MarkdownCommon([]byte(content)))
|
2015-08-30 08:20:35 -04:00
|
|
|
if w == nil {
|
|
|
|
out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
w = out
|
|
|
|
}
|
2015-08-29 12:46:05 -04:00
|
|
|
if strings.HasSuffix(v["layout"], ".amber") {
|
2015-09-02 11:00:14 -04:00
|
|
|
return buildAmber(filepath.Join(ZSDIR, v["layout"]), w, v)
|
2015-08-29 12:46:05 -04:00
|
|
|
} else {
|
2015-09-02 11:00:14 -04:00
|
|
|
return buildHTML(filepath.Join(ZSDIR, v["layout"]), w, v)
|
2015-08-29 12:46:05 -04:00
|
|
|
}
|
2015-08-29 09:07:18 -04:00
|
|
|
}
|
|
|
|
|
2015-08-29 12:46:05 -04:00
|
|
|
// Renders text file expanding all variable macros inside it
|
2015-09-02 11:00:14 -04:00
|
|
|
func buildHTML(path string, w io.Writer, vars Vars) error {
|
2015-08-30 08:20:35 -04:00
|
|
|
b, err := ioutil.ReadFile(path)
|
2014-12-05 11:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-02 11:00:14 -04:00
|
|
|
content, err := render(string(b), vars)
|
2014-12-05 11:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-30 08:22:00 -04:00
|
|
|
if w == nil {
|
|
|
|
f, err := os.Create(filepath.Join(PUBDIR, path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
w = f
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-30 08:22:00 -04:00
|
|
|
_, err = io.WriteString(w, content)
|
|
|
|
return err
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
|
2015-08-29 12:46:05 -04:00
|
|
|
// Renders .amber file into .html
|
2015-09-02 11:00:14 -04:00
|
|
|
func buildAmber(path string, w io.Writer, vars Vars) error {
|
2015-08-29 11:47:16 -04:00
|
|
|
a := amber.New()
|
2015-08-30 08:20:35 -04:00
|
|
|
err := a.ParseFile(path)
|
2015-08-29 11:47:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-30 08:22:00 -04:00
|
|
|
|
|
|
|
data := map[string]interface{}{}
|
|
|
|
for k, v := range vars {
|
|
|
|
data[k] = v
|
|
|
|
}
|
|
|
|
|
2015-08-29 11:47:16 -04:00
|
|
|
t, err := a.Compile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
if w == nil {
|
|
|
|
f, err := os.Create(filepath.Join(PUBDIR, renameExt(path, ".amber", ".html")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
w = f
|
2015-08-29 11:47:16 -04:00
|
|
|
}
|
2015-08-30 08:22:00 -04:00
|
|
|
return t.Execute(w, data)
|
2015-08-29 11:47:16 -04:00
|
|
|
}
|
|
|
|
|
2015-08-29 12:46:05 -04:00
|
|
|
// Compiles .gcss into .css
|
2015-08-30 08:20:35 -04:00
|
|
|
func buildGCSS(path string, w io.Writer) error {
|
2015-08-29 12:46:05 -04:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-08-30 08:20:35 -04:00
|
|
|
if w == nil {
|
|
|
|
s := strings.TrimSuffix(path, ".gcss") + ".css"
|
|
|
|
css, err := os.Create(filepath.Join(PUBDIR, s))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
defer css.Close()
|
|
|
|
w = css
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
_, err = gcss.Compile(w, f)
|
2014-12-05 14:03:33 -05:00
|
|
|
return err
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:20:35 -04:00
|
|
|
// Copies file as is from path to writer
|
|
|
|
func buildRaw(path string, w io.Writer) error {
|
|
|
|
in, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-08-29 13:54:55 -04:00
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
defer in.Close()
|
|
|
|
if w == nil {
|
|
|
|
if out, err := os.Create(filepath.Join(PUBDIR, path)); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer out.Close()
|
|
|
|
w = out
|
2015-08-29 12:46:05 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
_, err = io.Copy(w, in)
|
|
|
|
return err
|
2015-08-29 17:43:59 -04:00
|
|
|
}
|
|
|
|
|
2015-09-02 11:00:14 -04:00
|
|
|
func build(path string, w io.Writer, vars Vars) error {
|
2015-08-30 08:20:35 -04:00
|
|
|
ext := filepath.Ext(path)
|
|
|
|
if ext == ".md" || ext == ".mkd" {
|
2015-09-02 11:00:14 -04:00
|
|
|
return buildMarkdown(path, w, vars)
|
2015-08-30 08:20:35 -04:00
|
|
|
} else if ext == ".html" || ext == ".xml" {
|
2015-09-02 11:00:14 -04:00
|
|
|
return buildHTML(path, w, vars)
|
2015-08-30 08:20:35 -04:00
|
|
|
} else if ext == ".amber" {
|
2015-09-02 11:00:14 -04:00
|
|
|
return buildAmber(path, w, vars)
|
2015-08-30 08:20:35 -04:00
|
|
|
} else if ext == ".gcss" {
|
|
|
|
return buildGCSS(path, w)
|
|
|
|
} else {
|
|
|
|
return buildRaw(path, w)
|
2015-08-29 12:46:05 -04:00
|
|
|
}
|
2015-08-29 13:54:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-30 08:20:35 -04:00
|
|
|
func buildAll(watch bool) {
|
2015-08-29 13:54:55 -04:00
|
|
|
lastModified := time.Unix(0, 0)
|
|
|
|
modified := false
|
|
|
|
|
|
|
|
vars := globals()
|
2014-12-05 11:59:56 -05: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
|
|
|
|
}
|
2015-08-30 17:16:51 -04:00
|
|
|
// inform user about fs walk errors, but continue iteration
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-05 11:59:56 -05:00
|
|
|
|
2014-12-05 14:03:33 -05: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 12:46:05 -04:00
|
|
|
// FIXME on windows it might not work well
|
2014-12-05 14:03:33 -05:00
|
|
|
run(filepath.Join(ZSDIR, "pre"), []string{}, nil, nil)
|
|
|
|
modified = true
|
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
log.Println("build: ", path)
|
2015-09-02 11:00:14 -04:00
|
|
|
return build(path, nil, vars)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
}
|
2014-12-05 14:03:33 -05:00
|
|
|
if modified {
|
|
|
|
// Something was modified, so post-build hook
|
2015-08-29 12:46:05 -04:00
|
|
|
// FIXME on windows it might not work well
|
2014-12-05 14:03:33 -05:00
|
|
|
run(filepath.Join(ZSDIR, "post"), []string{}, nil, nil)
|
|
|
|
modified = false
|
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
if !watch {
|
2014-12-05 11:59:56 -05:00
|
|
|
break
|
|
|
|
}
|
2015-08-30 08:20:35 -04:00
|
|
|
lastModified = time.Now()
|
2014-12-05 11:59:56 -05:00
|
|
|
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":
|
2015-08-30 08:22:00 -04:00
|
|
|
if len(args) == 0 {
|
|
|
|
buildAll(false)
|
|
|
|
} else if len(args) == 1 {
|
2015-09-02 11:00:14 -04:00
|
|
|
if err := build(args[0], os.Stdout, globals()); err != nil {
|
2015-08-30 08:22:00 -04:00
|
|
|
fmt.Println("ERROR: " + err.Error())
|
|
|
|
}
|
2014-12-05 11:59:56 -05:00
|
|
|
} else {
|
2015-08-30 08:22:00 -04:00
|
|
|
fmt.Println("ERROR: too many arguments")
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-30 08:22:00 -04:00
|
|
|
case "watch":
|
|
|
|
buildAll(true)
|
2015-08-30 08:20:35 -04:00
|
|
|
case "var":
|
2015-09-02 11:00:14 -04:00
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Println("var: filename expected")
|
|
|
|
} else {
|
|
|
|
s := ""
|
|
|
|
if vars, _, err := md(args[0], globals()); err != nil {
|
|
|
|
fmt.Println("var: " + err.Error())
|
|
|
|
} else {
|
|
|
|
if len(args) > 1 {
|
|
|
|
for _, a := range args[1:] {
|
|
|
|
s = s + vars[a] + "\n"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for k, v := range vars {
|
|
|
|
s = s + k + ":" + v + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(strings.TrimSpace(s))
|
|
|
|
}
|
2014-12-05 11:59:56 -05:00
|
|
|
default:
|
2015-08-30 09:29:12 -04:00
|
|
|
err := run(path.Join(ZSDIR, cmd), args, globals(), os.Stdout)
|
2014-12-05 13:09:03 -05:00
|
|
|
if err != nil {
|
2015-08-29 17:43:59 -04:00
|
|
|
log.Println("ERROR:", err)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|