forked from mgbelisle/static-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
330 lines (314 loc) · 8.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"crypto/rand"
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
var usagePrefix = fmt.Sprintf(`Builds a static site using the html/template package, with TemplateData provided.
Usage: %s [OPTIONS]
OPTIONS:
`, os.Args[0])
var (
inFlag = flag.String("in", "src", "Input dir")
outFlag = flag.String("out", "docs", "Output dir")
dataFlag = flag.String("data", "data", "Data dir (for json data)")
templatesFlag = flag.String("templates", "templates/base.html templates", "String separated list of template files/dirs. The first one is the base template (required)")
verboseFlag = flag.Bool("verbose", false, "Verbose output")
addrFlag = flag.String("addr", "", "Address to serve output dir, if provided")
maxOpenFlag = flag.Int("max-open", 100, "Max number of files to open at once")
)
type TemplateData struct {
URL func(string) (string, error)
Active func(string) (bool, error)
}
var TemplateFuncs = template.FuncMap{
"json": func(file string) (interface{}, error) {
data, err := ioutil.ReadFile(filepath.Join(*dataFlag, file))
if err != nil {
return nil, err
}
var obj interface{}
return obj, json.Unmarshal(data, &obj)
},
"sprintf": func(format string, a ...interface{}) string {
return fmt.Sprintf(format, a...)
},
"uniq": func() string {
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x", b)
},
"read": func(file string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(*dataFlag, file))
if err != nil {
return "", err
}
return string(data), nil
},
"html": func(v string) template.HTML {
return template.HTML(v)
},
}
var (
logPrefix = os.Args[0] + ": "
verboseLogger = log.New(ioutil.Discard, logPrefix, log.LstdFlags)
errLogger = log.New(os.Stderr, logPrefix, log.LstdFlags)
maxOpenInLimit = make(chan struct{})
maxOpenOutLimit = make(chan struct{})
)
func main() {
// Flag setup
flag.Usage = func() {
fmt.Fprint(os.Stderr, usagePrefix)
flag.PrintDefaults()
}
flag.Parse()
// Logger setup
if *verboseFlag {
verboseLogger = log.New(os.Stdout, logPrefix, log.LstdFlags)
}
maxOpenInLimit = make(chan struct{}, *maxOpenFlag/2)
maxOpenOutLimit = make(chan struct{}, *maxOpenFlag/2)
// Build once
build(func(err error) {
errLogger.Panic(err)
})
wg := sync.WaitGroup{}
if *addrFlag != "" {
// Serve at addr if provided
wg.Add(1)
go func() {
defer wg.Add(-1)
verboseLogger.Printf("Serving %s on %s", *outFlag, *addrFlag)
if err := http.ListenAndServe(*addrFlag, http.FileServer(http.Dir(*outFlag))); err != nil {
errLogger.Panic(err)
}
}()
// Listen for changes
wg.Add(1)
go func() {
defer wg.Add(-1)
prevModTime := time.Now()
for {
rebuild := false
checkChange := func(path string, info os.FileInfo) {
if info.ModTime().After(prevModTime) {
verboseLogger.Printf("Change detected in %s", path)
rebuild = true
prevModTime = info.ModTime()
}
}
for _, path := range append([]string{
*inFlag,
*dataFlag,
}, strings.Fields(*templatesFlag)...) {
info, err := os.Stat(path)
if err != nil {
errLogger.Print(err)
break
}
if info.IsDir() {
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
checkChange(path, info)
return nil
}); err != nil {
errLogger.Print(err)
break
}
} else {
checkChange(path, info)
}
}
if rebuild {
build(func(err error) {
errLogger.Print(err)
})
}
time.Sleep(time.Second)
}
}()
}
wg.Wait()
}
func build(errLogFunc func(error)) {
// Templates setup
templatesFields := strings.Fields(*templatesFlag)
if len(templatesFields) < 1 {
errLogFunc(errors.New("--templates requires at least the base template"))
return
}
tmpl, err := template.New(filepath.Base(templatesFields[0])).Funcs(TemplateFuncs).ParseFiles(templatesFields[0])
if err != nil {
errLogFunc(err)
return
}
verboseLogger.Printf("Parsed base template: %s", templatesFields[0])
for _, path := range templatesFields[1:] {
info, err := os.Stat(path)
if err != nil {
errLogFunc(err)
return
}
if info.IsDir() {
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
tmpl, err = tmpl.ParseFiles(path)
if err != nil {
return err
}
}
return nil
}); err != nil {
errLogFunc(err)
return
}
} else {
tmpl, err = tmpl.ParseFiles(path)
if err != nil {
errLogFunc(err)
return
}
}
verboseLogger.Printf("Parsed templates: %s", path)
}
// Render the files
if err := os.RemoveAll(*outFlag); err != nil {
errLogFunc(err)
return
}
wg := sync.WaitGroup{}
if err := filepath.Walk(*inFlag, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(*inFlag, path)
if err != nil {
return err
}
outPath := filepath.Join(*outFlag, relPath)
if info.IsDir() {
// Make the dir
verboseLogger.Printf("Creating dir: %s", outPath)
if err := os.Mkdir(outPath, info.Mode()); err != nil {
return err
}
} else {
// Otherwise execute the template or copy the file, whichever is appropriate.
// Do them all in parallel
wg.Add(1)
go func(path string, outPath string, info os.FileInfo) {
defer wg.Add(-1)
maxOpenOutLimit <- struct{}{}
outFile, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE, info.Mode())
defer func() {
if outFile != nil {
outFile.Close()
}
<-maxOpenOutLimit
}()
if err != nil {
errLogFunc(err)
return
}
rootPath, err := filepath.Rel(filepath.Dir(path), *inFlag)
if err != nil {
errLogFunc(err)
return
}
if tmpl != nil && filepath.Ext(path) == ".html" {
verboseLogger.Printf("Executing template: %s", path)
tmpl2, err := tmpl.Clone()
if err != nil {
errLogFunc(err)
return
}
tmpl2, err = tmpl2.ParseFiles(path)
if err != nil {
errLogFunc(err)
return
}
if err := tmpl2.Execute(outFile, &TemplateData{
URL: func(url string) (string, error) {
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return url, nil
}
fromSlash := filepath.FromSlash(url)
stat := fromSlash
if filepath.IsAbs(stat) {
stat = filepath.Join(*inFlag, stat)
} else {
return "", errors.New("Relative paths not supported yet") // TODO
}
if info, err := os.Stat(stat); err != nil {
return "", err
} else if info.IsDir() {
if _, err := os.Stat(filepath.Join(stat, "index.html")); err != nil {
return "", err
}
}
return filepath.ToSlash(filepath.Join(rootPath, fromSlash)), nil
},
Active: func(url string) (bool, error) {
if url == "/" {
return relPath == "index.html", nil
}
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return false, nil
}
fromSlash := filepath.FromSlash(url)
if filepath.IsAbs(fromSlash) {
return strings.HasPrefix(relPath, strings.TrimPrefix(fromSlash, string(filepath.Separator))), nil
} else {
return false, errors.New("Relative paths not supported yet") // TODO
}
},
}); err != nil {
errLogFunc(err)
return
}
} else {
verboseLogger.Printf("Copying file: %s", path)
maxOpenInLimit <- struct{}{}
inFile, err := os.Open(path)
defer func() {
if inFile != nil {
inFile.Close()
}
<-maxOpenInLimit
}()
if err != nil {
errLogFunc(err)
return
}
if _, err := io.Copy(outFile, inFile); err != nil {
errLogFunc(err)
return
}
}
}(path, outPath, info)
}
return nil
}); err != nil {
errLogFunc(err)
return
}
wg.Wait()
}