commit 697d277
Artur Manuel
·
2026-05-09 01:03:41 +0000 UTC
parent 3a816a8
meta: write a bit of the SSG
6 files changed,
+122,
-0
A
Makefile
+15,
-0
1@@ -0,0 +1,15 @@
2+.POSIX:
3+
4+GO ?= go
5+
6+all:
7+ $(GO) build ./cmd/generate_pages
8+
9+clean:
10+ rm generate_pages
11+
12+lint:
13+ $(GO) fmt ./...
14+ $(GO) vet ./...
15+ $(GO) fix ./...
16+ staticcheck ./...
+61,
-0
1@@ -0,0 +1,61 @@
2+package main
3+
4+import (
5+ "context"
6+ "fmt"
7+ "os"
8+ "path/filepath"
9+ "strings"
10+
11+ "github.com/urfave/cli/v3"
12+
13+ "codeberg.org/dani-institute/pages/pkg/ssg"
14+)
15+
16+var cmd = cli.Command{
17+ Name: "generate_pages",
18+ Version: "2026.5.0",
19+ Flags: []cli.Flag{
20+ &cli.StringFlag{
21+ Name: "pages-dir",
22+ Required: true,
23+ },
24+ &cli.StringFlag{
25+ Name: "output-dir",
26+ Required: true,
27+ },
28+ },
29+ Action: func(ctx context.Context, c *cli.Command) error {
30+ pagesDir := c.String("pages-dir")
31+ outputDir := c.String("output-dir")
32+ if _, err := os.ReadDir(outputDir); os.IsNotExist(err) {
33+ if err := os.MkdirAll(outputDir, 0o700); err != nil {
34+ return err
35+ }
36+ }
37+ pages, err := os.ReadDir(pagesDir)
38+ if err != nil {
39+ return err
40+ }
41+ for _, page := range pages {
42+ name := page.Name()
43+ pagePath := filepath.Join(pagesDir, name)
44+ outputPath := filepath.Join(outputDir, strings.ReplaceAll(name, ".md", ".html"))
45+ w, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0o600)
46+ if err != nil {
47+ return err
48+ }
49+ if err := ssg.BuildTemplate(name, pagePath, w, nil); err != nil {
50+ return err
51+ }
52+ }
53+ return nil
54+ },
55+}
56+
57+func main() {
58+ if err := cmd.Run(context.Background(), os.Args); err != nil {
59+ fmt.Fprintf(os.Stderr, "%v\n", err)
60+ os.Exit(1)
61+ }
62+}
A
go.mod
+8,
-0
1@@ -0,0 +1,8 @@
2+module codeberg.org/dani-institute/pages
3+
4+go 1.26.2
5+
6+require (
7+ github.com/urfave/cli/v3 v3.8.0 // indirect
8+ github.com/yuin/goldmark v1.8.2 // indirect
9+)
A
go.sum
+4,
-0
1@@ -0,0 +1,4 @@
2+github.com/urfave/cli/v3 v3.8.0 h1:XqKPrm0q4P0q5JpoclYoCAv0/MIvH/jZ2umzuf8pNTI=
3+github.com/urfave/cli/v3 v3.8.0/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
4+github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
5+github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
+4,
-0
1@@ -0,0 +1,4 @@
2+---
3+---
4+
5+# Hello :)
+30,
-0
1@@ -0,0 +1,30 @@
2+package ssg
3+
4+import (
5+ "bytes"
6+ "io"
7+ "text/template"
8+
9+ "github.com/yuin/goldmark"
10+ "github.com/yuin/goldmark/parser"
11+ "github.com/yuin/goldmark/renderer/html"
12+)
13+
14+func BuildTemplate(name string, fn string, output io.Writer, data any) error {
15+ masterTemplate, err := template.New(name).ParseFiles(fn)
16+ if err != nil {
17+ return err
18+ }
19+ var b bytes.Buffer
20+ if err := masterTemplate.ExecuteTemplate(&b, name, data); err != nil {
21+ return err
22+ }
23+ gm := goldmark.New(
24+ goldmark.WithParserOptions(parser.WithAutoHeadingID()),
25+ goldmark.WithRendererOptions(html.WithXHTML()),
26+ )
27+ if err := gm.Convert(b.Bytes(), output); err != nil {
28+ return err
29+ }
30+ return nil
31+}