annotate zs.go @ 36:a88ccfbd4103 draft

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