-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
897 lines (787 loc) · 24.4 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
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
package main
import (
"bytes"
"embed"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"log"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"slices"
"sort"
"strconv"
"strings"
"time"
"github.com/Bios-Marcel/feeds"
"github.com/goccy/go-yaml"
"github.com/otiai10/copy"
"golang.org/x/net/html"
)
//go:embed skeletons/*
var skeletonFS embed.FS
type ArticleHeaders struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Date string `yaml:"date"`
dateParsed time.Time
Tags []string `yaml:"tags"`
// Draft will prevent inclusion of the given page in a non-draft build.
Draft bool `yaml:"draft"`
// Hidden will not show any links to the given page. This works for both
// custom pages and articles.
Hidden bool `yaml:"hidden"`
Author string `yaml:"author"`
AuthorEmail string `yaml:"author-email"`
PodcastAudio string `yaml:"podcast-audio"`
}
func (headers *ArticleHeaders) Parse() error {
var errs []error
if headers.Date != "" {
dateParsed, err := time.Parse("2006-01-02", headers.Date)
if err != nil {
errs = append(errs, err)
}
headers.dateParsed = dateParsed
}
return errors.Join(errs...)
}
// Builder contains anything that is shared between builds. Multiple builds
// occur during live mode, where changes are constantly applied to the running
// server.
type Builder struct {
templates *template.Template
}
func NewBuilder() (*Builder, error) {
builder := &Builder{}
var err error
builder.templates, err = template.New("").
Funcs(template.FuncMap{
"sub": func(a, b int) int {
return a - b
},
"add": func(a, b int) int {
return a + b
},
}).
ParseFS(skeletonFS, "skeletons/*.html")
if err != nil {
return nil, fmt.Errorf("couldn't parse HTML templates: %w", err)
}
return builder, nil
}
func (builder *Builder) Build(sourceFolder, output, config string, minifyOutput, draft bool) error {
if err := cleanup(output); err != nil {
return fmt.Errorf("error performing cleanup: %w", err)
}
// Create empty directories
err := createDirectories(
filepath.Join(output, "media"),
filepath.Join(output, "articles"),
filepath.Join(output, "pages"),
)
if err != nil {
return fmt.Errorf("error preparing target folder structure: %w", err)
}
loadedPageConfig := pageConfig{
DateFormat: "2 January 2006",
MaxIndexEntries: 10,
}
var configPath string
if config != "" {
configPath = config
} else {
configPath = filepath.Join(sourceFolder, "config.json")
}
configFile, err := os.Open(configPath)
if err != nil {
return fmt.Errorf("error loading config '%s': %w", configPath, err)
}
if err := json.NewDecoder(configFile).Decode(&loadedPageConfig); err != nil {
log.Fatalf("Error decoding config: %s\n", err)
}
if loadedPageConfig.BasePath != "" {
// Making sure there's not too many or too little slashes ;)
loadedPageConfig.BasePath = "/" + strings.Trim(loadedPageConfig.BasePath, `/\`)
}
loadedPageConfig.Favicon, err = copyFavicon(sourceFolder, output)
if err != nil {
return fmt.Errorf("error copying favicon: %w", err)
}
if *verbose {
if loadedPageConfig.Favicon == "" {
log.Println("Warning: Neither 'favicon.ico' nor 'favicon.png' were found, is this intentional?")
} else {
log.Printf("Using favicon '%s'.\n", loadedPageConfig.Favicon)
}
}
customPageFiles, err := os.ReadDir(filepath.Join(sourceFolder, "pages"))
if err != nil {
return fmt.Errorf("couldn't handle pages directory: %w", err)
}
// We collect these to display them on the page header.
customPages := make([]*customPageEntry, len(customPageFiles))
if *verbose {
log.Printf("Indexing and writing custom pages ...\n")
}
for index, customPage := range customPageFiles {
customPageSkeletonClone, err := builder.templates.Lookup("page").Clone()
if err != nil {
return fmt.Errorf("couldn't clone 'page' template: %w", err)
}
sourcePath := filepath.Join(sourceFolder, "pages", customPage.Name())
headers, rawContent, err := parsePage(sourcePath)
if err != nil {
return fmt.Errorf("error parsing page '%s': %w", customPage.Name(), err)
}
if !draft && headers.Draft {
if *verbose {
fmt.Printf("Skipping page draft '%s'\n", customPage.Name())
}
continue
}
rawContent, err = transformPage(rawContent, false)
if err != nil {
return fmt.Errorf("error transforming page: %w", err)
}
customPageTemplate, err := customPageSkeletonClone.Parse(`{{define "content"}}` + string(rawContent) + `{{end}}`)
if err != nil {
return fmt.Errorf("couldn't parse custom page '%s': %w", customPage.Name(), err)
}
data := &customPageData{
pageConfig: loadedPageConfig,
CustomPages: customPages,
}
data.Hidden = headers.Hidden
data.Title = headers.Title
file := path.Join("pages", customPage.Name())
customPages[index] = &customPageEntry{
Title: headers.Title,
Hidden: headers.Hidden,
File: file,
data: data,
template: customPageTemplate,
}
}
for _, page := range customPages {
if err := writeTemplateToFile(page.template, page.data, output, page.File, minifyOutput); err != nil {
return fmt.Errorf("error writing custom page: %w", err)
}
}
articles, err := os.ReadDir(filepath.Join(sourceFolder, "articles"))
if err != nil {
return fmt.Errorf("couldn't read source articles: %w", err)
}
if *verbose {
log.Println("Indexing and writing articles ...")
}
indexedArticles := make([]*indexedArticle, 0, len(articles))
for _, article := range articles {
//Other files are ignored. For example I use this to create
//.html-draft files which are posts that I don't want to publish
//yet, but still have in the blog source directory.
if !strings.HasSuffix(article.Name(), ".html") {
continue
}
newArticleSkeleton, err := builder.templates.Lookup("article").Clone()
if err != nil {
return fmt.Errorf("couldn't clone article template: %w", err)
}
sourcePath := filepath.Join(sourceFolder, "articles", article.Name())
headers, rawContent, err := parsePage(sourcePath)
if err != nil {
return fmt.Errorf("error parsing article '%s': %w", article.Name(), err)
}
if !draft && headers.Draft {
if *verbose {
fmt.Printf("Skipping article draft '%s'\n", article.Name())
}
continue
}
transformedContent, err := transformPage([]byte(rawContent), false)
if err != nil {
return fmt.Errorf("error transforming article: %w", err)
}
specificArticleTemplate, err := newArticleSkeleton.Parse(
`{{define "content"}}` + string(transformedContent) + `{{end}}`,
)
if err != nil {
return fmt.Errorf("couldn't parse article '%s': %w", article.Name(), err)
}
articleData := &articlePageData{
pageConfig: loadedPageConfig,
CustomPages: customPages,
}
articleData.Hidden = headers.Hidden
articleData.Title = headers.Title
articleData.Description = headers.Description
for tagIndex, tag := range headers.Tags {
headers.Tags[tagIndex] = strings.ToLower(strings.TrimSpace(tag))
}
sort.Slice(headers.Tags, func(a, b int) bool {
return strings.Compare(headers.Tags[a], headers.Tags[b]) == -1
})
articleData.Tags = headers.Tags
articleData.RFC3339Time = headers.dateParsed.Format(time.RFC3339)
articleData.HumanTime = headers.dateParsed.Format(loadedPageConfig.DateFormat)
if headers.PodcastAudio != "" {
if strings.HasPrefix(strings.TrimPrefix(headers.PodcastAudio, "/"), "media") {
articleData.PodcastAudio = path.Join(loadedPageConfig.BasePath, headers.PodcastAudio)
}
}
articleTargetPath := filepath.Join("articles", article.Name())
if !articleData.Hidden {
// For feeds, we don't want certain elements, as they cause issues with
// feed readers.
feedContent, err := transformPage(rawContent, true)
if err != nil {
return fmt.Errorf("error transforming content for feed: %w", err)
}
newIndexedArticle := &indexedArticle{
pageConfig: loadedPageConfig,
podcastAudio: articleData.PodcastAudio,
Title: headers.Title,
File: path.Join("articles", article.Name()),
RFC3339Time: headers.dateParsed,
HumanTime: articleData.HumanTime,
FeedContent: string(feedContent),
Tags: headers.Tags,
AuthorName: headers.Author,
AuthorEmail: headers.AuthorEmail,
}
// Fix page metadata to include correct name instead of main author.
if newIndexedArticle.AuthorName != "" {
newIndexedArticle.Author = newIndexedArticle.AuthorName
articleData.Author = newIndexedArticle.AuthorName
}
indexedArticles = append(indexedArticles, newIndexedArticle)
}
if err := writeTemplateToFile(specificArticleTemplate, articleData, output, articleTargetPath, minifyOutput); err != nil {
return fmt.Errorf("error writing article: %w", err)
}
}
// Sort articles to make sure the RSS feed and index have the right ordering.
sort.Slice(indexedArticles, func(a, b int) bool {
articleA := indexedArticles[a]
articleB := indexedArticles[b]
return articleB.RFC3339Time.Before(articleA.RFC3339Time)
})
// Collect tags from all articles for listing them in the index files.
var tags []string
for _, article := range indexedArticles {
NEW_TAG_LOOP:
for _, tag := range article.Tags {
for _, existingTag := range tags {
if existingTag == tag {
continue NEW_TAG_LOOP
}
}
tags = append(tags, tag)
}
}
slices.Sort(tags)
if *verbose {
log.Println("Writing main index files.")
}
indexTemplate := builder.templates.Lookup("index")
writeIndexFiles(indexTemplate, indexedArticles, customPages, loadedPageConfig,
tags, "", "index.html", "index-%d.html", output, minifyOutput)
if *verbose {
log.Println("Writing tagged index files.")
}
// Special Index-Files with tag-filters
for _, tag := range tags {
var tagFilteredArticles []*indexedArticle
ARTICLE_LOOP:
for _, article := range indexedArticles {
for _, articleTag := range article.Tags {
if articleTag == tag {
tagFilteredArticles = append(tagFilteredArticles, article)
continue ARTICLE_LOOP
}
}
}
writeIndexFiles(indexTemplate, tagFilteredArticles, customPages, loadedPageConfig,
tags, tag, "index-"+tag+".html", "index-"+tag+"-%d.html", output, minifyOutput)
}
if *verbose {
log.Println("Writing RSS feed.")
}
if err := writeRSSFeed(sourceFolder, output, indexedArticles, loadedPageConfig); err != nil {
return fmt.Errorf("error writing rss feed: %w", err)
}
baseCSSFile, err := skeletonFS.Open("skeletons/base.css")
if err != nil {
return fmt.Errorf("couldn't read base.css: %w", err)
}
if minifyOutput {
if *verbose {
log.Println("Copying and minifying base.css.")
}
baseCSSOutput, err := createFile(filepath.Join(output, "base.css"))
if err != nil {
return err
}
defer baseCSSOutput.Close()
if err := minifier.Minify("text/css", baseCSSOutput, baseCSSFile); err != nil {
return fmt.Errorf("couldn't minify base.css: %w", err)
}
} else {
if *verbose {
log.Println("Copying base.css.")
}
if err := copyDataIntoFile(baseCSSFile, filepath.Join(output, "base.css")); err != nil {
return err
}
}
if *verbose {
log.Println("Copying media directory.")
}
if err := copy.Copy(filepath.Join(sourceFolder, "media"), filepath.Join(output, "media")); err != nil {
return fmt.Errorf("couldn't copy media directory: %w", err)
}
if *verbose {
log.Println("Writing 404.html")
}
return writeTemplateToFile(builder.templates.Lookup("404"), &customPageData{
pageConfig: loadedPageConfig,
CustomPages: customPages,
}, output, "404.html", minifyOutput)
}
func copyFavicon(sourceFolder, output string) (string, error) {
// .ico is preferred, as it has multi resolution support.
err := copyFileByPath(
filepath.Join(sourceFolder, "favicon.ico"),
filepath.Join(output, "favicon.ico"))
if err == nil {
return "favicon.ico", nil
}
// If we encounter any error, aside from non-existence, we early
// exit, as trying the other format doesn't make sense.
if !os.IsNotExist(err) {
return "favicon.ico", fmt.Errorf("error copying favicon.ico: %w", err)
}
// Doesn't exist, fallthrough to png.
err = copyFileByPath(
filepath.Join(sourceFolder, "favicon.png"),
filepath.Join(output, "favicon.png"))
if err == nil {
return "favicon.png", nil
}
if !os.IsNotExist(err) {
return "favicon.png", fmt.Errorf("error copying favicon.png: %w", err)
}
return "", nil
}
func parsePage(sourcePath string) (ArticleHeaders, []byte, error) {
var headers ArticleHeaders
sourceFile, err := os.Open(sourcePath)
if err != nil {
return headers, nil, fmt.Errorf("error opening article: %w", err)
}
articleBytes, err := io.ReadAll(sourceFile)
if err != nil {
return headers, nil, fmt.Errorf("error reading article: %w", err)
}
defer sourceFile.Close()
// Prevent follow-up errors on windows.
articleBytes = bytes.ReplaceAll(articleBytes, []byte("\r\n"), []byte("\n"))
parts := bytes.SplitN(articleBytes, []byte("\n---\n"), 2)
if len(parts) < 2 {
return headers, nil, errors.New("header missing, separate with `\n---\n`")
}
if err := yaml.Unmarshal(parts[0], &headers); err != nil {
return headers, nil, fmt.Errorf("error reading headers: %w", err)
}
if err := headers.Parse(); err != nil {
return headers, nil, fmt.Errorf("error parsing headers: %w", err)
}
return headers, parts[1], nil
}
func transformPage(post []byte, feed bool) ([]byte, error) {
// We don't want to transform any elements for the feed as of now, as
// these are things the RSS reader should do. We only provide content,
// not style.
if feed {
return post, nil
}
reader := bytes.NewReader(post)
writer := bytes.NewBuffer(make([]byte, 0, len(post)+1048))
tokenizer := html.NewTokenizer(reader)
handleErr := func(err error) ([]byte, error) {
if errors.Is(err, io.EOF) {
return writer.Bytes(), nil
}
return nil, err
}
for {
tokenType := tokenizer.Next()
if tokenType == html.ErrorToken {
return handleErr(tokenizer.Err())
}
token := tokenizer.Token()
if token.Type == html.StartTagToken {
switch token.Data {
case "h2", "h3", "h4", "h5", "h6":
if err := transformHeading(tokenizer, token, writer); err != nil {
return handleErr(err)
}
continue
case "img":
if err := transformImage(token, writer); err != nil {
return handleErr(err)
}
continue
}
}
// Some tags are self-closing, such as "img". Meaning it doesn't matter
// whether you put "<img>" or "</img>". However, the tokenizer will
// still output a different token type, as the parsing isn't semantic,
// so we treat both types, as browsers are lenient.
if token.Type == html.SelfClosingTagToken {
switch token.Data {
case "img":
if err := transformImage(token, writer); err != nil {
return handleErr(err)
}
continue
}
}
writer.WriteString(token.String())
}
}
var (
idCharsToRemove = regexp.MustCompile("[^a-z0-9 ]")
idCharsToReplace = regexp.MustCompile("[ ]")
)
func convertToElementId(text string) string {
id := strings.ToLower(text)
id = idCharsToRemove.ReplaceAllLiteralString(id, "")
id = idCharsToReplace.ReplaceAllLiteralString(id, "_")
return id
}
func next(tokenizer *html.Tokenizer) (html.TokenType, html.Token, error) {
tokenType := tokenizer.Next()
token := tokenizer.Token()
if tokenType == html.ErrorToken {
return tokenType, token, tokenizer.Err()
}
return tokenType, token, nil
}
func attr(token html.Token, key string) (string, bool) {
for _, attr := range token.Attr {
if attr.Key == key {
return attr.Val, true
}
}
return "", false
}
func transformImage(imageToken html.Token, writer *bytes.Buffer) error {
_, hasWidth := attr(imageToken, "width")
_, hasHeight := attr(imageToken, "height")
loading, _ := attr(imageToken, "loading")
if loading == "lazy" {
if !hasWidth || !hasHeight {
return fmt.Errorf("image tag '%s' is set to load lazy, but doesn't have a width and height", imageToken.String())
}
}
// If width and height are available, we default to lazy loading, if eager
// isn't defined explicitly.
if loading == "" && hasWidth && hasHeight {
imageToken.Attr = append(imageToken.Attr, html.Attribute{Key: "loading", Val: "lazy"})
}
writer.WriteString(imageToken.String())
return nil
}
func transformHeading(tokenizer *html.Tokenizer, headingOpen html.Token, writer *bytes.Buffer) error {
var lastText string
for {
tokenType, token, err := next(tokenizer)
if err != nil {
return err
}
if tokenType == html.TextToken {
lastText = token.String()
} else if tokenType == html.EndTagToken {
if lastText != "" {
id := convertToElementId(lastText)
headingOpen.Attr = append(headingOpen.Attr, html.Attribute{Key: "id", Val: id})
writer.WriteString(headingOpen.String())
writer.WriteString(lastText)
writer.WriteString(fmt.Sprintf(`<a class="h-a" href="#%s">#</a>`, id))
lastText = ""
}
writer.WriteString(token.String())
return nil
} else {
writer.WriteString(token.String())
}
}
}
// cleanup deletes previously generated files.
func cleanup(output string) error {
if *verbose {
log.Println("Clearing output directory.")
}
if err := removeAll(
filepath.Join(output, "media"),
filepath.Join(output, "articles"),
filepath.Join(output, "pages"),
filepath.Join(output, "favicon.ico"),
filepath.Join(output, "favicon.png"),
filepath.Join(output, "base.css"),
filepath.Join(output, "404.html"),
filepath.Join(output, "feed.xml"),
); err != nil {
return err
}
files, err := filepath.Glob(filepath.Join(output, "index*.html"))
if err != nil {
return fmt.Errorf("couldn't delete old index*.html files: %w", err)
}
for _, indexToDelete := range files {
os.Remove(indexToDelete)
}
return nil
}
// writeIndexFiles writes paginated index files. It supports both tagged
// index files and untagged (default) index files.
func writeIndexFiles(
indexTemplate *template.Template,
indexedArticles []*indexedArticle,
customPages []*customPageEntry,
loadedPageConfig pageConfig,
tags []string,
filterTag string,
firstIndexName string,
indexNameTemplate string,
outputFolder string,
minifyOutput bool,
) error {
currentPageNumber := 1
lastPageNumber := len(indexedArticles) / loadedPageConfig.MaxIndexEntries
if len(indexedArticles)%loadedPageConfig.MaxIndexEntries != 0 {
lastPageNumber++
}
for i := 1; i <= len(indexedArticles); i += loadedPageConfig.MaxIndexEntries {
var pageName string
if currentPageNumber == 1 {
pageName = firstIndexName
} else {
pageName = fmt.Sprintf(indexNameTemplate, currentPageNumber)
}
data := &indexData{
pageConfig: loadedPageConfig,
Tags: tags,
FilterTag: filterTag,
CustomPages: customPages,
IndexedArticles: indexedArticles[i-1 : min(i-1+loadedPageConfig.MaxIndexEntries, len(indexedArticles))],
PageNameTemplate: indexNameTemplate,
CurrentPageNum: currentPageNumber,
FirstPage: firstIndexName,
LastPageNum: lastPageNumber,
}
if i+loadedPageConfig.MaxIndexEntries <= len(indexedArticles) {
data.NextPageNum = currentPageNumber + 1
}
if currentPageNumber > 1 {
data.PrevPageNum = currentPageNumber - 1
}
if err := writeTemplateToFile(indexTemplate, data, outputFolder, pageName, minifyOutput); err != nil {
return nil
}
currentPageNumber++
}
return nil
}
func writeRSSFeed(sourceFolder, outputFolder string, articles []*indexedArticle, loadedPageConfig pageConfig) error {
var mainAuthor *feeds.Author
if loadedPageConfig.Email != "" {
mainAuthor = &feeds.Author{
Name: loadedPageConfig.Author,
Email: loadedPageConfig.Email,
}
}
feed := &feeds.Feed{
Title: loadedPageConfig.SiteName,
Description: loadedPageConfig.Description,
Author: mainAuthor,
}
if loadedPageConfig.URL != "" {
feed.Link = &feeds.Link{Href: loadedPageConfig.URL}
}
if loadedPageConfig.CreationDate != "" {
var err error
feed.Created, err = time.Parse(time.RFC3339, loadedPageConfig.CreationDate)
if err != nil {
return err
}
}
for _, article := range articles {
newFeedItem := &feeds.Item{
Title: article.Title,
Author: mainAuthor,
Content: article.FeedContent,
Description: article.Description,
Created: article.RFC3339Time,
}
if article.AuthorEmail != "" || article.AuthorName != "" {
articleAuthor := &feeds.Author{
Name: article.AuthorName,
}
if article.AuthorEmail != "" {
articleAuthor.Email = article.AuthorEmail
} else if mainAuthor != nil {
articleAuthor.Email = mainAuthor.Email
}
newFeedItem.Author = articleAuthor
}
if article.podcastAudio != "" {
audioFilepath := filepath.Join(sourceFolder, article.podcastAudio)
audioFile, err := os.Stat(audioFilepath)
if err != nil {
return fmt.Errorf("couldn't read podcast audio file '%s': %w", audioFilepath, err)
}
audioURL, err := joinURLParts(feed.Link.Href, article.podcastAudio)
if err != nil {
return fmt.Errorf("couldn't generate audio URL: %w", err)
}
newFeedItem.Enclosure = &feeds.Enclosure{
Type: "audio/mp3",
Length: strconv.FormatInt(audioFile.Size(), 10),
Url: audioURL,
}
}
feed.Items = append(feed.Items, newFeedItem)
if loadedPageConfig.URL != "" {
articleURL, err := joinURLParts(loadedPageConfig.URL, article.File)
if err != nil {
return fmt.Errorf("couldn't generate article URL: %w", err)
}
newFeedItem.Link = &feeds.Link{Href: articleURL}
}
}
rssFile, err := createFile(filepath.Join(outputFolder, "feed.xml"))
if err != nil {
return err
}
defer rssFile.Close()
rssData, err := feed.ToRss()
if err != nil {
return fmt.Errorf("couldn't generate RSS feed: %w", err)
}
_, err = rssFile.WriteString(rssData)
if err != nil {
return fmt.Errorf("couldn't write RSS feed: %w", err)
}
return nil
}
// joinURLParts puts together two URL pieces without duplicating separators
// or removing separators. Before, this was done by path.Join directly which
// caused the resulting URL to be missing a forward slash behind the protocol.
// Using filepath.Join would cause incorrect separators on windows, as URLs
// should always use forward slashes, but windows uses backward slashes.
func joinURLParts(partOne, partTwo string) (string, error) {
url, err := url.Parse(partOne)
if err != nil {
return "", err
}
url.Path = path.Join(url.Path, partTwo)
return url.String(), nil
}
type pageConfig struct {
BasePath string
// Hidden will not show any links to the given page. This works for both
// custom pages and articles.
Hidden bool
// Title is the page titel, which differs from the SiteName.
Title string
SiteName string
Author string
URL string
Description string
DateFormat string
Email string
CreationDate string
UtterancesRepo string
MaxIndexEntries int
AddOptionalMetaData bool
Favicon string
}
type customPageEntry struct {
Title string
File string
// Hidden will not show any links to the given page. This works for both
// custom pages and articles.
Hidden bool
data *customPageData
template *template.Template
}
type articlePageData struct {
pageConfig
// Time article was published in RFC3339 format.
RFC3339Time string
// HumanTime is a human readable time format.
HumanTime string
// PodcastAudio file link
PodcastAudio string
// Tags for metadata
Tags []string
// CustomPages are listed right of the default pages in the site navbar /
// header.
CustomPages []*customPageEntry
}
type customPageData struct {
pageConfig
// CustomPages are listed right of the default pages in the site navbar /
// header.
CustomPages []*customPageEntry
}
type indexData struct {
pageConfig
// Tags are all available tags used accross all posts
Tags []string
// FilterTag that is currently filtered for
FilterTag string
// CustomPages are listed right of the default pages in the site navbar /
// header.
CustomPages []*customPageEntry
// IndexedArticles are the articles to display.
IndexedArticles []*indexedArticle
PageNameTemplate string
FirstPage string
CurrentPageNum int
PrevPageNum int
NextPageNum int
LastPageNum int
}
func templateToString(temp *template.Template) (string, error) {
buffer := &bytes.Buffer{}
if err := temp.Execute(buffer, nil); err != nil {
return "", fmt.Errorf("couldn't execute template '%s': %w", temp.Name(), err)
}
return buffer.String(), nil
}
func templateToOptionalString(temp *template.Template) (string, error) {
if temp == nil {
return "", nil
}
return templateToString(temp)
}
type indexedArticle struct {
pageConfig
AuthorName string
AuthorEmail string
Title string
File string
RFC3339Time time.Time
podcastAudio string
HumanTime string
FeedContent string
Tags []string
}