annotate zs.go @ 23:40f55059fbfa 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:20:35 +0000
parents f5627d4212a3
children d052f3a44195
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"
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
10 "path"
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"
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
19 )
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
20
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
21 const (
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
22 ZSDIR = ".zs"
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
23 PUBDIR = ".pub"
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
24 )
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
25
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
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
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
28
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
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
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
31 b, err := ioutil.ReadFile(path)
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
32 if err != nil {
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
33 return nil, "", err
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
34 }
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
35 s := string(b)
6
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
36 url := path[:len(path)-len(filepath.Ext(path))] + ".html"
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
37 v := Vars{
6
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
38 "file": path,
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
39 "url": url,
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
40 "output": filepath.Join(PUBDIR, url),
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
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
fd79b3a90bef fixed empty header in markdown
zaitsev.serge
parents: 0
diff changeset
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
fd79b3a90bef fixed empty header in markdown
zaitsev.serge
parents: 0
diff changeset
56 }
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
57 header, body := split2(s, "\n\n")
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
58 for _, line := range strings.Split(header, "\n") {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
59 key, value := split2(line, ":")
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
60 v[strings.ToLower(strings.TrimSpace(key))] = strings.TrimSpace(value)
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
61 }
6
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
62 if strings.HasPrefix(v["url"], "./") {
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
63 v["url"] = v["url"][2:]
cb66f5b86616 rewritted default variables assignment
zaitsev.serge
parents: 5
diff changeset
64 }
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
65 return v, body, nil
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
66 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
67
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
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
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
71 for k, v := range funcs {
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
72 f[k] = v
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
73 }
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
74 for k, v := range vars {
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
75 f[k] = varFunc(v)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
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
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
89 if err != nil {
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
90 return "", err
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
91 }
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
92 out := &bytes.Buffer{}
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
93 if err := tmpl.Execute(out, vars); err != nil {
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
94 return "", err
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
95 }
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
96 return string(out.Bytes()), nil
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
97 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
98
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
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
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
102 if err != nil {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
103 return err
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
104 }
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
105 content, err := render(body, funcs, v)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
106 if err != nil {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
107 return err
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
108 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
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
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
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
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
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
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
122 }
14
515078352442 moved html rendering into a separate function
zaitsev.serge
parents: 13
diff changeset
123 }
515078352442 moved html rendering into a separate function
zaitsev.serge
parents: 13
diff changeset
124
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
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
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
128 if err != nil {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
129 return err
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
130 }
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
131 content, err := render(string(b), funcs, vars)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
132 if err != nil {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
133 return err
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
134 }
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
135 output := filepath.Join(PUBDIR, path)
14
515078352442 moved html rendering into a separate function
zaitsev.serge
parents: 13
diff changeset
136 if s, ok := vars["output"]; ok {
515078352442 moved html rendering into a separate function
zaitsev.serge
parents: 13
diff changeset
137 output = s
515078352442 moved html rendering into a separate function
zaitsev.serge
parents: 13
diff changeset
138 }
515078352442 moved html rendering into a separate function
zaitsev.serge
parents: 13
diff changeset
139 err = ioutil.WriteFile(output, []byte(content), 0666)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
140 if err != nil {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
141 return err
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
142 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
143 return nil
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
144 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
145
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
146 // 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
147 func buildAmber(path string, w io.Writer, funcs Funcs, vars Vars) error {
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
148 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
149 err := a.ParseFile(path)
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
150 if err != nil {
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
151 return err
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
152 }
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
153 t, err := a.Compile()
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
154 if err != nil {
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
155 return err
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
156 }
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
157 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
158 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
159 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
160 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
161 }
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 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
163 w = f
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
164 }
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
165 return t.Execute(w, vars)
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
166 }
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
167
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
168 // 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
169 func buildGCSS(path string, w io.Writer) error {
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
170 f, err := os.Open(path)
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
171 if err != nil {
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
172 return err
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
173 }
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
174 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
175
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 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
177 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
178 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
179 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
180 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
181 }
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 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
183 w = css
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
184 }
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
185 _, err = gcss.Compile(w, f)
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
186 return err
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
187 }
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
188
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
189 // 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
190 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
191 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
192 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
193 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
194 }
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 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
196 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
197 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
198 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
199 } else {
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
200 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
201 w = out
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
202 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
203 }
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
204 _, err = io.Copy(w, in)
7
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
205 return err
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
206 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
207
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
208 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
209 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
210 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
211 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
212 } 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
213 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
214 } 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
215 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
216 } 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
217 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
218 } 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
219 return buildRaw(path, w)
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
220 }
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
221 }
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
222
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 func buildAll(watch bool) {
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
224 lastModified := time.Unix(0, 0)
7
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
225 modified := false
19
802b96e67bae added global variables, added default date for markdown
zaitsev.serge
parents: 18
diff changeset
226
802b96e67bae added global variables, added default date for markdown
zaitsev.serge
parents: 18
diff changeset
227 vars := globals()
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
228 for {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
229 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
230 funcs := builtins()
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
231 err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
232 // ignore hidden files and directories
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
233 if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
234 return nil
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
235 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
236
7
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
237 if info.IsDir() {
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
238 os.Mkdir(filepath.Join(PUBDIR, path), 0755)
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
239 return nil
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
240 } else if info.ModTime().After(lastModified) {
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
241 if !modified {
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
242 // About to be modified, so run pre-build hook
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
243 // 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
244 run(filepath.Join(ZSDIR, "pre"), []string{}, nil, nil)
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
245 modified = true
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
246 }
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
247 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
248 return build(path, nil, funcs, vars)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
249 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
250 return nil
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
251 })
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
252 if err != nil {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
253 log.Println("ERROR:", err)
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
254 }
7
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
255 if modified {
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
256 // Something was modified, so post-build hook
18
ae3116ea938b started migration to go templates
zaitsev.serge
parents: 17
diff changeset
257 // 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
258 run(filepath.Join(ZSDIR, "post"), []string{}, nil, nil)
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
259 modified = false
11073a30f71f simplified build process, added pre and post build hooks
zaitsev.serge
parents: 6
diff changeset
260 }
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
261 if !watch {
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
262 break
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
263 }
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
264 lastModified = time.Now()
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
265 time.Sleep(1 * time.Second)
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
266 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
267 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
268
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
269 func main() {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
270 if len(os.Args) == 1 {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
271 fmt.Println(os.Args[0], "<command> [args]")
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
272 return
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
273 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
274 cmd := os.Args[1]
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
275 args := os.Args[2:]
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
276 switch cmd {
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
277 case "build":
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
278 buildAll(false)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
279 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
280 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
281 case "print":
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 if len(args) != 1 {
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 fmt.Println("ERROR: filename expected")
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 } 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
285 build(args[0], os.Stdout, builtins(), globals())
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
286 }
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
287 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
288 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
289 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
290 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
291 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
292 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
293 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
294 fmt.Println(DateFmt(args))
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
295 default:
17
0214b1b5f5eb added amber and gcss compilers
zaitsev.serge
parents: 15
diff changeset
296 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
297 if err != nil {
20
f8373c23a3ff replaced amber with my own fork, fixed file paths for amber and html
zaitsev.serge
parents: 19
diff changeset
298 log.Println("ERROR:", err)
0
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
299 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
300 }
45a9e1fac18e initial commit
zaitsev.serge
parents:
diff changeset
301 }