source: code/trunk/main.go@ 51

Last change on this file since 51 was 48, checked in by prologic, 4 years ago

Forked project

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