-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
510 lines (461 loc) · 11.7 KB
/
build.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Command build writes out the static HTML for the Chitin website.
//
// Usage:
//
// go run build.go
//
// You will need a Go development environment and the necessary
// libraries installed.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/russross/blackfriday"
"github.com/tdewolff/minify"
mincss "github.com/tdewolff/minify/css"
minhtml "github.com/tdewolff/minify/html"
minjs "github.com/tdewolff/minify/js"
minjson "github.com/tdewolff/minify/json"
minsvg "github.com/tdewolff/minify/svg"
minxml "github.com/tdewolff/minify/xml"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
const outputDir = "output"
var builders = map[string]func(path string, info os.FileInfo) error{
".md": markdown,
".dot": graphvizDot,
".svg": svg,
}
var layout = template.Must(template.ParseFiles("template.html"))
var minifier = minify.New()
func init() {
minifier.AddFunc("text/css", mincss.Minify)
minifier.AddFunc("text/html", minhtml.Minify)
minifier.AddFunc("text/javascript", minjs.Minify)
minifier.AddFunc("image/svg+xml", minsvg.Minify)
minifier.AddFuncRegexp(regexp.MustCompile("[/+]json$"), minjson.Minify)
minifier.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), minxml.Minify)
}
// get the text content of children of this node
func childText(node *html.Node) string {
var s string
for child := node.FirstChild; child != nil; child = child.NextSibling {
switch child.Type {
case html.TextNode:
s += child.Data
case html.ElementNode:
s += childText(child)
}
}
return s
}
func writeFile(path string, data []byte) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
tmp, err := ioutil.TempFile(dir, ".tmp-")
if err != nil {
return err
}
defer func() {
if tmp != nil {
_ = os.Remove(tmp.Name())
}
}()
if _, err := tmp.Write(data); err != nil {
return err
}
if err := os.Rename(tmp.Name(), path); err != nil {
return err
}
tmp = nil
return nil
}
func markdown(path string, info os.FileInfo) error {
input, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// extract the header out of the markdown, so we can control the
// layout better; blackfriday would put the toc above the h1, and
// include the singular h1 in the toc, causing stutter.
idx := bytes.IndexByte(input, '\n')
if idx == -1 {
return errors.New("markdown has no content")
}
titleMD, input := input[:idx], input[idx+1:]
htmlFlags := (0 |
blackfriday.HTML_USE_SMARTYPANTS |
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES |
blackfriday.HTML_USE_XHTML |
blackfriday.HTML_FOOTNOTE_RETURN_LINKS |
0)
// HtmlRenderer demands a title and a css path here, but we only
// render a fragment so those are not used
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
extensions := (0 |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_FOOTNOTES |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_AUTO_HEADER_IDS |
0)
titleHTML := blackfriday.Markdown(titleMD, renderer, extensions)
contentHTML := blackfriday.Markdown(input, renderer, extensions)
tocFlags := htmlFlags | blackfriday.HTML_TOC | blackfriday.HTML_OMIT_CONTENTS
tocRenderer := blackfriday.HtmlRenderer(tocFlags, "", "")
tocHTML := blackfriday.Markdown(input, tocRenderer, extensions)
body := &html.Node{
Type: html.ElementNode,
Data: "body",
DataAtom: atom.Body,
}
nodes, err := html.ParseFragment(bytes.NewReader(titleHTML), body)
if err != nil {
return fmt.Errorf("cannot parse generated html: %v", err)
}
if len(nodes) == 0 ||
nodes[0].Type != html.ElementNode ||
nodes[0].DataAtom != atom.H1 {
return errors.New("markdown does not start with a header")
}
title := childText(nodes[0])
var buf bytes.Buffer
prettyPath := "/" + strings.TrimSuffix(path, ".md")
if dir, file := filepath.Split(prettyPath); file == "index" {
prettyPath = dir
}
data := struct {
Path string
Title string
H1 template.HTML
TOC template.HTML
Content template.HTML
}{
Path: prettyPath,
Title: title,
H1: template.HTML(titleHTML),
TOC: template.HTML(tocHTML),
Content: template.HTML(contentHTML),
}
if err := layout.Execute(&buf, data); err != nil {
return fmt.Errorf("executing template: %v", err)
}
min, err := minify.Bytes(minifier, "text/html", buf.Bytes())
if err != nil {
return fmt.Errorf("cannot minify html: %v", err)
}
dst := filepath.Join(outputDir, strings.TrimSuffix(path, ".md")+".html")
if err := writeFile(dst, min); err != nil {
return err
}
return nil
}
func graphvizDot(path string, info os.FileInfo) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
cmd := exec.Command("dot", "-Tsvg")
cmd.Stdin = f
buf, err := cmd.Output()
if err != nil {
return fmt.Errorf("error running dot: %v", err)
}
min, err := minify.Bytes(minifier, "image/svg+xml", buf)
if err != nil {
return fmt.Errorf("cannot minify svg: %v", err)
}
dst := filepath.Join(outputDir, strings.TrimSuffix(path, ".dot")+".svg")
if err := writeFile(dst, min); err != nil {
return err
}
return nil
}
func svg(path string, info os.FileInfo) error {
input, err := ioutil.ReadFile(path)
if err != nil {
return err
}
min, err := minify.Bytes(minifier, "image/svg+xml", input)
if err != nil {
return fmt.Errorf("cannot minify svg: %v", err)
}
dst := filepath.Join(outputDir, path)
if err := writeFile(dst, min); err != nil {
return err
}
return nil
}
func processFile(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == outputDir {
// do not recurse into our output directory
return filepath.SkipDir
}
if info.Name()[0] == '.' {
// ignore hidden files, do not recurse into hidden dirs
if info.IsDir() && info.Name() != "." {
return filepath.SkipDir
}
return nil
}
if !info.Mode().IsRegular() {
return nil
}
if info.Name() == "README.md" {
return nil
}
ext := filepath.Ext(info.Name())
if fn, ok := builders[ext]; ok {
log.Printf("source %v", path)
if err := fn(path, info); err != nil {
return fmt.Errorf("build failed: %v: %v", path, err)
}
}
return nil
}
// environReplace updates the list of environment variables to add or
// replace the given variable.
func environReplace(env []string, name, val string) []string {
r := env[:0]
prefix := name + "="
for _, s := range env {
if strings.HasPrefix(s, prefix) {
continue
}
r = append(r, s)
}
r = append(r, name+"="+val)
return r
}
func gitDescribe() (string, error) {
cmd := exec.Command("git", "describe", "--always", "--dirty")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return "", err
}
desc, err := oneLine(buf)
if err != nil {
return "", fmt.Errorf("cannot parse git describe output: %v", err)
}
return desc, nil
}
func gitAddOutput(index string) error {
cmd := exec.Command("git", "update-index", "--add", "-z", "--verbose", "--stdin")
cmd.Env = environReplace(os.Environ(), "GIT_INDEX_FILE", index)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
defer stdin.Close()
if err := cmd.Start(); err != nil {
return err
}
defer func() {
if !cmd.ProcessState.Exited() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
}
}()
fn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
if _, err := io.WriteString(stdin, path+"\x00"); err != nil {
return err
}
return nil
}
if err := filepath.Walk(outputDir, fn); err != nil {
return err
}
if err := stdin.Close(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
func oneLine(b []byte) (string, error) {
if len(b) == 0 {
return "", errors.New("must not be empty")
}
if b[len(b)-1] != '\n' {
return "", fmt.Errorf("must end in newline: %q", b)
}
b = b[:len(b)-1]
if idx := bytes.IndexByte(b, '\n'); idx >= 0 {
return "", fmt.Errorf("must be a single line: %q", b)
}
return string(b), nil
}
func gitWriteTree(index string, prefix string) (string, error) {
cmd := exec.Command("git", "write-tree", "--prefix="+prefix+"/")
cmd.Env = environReplace(os.Environ(), "GIT_INDEX_FILE", index)
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return "", err
}
sha, err := oneLine(buf)
if err != nil {
return "", fmt.Errorf("cannot parse git write-tree output: %v", err)
}
return sha, nil
}
func gitCommit(tree string, message string) (string, error) {
cmd := exec.Command("git", "commit-tree", "-m", message, tree)
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return "", err
}
sha, err := oneLine(buf)
if err != nil {
return "", fmt.Errorf("cannot parse git commit-tree output: %v", err)
}
return sha, nil
}
func gitUpdateRef(ref string, commit string) error {
cmd := exec.Command("git", "update-ref", "-m", "Build", ref, commit)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func gitIsMaster() (bool, error) {
cmd := exec.Command("git", "symbolic-ref", "HEAD")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return false, err
}
ref, err := oneLine(buf)
if err != nil {
return false, fmt.Errorf("cannot parse git symbolic-ref output: %v", err)
}
isMaster := ref == "refs/heads/master"
return isMaster, nil
}
func writeToGit() error {
index := ".git/index.build"
// the index file must not actually exist before we call git
if err := os.Remove(index); err != nil && !os.IsNotExist(err) {
return err
}
defer func() {
_ = os.Remove(index)
}()
desc, err := gitDescribe()
if err != nil {
return err
}
if err := gitAddOutput(index); err != nil {
return err
}
tree, err := gitWriteTree(index, outputDir)
if err != nil {
return err
}
message := fmt.Sprintf("Regenerated site from %s\n", desc)
commit, err := gitCommit(tree, message)
if err != nil {
return err
}
log.Printf("Prepared %s", commit)
isMaster, err := gitIsMaster()
if err != nil {
return err
}
if !isMaster {
log.Printf("Not on master, not publishing.")
return nil
}
if strings.HasSuffix(desc, "-dirty") {
log.Printf("Dirty, not publishing.")
return nil
}
if gitUpdateRef("refs/heads/autogenerated", commit); err != nil {
return err
}
/*
This assumes you have .git/config section
[remote "ghpages"]
url = https://github.com/chitin-io/chitin-io.github.io
fetch = +refs/heads/*:refs/remotes/ghpages/*
push = +refs/heads/autogenerated:refs/heads/master
*/
log.Printf("To see diff run\n\n %s\n\n",
"git diff ghpages/master autogenerated")
log.Printf("To push run\n\n %s\n\n",
"git push ghpages")
return nil
}
func run() error {
if err := os.Mkdir(outputDir, 0755); err != nil && !os.IsExist(err) {
return err
}
if err := filepath.Walk(".", processFile); err != nil {
return err
}
if err := writeFile(filepath.Join(outputDir, ".nojekyll"), nil); err != nil {
return err
}
if err := writeFile(filepath.Join(outputDir, "CNAME"), []byte("chitin.io\n")); err != nil {
return err
}
if err := writeToGit(); err != nil {
return err
}
return nil
}
var prog = filepath.Base(os.Args[0])
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, " %s\n", prog)
fmt.Fprintf(os.Stderr, "(the command takes no options)\n")
}
func main() {
log.SetFlags(0)
log.SetPrefix(prog + ": ")
flag.Usage = usage
flag.Parse()
if flag.NArg() > 0 {
flag.Usage()
os.Exit(2)
}
if err := run(); err != nil {
log.Fatal(err)
}
}