about summary refs log tree commit diff stats
path: root/cmd/build
diff options
context:
space:
mode:
authorAlan Pearce2024-04-15 22:52:41 +0200
committerAlan Pearce2024-04-16 18:10:17 +0200
commit27448f33dce9141316bb4ea6d905305d8a846cac (patch)
treed39bfed3da6beac0b8a5a4c2d163c6a0f2aef1f8 /cmd/build
parent0b61d3995bc01a5c8345b719459da261b92d3063 (diff)
downloadwebsite-27448f33dce9141316bb4ea6d905305d8a846cac.tar.lz
website-27448f33dce9141316bb4ea6d905305d8a846cac.tar.zst
website-27448f33dce9141316bb4ea6d905305d8a846cac.zip
wip: verbose error handling
Diffstat (limited to 'cmd/build')
-rw-r--r--cmd/build/main.go332
1 files changed, 234 insertions, 98 deletions
diff --git a/cmd/build/main.go b/cmd/build/main.go
index 0776951..6519fa8 100644
--- a/cmd/build/main.go
+++ b/cmd/build/main.go
@@ -4,7 +4,9 @@ import (
 	"bytes"
 	"encoding/xml"
 	"fmt"
+	"io/fs"
 	"log"
+	"log/slog"
 	"os"
 	"path"
 	"path/filepath"
@@ -14,11 +16,13 @@ import (
 
 	. "alanpearce.eu/website/internal/config"
 
+	"github.com/BurntSushi/toml"
 	"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/pkg/errors"
 	"github.com/yuin/goldmark"
 	"github.com/yuin/goldmark/extension"
 	"github.com/yuin/goldmark/renderer/html"
@@ -44,30 +48,31 @@ type Post struct {
 
 type Tags mapset.Set[string]
 
-func check(err error) {
-	if err != nil {
-		log.Panic(err)
-	}
-}
-
-func getPost(filename string) (PostMatter, []byte) {
+func getPost(filename string) (*PostMatter, *[]byte, error) {
 	matter := PostMatter{}
 	content, err := os.Open(filename)
-	check(err)
-	rest, err := frontmatter.Parse(content, &matter)
-	check(err)
+	defer content.Close()
+	if err != nil {
+		return nil, nil, errors.WithMessagef(err, "could not open post %s", filename)
+	}
+	rest, err := frontmatter.MustParse(content, &matter)
+	if err != nil {
+		return nil, nil, errors.WithMessagef(err, "could not parse front matter of post %s", filename)
+	}
 
-	return matter, rest
+	return &matter, &rest, nil
 }
 
-func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) {
+func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags, error) {
 	tags := mapset.NewSet[string]()
 	posts := []Post{}
 	subdir := filepath.Join(root, inputDir)
 	files, err := os.ReadDir(subdir)
+	if err != nil {
+		return nil, nil, errors.WithMessagef(err, "could not read post directory %s", subdir)
+	}
 	outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html")
 	urlReplacer := strings.NewReplacer(root, "", ".md", "/")
-	check(err)
 	md := goldmark.New(
 		goldmark.WithRendererOptions(
 			html.WithUnsafe(),
@@ -80,25 +85,28 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) {
 	)
 	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)
+			matter, content, err := getPost(pathFromRoot)
+			if err != nil {
+				return nil, nil, err
+			}
 
 			for _, tag := range matter.Taxonomies.Tags {
 				tags.Add(strings.ToLower(tag))
 			}
 
 			var buf bytes.Buffer
-			err := md.Convert(content, &buf)
-			check(err)
+			if err := md.Convert(*content, &buf); err != nil {
+				return nil, nil, errors.WithMessage(err, "could not convert markdown content")
+			}
 			post := Post{
 				Input:      pathFromRoot,
 				Output:     output,
 				Basename:   filepath.Base(url),
 				URL:        url,
-				PostMatter: matter,
+				PostMatter: *matter,
 				Content:    buf.String(),
 			}
 
@@ -108,15 +116,23 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) {
 	slices.SortFunc(posts, func(a, b Post) int {
 		return b.Date.Compare(a.Date)
 	})
-	return posts, tags
+	return posts, tags, nil
 }
 
-func layout(filename string, config Config, pageTitle string) *goquery.Document {
-	css, err := os.ReadFile("templates/style.css")
-	check(err)
+func layout(filename string, config Config, pageTitle string) (*goquery.Document, error) {
 	html, err := os.Open(filename)
+	if err != nil {
+		return nil, err
+	}
+	defer html.Close()
+	css, err := os.ReadFile("templates/style.css")
+	if err != nil {
+		return nil, err
+	}
 	doc, err := goquery.NewDocumentFromReader(html)
-	check(err)
+	if err != nil {
+		return nil, err
+	}
 	doc.Find("html").SetAttr("lang", config.DefaultLanguage)
 	doc.Find("head > link[rel=alternate]").SetAttr("title", config.Title)
 	doc.Find(".title").SetText(config.Title)
@@ -128,14 +144,19 @@ func layout(filename string, config Config, pageTitle string) *goquery.Document
 	for _, link := range config.Menus["main"] {
 		nav.AppendSelection(navLink.Clone().SetAttr("href", link.URL).SetText(link.Name))
 	}
-	return doc
+	return doc, nil
 }
 
-func renderPost(post Post, config Config) string {
-	doc := layout("templates/post.html", config, post.PostMatter.Title)
+func renderPost(post Post, config Config) (string, error) {
+	doc, err := layout("templates/post.html", config, post.PostMatter.Title)
+	if err != nil {
+		return "", err
+	}
 	doc.Find(".title").AddClass("h-card p-author").SetAttr("rel", "author")
 	datetime, err := post.PostMatter.Date.MarshalText()
-	check(err)
+	if err != nil {
+		return "", err
+	}
 	doc.Find(".h-entry .dt-published").SetAttr("datetime", string(datetime)).SetText(
 		post.PostMatter.Date.Format("2006-01-02"),
 	)
@@ -146,13 +167,14 @@ func renderPost(post Post, config Config) string {
 	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
+	return doc.Html()
 }
 
-func renderTags(tags Tags, config Config) string {
-	doc := layout("templates/tags.html", config, config.Title)
+func renderTags(tags Tags, config Config) (string, error) {
+	doc, err := layout("templates/tags.html", config, config.Title)
+	if err != nil {
+		return "", err
+	}
 	tagList := doc.Find(".tags")
 	tpl := doc.Find(".h-feed")
 	tpl.Remove()
@@ -161,19 +183,20 @@ func renderTags(tags Tags, config Config) string {
 			tpl.Clone().SetAttr("href", fmt.Sprintf("/tags/%s/", tag)).SetText("#" + tag),
 		)
 	}
-	html, err := doc.Html()
-	check(err)
-	return html
+	return doc.Html()
 }
 
-func renderListPage(tag string, config Config, posts []Post) string {
+func renderListPage(tag string, config Config, posts []Post) (string, error) {
 	var title string
 	if len(tag) > 0 {
 		title = tag
 	} else {
 		title = config.Title
 	}
-	doc := layout("templates/list.html", config, title)
+	doc, err := layout("templates/list.html", config, title)
+	if err != nil {
+		return "", err
+	}
 	feed := doc.Find(".h-feed")
 	tpl := feed.Find(".h-entry")
 	tpl.Remove()
@@ -188,28 +211,35 @@ func renderListPage(tag string, config Config, posts []Post) string {
 	for _, post := range posts {
 		entry := tpl.Clone()
 		datetime, err := post.PostMatter.Date.MarshalText()
-		check(err)
+		if err != nil {
+			return "", 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
+	return doc.Html()
 }
 
-func renderHomepage(config Config, posts []Post) string {
-	_, index := getPost("content/_index.md")
-	doc := layout("templates/homepage.html", config, config.Title)
+func renderHomepage(config Config, posts []Post) (string, error) {
+	_, index, err := getPost("content/_index.md")
+	if err != nil {
+		return "", err
+	}
+	doc, err := layout("templates/homepage.html", config, config.Title)
+	if err != nil {
+		return "", err
+	}
 	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)
+	if err := md.Convert(*index, &buf); err != nil {
+		return "", err
+	}
 	doc.Find("#content").SetHtml(buf.String())
 
 	feed := doc.Find(".h-feed")
@@ -221,7 +251,9 @@ func renderHomepage(config Config, posts []Post) string {
 		entry.Find(".p-name").SetText(post.Title)
 		entry.Find(".u-url").SetAttr("href", post.URL)
 		datetime, err := post.PostMatter.Date.MarshalText()
-		check(err)
+		if err != nil {
+			return "", err
+		}
 		entry.
 			Find(".dt-published").
 			SetAttr("datetime", string(datetime)).
@@ -243,21 +275,23 @@ func renderHomepage(config Config, posts []Post) string {
 		elsewhere.AppendSelection(el)
 	}
 
-	html, err := doc.Html()
-	check(err)
-	return html
+	return doc.Html()
 }
 
-func render404(config Config) string {
-	doc := layout("templates/404.html", config, "404 Not Found")
-	html, err := doc.Html()
-	check(err)
-	return html
+func render404(config Config) (string, error) {
+	doc, err := layout("templates/404.html", config, "404 Not Found")
+	if err != nil {
+		return "", err
+	}
+	return doc.Html()
 }
 
-func renderFeed(title string, config Config, posts []Post, specific string) string {
+func renderFeed(title string, config Config, posts []Post, specific string) (string, error) {
 	reader, err := os.Open("templates/feed.xml")
-	check(err)
+	if err != nil {
+		return "", err
+	}
+	defer reader.Close()
 	doc, err := xmlquery.Parse(reader)
 	feed := doc.SelectElement("feed")
 	feed.SelectElement("title").FirstChild.Data = title
@@ -281,7 +315,9 @@ func renderFeed(title string, config Config, posts []Post, specific string) stri
 				Type:    "html",
 			},
 		}, "  ", "    ")
-		check(err)
+		if err != nil {
+			return "", err
+		}
 		entry, err := xmlquery.ParseWithOptions(strings.NewReader(string(text)), xmlquery.ParserOptions{
 			Decoder: &xmlquery.DecoderOptions{
 				Strict:    false,
@@ -289,54 +325,87 @@ func renderFeed(title string, config Config, posts []Post, specific string) stri
 				Entity:    xml.HTMLEntity,
 			},
 		})
-		check(err)
+		if err != nil {
+			return "", err
+		}
 		xmlquery.AddChild(feed, entry.SelectElement("entry"))
 	}
 
-	return doc.OutputXML(true)
+	return doc.OutputXML(true), nil
 }
 
-func renderFeedStyles() string {
+func renderFeedStyles() (string, error) {
 	reader, err := os.Open("templates/feed-styles.xsl")
+	if err != nil {
+		return "", err
+	}
+	defer reader.Close()
 	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)
+	if err != nil {
+		return "", err
+	}
 	style := xmlquery.QuerySelector(doc, expr)
 	css, err := os.ReadFile("templates/style.css")
-	check(err)
+	if err != nil {
+		return "", err
+	}
 	xmlquery.AddChild(style, &xmlquery.Node{
 		Type: xmlquery.TextNode,
 		Data: string(css),
 	})
-	return doc.OutputXML(true)
+	return doc.OutputXML(true), nil
 }
 
-func main() {
-	err, config := GetConfig()
+type errFiler struct {
+	err error
+}
+
+func build() error {
+	config, err := GetConfig()
+	if err != nil {
+		return err
+	}
 	outDir := "new"
-	check(err)
-	err = os.MkdirAll(path.Join(outDir, "post"), 0755)
-	check(err)
-	log.Print("Generating site...")
-	posts, tags := readPosts("content", "post", outDir)
+	if err := os.MkdirAll(path.Join(outDir, "post"), 0755); err != nil {
+		return errors.WithMessage(err, "could not create post output directory")
+	}
+	posts, tags, err := readPosts("content", "post", outDir)
+	if err != nil {
+		return err
+	}
+
 	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)
+		dst := path.Join(outDir, "post", post.Basename)
+		if err := os.MkdirAll(dst, 0755); err != nil {
+			return errors.WithMessagef(err, "could not create directory for post %s", dst)
+		}
+		output, err := renderPost(post, *config)
+		if err != nil {
+			return errors.WithMessagef(err, "could not render post %s", post.Input)
+		}
+		if err := os.WriteFile(post.Output, []byte(output), 0755); err != nil {
+			return errors.WithMessage(err, "could not write output file")
+		}
+	}
+
+	if err := os.MkdirAll(path.Join(outDir, "tags"), 0755); err != nil {
+		return errors.WithMessage(err, "could not create directory for tags")
+	}
+	tagsHtml, err := renderTags(tags, *config)
+	if err != nil {
+		return errors.WithMessage(err, "could not render tags")
+	}
+	err = os.WriteFile(path.Join(outDir, "tags", "index.html"), []byte(tagsHtml), 0644)
+	if err != nil {
+		return errors.WithMessage(err, "could not write output file")
+	}
+
 	for _, tag := range tags.ToSlice() {
 		matchingPosts := []Post{}
 		for _, post := range posts {
@@ -344,27 +413,94 @@ func main() {
 				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)
+		if err := os.MkdirAll(path.Join(outDir, "tags", tag), 0755); err != nil {
+			return errors.WithMessage(err, "could not create directory")
+		}
+		tagPage, err := renderListPage(tag, *config, matchingPosts)
+		if err != nil {
+			return errors.WithMessage(err, "could not render tag page")
+		}
+		if err := os.WriteFile(path.Join(outDir, "tags", tag, "index.html"), []byte(tagPage), 0644); err != nil {
+			return errors.WithMessage(err, "could not write tag output file")
+		}
+
+		feedPage, err := renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), *config, matchingPosts, tag)
+		if err != nil {
+			return errors.WithMessage(err, "could not render tag feed page")
+		}
+		if err := os.WriteFile(path.Join(outDir, "tags", tag, "atom.xml"), []byte(feedPage), 0644); err != nil {
+			return errors.WithMessage(err, "could not write tag feed output file")
+		}
+	}
+	listPage, err := renderListPage("", *config, posts)
+	if err != nil {
+		return errors.WithMessage(err, "could not render list page")
+	}
+	if err := os.WriteFile(path.Join(outDir, "post", "index.html"), []byte(listPage), 0644); err != nil {
+		return errors.WithMessage(err, "could not write list page output file")
+	}
+
+	feed, err := renderFeed(config.Title, *config, posts, "feed")
+	if err != nil {
+		return errors.WithMessage(err, "could not render feed")
+	}
+	if err := os.WriteFile(path.Join(outDir, "atom.xml"), []byte(feed), 0644); err != nil {
+		return errors.WithMessage(err, "could not write feed")
+	}
 
-		// fmt.Printf("%+v\n", renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), config, matchingPosts, tag))
+	feedStyles, err := renderFeedStyles()
+	if err != nil {
+		return errors.WithMessage(err, "could not render feed styles")
+	}
+	if err := os.WriteFile(path.Join(outDir, "feed-styles.xsl"), []byte(feedStyles), 0644); err != nil {
+		return errors.WithMessage(err, "could not write feed styles")
+	}
 
-		// fmt.Printf("%+v\n", renderListPage("", config, posts))
+	homePage, err := renderHomepage(*config, posts)
+	if err != nil {
+		return errors.WithMessage(err, "could not render homepage")
+	}
+	if err := os.WriteFile(path.Join(outDir, "index.html"), []byte(homePage), 0644); err != nil {
+		return errors.WithMessage(err, "could not write homepage")
+	}
 
-		// fmt.Printf("%+v\n", renderFeed(config.Title, config, posts, "feed"))
+	notFound, err := render404(*config)
+	if err != nil {
+		return errors.WithMessage(err, "could not render 404 page")
+	}
+	if err := os.WriteFile(path.Join(outDir, "404.html"), []byte(notFound), 0644); err != nil {
+		return errors.WithMessage(err, "could not write 404 file")
+	}
 
-		// fmt.Printf("%+v\n", renderFeedStyles())
+	return nil
+}
 
-		// fmt.Printf("%+v\n", renderHomepage(config, posts))
+func main() {
+	if true {
+		slog.SetLogLoggerLevel(slog.LevelDebug)
+	}
+	slog.Debug("starting build process")
+	_, err := os.Getwd()
+	if err != nil {
+		log.Panic(errors.Errorf("working directory does not exist: %v", err))
+	}
 
-		fmt.Printf("%+v\n", render404(config))
-		fmt.Println(config.DefaultLanguage)
+	// log.SetFlags(log.Lshortfile)
+	if err := build(); err != nil {
+		switch cause := errors.Cause(err).(type) {
+		case *fs.PathError:
+			slog.Info("pathError")
+			slog.Error(fmt.Sprintf("%s", err))
+		case toml.ParseError:
+			slog.Info("parseError")
+			slog.Error(fmt.Sprintf("%s", err))
+		default:
+			slog.Info("other")
+			slog.Error(fmt.Sprintf("cause:%+v", errors.Cause(cause)))
+			slog.Error(fmt.Sprintf("%+v", cause))
+		}
+		os.Exit(1)
 	}
 
-	fmt.Printf("%+v\n", tags)
-	fmt.Println()
+	slog.Debug("done")
 }