-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeynote.go
576 lines (490 loc) · 14.3 KB
/
keynote.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
tt "text/template"
"time"
"github.com/gin-gonic/gin"
"github.com/gorilla/feeds"
cp "github.com/otiai10/copy"
"gopkg.in/yaml.v3"
)
type Site struct {
Logo, Link, Icon,
Name, Title, Author,
Desc, Summry, Copyright,
Beian, BeianLink string
StaticPath, StaticFile []string
}
func loadSite(conf string) (site *Site) {
data, err := os.ReadFile(conf)
fatalErr(err)
site = &Site{}
fatalErr(yaml.Unmarshal(data, site))
return
}
type FileKind string
func (kind FileKind) IsGitbook() bool {
return kind == GITBOOK
}
func (kind FileKind) IsKeynote() bool {
return kind == KEYNOTE
}
func (kind FileKind) IsDocsify() bool {
return kind == DOCSIFY
}
const (
KEYNOTE FileKind = "keynote"
DOCSIFY FileKind = "docsify"
GITBOOK FileKind = "gitbook"
)
func FileKinds() []FileKind {
return []FileKind{KEYNOTE, DOCSIFY, GITBOOK}
}
type FolderProps struct {
Keynote, Docsify, Gitbook, Ignore, Copy []string
}
func (props *FolderProps) getFileKind(file string) (fileKind FileKind, found bool) {
for _, f := range props.Docsify {
if f == file {
return DOCSIFY, true
}
}
for _, f := range props.Gitbook {
if f == file {
return GITBOOK, true
}
}
for _, f := range props.Keynote {
if f == file {
return KEYNOTE, true
}
}
return "", false
}
func loadFolderProps(conf string) (props *FolderProps) {
data, err := os.ReadFile(conf)
fatalErr(err)
props = &FolderProps{}
fatalErr(yaml.Unmarshal(data, props))
return
}
type File struct {
Name, Title string
Kind FileKind
Ctime time.Time
}
type Folder struct {
// private fields
path string
// public fields
Name, Title string
Breadcrumb []string
SubFolders []*Folder
Files []*File
Copy []string
}
type SitemapItem struct {
Link string
LastMod time.Time
}
// Only support files with `.md` suffix or gitbook directories.
func loadKeynotes(keynotesDir, folderName string, breadcrumb []string) (folder *Folder) {
folder = &Folder{
path: keynotesDir,
Name: folderName,
Title: strings.ReplaceAll(folderName, "-", " "),
Breadcrumb: breadcrumb,
}
folderProps := loadFolderProps(filepath.Join(keynotesDir, ".folder.yaml"))
entries, _ := os.ReadDir(keynotesDir)
outer:
for _, v := range entries {
// check ignore list
for _, ignore := range folderProps.Ignore {
if v.Name() == ignore {
continue outer
}
}
// ignore hidden file or directory
if strings.HasPrefix(v.Name(), ".") {
continue
}
// check copy list
for _, copy := range folderProps.Copy {
if v.Name() == copy {
folder.Copy = append(folder.Copy, copy)
continue outer
}
}
fileKind, ok := folderProps.getFileKind(v.Name())
if !v.IsDir() {
// The file without `.md` suffix is ignored.
if isMarkdown := strings.HasSuffix(v.Name(), ".md"); !isMarkdown {
log.Println(v.Name(), "file ignored because not supported")
continue
}
// The file is ignored, if it is:
// 1) absent from `.folder.yaml`.
// 2) neither a keynote nor a docsify.
if !(ok && (fileKind.IsKeynote() || fileKind.IsDocsify())) {
log.Println(v.Name(), "file ignored because of invalid config")
continue
}
} else {
// The directory is ignored, if it appeared
// in `.folder.yaml`, but isn't a gitbook.
// In other words, the directory must be a `folder` or a gitbook.
if ok && !fileKind.IsGitbook() {
log.Println(v.Name(), "dir ignored because of invalid config")
continue
}
}
// If the directory isn't a gitbook, then it's a sub-folder.
if v.IsDir() && !fileKind.IsGitbook() {
subBreadcrumb := make([]string, len(folder.Breadcrumb)+1)
copy(subBreadcrumb, folder.Breadcrumb)
subBreadcrumb[len(subBreadcrumb)-1] = v.Name()
// Recursively load sub-folder
subFolder := loadKeynotes(filepath.Join(keynotesDir, v.Name()), v.Name(), subBreadcrumb)
// Add a sub-folder
folder.SubFolders = append(folder.SubFolders, subFolder)
continue
}
// Ignored if error occurs
info, err := v.Info()
if err != nil {
continue
}
// Get time of the last change
stat := info.Sys().(*syscall.Stat_t)
ctime := time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
// Remove suffix '.md' of keynote or docsify
name := v.Name()
if fileKind.IsKeynote() || fileKind.IsDocsify() {
name = name[:len(name)-3]
}
// Make sure gitbook's name is different from the name of keynote or docsify (with suffix `.md` removed).
for _, file := range folder.Files {
if file.Name == name {
log.Println(v.Name(), "file ignored because of duplicated name")
continue
}
}
// Add a file
folder.Files = append(folder.Files, &File{
Name: name,
Title: strings.ReplaceAll(name, "-", " "),
Kind: fileKind,
Ctime: ctime,
})
}
// Sort files by time of the last change in descending order
if len(folder.Files) > 0 {
sort.Slice(folder.Files, func(i, j int) bool {
return folder.Files[i].Ctime.After(folder.Files[j].Ctime)
})
}
return
}
//go:embed templates/* templates/blocks/*
var tmplFS embed.FS
func homeRender(ch chan<- chan<- []any) func(*gin.Context) {
return func(c *gin.Context) {
site, _ := getData(ch)
c.HTML(http.StatusOK, "index.htm", gin.H{"Site": site, "Year": time.Now().Year()})
}
}
func foldersApi(ch chan<- chan<- []any) func(*gin.Context) {
return func(c *gin.Context) {
_, rootFolder := getData(ch)
c.JSON(http.StatusOK, gin.H{
"RootFolder": rootFolder,
})
}
}
func keynoteRender(c *gin.Context, tmplHtm string, site *Site, keynoteDir, keynoteName, keynoteTitle string) {
c.HTML(http.StatusOK, tmplHtm, gin.H{
"KeynoteDir": keynoteDir,
"KeynoteName": keynoteName,
"KeynoteTitle": keynoteTitle,
"Site": site,
})
}
func noRouteHandler(ch chan<- chan<- []any) func(*gin.Context) {
return func(c *gin.Context) {
site, rootFolder := getData(ch)
path := c.Request.URL.Path
kinds := FileKinds()
for _, kind := range kinds {
if keynoteName, found := strings.CutPrefix(path, fmt.Sprintf("/%ss/", kind)); found {
breadcrumb := strings.Split(keynoteName, "/")
if len(breadcrumb) == 1 {
for _, kn := range rootFolder.Files {
if kn.Name == breadcrumb[0] {
keynoteRender(c, fmt.Sprintf("%s.htm", kind), &site, fmt.Sprintf("%ss", kind), keynoteName, kn.Title)
return
}
}
} else {
p := rootFolder.SubFolders
for i := 0; i < len(breadcrumb)-1; i++ {
for _, f := range p {
if f.Name == breadcrumb[i] {
if i == len(breadcrumb)-2 {
for _, kn := range f.Files {
if kn.Name == breadcrumb[len(breadcrumb)-1] {
keynoteRender(c, fmt.Sprintf("%s.htm", kind), &site, fmt.Sprintf("%ss", kind), keynoteName, kn.Title)
return
}
}
} else {
p = f.SubFolders
break
}
}
}
}
}
}
}
c.AbortWithStatus(http.StatusNotFound)
}
}
func fatalErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func newTemplate() (tmpl *template.Template) {
tmpl = template.Must(template.New("").ParseFS(tmplFS, "templates/*.htm", "templates/blocks/*.htm"))
return
}
func newTextTemplate() (tmpl *tt.Template) {
tmpl = tt.Must(tt.New("").ParseFS(tmplFS, "templates/*.tmpl"))
return
}
func getKeynoteName(folder *Folder, file *File) string {
urlPath := strings.Join(folder.Breadcrumb[1:], "/")
keynoteName, _ := url.JoinPath(urlPath, file.Name)
return keynoteName
}
func getFilteredFiles(folder *Folder, kind FileKind) (filteredFiles []*File) {
for _, kn := range folder.Files {
if kn.Kind == kind {
filteredFiles = append(filteredFiles, kn)
}
}
return
}
func getItemLink(site *Site, basePath string, kind FileKind, folder *Folder, file *File) (itemLink string) {
itemLink, _ = url.JoinPath(
site.Link,
getKeynoteDir(basePath, kind),
getKeynoteName(folder, file),
)
if kind.IsGitbook() {
itemLink, _ = url.JoinPath(itemLink, "latest")
}
return
}
func getKeynoteDir(basePath string, kind FileKind) string {
return filepath.Join(basePath, fmt.Sprintf("%ss", kind))[1:]
}
func genKeynoteHtml(kind FileKind, tmpl *template.Template, site *Site, folder *Folder, path, basePath string, feed *feeds.Feed, items *[]SitemapItem) {
filteredFiles := getFilteredFiles(folder, kind)
if len(filteredFiles) > 0 || len(folder.SubFolders) > 0 {
os.Mkdir(path, os.ModePerm)
for _, kn := range filteredFiles {
if kind.IsKeynote() || kind.IsDocsify() {
// copy original `.md` files for keynote and docsify
mdFile := kn.Name + ".md"
data, _ := os.ReadFile(filepath.Join(folder.path, mdFile))
mdPath := filepath.Join(path, mdFile)
os.WriteFile(mdPath, data, os.ModePerm)
// generate `.html` file
knHtmlPath := filepath.Join(path, kn.Name+".html")
knHtml, _ := os.Create(knHtmlPath)
keynoteName := getKeynoteName(folder, kn)
tmpl.ExecuteTemplate(knHtml, fmt.Sprintf("%s.htm", kind), gin.H{
"KeynoteDir": getKeynoteDir(basePath, kind),
"KeynoteName": keynoteName,
"KeynoteTitle": kn.Title,
"Site": site,
})
} else if kind.IsGitbook() {
// copy latest dir for gitbook
latestDir := filepath.Join(folder.path, kn.Name, "latest")
gitbookDir := filepath.Join(path, kn.Name, "latest")
os.MkdirAll(gitbookDir, os.ModePerm)
fatalErr(cp.Copy(latestDir, gitbookDir))
}
href := getItemLink(site, basePath, kind, folder, kn)
feed.Items = append(feed.Items, &feeds.Item{
Title: kn.Title,
Description: kn.Title,
Author: &feeds.Author{Name: site.Author},
Created: kn.Ctime,
Link: &feeds.Link{Href: href},
})
*items = append(*items, SitemapItem{
Link: href,
LastMod: kn.Ctime,
})
}
for _, f := range folder.SubFolders {
genKeynoteHtml(kind, tmpl, site, f, filepath.Join(path, f.Name), basePath, feed, items)
}
// copy list
for _, copy := range folder.Copy {
fatalErr(cp.Copy(filepath.Join(folder.path, copy), filepath.Join(path, copy)))
}
}
}
func genStaticSite(conf, keynotesDir, outputDir, basePath string) {
if _, err := os.Stat(keynotesDir); os.IsNotExist(err) {
fatalErr(err)
}
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
fatalErr(err)
}
tmpl := newTemplate()
ttmpl := newTextTemplate()
// load data
site := loadSite(conf)
rootFolder := loadKeynotes(keynotesDir, "/", []string{"/"})
// clear old index.html
indexPath := filepath.Join(outputDir, "index.html")
os.Remove(indexPath)
// re-generate index.html
indexHtml, _ := os.Create(indexPath)
tmpl.ExecuteTemplate(indexHtml, "index.htm", gin.H{"Site": site, "Year": time.Now().Year()})
// clear old folders.json
foldersJsonPath := filepath.Join(outputDir, "folders.json")
os.Remove(foldersJsonPath)
// re-generate folders.json
if data, err := json.Marshal(gin.H{
"RootFolder": rootFolder,
}); err != nil {
fatalErr(err)
} else {
fatalErr(os.WriteFile(foldersJsonPath, data, os.ModePerm))
}
feed := &feeds.Feed{
Title: fmt.Sprintf("%s(%s)", site.Title, site.Copyright),
Link: &feeds.Link{Href: site.Link},
Description: site.Summry,
Author: &feeds.Author{Name: site.Author},
Created: time.Now(),
Copyright: fmt.Sprintf("© %s", site.Copyright),
}
var items []SitemapItem
// generate keynotes
for _, kind := range FileKinds() {
// clear old keynotes
keynotesPath := filepath.Join(outputDir, fmt.Sprintf("%ss", kind))
os.RemoveAll(keynotesPath)
// re-generate keynotes
genKeynoteHtml(kind, tmpl, site, rootFolder, keynotesPath, basePath, feed, &items)
}
// generate rss
rssPath := filepath.Join(outputDir, "rss")
os.Remove(rssPath)
atom, _ := feed.ToRss()
fatalErr(os.WriteFile(rssPath, []byte(atom), os.ModePerm))
// generate sitemap
sitemapPath := filepath.Join(outputDir, "sitemap")
os.Remove(sitemapPath)
sitemap, _ := os.Create(sitemapPath)
ttmpl.ExecuteTemplate(sitemap, "sitemap.tmpl", gin.H{"Site": site, "LastMod": time.Now(), "Items": items})
}
func startServer(port int, host, conf, keynotesDir string) {
ch := make(chan chan<- []any, 1024)
go loadData(conf, keynotesDir, ch)
router := gin.Default()
router.SetHTMLTemplate(newTemplate())
for _, kind := range FileKinds() {
router.StaticFS(fmt.Sprintf("%ss", kind), gin.Dir(keynotesDir, false))
}
site, _ := getData(ch)
// Site.StaticPath is a server mode parameter, please restart server after modification.
for _, path := range site.StaticPath {
router.StaticFS(path, gin.Dir(keynotesDir, false))
}
// Site.StaticFile is a server mode parameter, please restart server after modification.
for _, file := range site.StaticFile {
router.StaticFile(file, file)
}
router.GET("/", homeRender(ch))
router.GET("/folders.json", foldersApi(ch))
router.NoRoute(noRouteHandler(ch))
router.SetTrustedProxies(nil)
router.Run(fmt.Sprintf("%s:%d", host, port))
}
func getData(ch chan<- chan<- []any) (site Site, rootFolder Folder) {
recv := make(chan []any, 1)
ch <- recv
arr := <-recv
site = arr[0].(Site)
rootFolder = arr[1].(Folder)
return
}
func loadData(conf, keynotesDir string, ch <-chan chan<- []any) {
ticker := time.NewTicker(3 * time.Second)
if production {
ticker.Stop()
} else {
defer ticker.Stop()
}
var (
site *Site
rootFolder *Folder
)
load := func() {
site = loadSite(conf)
rootFolder = loadKeynotes(keynotesDir, "/", []string{"/"})
}
load()
for {
select {
case res := <-ch:
res <- []any{*site, *rootFolder}
case <-ticker.C:
load()
}
}
}
var production bool
func main() {
var (
port int
host, conf, keynotesDir string
gen bool
outputDir, basePath string
)
flag.IntVar(&port, "port", 8000, "the port that server listen on")
flag.StringVar(&host, "host", "0.0.0.0", "the host that server listen on")
flag.StringVar(&conf, "conf", "keynote.yaml", "the config of the site")
flag.StringVar(&keynotesDir, "src", "src", "where the keynote sources store")
flag.BoolVar(&production, "pro", false, "production mode (without auto reload)")
flag.BoolVar(&gen, "gen", false, "generate static site")
flag.StringVar(&outputDir, "output", ".", "where the generated files store")
flag.StringVar(&basePath, "base", "/", "base path of the static site")
flag.Parse()
if gen {
genStaticSite(conf, keynotesDir, outputDir, basePath)
} else {
startServer(port, host, conf, keynotesDir)
}
}