Mercurial > yakumo_izuru > aya
annotate zs.go @ 31:b2f491299cee draft
dead end with template functions
author | zaitsev.serge |
---|---|
date | Wed, 02 Sep 2015 14:54:16 +0000 |
parents | 526ba3c717ba |
children | 75822e38c3e0 |
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{ |
28
5ee89d094279
removed default date value, added some default title/description/keywords for markdown
zaitsev.serge
parents:
27
diff
changeset
|
38 "title": "", |
5ee89d094279
removed default date value, added some default title/description/keywords for markdown
zaitsev.serge
parents:
27
diff
changeset
|
39 "description": "", |
5ee89d094279
removed default date value, added some default title/description/keywords for markdown
zaitsev.serge
parents:
27
diff
changeset
|
40 "keywords": "", |
29
dd669a7e582f
file, url and output should not be overridden by globals
zaitsev.serge
parents:
28
diff
changeset
|
41 } |
dd669a7e582f
file, url and output should not be overridden by globals
zaitsev.serge
parents:
28
diff
changeset
|
42 for name, value := range globals { |
dd669a7e582f
file, url and output should not be overridden by globals
zaitsev.serge
parents:
28
diff
changeset
|
43 v[name] = value |
6 | 44 } |
20
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
45 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
|
46 v["layout"] = "layout.amber" |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
47 } else { |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
48 v["layout"] = "layout.html" |
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
49 } |
29
dd669a7e582f
file, url and output should not be overridden by globals
zaitsev.serge
parents:
28
diff
changeset
|
50 v["file"] = path |
dd669a7e582f
file, url and output should not be overridden by globals
zaitsev.serge
parents:
28
diff
changeset
|
51 v["url"] = url |
dd669a7e582f
file, url and output should not be overridden by globals
zaitsev.serge
parents:
28
diff
changeset
|
52 v["output"] = filepath.Join(PUBDIR, url) |
20
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
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) { |
31 | 70 f := makeFuncs(funcs, 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
|
71 tmpl, err := template.New("").Funcs(template.FuncMap(f)).Parse(s) |
18 | 72 if err != nil { |
73 return "", err | |
74 } | |
75 out := &bytes.Buffer{} | |
76 if err := tmpl.Execute(out, vars); err != nil { | |
77 return "", err | |
78 } | |
79 return string(out.Bytes()), nil | |
0 | 80 } |
81 | |
18 | 82 // 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
|
83 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
|
84 v, body, err := md(path, vars) |
0 | 85 if err != nil { |
86 return err | |
87 } | |
18 | 88 content, err := render(body, funcs, v) |
0 | 89 if err != nil { |
90 return err | |
91 } | |
31 | 92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 } |
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
|
98 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
|
99 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
|
100 } |
18 | 101 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
|
102 return buildAmber(filepath.Join(ZSDIR, v["layout"]), w, funcs, v) |
18 | 103 } 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
|
104 return buildHTML(filepath.Join(ZSDIR, v["layout"]), w, funcs, v) |
18 | 105 } |
14 | 106 } |
107 | |
18 | 108 // 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
|
109 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
|
110 b, err := ioutil.ReadFile(path) |
0 | 111 if err != nil { |
112 return err | |
113 } | |
18 | 114 content, err := render(string(b), funcs, vars) |
0 | 115 if err != nil { |
116 return err | |
117 } | |
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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 } |
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
|
123 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
|
124 w = f |
14 | 125 } |
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
|
126 _, 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
|
127 return err |
0 | 128 } |
129 | |
18 | 130 // 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
|
131 func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error { |
17 | 132 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
|
133 err := a.ParseFile(path) |
17 | 134 if err != nil { |
135 return err | |
136 } | |
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
|
137 |
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 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
|
139 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
|
140 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
|
141 } |
31 | 142 for k, v := range makeFuncs(funcs, Vars{}) { |
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 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
|
144 } |
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
|
145 |
17 | 146 t, err := a.Compile() |
147 if err != nil { | |
148 return err | |
149 } | |
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 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
|
151 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
|
152 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
|
153 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
|
154 } |
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
|
155 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
|
156 w = f |
17 | 157 } |
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
|
158 return t.Execute(w, data) |
17 | 159 } |
160 | |
18 | 161 // 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
|
162 func buildGCSS(path string, w io.Writer) error { |
18 | 163 f, err := os.Open(path) |
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 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
|
168 |
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 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 } |
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
|
175 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
|
176 w = css |
18 | 177 } |
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
|
178 _, err = gcss.Compile(w, f) |
18 | 179 return err |
180 } | |
181 | |
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
|
182 // 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 } |
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 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
|
189 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
|
190 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
|
191 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
|
192 } else { |
0 | 193 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
|
194 w = out |
0 | 195 } |
196 } | |
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
|
197 _, err = io.Copy(w, in) |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
198 return err |
0 | 199 } |
200 | |
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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 } 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
|
206 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
|
207 } 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
|
208 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
|
209 } 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
|
210 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
|
211 } 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
|
212 return buildRaw(path, w) |
18 | 213 } |
214 } | |
215 | |
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
|
216 func buildAll(watch bool) { |
0 | 217 lastModified := time.Unix(0, 0) |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
218 modified := false |
19
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
219 |
802b96e67bae
added global variables, added default date for markdown
zaitsev.serge
parents:
18
diff
changeset
|
220 vars := globals() |
0 | 221 for { |
222 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
|
223 funcs := builtins() |
0 | 224 err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error { |
225 // ignore hidden files and directories | |
226 if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") { | |
227 return nil | |
228 } | |
30 | 229 // inform user about fs walk errors, but continue iteration |
230 if err != nil { | |
231 log.Println("ERROR:", err) | |
232 return nil | |
233 } | |
0 | 234 |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
235 if info.IsDir() { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
236 os.Mkdir(filepath.Join(PUBDIR, path), 0755) |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
237 return nil |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
238 } else if info.ModTime().After(lastModified) { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
239 if !modified { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
240 // About to be modified, so run pre-build hook |
18 | 241 // 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
|
242 run(filepath.Join(ZSDIR, "pre"), []string{}, nil, nil) |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
243 modified = true |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
244 } |
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
|
245 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
|
246 return build(path, nil, funcs, vars) |
0 | 247 } |
248 return nil | |
249 }) | |
250 if err != nil { | |
251 log.Println("ERROR:", err) | |
252 } | |
7
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
253 if modified { |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
254 // Something was modified, so post-build hook |
18 | 255 // 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
|
256 run(filepath.Join(ZSDIR, "post"), []string{}, nil, nil) |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
257 modified = false |
11073a30f71f
simplified build process, added pre and post build hooks
zaitsev.serge
parents:
6
diff
changeset
|
258 } |
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
|
259 if !watch { |
0 | 260 break |
261 } | |
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
|
262 lastModified = time.Now() |
0 | 263 time.Sleep(1 * time.Second) |
264 } | |
265 } | |
266 | |
267 func main() { | |
268 if len(os.Args) == 1 { | |
269 fmt.Println(os.Args[0], "<command> [args]") | |
270 return | |
271 } | |
272 cmd := os.Args[1] | |
273 args := os.Args[2:] | |
274 switch cmd { | |
275 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
|
276 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
|
277 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
|
278 } 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
|
279 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
|
280 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
|
281 } |
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
|
282 } 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
|
283 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
|
284 } |
0 | 285 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
|
286 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
|
287 case "var": |
31 | 288 fmt.Println(Var(args...)) |
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
|
289 case "lorem": |
31 | 290 fmt.Println(Lorem(args...)) |
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
|
291 case "dateparse": |
31 | 292 fmt.Println(DateParse(args...)) |
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
|
293 case "datefmt": |
31 | 294 fmt.Println(DateFmt(args...)) |
25 | 295 case "wc": |
31 | 296 fmt.Println(WordCount(args...)) |
297 case "ttr": | |
298 fmt.Println(TimeToRead(args...)) | |
299 case "ls": | |
300 fmt.Println(strings.Join(List(args...), "\n")) | |
301 case "sort": | |
302 fmt.Println(strings.Join(Sort(args...), "\n")) | |
303 case "exec": | |
304 // TODO | |
0 | 305 default: |
27 | 306 err := run(path.Join(ZSDIR, cmd), args, globals(), os.Stdout) |
4
05fc24caac37
render uses strings, not bytes; added helpers for env and run
zaitsev.serge
parents:
3
diff
changeset
|
307 if err != nil { |
20
f8373c23a3ff
replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents:
19
diff
changeset
|
308 log.Println("ERROR:", err) |
0 | 309 } |
310 } | |
311 } |