-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
345 lines (289 loc) · 6.88 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"github.com/sbertrang/atomic"
)
var quiet bool = false
var upre = regexp.MustCompile(`[A-Z]+`)
func htmlpath(mdpath string) string {
// only change .md extension
if mdpath[len(mdpath)-3:len(mdpath)] != ".md" {
return mdpath
}
// only treat the filename
dir, mdfile := path.Split(mdpath)
// uppercase to lowercase
x := upre.ReplaceAllStringFunc(mdfile, strings.ToLower)
if x == "readme.md" {
x = "index.md"
}
// underscores to dashes
x = strings.ReplaceAll(x, "_", "-")
// same direcory, new basename, new extension
return path.Join(dir, x[0:len(x)-3]+".html")
}
func writeResult(tpl *template.Template, html string, file string, modtime time.Time) ([]byte, error) {
data := struct {
Body template.HTML
DateModified time.Time
}{
Body: template.HTML(html),
DateModified: modtime,
}
// get bytes and checksum at once
b := bytes.NewBuffer([]byte{})
h := sha1.New()
w := io.MultiWriter(b, h)
if err := tpl.ExecuteTemplate(w, tpl.Name(), &data); err != nil {
return nil, err
}
srcsum := h.Sum(nil)
// try to open target
f, err := os.Open(file)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
// compare if target exists
if f != nil {
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
f.Close()
return nil, err
}
f.Close()
dstsum := h.Sum(nil)
// all done when both are the same
if bytes.Equal(dstsum, srcsum) {
if !quiet {
log.Printf("same checksum: %s (%s)", file, hex.EncodeToString(dstsum))
}
return dstsum, nil
}
}
if !quiet {
log.Printf("updating file: %s (%s)", file, hex.EncodeToString(srcsum))
}
if err := atomic.WriteFile(file, b, 0644); err != nil {
return nil, err
}
return srcsum, nil
}
func copyFile(src, dst string) error {
fin, err := os.Open(src)
if err != nil {
return err
}
defer fin.Close()
finstat, err := fin.Stat()
if err != nil {
return err
}
if !finstat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
buf := bytes.NewBuffer([]byte{})
h := sha1.New()
w := io.MultiWriter(buf, h)
if _, err := io.Copy(w, fin); err != nil {
return err
}
finsum := h.Sum(nil)
// check destination
fout, err := os.Open(dst)
// it's okay for files to not exist, the rest is not
if err != nil && !os.IsNotExist(err) {
return err
}
// get previous checksum
if fout != nil {
defer fout.Close()
h := sha1.New()
if _, err := io.Copy(h, fout); err != nil {
return err
}
foutsum := h.Sum(nil)
// same checksum, we're good
if bytes.Equal(foutsum, finsum) {
if !quiet {
log.Printf("same checksum: %s (%s)", dst, hex.EncodeToString(finsum))
}
return nil
}
}
if !quiet {
log.Printf("updating file: %s (%s)", dst, hex.EncodeToString(finsum))
}
if err := atomic.WriteFile(dst, buf, 0644); err != nil {
return err
}
return nil
}
type MarkdownFile struct {
Content []byte
Path string
Dir string
Name string
DateModified time.Time
}
type AssetFile struct {
Path string
Info os.FileInfo
}
func usage() {
progname, err := os.Executable()
if err != nil {
log.Fatal(err)
}
_, progname = path.Split(progname)
fmt.Fprintf(os.Stderr, "usage: %s [-destdir dir] [-assetdir dir] [-tmplfile file] md [md ...]\n", progname)
os.Exit(1)
}
func main() {
var destdir string
var assetdir string
var tmplfile string
flag.StringVar(&destdir, "destdir", "", "destination directory for output files")
flag.StringVar(&assetdir, "assetdir", "", "asset source directory")
flag.StringVar(&tmplfile, "tmplfile", "template.html", "html template")
flag.BoolVar(&quiet, "quiet", false, "hide detailed log output")
flag.Parse()
files := make([]string, 0)
data := make(map[string]MarkdownFile)
xmap := make(map[string]string)
args := flag.Args()
files = append(files, args...)
tpl, err := template.ParseFiles(tmplfile)
if err != nil {
log.Fatal(err)
}
if len(files) == 0 {
usage()
}
if !quiet {
log.Println("reading input...")
}
re := regexp.MustCompile("([^()]+\\.md)")
for n := 0; n < len(files); n++ {
fpath := files[n]
dir, file := path.Split(fpath)
// XXX: subdir check
_, found := data[fpath]
if found {
continue
}
xmap[file] = fpath
content, err := ioutil.ReadFile(fpath)
if err != nil {
log.Fatal(err)
}
fi, err := os.Stat(fpath)
if err != nil {
log.Fatal(err)
}
data[fpath] = MarkdownFile{
Content: content,
Path: fpath,
Dir: dir,
Name: file,
DateModified: fi.ModTime(),
}
datafiles := re.FindAllString(string(content), -1)
for _, datafile := range datafiles {
files = append(files, path.Join(dir, datafile))
}
if !quiet {
log.Printf("found markdown: %s", file)
}
}
files = nil
for k := range data {
files = append(files, k)
}
sort.Strings(files)
hre := regexp.MustCompile(`href="[^"]+\.md"`)
if !quiet {
log.Println("generating html...")
}
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.CommonHTMLFlags | blackfriday.HrefTargetBlank,
})
p := bluemonday.UGCPolicy()
p.AllowAttrs("target").Matching(regexp.MustCompile(`^_blank$`)).OnElements("a")
for _, file := range files {
unsafe := blackfriday.Run([]byte(data[file].Content),
blackfriday.WithExtensions(blackfriday.CommonExtensions|blackfriday.NoEmptyLineBeforeBlock|blackfriday.AutoHeadingIDs),
blackfriday.WithRenderer(renderer),
)
html := p.SanitizeBytes(unsafe)
newhtml := hre.ReplaceAllStringFunc(string(html), func(in string) string {
f := in[6 : len(in)-1]
return `href="` + htmlpath(f) + `"`
})
destpath := htmlpath(file)
if destdir != "" {
_, base := path.Split(destpath)
destpath = path.Join(destdir, base)
}
_, err := writeResult(tpl, newhtml, destpath, data[file].DateModified)
if err != nil {
log.Fatal(err)
}
}
// all done without assets to copy
if assetdir == "" {
return
}
if !quiet {
log.Printf("scanning for assets: %s\n", assetdir)
}
assets := make([]AssetFile, 0)
if err := filepath.Walk(assetdir, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name()[0:1] == "." || info.IsDir() {
return nil
}
rel, err := filepath.Rel(assetdir, file)
if err != nil {
log.Fatal(err)
}
assets = append(assets, AssetFile{
Path: rel,
Info: info,
})
return nil
}); err != nil {
log.Fatal(err)
}
for _, asset := range assets {
dst := path.Join(destdir, asset.Path)
dir, _ := path.Split(dst)
if dir != destdir {
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatal(err)
}
}
src := path.Join(assetdir, asset.Path)
if err := copyFile(src, dst); err != nil {
log.Fatal(err)
}
}
}