source: code/trunk/zs.go@ 36

Last change on this file since 36 was 36, checked in by zaitsev.serge, 10 years ago

removed fs/walk error check, added title default value heuristics

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