-
Notifications
You must be signed in to change notification settings - Fork 15
/
zs.go
426 lines (392 loc) · 9.65 KB
/
zs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/eknkc/amber"
"github.com/yosssi/gcss"
"gopkg.in/russross/blackfriday.v2"
"gopkg.in/yaml.v2"
)
const (
ZSDIR = ".zs"
PUBDIR = ".pub"
)
type Vars map[string]string
// renameExt renames extension (if any) from oldext to newext
// If oldext is an empty string - extension is extracted automatically.
// If path has no extension - new extension is appended
func renameExt(path, oldext, newext string) string {
if oldext == "" {
oldext = filepath.Ext(path)
}
if oldext == "" || strings.HasSuffix(path, oldext) {
return strings.TrimSuffix(path, oldext) + newext
} else {
return path
}
}
// globals returns list of global OS environment variables that start
// with ZS_ prefix as Vars, so the values can be used inside templates
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
}
// run executes a command or a script. Vars define the command environment,
// each zs var is converted into OS environemnt variable with ZS_ prefix
// prepended. Additional variable $ZS contains path to the zs binary. Command
// stderr is printed to zs stderr, command output is returned as a string.
func run(vars Vars, cmd string, args ...string) (string, error) {
// First check if partial exists (.amber or .html)
if b, err := ioutil.ReadFile(filepath.Join(ZSDIR, cmd+".amber")); err == nil {
return string(b), nil
}
if b, err := ioutil.ReadFile(filepath.Join(ZSDIR, cmd+".html")); err == nil {
return string(b), nil
}
var errbuf, outbuf bytes.Buffer
c := exec.Command(cmd, args...)
env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
env = append(env, os.Environ()...)
for k, v := range vars {
env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
}
c.Env = env
c.Stdout = &outbuf
c.Stderr = &errbuf
err := c.Run()
if errbuf.Len() > 0 {
log.Println("ERROR:", errbuf.String())
}
if err != nil {
return "", err
}
return string(outbuf.Bytes()), nil
}
// getVars returns list of variables defined in a text file and actual file
// content following the variables declaration. Header is separated from
// content by an empty line. Header can be either YAML or JSON.
// If no empty newline is found - file is treated as content-only.
func getVars(path string, globals Vars) (Vars, string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, "", err
}
s := string(b)
// Pick some default values for content-dependent variables
v := Vars{}
title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1)
v["title"] = strings.ToTitle(title)
v["description"] = ""
v["file"] = path
v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
v["output"] = filepath.Join(PUBDIR, v["url"])
// Override default values with globals
for name, value := range globals {
v[name] = value
}
// Add layout if none is specified
if _, ok := v["layout"]; !ok {
if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil {
v["layout"] = "layout.amber"
} else {
v["layout"] = "layout.html"
}
}
delim := "\n---\n"
if sep := strings.Index(s, delim); sep == -1 {
return v, s, nil
} else {
header := s[:sep]
body := s[sep+len(delim):]
vars := Vars{}
if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
fmt.Println("ERROR: failed to parse header", err)
return nil, "", err
} else {
// Override default values + globals with the ones defines in the file
for key, value := range vars {
v[key] = value
}
}
if strings.HasPrefix(v["url"], "./") {
v["url"] = v["url"][2:]
}
return v, body, nil
}
}
// Render expanding zs plugins and variables
func render(s string, vars Vars) (string, error) {
delim_open := "{{"
delim_close := "}}"
out := &bytes.Buffer{}
for {
if from := strings.Index(s, delim_open); from == -1 {
out.WriteString(s)
return out.String(), nil
} else {
if to := strings.Index(s, delim_close); to == -1 {
return "", fmt.Errorf("Close delim not found")
} else {
out.WriteString(s[:from])
cmd := s[from+len(delim_open) : to]
s = s[to+len(delim_close):]
m := strings.Fields(cmd)
if len(m) == 1 {
if v, ok := vars[m[0]]; ok {
out.WriteString(v)
continue
}
}
if res, err := run(vars, m[0], m[1:]...); err == nil {
out.WriteString(res)
} else {
fmt.Println(err)
}
}
}
}
return s, nil
}
// Renders markdown with the given layout into html expanding all the macros
func buildMarkdown(path string, w io.Writer, vars Vars) error {
v, body, err := getVars(path, vars)
if err != nil {
return err
}
content, err := render(body, v)
if err != nil {
return err
}
v["content"] = string(blackfriday.Run([]byte(content)))
if w == nil {
out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html")))
if err != nil {
return err
}
defer out.Close()
w = out
}
if strings.HasSuffix(v["layout"], ".amber") {
return buildAmber(filepath.Join(ZSDIR, v["layout"]), w, v)
} else {
return buildHTML(filepath.Join(ZSDIR, v["layout"]), w, v)
}
}
// Renders text file expanding all variable macros inside it
func buildHTML(path string, w io.Writer, vars Vars) error {
v, body, err := getVars(path, vars)
if err != nil {
return err
}
if body, err = render(body, v); err != nil {
return err
}
tmpl, err := template.New("").Delims("<%", "%>").Parse(body)
if err != nil {
return err
}
if w == nil {
f, err := os.Create(filepath.Join(PUBDIR, path))
if err != nil {
return err
}
defer f.Close()
w = f
}
return tmpl.Execute(w, vars)
}
// Renders .amber file into .html
func buildAmber(path string, w io.Writer, vars Vars) error {
v, body, err := getVars(path, vars)
if err != nil {
return err
}
a := amber.New()
if err := a.Parse(body); err != nil {
fmt.Println(body)
return err
}
t, err := a.Compile()
if err != nil {
return err
}
htmlBuf := &bytes.Buffer{}
if err := t.Execute(htmlBuf, v); err != nil {
return err
}
if body, err = render(string(htmlBuf.Bytes()), v); err != nil {
return err
}
if w == nil {
f, err := os.Create(filepath.Join(PUBDIR, renameExt(path, ".amber", ".html")))
if err != nil {
return err
}
defer f.Close()
w = f
}
_, err = io.WriteString(w, body)
return err
}
// Compiles .gcss into .css
func buildGCSS(path string, w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if w == nil {
s := strings.TrimSuffix(path, ".gcss") + ".css"
css, err := os.Create(filepath.Join(PUBDIR, s))
if err != nil {
return err
}
defer css.Close()
w = css
}
_, err = gcss.Compile(w, f)
return err
}
// 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
}
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
}
}
_, err = io.Copy(w, in)
return err
}
func build(path string, w io.Writer, vars Vars) error {
ext := filepath.Ext(path)
if ext == ".md" || ext == ".mkd" {
return buildMarkdown(path, w, vars)
} else if ext == ".html" || ext == ".xml" {
return buildHTML(path, w, vars)
} else if ext == ".amber" {
return buildAmber(path, w, vars)
} else if ext == ".gcss" {
return buildGCSS(path, w)
} else {
return buildRaw(path, w)
}
}
func buildAll(watch bool) {
lastModified := time.Unix(0, 0)
modified := false
vars := globals()
for {
os.Mkdir(PUBDIR, 0755)
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
}
// inform user about fs walk errors, but continue iteration
if err != nil {
fmt.Println("error:", err)
return nil
}
if info.IsDir() {
os.Mkdir(filepath.Join(PUBDIR, path), 0755)
return nil
} else if info.ModTime().After(lastModified) {
if !modified {
// First file in this build cycle is about to be modified
run(vars, "prehook")
modified = true
}
log.Println("build:", path)
return build(path, nil, vars)
}
return nil
})
if modified {
// At least one file in this build cycle has been modified
run(vars, "posthook")
modified = false
}
if !watch {
break
}
lastModified = time.Now()
time.Sleep(1 * time.Second)
}
}
func init() {
// prepend .zs to $PATH, so plugins will be found before OS commands
p := os.Getenv("PATH")
p = ZSDIR + ":" + p
os.Setenv("PATH", p)
}
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":
if len(args) == 0 {
buildAll(false)
} else if len(args) == 1 {
if err := build(args[0], os.Stdout, globals()); err != nil {
fmt.Println("ERROR: " + err.Error())
}
} else {
fmt.Println("ERROR: too many arguments")
}
case "watch":
buildAll(true)
case "var":
if len(args) == 0 {
fmt.Println("var: filename expected")
} else {
s := ""
if vars, _, err := getVars(args[0], Vars{}); 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))
}
default:
if s, err := run(globals(), cmd, args...); err != nil {
fmt.Println(err)
} else {
fmt.Println(s)
}
}
}