-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
771 lines (648 loc) · 17.5 KB
/
site.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
// © 2022 Ilya Mateyko. All rights reserved.
// Use of this source code is governed by the ISC
// license that can be found in the LICENSE file.
/*
Package site builds https://astrophena.name.
# Disclaimer
Here be dragons: this package is intended only for building
https://astrophena.name and has no backward compatibility.
# Directory Structure
Site has the following directories:
build This is where the generated site will be placed by default.
pages All content for the site lives inside this directory. HTML and
Markdown formats can be used.
static Files in this directory will be copied verbatim to the
generated site.
templates These are the templates that wrap pages. Templates are
chosen on a page-by-page basis in the front matter.
They must have the '.html' extension.
# Page Layout
Each page must be of the supported format (HTML or Markdown) and have JSON front
matter in the beginning:
{
"title": "Hello, world!",
"template": "layout",
"permalink": "/hello-world"
}
See Page for all available front matter fields.
*/
package site
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"io/fs"
"log"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
ttemplate "text/template"
"time"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/feeds"
"github.com/russross/blackfriday/v2"
)
// Possible errors, used in tests.
var (
errFrontmatterSplit = errors.New("failed to split frontmatter and contents")
errFrontmatterParse = errors.New("failed to parse frontmatter")
errFrontmatterMissing = errors.New("missing frontmatter")
errFrontmatterMissingParam = errors.New("missing required frontmatter parameter (title, template, permalink)")
errFormatUnsupported = errors.New("format unsupported")
errPermalinkInvalid = errors.New("invalid permalink")
)
// Logf is a simple printf-like logging function.
type Logf func(format string, args ...any)
// Config represents a build configuration.
type Config struct {
// Title is the title of the site.
Title string
// Author is the name of the author of the site.
Author string
// BaseURL is the base URL of the site.
BaseURL *url.URL
// Src is the directory where to read files from. If empty, uses the current
// directory.
Src string
// Dst is the directory where to write files. If empty, uses the build
// directory.
Dst string
// Logf specifies a logger to use. If nil, log.Printf is used.
Logf Logf
// Prod determines if the site should be built in a production mode. This
// means that drafts are excluded and the base URL is used to derive absolute
// URLs from relative ones.
Prod bool
feedCreated time.Time // used in tests
}
func (c *Config) setDefaults() {
if c == nil {
c = &Config{}
}
if c.Logf == nil {
c.Logf = log.Printf
}
if c.Title == "" {
c.Title = "Ilya Mateyko"
}
if c.Author == "" {
c.Author = "Ilya Mateyko"
}
if c.BaseURL == nil {
c.BaseURL = &url.URL{
Scheme: "https",
Host: "astrophena.name",
}
}
if c.Src == "" {
c.Src = filepath.Join(".")
}
if c.Dst == "" {
c.Dst = filepath.Join(".", "build")
}
}
// Build builds a site based on the provided Config.
func Build(c *Config) error {
c.setDefaults()
b := newBuildContext(c)
// Parse templates and pages.
if err := filepath.WalkDir(filepath.Join(b.c.Src, "templates"), b.parseTemplates); err != nil {
return err
}
if err := filepath.WalkDir(filepath.Join(b.c.Src, "pages"), b.parsePages); err != nil {
return err
}
// Sort pages by date. Pages without date are pushed to the end.
sort.SliceStable(b.pages, func(i, j int) bool {
if b.pages[i].Date == nil || b.pages[j].Date == nil {
return true
}
return !b.pages[i].Date.Time.Before(b.pages[j].Date.Time)
})
// Clean up after previous build.
if _, err := os.Stat(b.c.Dst); err == nil {
if err := os.RemoveAll(b.c.Dst); err != nil {
return err
}
}
if err := os.MkdirAll(b.c.Dst, 0o755); err != nil {
return err
}
// Build pages and RSS feed.
for _, p := range b.pages {
if err := os.MkdirAll(filepath.Dir(filepath.Join(b.c.Dst, p.dstPath)), 0o755); err != nil {
return err
}
f, err := os.Create(filepath.Join(b.c.Dst, p.dstPath))
if err != nil {
return err
}
defer f.Close()
tpl, ok := b.templates[p.Template]
if !ok {
return fmt.Errorf("%s: no such template %q", p.path, p.Template)
}
if err := p.build(b, tpl, f); err != nil {
return err
}
}
if err := b.buildFeed(); err != nil {
return err
}
// Copy static files.
if err := filepath.WalkDir(filepath.Join(b.c.Src, "static"), b.copyStatic); err != nil {
return err
}
return nil
}
var serveReadyHook func() // used in tests, called when Serve started serving the site
// Serve builds the site and starts serving it on a provided host:port.
func Serve(ctx context.Context, c *Config, addr string) error {
c.setDefaults()
c.Logf("Performing an initial build...")
if err := Build(c); err != nil {
c.Logf("Initial build failed: %v", err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
for _, dir := range []string{
filepath.Join(c.Src, "pages"),
filepath.Join(c.Src, "static"),
filepath.Join(c.Src, "templates"),
} {
if err := watchRecursive(watcher, dir); err != nil {
return err
}
}
defer watcher.Close()
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer l.Close()
c.Logf("Listening on http://%s...", l.Addr().String())
httpSrv := &http.Server{Handler: &staticHandler{fs: os.DirFS(c.Dst)}}
errCh := make(chan error, 1)
go func() {
if err := httpSrv.Serve(l); err != nil {
if err != http.ErrServerClosed {
errCh <- err
}
}
}()
go func() {
c.Logf("Started watching for new changes.")
for event := range watcher.Events {
if !shouldRebuild(event.Name, event.Op) {
continue
}
c.Logf("Changed %s (%v), rebuilding the site.", event.Name, event.Op)
if err := Build(c); err != nil {
c.Logf("Failed to rebuild the site: %v", err)
}
}
}()
if serveReadyHook != nil {
serveReadyHook()
}
select {
case <-ctx.Done():
c.Logf("Gracefully shutting down...")
case <-errCh:
return err
}
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return httpSrv.Shutdown(shutdownCtx)
}
func watchRecursive(w *fsnotify.Watcher, dir string) error {
return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
return w.Add(path)
})
}
// Copied from
// https://github.com/brandur/modulir/blob/1ff912fdc45a79cb4d8d9f199d213ae9c3598cbd/watch.go#L201.
func shouldRebuild(path string, op fsnotify.Op) bool {
base := filepath.Base(path)
// Mac OS' worst mistake.
if base == ".DS_Store" {
return false
}
// Vim creates this temporary file to see whether it can write into a target
// directory. It screws up our watching algorithm, so ignore it.
if base == "4913" {
return false
}
// A special case, but ignore creates on files that look like Vim backups.
if strings.HasSuffix(base, "~") {
return false
}
if op&fsnotify.Create != 0 {
return true
}
if op&fsnotify.Remove != 0 {
return true
}
if op&fsnotify.Write != 0 {
return true
}
/*
Ignore everything else. Rationale:
* chmod: we don't really care about these as they won't affect build
output (unless potentially we no longer can read the file, but we'll go
down that path if it ever becomes a problem).
* rename: will produce a following create event as well, so just listen
for that instead.
*/
return false
}
type staticHandler struct {
fs fs.FS
}
func (h *staticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
if p == "/" {
p += "/index.html"
}
p = strings.TrimPrefix(path.Clean(p), "/")
// Special case: /foo will serve content from foo.html, if it exists.
if _, err := fs.Stat(h.fs, p+".html"); err == nil {
p += ".html"
}
d, err := fs.Stat(h.fs, p)
if errors.Is(err, fs.ErrNotExist) {
h.serveNotFound(w, r)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if d.IsDir() {
h.serveNotFound(w, r)
return
}
b, err := fs.ReadFile(h.fs, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(w, r, d.Name(), d.ModTime(), bytes.NewReader(b))
}
func (h *staticHandler) serveNotFound(w http.ResponseWriter, r *http.Request) {
f, err := h.fs.Open("404.html")
if errors.Is(err, fs.ErrNotExist) {
http.NotFound(w, r)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
w.WriteHeader(http.StatusNotFound)
io.Copy(w, f)
}
type buildContext struct {
c *Config
funcs template.FuncMap
pages []*Page
templates map[string]*template.Template
}
func newBuildContext(c *Config) *buildContext {
b := &buildContext{
c: c,
templates: make(map[string]*template.Template),
}
b.funcs = template.FuncMap{
"content": func(p *Page) template.HTML { return template.HTML(p.contents) },
"time": b.time,
"icon": b.icon,
"image": b.image,
"navLink": b.navLink,
"pages": b.pagesByType,
"url": b.url,
}
return b
}
func (b *buildContext) icon(name string) template.HTML {
return template.HTML(fmt.Sprintf(`
<svg class="icon" aria-hidden="true">
<use xlink:href="%s#icon-%s"/>
</svg>`, b.url("/icons/sprite.svg"), name))
}
func (b *buildContext) image(path, caption string) template.HTML {
const tmpl = `<figure>
<img alt="%[2]s" src="%[1]s" loading="lazy"/>
<figcaption>%[2]s</figcaption>
</figure>`
s := fmt.Sprintf(tmpl, b.url(path), caption)
return template.HTML(s)
}
func (b *buildContext) navLink(p *Page, title, iconName, path string) template.HTML {
var add string
if p.Permalink == path {
add = ` class="current"`
}
return template.HTML(fmt.Sprintf(`<a href="%s"%s>%s%s</a>`, b.url(path), add, b.icon(iconName), title))
}
func (b *buildContext) pagesByType(typ string) []*Page {
if typ == "" {
return b.pages
}
var pages []*Page
for _, p := range b.pages {
if p.Type == typ {
pages = append(pages, p)
}
}
return pages
}
func (b *buildContext) time(format string, d *date) template.HTML {
return template.HTML(fmt.Sprintf(`<date datetime="%s">%s</date>`,
d.Format(time.RFC3339),
d.Format(format),
))
}
func isFullURL(url string) bool {
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
}
func (b *buildContext) url(base string) string {
if isFullURL(base) || !b.c.Prod || b.c.BaseURL == nil {
return base
}
u := *b.c.BaseURL
u.Path = path.Join(u.Path, base)
return u.String()
}
func (b *buildContext) parseTemplates(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
if filepath.Ext(path) != ".html" {
return nil
}
name, err := filepath.Rel(filepath.Join(b.c.Src, "templates"), path)
if err != nil {
return err
}
name = strings.TrimSuffix(name, filepath.Ext(name))
// Ensure that we have slash-separated path everywhere.
name = filepath.ToSlash(name)
bb, err := os.ReadFile(path)
if err != nil {
return err
}
b.templates[name], err = template.New(name).Funcs(b.funcs).Parse(string(bb))
if err != nil {
return err
}
return nil
}
func (b *buildContext) parsePages(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
// Ignore files that look like Vim backups.
if strings.HasSuffix(path, "~") {
return nil
}
// Ignore .gitignore files.
if strings.Contains(path, ".gitignore") {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
p := &Page{path: path}
if err := p.parse(f); err != nil {
return err
}
if !p.Draft || !b.c.Prod {
b.pages = append(b.pages, p)
}
return nil
}
// Page represents a site page. The exported fields is the front matter fields.
type Page struct {
Title string `json:"title"` // title: Page title, required.
Summary string `json:"summary"` // summary: Page summary, used in RSS feed, optional.
Type string `json:"type"` // type: Used to distinguish different kinds of pages, page by default.
Permalink string `json:"permalink"` // permalink: Output path for the page, required.
Date *date `json:"date"` // date: Publication date in the 'year-month-day' format, e.g. 2006-01-02, optional.
Draft bool `json:"draft"` // draft: Determines whether this page should be not included in production builds, false by default.
Template string `json:"template"` // template: Template that should be used for rendering this page, required.
ContentOnly bool `json:"content_only"` // content_only: Determines whether this page should be rendered without header and footer, false by default.
path string // path to the page source
dstPath string // where to write the built page
contents []byte // page contents without front matter
}
type date struct {
time.Time
}
const dateLayout = "2006-01-02"
func (d *date) UnmarshalJSON(p []byte) error {
s := strings.Trim(string(p), "\"")
if s == "null" {
d.Time = time.Time{}
return nil
}
dt, err := time.Parse(dateLayout, s)
if err != nil {
return err
}
d.Time = dt
return nil
}
func (p *Page) parse(r io.Reader) error {
// Check that format of the page is supported.
var supported bool
for _, f := range []string{".html", ".md"} {
if filepath.Ext(p.path) == f {
supported = true
break
}
}
if !supported {
return fmt.Errorf("%s: %w", p.path, errFormatUnsupported)
}
const (
leftDelim = "{\n"
rightDelim = "}\n"
)
// Split the front matter and contents.
scanner := bufio.NewScanner(r)
var (
frontmatter, contents []byte
reachedFrontmatter bool
reachedContents bool
)
for scanner.Scan() {
line := scanner.Text() + "\n"
if !reachedContents {
if line == leftDelim {
reachedFrontmatter = true
}
if line == rightDelim {
reachedFrontmatter = false
frontmatter = append(frontmatter, line...)
reachedContents = true
continue
}
}
if reachedFrontmatter {
frontmatter = append(frontmatter, line...)
continue
}
if reachedContents {
contents = append(contents, line...)
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("%s: %w: %v", p.path, errFrontmatterSplit, err)
}
if len(frontmatter) == 0 {
return fmt.Errorf("%s: %w", p.path, errFrontmatterMissing)
}
p.contents = contents
// Parse the front matter.
if err := json.Unmarshal(frontmatter, p); err != nil {
return fmt.Errorf("%s: %w: %v", p.path, errFrontmatterParse, err)
}
// Set the default page type.
if p.Type == "" {
p.Type = "page"
}
// Check front matter fields.
if p.Title == "" || p.Template == "" || p.Permalink == "" {
return fmt.Errorf("%s: %w", p.path, errFrontmatterMissingParam)
}
if _, err := url.ParseRequestURI(p.Permalink); err != nil {
return fmt.Errorf("%s: %w: %v", p.path, errPermalinkInvalid, err)
}
p.dstPath = p.Permalink
if !strings.HasSuffix(p.dstPath, ".html") {
if p.dstPath == "/" {
p.dstPath = p.dstPath + "index"
}
p.dstPath = p.dstPath + ".html"
}
p.dstPath = path.Clean(p.dstPath)
return nil
}
var htmlCommentRe = regexp.MustCompile("<!--(.*?)-->")
func (p *Page) build(b *buildContext, tpl *template.Template, w io.Writer) error {
// We use here text/template, but not html/template because we don't want to
// escape any HTML on the Markdown source.
ptpl, err := ttemplate.New(p.path).Funcs(ttemplate.FuncMap(b.funcs)).Parse(string(p.contents))
if err != nil {
return err
}
var pbuf bytes.Buffer
if err = ptpl.Execute(&pbuf, p); err != nil {
return fmt.Errorf("%s: failed to execute page template: %w", p.path, err)
}
p.contents = pbuf.Bytes()
if filepath.Ext(p.path) == ".md" {
p.contents = blackfriday.Run(p.contents)
}
p.contents = htmlCommentRe.ReplaceAll(p.contents, []byte{})
var buf bytes.Buffer
if err := tpl.Execute(&buf, p); err != nil {
return fmt.Errorf("%s: failed to execute template %q: %w", p.path, p.Template, err)
}
_, err = buf.WriteTo(w)
return err
}
func (b *buildContext) copyStatic(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
from, err := os.Open(path)
if err != nil {
return err
}
defer from.Close()
toPath, err := filepath.Rel(filepath.Join(b.c.Src, "static"), path)
if err != nil {
return err
}
toPath = filepath.Join(b.c.Dst, toPath)
if err := os.MkdirAll(filepath.Dir(toPath), 0o755); err != nil {
return err
}
to, err := os.Create(toPath)
if err != nil {
return err
}
defer to.Close()
if _, err := io.Copy(to, from); err != nil {
return err
}
return nil
}
func (b *buildContext) buildFeed() error {
feed := &feeds.Feed{
Title: b.c.Title,
Link: &feeds.Link{Href: b.c.BaseURL.String() + "/"},
Author: &feeds.Author{Name: b.c.Author},
Created: time.Now(),
}
if !b.c.feedCreated.IsZero() {
feed.Created = b.c.feedCreated
}
for _, p := range b.pages {
if p.Type != "post" {
continue
}
if p.Draft && b.c.Prod {
continue
}
pu := *b.c.BaseURL
pu.Path = path.Join(pu.Path, p.Permalink)
item := &feeds.Item{
Title: p.Title,
Link: &feeds.Link{Href: pu.String()},
Author: feed.Author,
Description: p.Summary,
Content: string(p.contents),
}
if p.Date != nil {
item.Created = p.Date.Time
}
feed.Items = append(feed.Items, item)
}
bf, err := feed.ToAtom()
if err != nil {
return err
}
return os.WriteFile(filepath.Join(b.c.Dst, "feed.xml"), []byte(bf), 0o644)
}