From 4536e9e5dd826eb0e69c930312ba8f66e9c16175 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 14 Apr 2024 20:11:14 +0200 Subject: wip: read posts --- flake.lock | 50 +++++++++++++++++++++-------- flake.nix | 20 +++++++++--- go.mod | 13 ++++++++ go.sum | 16 ++++++++++ src/build.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 18 deletions(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 src/build.go diff --git a/flake.lock b/flake.lock index 34ed9ad..b048c4e 100644 --- a/flake.lock +++ b/flake.lock @@ -1,6 +1,6 @@ { "nodes": { - "flake-compat": { + "compat": { "flake": false, "locked": { "lastModified": 1696426674, @@ -16,21 +16,26 @@ "type": "github" } }, - "flake-utils": { + "gomod2nix": { "inputs": { - "systems": "systems" + "flake-utils": [ + "utils" + ], + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "lastModified": 1710154385, + "narHash": "sha256-4c3zQ2YY4BZOufaBJB4v9VBBeN2dH7iVdoJw8SDNCfI=", + "owner": "tweag", + "repo": "gomod2nix", + "rev": "872b63ddd28f318489c929d25f1f0a3c6039c971", "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", + "owner": "tweag", + "repo": "gomod2nix", "type": "github" } }, @@ -52,9 +57,10 @@ }, "root": { "inputs": { - "flake-compat": "flake-compat", - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "compat": "compat", + "gomod2nix": "gomod2nix", + "nixpkgs": "nixpkgs", + "utils": "utils" } }, "systems": { @@ -71,6 +77,24 @@ "repo": "default", "type": "github" } + }, + "utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 67bcc1f..2faa85b 100644 --- a/flake.nix +++ b/flake.nix @@ -1,14 +1,19 @@ { description = "My website, alanpearce.eu"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - inputs.flake-utils.url = "github:numtide/flake-utils"; - inputs.flake-compat = { + inputs.utils.url = "github:numtide/flake-utils"; + inputs.compat = { url = "github:edolstra/flake-compat"; flake = false; }; + inputs.gomod2nix = { + url = "github:tweag/gomod2nix"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.flake-utils.follows = "utils"; + }; - outputs = { nixpkgs, flake-utils, ... }: - flake-utils.lib.eachDefaultSystem + outputs = { nixpkgs, utils, gomod2nix, ... }: + utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; @@ -19,13 +24,18 @@ gzip zstd git + go ]; in rec { devShells = { default = pkgs.mkShell { packages = with pkgs; [ - bun + gopls + gotools + go-tools + gomod2nix.packages.${system}.default + gci flyctl ] ++ nativeBuildInputs; }; diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5f15dec --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module alanpearce.eu/website + +go 1.22.1 + +require ( + github.com/BurntSushi/toml v1.2.1 // indirect + github.com/adrg/frontmatter v0.2.0 // indirect + github.com/deckarep/golang-set/v2 v2.6.0 // indirect + github.com/yuin/goldmark v1.7.1 // indirect + go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect + gopkg.in/yaml.v2 v2.3.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e0469b8 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4= +github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE= +github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM= +github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= +github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U= +github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= +go.abhg.dev/goldmark/frontmatter v0.2.0 h1:P8kPG0YkL12+aYk2yU3xHv4tcXzeVnN+gU0tJ5JnxRw= +go.abhg.dev/goldmark/frontmatter v0.2.0/go.mod h1:XqrEkZuM57djk7zrlRUB02x8I5J0px76YjkOzhB4YlU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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) +} -- cgit 1.4.1