about summary refs log tree commit diff stats
path: root/cmd/build/main.go
blob: 0776951a07bce939b9e03d180e9a7285f05e9e80 (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
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
package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"log"
	"os"
	"path"
	"path/filepath"
	"slices"
	"strings"
	"time"

	. "alanpearce.eu/website/internal/config"

	"github.com/PuerkitoBio/goquery"
	"github.com/adrg/frontmatter"
	"github.com/antchfx/xmlquery"
	"github.com/antchfx/xpath"
	mapset "github.com/deckarep/golang-set/v2"
	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/extension"
	"github.com/yuin/goldmark/renderer/html"
)

type PostMatter struct {
	Date        time.Time `toml:"date"`
	Description string    `toml:"description"`
	Title       string    `toml:"title"`
	Taxonomies  struct {
		Tags []string `toml:"tags"`
	} `toml:"taxonomies"`
}

type Post struct {
	Input    string
	Output   string
	Basename string
	URL      string
	Content  string
	PostMatter
}

type Tags mapset.Set[string]

func check(err error) {
	if err != nil {
		log.Panic(err)
	}
}

func getPost(filename string) (PostMatter, []byte) {
	matter := PostMatter{}
	content, err := os.Open(filename)
	check(err)
	rest, err := frontmatter.Parse(content, &matter)
	check(err)

	return matter, rest
}

func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) {
	tags := mapset.NewSet[string]()
	posts := []Post{}
	subdir := filepath.Join(root, inputDir)
	files, err := os.ReadDir(subdir)
	outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html")
	urlReplacer := strings.NewReplacer(root, "", ".md", "/")
	check(err)
	md := goldmark.New(
		goldmark.WithRendererOptions(
			html.WithUnsafe(),
		),
		goldmark.WithExtensions(
			extension.GFM,
			extension.Footnote,
			extension.Typographer,
		),
	)
	for _, f := range files {
		pathFromRoot := filepath.Join(subdir, f.Name())
		check(err)
		if !f.IsDir() && path.Ext(pathFromRoot) == ".md" {
			output := outputReplacer.Replace(pathFromRoot)
			url := urlReplacer.Replace(pathFromRoot)
			matter, content := getPost(pathFromRoot)

			for _, tag := range matter.Taxonomies.Tags {
				tags.Add(strings.ToLower(tag))
			}

			var buf bytes.Buffer
			err := md.Convert(content, &buf)
			check(err)
			post := Post{
				Input:      pathFromRoot,
				Output:     output,
				Basename:   filepath.Base(url),
				URL:        url,
				PostMatter: matter,
				Content:    buf.String(),
			}

			posts = append(posts, post)
		}
	}
	slices.SortFunc(posts, func(a, b Post) int {
		return b.Date.Compare(a.Date)
	})
	return posts, tags
}

func layout(filename string, config Config, pageTitle string) *goquery.Document {
	css, err := os.ReadFile("templates/style.css")
	check(err)
	html, err := os.Open(filename)
	doc, err := goquery.NewDocumentFromReader(html)
	check(err)
	doc.Find("html").SetAttr("lang", config.DefaultLanguage)
	doc.Find("head > link[rel=alternate]").SetAttr("title", config.Title)
	doc.Find(".title").SetText(config.Title)
	doc.Find("title").Add(".p-name").SetText(pageTitle)
	doc.Find("head > style").SetText(string(css))
	nav := doc.Find("nav")
	navLink := doc.Find("nav a")
	nav.Empty()
	for _, link := range config.Menus["main"] {
		nav.AppendSelection(navLink.Clone().SetAttr("href", link.URL).SetText(link.Name))
	}
	return doc
}

func renderPost(post Post, config Config) string {
	doc := layout("templates/post.html", config, post.PostMatter.Title)
	doc.Find(".title").AddClass("h-card p-author").SetAttr("rel", "author")
	datetime, err := post.PostMatter.Date.MarshalText()
	check(err)
	doc.Find(".h-entry .dt-published").SetAttr("datetime", string(datetime)).SetText(
		post.PostMatter.Date.Format("2006-01-02"),
	)
	doc.Find(".h-entry .e-content").SetHtml(post.Content)
	categories := doc.Find(".h-entry .p-categories")
	cat := categories.Find(".p-category").ParentsUntilSelection(categories)
	cat.Remove()
	for _, tag := range post.Taxonomies.Tags {
		categories.AppendSelection(cat.Clone().Find(".p-category").SetAttr("href", fmt.Sprintf("/tags/%s/", tag)).SetText("#" + tag)).Parent()
	}
	html, err := doc.Html()
	check(err)
	return html
}

func renderTags(tags Tags, config Config) string {
	doc := layout("templates/tags.html", config, config.Title)
	tagList := doc.Find(".tags")
	tpl := doc.Find(".h-feed")
	tpl.Remove()
	for _, tag := range mapset.Sorted(tags) {
		tagList.AppendSelection(
			tpl.Clone().SetAttr("href", fmt.Sprintf("/tags/%s/", tag)).SetText("#" + tag),
		)
	}
	html, err := doc.Html()
	check(err)
	return html
}

func renderListPage(tag string, config Config, posts []Post) string {
	var title string
	if len(tag) > 0 {
		title = tag
	} else {
		title = config.Title
	}
	doc := layout("templates/list.html", config, title)
	feed := doc.Find(".h-feed")
	tpl := feed.Find(".h-entry")
	tpl.Remove()

	doc.Find(".title").AddClass("h-card p-author").SetAttr("rel", "author")
	if tag == "" {
		doc.Find(".filter").Remove()
	} else {
		doc.Find(".filter").Find("h3").SetText("#" + tag)
	}

	for _, post := range posts {
		entry := tpl.Clone()
		datetime, err := post.PostMatter.Date.MarshalText()
		check(err)

		entry.Find(".p-name").SetText(post.Title).SetAttr("href", post.URL)
		entry.Find(".dt-published").SetAttr("datetime", string(datetime)).SetText(post.PostMatter.Date.Format("2006-01-02"))
		feed.AppendSelection(entry)
	}

	html, err := doc.Html()
	check(err)
	return html
}

func renderHomepage(config Config, posts []Post) string {
	_, index := getPost("content/_index.md")
	doc := layout("templates/homepage.html", config, config.Title)
	doc.Find("body").AddClass("h-card")
	doc.Find(".title").AddClass("p-name u-url")
	var buf bytes.Buffer

	md := goldmark.New(goldmark.WithRendererOptions(html.WithUnsafe()))
	err := md.Convert(index, &buf)
	check(err)
	doc.Find("#content").SetHtml(buf.String())

	feed := doc.Find(".h-feed")
	tpl := feed.Find(".h-entry")
	tpl.Remove()

	for _, post := range posts {
		entry := tpl.Clone()
		entry.Find(".p-name").SetText(post.Title)
		entry.Find(".u-url").SetAttr("href", post.URL)
		datetime, err := post.PostMatter.Date.MarshalText()
		check(err)
		entry.
			Find(".dt-published").
			SetAttr("datetime", string(datetime)).
			SetText(post.PostMatter.Date.Format("2006-01-02"))

		feed.AppendSelection(entry)
	}
	doc.Find(".u-email").
		SetAttr("href", fmt.Sprintf("mailto:%s", config.Email)).
		SetText(config.Email)

	elsewhere := doc.Find(".elsewhere")
	linkRelMe := elsewhere.Find(".u-url[rel=me]").ParentsUntil("ul")
	linkRelMe.Remove()

	for _, link := range config.Menus["me"] {
		el := linkRelMe.Clone()
		el.Find("a").SetAttr("href", link.URL).SetText(link.Name)
		elsewhere.AppendSelection(el)
	}

	html, err := doc.Html()
	check(err)
	return html
}

func render404(config Config) string {
	doc := layout("templates/404.html", config, "404 Not Found")
	html, err := doc.Html()
	check(err)
	return html
}

func renderFeed(title string, config Config, posts []Post, specific string) string {
	reader, err := os.Open("templates/feed.xml")
	check(err)
	doc, err := xmlquery.Parse(reader)
	feed := doc.SelectElement("feed")
	feed.SelectElement("title").FirstChild.Data = title
	feed.SelectElement("link").SetAttr("href", config.BaseURL)
	feed.SelectElement("id").FirstChild.Data = makeTagURI(config, specific)
	datetime, err := posts[0].Date.MarshalText()
	feed.SelectElement("updated").FirstChild.Data = string(datetime)
	tpl := feed.SelectElement("entry")
	xmlquery.RemoveFromTree(tpl)

	for _, post := range posts {
		text, err := xml.MarshalIndent(&FeedEntry{
			Title:   post.Title,
			Link:    makeLink(post.URL),
			Id:      makeTagURI(config, post.Basename),
			Updated: post.Date,
			Summary: post.Description,
			Author:  config.Title,
			Content: FeedContent{
				Content: post.Content,
				Type:    "html",
			},
		}, "  ", "    ")
		check(err)
		entry, err := xmlquery.ParseWithOptions(strings.NewReader(string(text)), xmlquery.ParserOptions{
			Decoder: &xmlquery.DecoderOptions{
				Strict:    false,
				AutoClose: xml.HTMLAutoClose,
				Entity:    xml.HTMLEntity,
			},
		})
		check(err)
		xmlquery.AddChild(feed, entry.SelectElement("entry"))
	}

	return doc.OutputXML(true)
}

func renderFeedStyles() string {
	reader, err := os.Open("templates/feed-styles.xsl")
	nsMap := map[string]string{
		"xsl":   "http://www.w3.org/1999/XSL/Transform",
		"atom":  "http://www.w3.org/2005/Atom",
		"xhtml": "http://www.w3.org/1999/xhtml",
	}
	check(err)
	doc, err := xmlquery.Parse(reader)
	expr, err := xpath.CompileWithNS("//xhtml:style", nsMap)
	check(err)
	style := xmlquery.QuerySelector(doc, expr)
	css, err := os.ReadFile("templates/style.css")
	check(err)
	xmlquery.AddChild(style, &xmlquery.Node{
		Type: xmlquery.TextNode,
		Data: string(css),
	})
	return doc.OutputXML(true)
}

func main() {
	err, config := GetConfig()
	outDir := "new"
	check(err)
	err = os.MkdirAll(path.Join(outDir, "post"), 0755)
	check(err)
	log.Print("Generating site...")
	posts, tags := readPosts("content", "post", outDir)
	for _, post := range posts {
		err := os.MkdirAll(path.Join(outDir, "post", post.Basename), 0755)
		check(err)
		// output := renderPost(post, config)
		// err = os.WriteFile(post.Output, []byte(output), 0755)
		// check(err)
	}
	err = os.MkdirAll(path.Join(outDir, "tags"), 0755)
	check(err)
	// fmt.Printf("%+v\n", renderTags(tags, config))
	// err = os.WriteFile(path.Join(outDir, "tags", "index.html"), []byte(renderTags(tags, config)), 0755)
	// check(err)
	for _, tag := range tags.ToSlice() {
		matchingPosts := []Post{}
		for _, post := range posts {
			if slices.Contains(post.Taxonomies.Tags, tag) {
				matchingPosts = append(matchingPosts, post)
			}
		}
		err := os.MkdirAll(path.Join(outDir, "tags", tag), 0755)
		check(err)
		// tagPage := renderListPage(tag, config, matchingPosts)
		// fmt.Printf("%+v\n", tagPage)
		// err = os.WriteFile(path.Join(outDir, "tags", tag, "index.html"), []byte(tagPage), 0755)
		// check(err)

		// fmt.Printf("%+v\n", renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), config, matchingPosts, tag))

		// fmt.Printf("%+v\n", renderListPage("", config, posts))

		// fmt.Printf("%+v\n", renderFeed(config.Title, config, posts, "feed"))

		// fmt.Printf("%+v\n", renderFeedStyles())

		// fmt.Printf("%+v\n", renderHomepage(config, posts))

		fmt.Printf("%+v\n", render404(config))
		fmt.Println(config.DefaultLanguage)
	}

	fmt.Printf("%+v\n", tags)
	fmt.Println()
}