summaryrefslogtreecommitdiff
path: root/pages/pages.go
blob: 8adb9e1603cccd45d5b3faed7a2e9cc040828416 (plain)
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
package pages

import (
	"bytes"
	"fmt"
	"sort"
	"text/template"

	"git.sr.ht/~a73x/home/html"
	"git.sr.ht/~a73x/home/markdown"
	"git.sr.ht/~a73x/home/templates"
)

type Navigation struct {
	Title string
	Path  string
}
type GlobalState struct {
	Collections map[string][]markdown.Content
	Nav         []Navigation
}

type ParserPair struct {
	GlobalState
	markdown.Content
}

func renderTemplate(config GlobalState, content markdown.Content) (string, error) {
	tmpl := content.Meta["template"]
	chosenTemplate := fmt.Sprintf("%s.html", tmpl)
	t, err := template.ParseFS(templates.FS, chosenTemplate, "layouts/*.html")
	if err != nil {
		return "", fmt.Errorf("failed to parse layouts: %v", err)
	}

	contentParser, err := template.New("content").Parse(string(content.Body))
	if err != nil {
		return "", fmt.Errorf("failed parsing content: %v", err)
	}

	data := ParserPair{
		config,
		content,
	}

	newContent := &bytes.Buffer{}
	if err := contentParser.Execute(newContent, data); err != nil {
		return "", fmt.Errorf("failed to execute content template: %v", err)
	}

	data.Body = string(html.MDToHTML(newContent.Bytes()))

	b := &bytes.Buffer{}
	if err := t.Execute(b, data); err != nil {
		return "", err
	}

	return b.String(), nil
}

type Page struct {
	Path    string
	Content string
}

func Collect() ([]Page, error) {
	contents, err := markdown.ParseContents()
	if err != nil {
		return nil, err
	}

	gs := GlobalState{
		Collections: map[string][]markdown.Content{
			"all": contents,
		},
		Nav: make([]Navigation, 0),
	}

	for _, content := range contents {
		tags, ok := content.Meta["tags"]
		if ok {
			switch tags := tags.(type) {
			case string:
				gs.Collections[tags] = append(gs.Collections[tags], content)
			case []string:
				for _, tag := range tags {
					gs.Collections[tag] = append(gs.Collections[tag], content)
				}
			}
		}

		nav, ok := content.Meta["nav"]
		if ok {
			gs.Nav = append(gs.Nav, Navigation{content.Meta["title"].(string), nav.(string)})
		}
	}

	sortNavBar(gs.Nav)

	pages := []Page{}
	for _, content := range contents {
		page, err := renderTemplate(gs, content)
		if err != nil {
			return nil, fmt.Errorf("failed to build site: %v", err)
		}

		path := content.Path
		if path == "index" {
			path = ""
		}

		pages = append(pages, Page{
			Path:    path,
			Content: page,
		})
	}

	return pages, nil
}

func sortNavBar(nav []Navigation) {
	// see no evil, speak no evil
	sort.Slice(nav, func(i, j int) bool {
		order := map[string]int{
			"home":  1,
			"posts": 2,
			"about": 3,
		}

		// Get the order value, default to a high number if not in the predefined order
		orderI, okI := order[nav[i].Title]
		if !okI {
			orderI = len(nav) + 1
		}

		orderJ, okJ := order[nav[j].Title]
		if !okJ {
			orderJ = len(nav) + 1
		}

		// If both strings have the same order value, sort lexicographically
		if orderI == orderJ {
			return nav[i].Title < nav[j].Title
		}

		return orderI < orderJ
	})
}