-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
1295 lines (1179 loc) · 36.9 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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
// original impetus
// * server crashed -> lost web dir folder with manual-ish copied over html files / pandoc'd wiki articles
// * wanted something to republish markdown articles from my wiki to static html files, and update an index over them
// * was tired of my old website, mostly due to the markup. but honestly also the design
// * the design is not impacted by this generation.. really wanna make something inspired by
// https://merveilles.town/@thomasorus/106456722974843498
// * wanted to try out something oscean-like, without copying devine's design and ideas wholesale—cause that'd be no fun
// project name: plain
import (
_ "embed"
"errors"
"flag"
"fmt"
"github.com/cblgh/plain/og"
"github.com/cblgh/plain/rss"
"github.com/cblgh/plain/util"
"github.com/gomarkdown/markdown"
"io"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
// TODO (2022-11-04):
// - add ability to set attribute on e.g. <a> elements such that i can do:
// ln cblgh.org
// attr rel="me" => <a href="cblgh.org" rel="me">
var verbose = true
func echo(s ...interface{}) {
if verbose {
fmt.Println(s...)
}
}
// git repo ideas:
// * set all git repos under <webroot>/_git/<reponame>.git
// * <reponame> is taken as the last path component of the passed in repository
// * faffing about needed:
// * create bare repo: git clone --bare <path-reponame> <webroot>/_git/<reponame>.git
// * in bare repo: hooks/post-update.sample move to hooks/post-update.sample
// * in bare repo: execute git update-server-info
// * in src repo: execute git add remote local <webroot/_git/reponame.git>
// * in src repo: .git/hooks/post-commit should exist, be executable, and run `git push local main`
//
// detect "readme.md"; detect if first line is a title, inject "Get the code: git clone git.<canonicalurl>/<reponame.git>
// play around with rendering latest commit info
// tt title
// bb oneline brief markdown description
// md path to markdown file for longer descriptions, or entire page content
// ln link to resource representing the described item
// ww path in webroot
// cf path containing ssg input (e.g. articles)
// cp copy an entire directory to the web root, preserving the folder name
// nn name navigation item & add to the main nav
// mv redirect the given url (by dumping a redirect page) to the current item
// cc create rss feed for listicle
// // comment, skip this
// gt git repo
// br git branch
const (
/* tt */ TITLE = iota
/* bb */ BRIEF
/* ln */ LINK
/* // */ SKIP
/* nn */ NAVIGATION_TITLE
/* cf */ PATH_SSG
/* md */ PATH_MD
/* ww */ PATH_WWWROOT
/* cp */ COPY_DIR
/* mv */ REDIRECT /* redirects a something.html to a /something/index.html route
/* as */ALIAS /* redirects from a route /something to a route /entirely-something-else (defined by md)*/
/* rn */RENAME /* renames a filename from the input source to a completely new filename, decoupling source filename from route name */
/* un */ UNDER_CATEGORY
/* cc */ CREATE_RSS
/* bg */ BACKGROUND
/* sf */ FOREGROUND_COLOR
/* sb */ BACKGROUND_COLOR
/* hi */ HEADER_IMAGE
/* sl */ LINK_COLOR
/* gt */ GIT_REPO
/* br */ GIT_BRANCH
/* xx */ NOIDEA
)
type feedDescription struct {
name, description string
}
type Pair struct {
code string
content string
}
type Element struct {
pairs []Pair
}
type Theme struct {
foreground string
background string
link string
}
type PageFragment struct {
theme Theme
underParent bool
title, brief, link string
background string
webpath, contents string
location string
metadata []string
}
type Page struct {
html []string
headerContent []string
pf PageFragment
parentDir bool
}
type mdFile struct {
contents string
images []string // slice of image paths, for later copying
}
type navigation struct {
link string
text string
}
func parseSymbols() {
symbols = make(map[string]int)
input, err := os.ReadFile("symbols")
util.Check(err)
parseConstant := func(s string) int {
switch s {
case "TITLE":
return TITLE
case "BRIEF":
return BRIEF
case "LINK":
return LINK
case "SKIP":
return SKIP
case "NAVIGATION_TITLE":
return NAVIGATION_TITLE
case "PATH_SSG":
return PATH_SSG
case "PATH_MD":
return PATH_MD
case "PATH_WWWROOT":
return PATH_WWWROOT
case "COPY_DIR":
return COPY_DIR
case "REDIRECT":
return REDIRECT
case "ALIAS":
return ALIAS
case "RENAME":
return RENAME
case "UNDER_CATEGORY":
return UNDER_CATEGORY
case "CREATE_RSS":
return CREATE_RSS
case "BACKGROUND":
return BACKGROUND
case "FOREGROUND_COLOR":
return FOREGROUND_COLOR
case "BACKGROUND_COLOR":
return BACKGROUND_COLOR
case "HEADER_IMAGE":
return HEADER_IMAGE
case "LINK_COLOR":
return LINK_COLOR
case "GIT_REPO":
return GIT_REPO
case "GIT_BRANCH":
return GIT_BRANCH
default:
return NOIDEA
}
}
for _, line := range strings.Split(string(input), "\n") {
if len(line) == 0 {
continue
}
parts := strings.Fields(string(line))
command, constant := parts[0], parts[1]
symbols[command] = parseConstant(constant)
}
}
func symbol(line string) int {
line = strings.TrimSpace(line)
if len(line) == 0 {
return SKIP
}
if sym, exists := symbols[strings.Fields(line)[0]]; exists {
return sym
}
return NOIDEA
}
func (page Page) produceHeader() []string {
if len(page.headerContent) == 0 {
return []string{}
}
return []string{fmt.Sprintf(`<header>
%s
</header>
`, strings.Join(page.headerContent, "\n"))}
}
var markdownImagePattern = regexp.MustCompile(`[!]\[.*\]\((\S+)\)`)
func extractImagePaths(content []byte) []string {
s := string(content)
var paths []string
matches := markdownImagePattern.FindAllStringSubmatch(s, -1)
if len(matches) > 0 {
for _, match := range matches {
// discard http[s]? matches; we can't very well copy them :)
if strings.HasPrefix(match[1], "https://") || strings.HasPrefix(match[1], "http://") {
continue
}
paths = append(paths, match[1])
}
}
return paths
}
var wikilinksPattern = regexp.MustCompile(`(\[\[(.*?)\]\])`)
func transformWikilinks(content []byte) []byte {
s := string(content)
matches := wikilinksPattern.FindAllStringSubmatch(s, -1)
// search and replace all instances of [[wiki]] syntax with a flat link to the subject e.g. /wiki
if len(matches) > 0 {
for _, match := range matches {
s = strings.ReplaceAll(s, match[1], fmt.Sprintf(`<a href="/%s">%s</a>`, strings.ToLower(match[2]), match[2]))
}
}
return []byte(s)
}
func markup(s string) string {
return string(markdown.ToHTML([]byte(strings.TrimSpace(s)), nil, nil))
}
func (pf PageFragment) assemble() string {
// if listicle entry omits title, don't list it as a listicle item (it's a hidden page)
if len(pf.title) == 0 {
return ""
}
if len(pf.link) > 0 {
return fmt.Sprintf(
`<dt><a href="%s">%s</a></dt>
<dd>%s</dd>
`,
pf.link, pf.title, markup(pf.brief))
} else {
return fmt.Sprintf(`
<dt>%s</dt>
<dd>%s</dd>`,
pf.title, markup(pf.brief))
}
}
func readTemplate(template, defaultContent string) (string, error) {
_, err := createIfNotExist(template, defaultContent)
if err != nil {
util.Check(err)
}
b, err := os.ReadFile(template)
if err != nil {
return "", err
}
return string(b), nil
}
var titlePattern = regexp.MustCompile(`(<title>(.*)<\/title>)`)
func htmlContent(content string) string {
return fmt.Sprintf("<main><article>%s</article></main>", content)
}
func htmlPreamble(pf PageFragment) string {
prevRoute := pf.webpath
var mainNav string
if prevRoute != "" {
returnName := strings.TrimPrefix(prevRoute, "/")
if returnName == "" {
returnName = "home"
}
mainNav += fmt.Sprintf(`<li><a href="%s">Back to %s</a></li>`, prevRoute, returnName)
} else {
mainNav = "<li></li> "
}
for _, nav := range navElements {
if nav.text == "" {
continue
}
mainNav += fmt.Sprintf(`<li><a href="%s">%s</a></li>`, nav.link, nav.text)
}
header, err := readTemplate("header.html", DEFAULT_HEADER)
if err != nil {
log.Fatalln(err)
}
// add background image to an article if it has been set
const backgroundSentinel = "<!-- background -->"
const themeSentinel = "<!-- theme -->"
const backgroundTemplate = `
<style>
html {
background-image: url("%s");
}
</style>
`
if pf.background != "" {
bg := fmt.Sprintf(backgroundTemplate, pf.background)
header = strings.ReplaceAll(header, backgroundSentinel, bg)
} else {
header = strings.ReplaceAll(header, backgroundSentinel, "")
}
if pf.theme.foreground != "" || pf.theme.background != "" || pf.theme.link != "" {
var theme string
if pf.theme.foreground != "" {
theme += fmt.Sprintf("--foreground: %s !important;", pf.theme.foreground)
}
if pf.theme.background != "" {
theme += fmt.Sprintf("--background: %s !important;", pf.theme.background)
}
if pf.theme.link != "" {
theme += fmt.Sprintf("--highlight: %s !important;", pf.theme.link)
}
rootStyle := fmt.Sprintf(`
:root {
%s
}
`, theme)
if pf.theme.link != "" {
rootStyle += fmt.Sprintf(`
a {
color: %s !important;
}`, pf.theme.link)
}
style := fmt.Sprintf(`<style>%s</style`, rootStyle)
header = strings.ReplaceAll(header, themeSentinel, style)
} else {
header = strings.ReplaceAll(header, backgroundSentinel, "")
}
var htmlMeta string
// augment html meta tags and titles with article metadata.
// grab unaugmented <title>
match := titlePattern.FindStringSubmatch(header)
if len(match) >= 3 {
if pf.title != "" {
htmlMeta += fmt.Sprintf(`<title>%s — %s</title>%s`, pf.title, match[2], "\n")
}
if pf.brief != "" {
htmlMeta += fmt.Sprintf(`<meta name="description" content="%s">%s`, pf.brief, "\n")
}
// generate opengraph metadata and image
if generateOG && pf.title != "" {
_, articleName := extractFilenames(pf.location)
// if rewrittenDest != "" {
// articleName = rewrittenDest
// }
imageName := fmt.Sprintf("%s.png", strings.ReplaceAll(strings.ToLower(articleName), " ", "-"))
imagePath := filepath.Join(OUTPATH, "og", imageName)
canonicalPath := fmt.Sprintf("%s/og/%s", canonicalUrl, imageName)
err = os.MkdirAll(filepath.Dir(imagePath), 0777)
util.Check(err)
settings := og.GetDefaultSettings()
htmlMeta += og.GenerateMetadata(pf.title, pf.brief, canonicalPath, settings)
// og.GenerateImage(pf.title, pf.brief, imagePath, settings)
}
}
// add other metadata, such as the experimental vcs discovery meta tags for repos
if len(pf.metadata) > 0 {
htmlMeta += strings.Join(pf.metadata, "\n")
}
if htmlMeta != "" {
header = strings.Replace(header, match[1], htmlMeta, -1)
}
return fmt.Sprintf(`%s
<nav>
<ul class="main-navigation">
%s
</ul>
</nav>`, header, mainNav)
}
func htmlEpilogue() string {
footer, err := readTemplate("footer.html", DEFAULT_FOOTER)
if err != nil {
log.Fatalln(err)
}
return footer
}
var OUTPATH = filepath.Join(".", "web")
func extractPageFragments(webpath string, underParent bool, elements []Element) []string {
// TODO: do 2 pass to identify alternate write paths for PATH_MD / COPY_DIR, as set by LINK tag?
var html []string
html = append(html, "<dl class='listicle'>")
for _, el := range elements {
pf := PageFragment{webpath: webpath, underParent: underParent}
pf.metadata = make([]string, 0)
var rewrittenDest string
branchName := "master" // used for GIT_REPO
// var background string
for _, p := range el.pairs {
switch symbol(p.code) {
case GIT_BRANCH:
branchName = p.content
case PATH_WWWROOT:
rewrittenDest = p.content
case TITLE:
pf.title = p.content
case BRIEF:
pf.brief = p.content
case BACKGROUND:
pf.background = p.content
case BACKGROUND_COLOR:
pf.theme.background = p.content
case FOREGROUND_COLOR:
pf.theme.foreground = p.content
case LINK_COLOR:
pf.theme.link = p.content
case LINK:
if pf.link != "" {
echo(fmt.Sprintf("err: already set link on page fragment? %v\n", el.pairs))
continue
}
pf.link = p.content
}
}
for _, p := range el.pairs {
switch symbol(p.code) {
case GIT_REPO:
setupBareRepo(p.content, filepath.Join(OUTPATH, "_git"), branchName)
repoName := filepath.Base(p.content)
stats := produceRepoStatistics(p.content, filepath.Join(OUTPATH, "_git"))
if pf.title == "" {
pf.title = repoName
}
clonePath := fmt.Sprintf(`http://git.%s/%s.git`, canonicalUrl, repoName)
// support VCS Autodiscovery (https://git.sr.ht/~ancarda/vcs-autodiscovery-rfc)
pf.metadata = append(pf.metadata, `<meta name="vcs" content="git" />`)
pf.metadata = append(pf.metadata, fmt.Sprintf(`<meta name="vcs:default-branch" content="%s" />`, branchName))
pf.metadata = append(pf.metadata, fmt.Sprintf(`<meta name="vcs:clone" content="%s" />`, clonePath))
pf.metadata = append(pf.metadata, fmt.Sprintf(`<meta name="forge:summary" content="https://%s/%s">`, canonicalUrl, repoName))
// check for readme variants to render
readmeVariations := []string{"README.md", "readme.md", "README"}
checkReadmeExists := func(p string) bool {
_, err := os.Stat(p)
if err != nil && errors.Is(err, os.ErrNotExist) {
return false
// alright this is the case when we want to continue! :)
}
return true
}
for _, readme := range readmeVariations {
readmePath := filepath.Join(p.content, readme)
exists := checkReadmeExists(readmePath)
rewrittenDest = repoName
if exists {
pf.location = readmePath
// yank'd out of CopyMarkdownFile so we can inject the git clone instruction
filename, _ := extractFilenames(pf.location)
md, err := ReadMarkdownFile(filename)
util.Check(err)
lines := strings.Split(md.contents, "\n")
injected := fmt.Sprintf(`<div id="clone"><span>%s</span><span>git clone %s</span></div>`, stats, clonePath)
if strings.Contains(lines[0], "<h1>") {
newLines := []string{lines[0], injected}
newLines = append(newLines, lines[1:]...)
md.contents = strings.Join(newLines, "\n")
} else {
newLines := []string{injected}
newLines = append(newLines, lines...)
md.contents = strings.Join(newLines, "\n")
}
err = WriteMarkdownAsHTML(pf, rewrittenDest, md)
util.Check(err)
_, articleName := extractFilenames(p.content)
if rewrittenDest != "" {
articleName = rewrittenDest
}
pf.link = filepath.Join("/", articleName)
break
}
}
case COPY_DIR:
// copy a directory from one place and into plain's webroot
echo("copying directory at", p.content)
if p.content == "/" || p.content == "~" {
echo(fmt.Sprintf("tried to copy '%s'; stopped the operation as it seems unlikely to be correct :)", p.content))
continue
}
err := CopyDirectory(p.content, OUTPATH, rewrittenDest)
util.Check(err)
base := filepath.Base(p.content)
if rewrittenDest != "" {
base = rewrittenDest
}
pf.link = filepath.Join("/", base)
case PATH_MD:
// source a markdown file from one place and output a corresponding html site in plain's webroot
pf.location = p.content
err := CopyMarkdownFile(pf, rewrittenDest)
if err != nil {
continue
}
_, articleName := extractFilenames(p.content)
if rewrittenDest != "" {
articleName = rewrittenDest
}
pf.link = filepath.Join("/", articleName)
if pf.underParent {
pf.link = filepath.Join("/", pf.webpath, articleName)
}
case REDIRECT:
err := DumpRedirectFile(p.content)
util.Check(err)
case ALIAS:
err := DumpAliasFile(p.content, pf.link)
util.Check(err)
case RENAME:
err := RenameFile(pf.link, p.content)
pf.link = filepath.Join("/", p.content)
util.Check(err)
}
}
html = append(html, pf.assemble())
}
html = append(html, "</dl>")
return html
}
var ignored = []string{".git", "node_modules"}
func containsIgnored(s string) bool {
for _, ignoredString := range ignored {
if ignoredString == s {
return true
}
}
return false
}
// Copy the contents of a directory to the webroot, preserving the directory's basename.
// Traverses readDir, copying files to the writeDir (of the form: filepath.Join(OUTPATH, filepath.Base(readDir)))
func CopyDirectory(readDir, writeDir, rewrittenDest string) error {
base := filepath.Base(readDir)
if rewrittenDest != "" {
base = rewrittenDest
}
dst := filepath.Join(writeDir, base)
files, err := os.ReadDir(readDir)
if err != nil {
return err
}
err = os.MkdirAll(dst, 0777)
if err != nil {
return err
}
// perform os.Create / os.Mkdir at dst (and not at writeDir)
for _, f := range files {
if f.IsDir() && !containsIgnored(f.Name()) {
CopyDirectory(filepath.Join(readDir, f.Name()), dst, "")
} else {
srcfile, err := os.Open(filepath.Join(readDir, f.Name()))
if err != nil {
return err
}
dstfile, err := os.Create(filepath.Join(dst, f.Name()))
if err != nil {
return err
}
io.Copy(dstfile, srcfile)
srcfile.Close()
dstfile.Close()
}
}
return nil
}
// processes the location and extracts the article name from the location, with the file md suffix & initial path removed
func extractFilenames(location string) (string, string) {
return strings.TrimSpace(location), strings.TrimSuffix(filepath.Base(location), ".md")
}
func (md *mdFile) rewriteImageUrls(mediadir string) {
for _, image := range md.images {
md.contents = strings.ReplaceAll(md.contents, image, filepath.Join("/", mediadir, filepath.Base(image)))
}
}
// copies markdown file at location, returns strings.TrimSuffix(filepath.Base(location), ".md")
func CopyMarkdownFile(pf PageFragment, rewrittenDest string) error {
filename, _ := extractFilenames(pf.location)
md, err := ReadMarkdownFile(filename)
if err != nil {
return err
}
err = WriteMarkdownAsHTML(pf, rewrittenDest, md)
return err
}
//go:embed default/redirect-template.html
var REDIRECT_TEMPLATE string
//go:embed default/alias-template.html
var ALIAS_TEMPLATE string
// The mv command to redirects from older routes to the declared one
// examples:
// md wiki/exjobb/trustnet.md
// mv /articles/trustnet.html creates a folder "articles", if it doesn't exist, and dumps the redirect in its "trustnet.html"
//
// md wiki/life/support.md
// mv /support.html dumps a "support.html" in the web dir
// mv /about creates a folder "about" & dumps the redirect in its index.html
func DumpRedirectFile(webpath string) error {
var outfile string
var dirStructure string
// redirecting a html-suffixed file, e.g. /web/articles/cool-article.html
if strings.HasSuffix(webpath, ".html") {
// grab everything leading up until (but excluding) the .html file
dirStructure = strings.TrimSpace(strings.TrimPrefix(filepath.Dir(webpath), "/"))
outfile = filepath.Join(OUTPATH, webpath)
} else {
// we're redirecting a different path, e.g. /web/articles/cool-article/
dirStructure = webpath
outfile = filepath.Join(OUTPATH, webpath, "index.html")
}
// make sure we have the appropriate folder structure
if len(dirStructure) > 0 {
err := os.MkdirAll(filepath.Join(OUTPATH, dirStructure), 0777)
if err != nil {
return err
}
}
// but first: make sure we're not clobbering something that's already there
_, err := os.Stat(outfile)
// ok: we're not clobbering anything, time to dump stuff
if errors.Is(err, os.ErrNotExist) {
err = os.WriteFile(outfile, []byte(REDIRECT_TEMPLATE), 0666)
return err
} else if err != nil {
return err
}
return nil
}
func RenameFile(oldpath, newpath string) error {
oldpath = filepath.Join(OUTPATH, oldpath)
newpath = filepath.Join(OUTPATH, newpath)
info, err := os.Stat(newpath)
if errors.Is(err, os.ErrNotExist) {
// the destination does not exist, great! we can proceed. if it's some other kind of error, we should just throw it
}
if info != nil && info.IsDir() {
err = os.RemoveAll(newpath)
if err != nil {
return err
}
}
// if we get a fileinfo back we know that the directory exists and we basically want to overwrite it -> let's do it
err = os.Rename(oldpath, newpath)
if err != nil {
return err
}
// make sure the old dir is removed
err = os.Remove(oldpath)
if errors.Is(err, os.ErrNotExist) {
return nil
}
return err
}
func DumpAliasFile(aliasPath, webpath string) error {
var outfile string
var dst string
dst = webpath
// we'll create outfile as it's the alias that will be visited intially (which will redirect to `dst`)
outfile = filepath.Join(OUTPATH, aliasPath, "index.html")
// make sure we have the appropriate folder structure
err := os.MkdirAll(filepath.Join(OUTPATH, aliasPath), 0777)
if err != nil {
return err
}
// but first: make sure we're not clobbering something that's already there
_, err = os.Stat(outfile)
// ok: we're not clobbering anything, time to dump stuff
if errors.Is(err, os.ErrNotExist) {
aliasInstance := strings.ReplaceAll(ALIAS_TEMPLATE, "$SENTINEL$", dst)
err = os.WriteFile(outfile, []byte(aliasInstance), 0666)
return err
} else if err != nil {
return err
}
return nil
}
// the "markdown" we're writing has actually already been parsed as html, so what we're writing is really just html. but
// i think this name is more representative of what we're doing: persisting what was a markdown file in one location, as
// a new html file in another location
func WriteMarkdownAsHTML(pf PageFragment, rewrittenDest string, md mdFile) error {
filename, articleName := extractFilenames(pf.location)
if rewrittenDest != "" {
articleName = rewrittenDest
}
outfile := filepath.Join(OUTPATH, articleName, "index.html")
if pf.underParent {
outfile = filepath.Join(OUTPATH, pf.webpath, articleName, "index.html")
}
echo("try to open", filename)
err := os.MkdirAll(filepath.Dir(outfile), 0777)
if err != nil {
return err
}
if len(md.images) > 0 {
err = persistImages(pf.location, md)
if err != nil {
return err
}
}
echo("writing file contents to", outfile)
err = os.WriteFile(outfile, []byte(wrap(pf, md.contents)), 0666)
return err
}
func persistImages (baseLocation string, md mdFile) error {
echo("persisting images")
mediabase := "media"
mediadir := filepath.Join(OUTPATH, mediabase)
// make sure the <OUTPATH>/media dir exists
err := os.MkdirAll(mediadir, 0777)
if err != nil {
return err
}
// copy all images from their source to mediadir
for _, img := range md.images {
base := strings.Split(filepath.ToSlash(baseLocation), "/")[0]
src := filepath.Join(base, img)
dst := filepath.Join(mediadir, filepath.Base(img))
echo(fmt.Sprintf("copying %s to %s\n", src, dst))
copyFile(src, dst)
}
md.rewriteImageUrls(mediabase)
return nil
}
func ReadMarkdownFile(filename string) (mdFile, error) {
b, err := os.ReadFile(strings.TrimSpace(filename))
if err != nil {
return mdFile{}, err
}
paths := extractImagePaths(b)
b = transformWikilinks(b)
return mdFile{contents: string(markdown.ToHTML(b, nil, nil)), images: paths}, nil
}
func produceRepoStatistics (repoSrcPath, dst string) string {
cwd, err := os.Getwd()
util.Check(err)
bareRepoPath := filepath.Join(cwd, dst, fmt.Sprintf("%s.git", filepath.Base(repoSrcPath)))
echo("git repo statistics", bareRepoPath)
// equivalent to running the following in a bash script:
// COMMITS=$(git rev-list --count HEAD)
// SIZE=$(git count-objects -H | cut -d',' -f2-)
// FILES=$(git ls-tree --full-tree -r HEAD | wc -l)
// count commits
cmd := exec.Command("git", "rev-list", "--count", "HEAD")
var out strings.Builder
cmd.Stdout = &out
cmd.Dir = bareRepoPath
err = cmd.Run()
if err != nil {
log.Fatalln(err)
}
commits := strings.TrimSpace(out.String())
out.Reset()
echo("commits counted")
// get repo size
cmd = exec.Command("git", "count-objects", "-H")
cmd.Stdout = &out
cmd.Dir = bareRepoPath
err = cmd.Run()
if err != nil {
log.Fatalln(err)
}
size := strings.TrimSpace(strings.Split(out.String(), ",")[1])
out.Reset()
echo("repo size estimated")
// count files
cmd = exec.Command("git", "ls-tree", "--full-tree", "-r", "HEAD")
cmd.Stdout = &out
cmd.Dir = bareRepoPath
err = cmd.Run()
if err != nil {
log.Fatalln(err)
}
count := strings.Count(out.String(), "\n")
out.Reset()
echo("files counted")
return fmt.Sprintf("%s commits, %d files, %s", commits, count, size)
}
func setupBareRepo(repoSrcPath, dst, defaultBranch string) {
// make sure we have _git base folder
err := os.MkdirAll(dst, 0777)
util.Check(err)
cwd, err := os.Getwd()
util.Check(err)
bareRepoPath := filepath.Join(cwd, dst, fmt.Sprintf("%s.git", filepath.Base(repoSrcPath)))
echo("git bare repo", bareRepoPath)
// check if we've already setup the repo
_, err = os.Stat(bareRepoPath)
if err != nil && errors.Is(err, os.ErrNotExist) {
// alright this is the case when we want to continue! :)
} else if err == nil {
return
}
// --bare cloning
cmd := exec.Command("git", "clone", "--bare", repoSrcPath, bareRepoPath)
var out strings.Builder
cmd.Stderr = &out
err = cmd.Run()
if err != nil {
log.Fatalln(err)
}
echo("git clone:", out.String())
out.Reset()
// moving post-update
updateHook := filepath.Join(bareRepoPath, "hooks", "post-update")
err = os.Rename(fmt.Sprintf("%s.sample", updateHook), updateHook)
if err != nil {
fmt.Println("failed to rename post-update.sample")
log.Fatalln(err)
} else {
echo("post-update hook enabled")
}
// running git update-serve-info
cmd = exec.Command("git", "update-server-info")
cmd.Dir = bareRepoPath
err = cmd.Run()
if err != nil {
fmt.Println("failed to run update-server-info")
log.Fatalln(err)
} else {
echo("update-server-info done")
}
// creating local remote
cmd = exec.Command("git", "remote", "add", "local", bareRepoPath)
cmd.Dir = repoSrcPath
err = cmd.Run()
if err != nil {
fmt.Println("failed to add remote local")
} else {
echo("remote local added")
}
// write post-commit hook
commitHook := fmt.Sprintf(`#!/bin/bash
git push local %s
`, defaultBranch)
err = os.WriteFile(filepath.Join(repoSrcPath, ".git", "hooks", "post-commit"), []byte(commitHook), 0777)
if err != nil {
fmt.Println("failed to add post-commit to source repository")
log.Fatalln(err)
} else {
echo("post-commit hook written")
}
}
func wrap(pf PageFragment, html string) string {
return fmt.Sprintf(`%s %s %s`, htmlPreamble(pf), htmlContent(html), htmlEpilogue())
}
func readListicle(filename string) []Element {
b, err := os.ReadFile(filename)
util.Check(err)
lines := strings.Split(string(b), "\n")
var el Element
var elements []Element
for _, line := range lines {
// newline detected (newline delineates individual elements / pair groupings)
if strings.TrimSpace(line) == "" && len(el.pairs) > 0 {
elements = append(elements, el)
el = Element{}
continue
}
if symbol(line) == SKIP {
continue
}
code := strings.Fields(line)[0]
content := strings.TrimSpace(line[len(code):])
el.pairs = append(el.pairs, Pair{code: code, content: content})
}
return elements
}
var navElements []navigation
func headerImageTemplate (imgPath string) string {
return fmt.Sprintf(`
<div class="header-image" style="background-image: url('%s');"></div>
`, imgPath)
}
func processRootListicle(elements []Element) {
var feeds []feedDescription
var pages = make(map[string]Page) // a mapping from the declared page route to the page object
// do two pass scan to populate the navigation elements
// TODO: find all other dependencies (ww?)
// first pass
for _, el := range elements {
var navEl navigation
var listicleName string
for _, p := range el.pairs {
switch symbol(p.code) {
case PATH_SSG:
listicleName = p.content
case CREATE_RSS:
if listicleName == "" {
fmt.Println("plain: listicle name was empty! did the create_rss (cc) directive come before the listicle declaration (cf)?")
}
feeds = append(feeds, feedDescription{name: listicleName, description: p.content})
case NAVIGATION_TITLE:
navEl.text = p.content
case PATH_WWWROOT:
navEl.link = p.content
default:
continue
}
}
navElements = append(navElements, navEl)
}
// output listicle enumerating rss feeds
if len(feeds) > 0 {
feeds = append(feeds, feedDescription{name: "all", description: fmt.Sprintf("all of %s", util.TrimUrl(canonicalUrl))})
OutputFeedsListicle(feeds)
}
// second pass: generate the content && html
for i, el := range elements {
var page Page
for _, p := range el.pairs {
switch symbol(p.code) {
case UNDER_CATEGORY: