Mercurial > yakumo_izuru > aya
annotate zs.go @ 24:d052f3a44195 draft
fixed output file names in html pages, fixed amber function bindings, replaced print command with build, fixed plugin functions, implemented zs and exec functions
author | zaitsev.serge |
---|---|
date | Sun, 30 Aug 2015 12:22:00 +0000 |
parents | 40f55059fbfa |
children | 42b0a9fa5883 |
rev | line source |
---|---|
0 | 1 package main |
2 | |
3 import ( | |
4 "bytes" | |
5 "fmt" | |
6 "io" | |
7 "io/ioutil" | |
8 "log" | |
9 "os" | |
10 "path" | |
11 "path/filepath" | |
12 "strings" | |
18 | 13 "text/template" |
0 | 14 "time" |
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 | 17 "github.com/russross/blackfriday" |
17 | 18 "github.com/yosssi/gcss" |
0 | 19 ) |
20 | |
21 const ( | |
22 ZSDIR = ".zs" | |
23 PUBDIR = ".pub" | |
24 ) | |
25 | |
17 | 26 type Vars map[string]string |
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
|
27 type Funcs template.FuncMap |
0 | 28 |
17 | 29 // Parses markdown content. Returns parsed header variables and content |
19
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
30 func md(path string, globals Vars) (Vars, string, error) { |
17 | 31 b, err := ioutil.ReadFile(path) |
32 if err != nil { | |
33 return nil, "", err | |
34 } | |
35 s := string(b) | |
6 | 36 url := path[:len(path)-len(filepath.Ext(path))] + ".html" |
17 | 37 v := Vars{ |
6 | 38 "file": path, |
39 "url": url, | |
40 "output": filepath.Join(PUBDIR, url), | |
41 } | |
20
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
42 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
|
43 v["layout"] = "layout.amber" |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
44 } else { |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
45 v["layout"] = "layout.html" |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
46 } |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
47 |
19
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
48 if info, err := os.Stat(path); err == nil { |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
49 v["date"] = info.ModTime().Format("02-01-2006") |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
50 } |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
51 for name, value := range globals { |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
52 v[name] = value |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
53 } |
2 | 54 if strings.Index(s, "\n\n") == -1 { |
19
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
55 return v, s, nil |
2 | 56 } |
0 | 57 header, body := split2(s, "\n\n") |
58 for _, line := range strings.Split(header, "\n") { | |
59 key, value := split2(line, ":") | |
60 v[strings.ToLower(strings.TrimSpace(key))] = strings.TrimSpace(value) | |
61 } | |
6 | 62 if strings.HasPrefix(v["url"], "./") { |
63 v["url"] = v["url"][2:] | |
64 } | |
17 | 65 return v, body, nil |
0 | 66 } |
67 | |
18 | 68 // Use standard Go templates |
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
|
69 func render(s string, funcs Funcs, vars Vars) (string, 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
|
70 f := Funcs{} |
18 | 71 for k, v := range funcs { |
72 f[k] = v | |
73 } | |
74 for k, v := range vars { | |
75 f[k] = varFunc(v) | |
0 | 76 } |
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
|
77 // Plugin functions |
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
|
78 files, _ := ioutil.ReadDir(ZSDIR) |
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
|
79 for _, file := range files { |
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
|
80 if !file.IsDir() { |
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
|
81 name := file.Name() |
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
|
82 if !strings.HasSuffix(name, ".html") && !strings.HasSuffix(name, ".amber") { |
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
|
83 f[strings.TrimSuffix(name, filepath.Ext(name))] = pluginFunc(name, vars) |
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
|
84 } |
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
|
85 } |
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
|
86 } |
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
|
87 |
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
|
88 tmpl, err := template.New("").Funcs(template.FuncMap(f)).Parse(s) |
18 | 89 if err != nil { |
90 return "", err | |
91 } | |
92 out := &bytes.Buffer{} | |
93 if err := tmpl.Execute(out, vars); err != nil { | |
94 return "", err | |
95 } | |
96 return string(out.Bytes()), nil | |
0 | 97 } |
98 | |
18 | 99 // Renders markdown with the given layout into html expanding all the macros |
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
|
100 func buildMarkdown(path string, w io.Writer, funcs Funcs, vars Vars) error { |
19
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
101 v, body, err := md(path, vars) |
0 | 102 if err != nil { |
103 return err | |
104 } | |
18 | 105 content, err := render(body, funcs, v) |
0 | 106 if err != nil { |
107 return err | |
108 } | |
109 v["content"] = string(blackfriday.MarkdownBasic([]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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 } |
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
|
115 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
|
116 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
|
117 } |
18 | 118 if strings.HasSuffix(v["layout"], ".amber") { |
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
|
119 return buildAmber(filepath.Join(ZSDIR, v["layout"]), w, funcs, v) |
18 | 120 } else { |
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
|
121 return buildHTML(filepath.Join(ZSDIR, v["layout"]), w, funcs, v) |
18 | 122 } |
14 | 123 } |
124 | |
18 | 125 // Renders text file expanding all variable macros inside it |
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
|
126 func buildHTML(path string, w io.Writer, funcs Funcs, vars Vars) 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
|
127 b, err := ioutil.ReadFile(path) |
0 | 128 if err != nil { |
129 return err | |
130 } | |
18 | 131 content, err := render(string(b), funcs, vars) |
0 | 132 if err != nil { |
133 return err | |
134 } | |
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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 } |
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
|
140 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
|
141 w = f |
14 | 142 } |
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
|
143 _, err = io.WriteString(w, content) |
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
|
144 return err |
0 | 145 } |
146 | |
18 | 147 // Renders .amber file into .html |
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
|
148 func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error { |
17 | 149 a := amber.New() |
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
|
150 err := a.ParseFile(path) |
17 | 151 if err != nil { |
152 return err | |
153 } | |
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
|
154 |
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
|
155 data := map[string]interface{}{} |
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
|
156 for k, v := range vars { |
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
|
157 data[k] = v |
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
|
158 } |
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
|
159 for k, v := range funcs { |
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
|
160 data[k] = v |
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
|
161 } |
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
|
162 |
17 | 163 t, err := a.Compile() |
164 if err != nil { | |
165 return err | |
166 } | |
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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 } |
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
|
172 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
|
173 w = f |
17 | 174 } |
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
|
175 return t.Execute(w, data) |
17 | 176 } |
177 | |
18 | 178 // 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
|
179 func buildGCSS(path string, w io.Writer) error { |
18 | 180 f, err := os.Open(path) |
181 if err != nil { | |
182 return err | |
183 } | |
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
|
184 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
|
185 |
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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 } |
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 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
|
193 w = css |
18 | 194 } |
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
|
195 _, err = gcss.Compile(w, f) |
18 | 196 return err |
197 } | |
198 | |
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
|
199 // 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 } |
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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 } else { |
0 | 210 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
|
211 w = out |
0 | 212 } |
213 } | |
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
|
214 _, err = io.Copy(w, in) |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
215 return err |
0 | 216 } |
217 | |
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
|
218 func build(path string, w io.Writer, funcs Funcs, vars Vars) 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
|
219 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
|
220 if ext == ".md" || ext == ".mkd" { |
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
|
221 return buildMarkdown(path, w, funcs, vars) |
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
|
222 } else if ext == ".html" || ext == ".xml" { |
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
|
223 return buildHTML(path, w, funcs, vars) |
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
|
224 } else if ext == ".amber" { |
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
|
225 return buildAmber(path, w, funcs, vars) |
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
|
226 } 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
|
227 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
|
228 } 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
|
229 return buildRaw(path, w) |
18 | 230 } |
231 } | |
232 | |
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
|
233 func buildAll(watch bool) { |
0 | 234 lastModified := time.Unix(0, 0) |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
235 modified := false |
19
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
236 |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
237 vars := globals() |
0 | 238 for { |
239 os.Mkdir(PUBDIR, 0755) | |
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
|
240 funcs := builtins() |
0 | 241 err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { |
242 // ignore hidden files and directories | |
243 if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") { | |
244 return nil | |
245 } | |
246 | |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
247 if info.IsDir() { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
248 os.Mkdir(filepath.Join(PUBDIR, path), 0755) |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
249 return nil |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
250 } else if info.ModTime().After(lastModified) { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
251 if !modified { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
252 // About to be modified, so run pre-build hook |
18 | 253 // FIXME on windows it might not work well |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
254 run(filepath.Join(ZSDIR, "pre"), []string{}, nil, nil) |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
255 modified = true |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
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 log.Println("build: ", 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
|
258 return build(path, nil, funcs, vars) |
0 | 259 } |
260 return nil | |
261 }) | |
262 if err != nil { | |
263 log.Println("ERROR:", err) | |
264 } | |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
265 if modified { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
266 // Something was modified, so post-build hook |
18 | 267 // FIXME on windows it might not work well |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
268 run(filepath.Join(ZSDIR, "post"), []string{}, nil, nil) |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
269 modified = false |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
270 } |
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
|
271 if !watch { |
0 | 272 break |
273 } | |
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
|
274 lastModified = time.Now() |
0 | 275 time.Sleep(1 * time.Second) |
276 } | |
277 } | |
278 | |
279 func main() { | |
280 if len(os.Args) == 1 { | |
281 fmt.Println(os.Args[0], "<command> [args]") | |
282 return | |
283 } | |
284 cmd := os.Args[1] | |
285 args := os.Args[2:] | |
286 switch cmd { | |
287 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
|
288 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
|
289 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
|
290 } else if len(args) == 1 { |
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
|
291 if err := build(args[0], os.Stdout, builtins(), globals()); 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
|
292 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
|
293 } |
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
|
294 } 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
|
295 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
|
296 } |
0 | 297 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
|
298 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
|
299 case "var": |
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 fmt.Println(Var(args)) |
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
|
301 case "lorem": |
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 fmt.Println(Lorem(args)) |
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
|
303 case "dateparse": |
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
|
304 fmt.Println(DateParse(args)) |
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 case "datefmt": |
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
|
306 fmt.Println(DateFmt(args)) |
0 | 307 default: |
17 | 308 err := run(path.Join(ZSDIR, cmd), args, Vars{}, os.Stdout) |
4
05fc24caac37
render uses strings, not bytes; added helpers for env and run
zaitsev.serge
parents:
3
diff
changeset
|
309 if err != nil { |
20
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
310 log.Println("ERROR:", err) |
0 | 311 } |
312 } | |
313 } |