about summary refs log tree commit diff stats
path: root/src/build.go
diff options
context:
space:
mode:
authorAlan Pearce2024-04-14 20:11:14 +0200
committerAlan Pearce2024-04-16 18:10:11 +0200
commit4536e9e5dd826eb0e69c930312ba8f66e9c16175 (patch)
treec217312bd2c89067828fd62216295a20c01c9a91 /src/build.go
parentebb96f1a7f9828119bced5e562798a776f5849d4 (diff)
downloadwebsite-4536e9e5dd826eb0e69c930312ba8f66e9c16175.tar.lz
website-4536e9e5dd826eb0e69c930312ba8f66e9c16175.tar.zst
website-4536e9e5dd826eb0e69c930312ba8f66e9c16175.zip
wip: read posts
Diffstat (limited to 'src/build.go')
-rw-r--r--src/build.go102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/build.go b/src/build.go
new file mode 100644
index 0000000..b2847c3
--- /dev/null
+++ b/src/build.go
@@ -0,0 +1,102 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+	"path"
+	"path/filepath"
+	"slices"
+	"strings"
+	"time"
+
+	"github.com/adrg/frontmatter"
+	mapset "github.com/deckarep/golang-set/v2"
+)
+
+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, string) {
+	matter := PostMatter{}
+	content, err := os.Open(filename)
+	check(err)
+	rest, err := frontmatter.Parse(content, &matter)
+	check(err)
+
+	return matter, string(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)
+	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))
+			}
+
+			post := Post{
+				Input:      pathFromRoot,
+				Output:     output,
+				Basename:   filepath.Base(url),
+				URL:        url,
+				PostMatter: matter,
+				Content:    content,
+			}
+
+			posts = append(posts, post)
+		}
+	}
+	slices.SortFunc(posts, func(a, b Post) int {
+		return b.Date.Compare(a.Date)
+	})
+	return posts, tags
+}
+
+func main() {
+	err := os.MkdirAll("public/post", 755)
+	check(err)
+	log.Print("Generating site...")
+	posts, tags := readPosts("content", "post", "public")
+	for _, post := range posts {
+		err := os.MkdirAll(path.Join("public", "post", post.Basename), 755)
+		check(err)
+		fmt.Printf("%+v\n", post.Date)
+	}
+	fmt.Printf("%+v\n", tags)
+}