commit 6f5b0fe

Artur Manuel  ·  2026-05-09 13:48:21 +0000 UTC
parent 697d277
ssg: add config support
7 files changed,  +38, -4
M go.mod
M go.sum
+15, -1
 1@@ -7,6 +7,7 @@ import (
 2 	"path/filepath"
 3 	"strings"
 4 
 5+	"github.com/BurntSushi/toml"
 6 	"github.com/urfave/cli/v3"
 7 
 8 	"codeberg.org/dani-institute/pages/pkg/ssg"
 9@@ -24,10 +25,23 @@ var cmd = cli.Command{
10 			Name:     "output-dir",
11 			Required: true,
12 		},
13+		&cli.StringFlag{
14+			Name:  "config",
15+			Value: "config.toml",
16+		},
17 	},
18 	Action: func(ctx context.Context, c *cli.Command) error {
19 		pagesDir := c.String("pages-dir")
20 		outputDir := c.String("output-dir")
21+		configFile := c.String("config")
22+		config := ssg.DefaultConfig()
23+		configBytes, err := os.ReadFile(configFile)
24+		if err != nil && os.IsExist(err) {
25+			return err
26+		}
27+		if err := toml.Unmarshal(configBytes, &config); err != nil {
28+			return err
29+		}
30 		if _, err := os.ReadDir(outputDir); os.IsNotExist(err) {
31 			if err := os.MkdirAll(outputDir, 0o700); err != nil {
32 				return err
33@@ -45,7 +59,7 @@ var cmd = cli.Command{
34 			if err != nil {
35 				return err
36 			}
37-			if err := ssg.BuildTemplate(name, pagePath, w, nil); err != nil {
38+			if err := ssg.BuildTemplate(name, pagePath, w, config); err != nil {
39 				return err
40 			}
41 		}
+4, -0
1@@ -0,0 +1,4 @@
2+title = "The Dani Institute"
3+
4+[extra]
5+test = "hi"
M go.mod
+1, -0
1@@ -3,6 +3,7 @@ module codeberg.org/dani-institute/pages
2 go 1.26.2
3 
4 require (
5+	github.com/BurntSushi/toml v1.6.0 // indirect
6 	github.com/urfave/cli/v3 v3.8.0 // indirect
7 	github.com/yuin/goldmark v1.8.2 // indirect
8 )
M go.sum
+2, -0
1@@ -1,3 +1,5 @@
2+github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
3+github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
4 github.com/urfave/cli/v3 v3.8.0 h1:XqKPrm0q4P0q5JpoclYoCAv0/MIvH/jZ2umzuf8pNTI=
5 github.com/urfave/cli/v3 v3.8.0/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
6 github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
+1, -1
1@@ -1,4 +1,4 @@
2 ---
3 ---
4 
5-# Hello :)
6+# {{ .Extra.test }} :)
+2, -2
 1@@ -10,13 +10,13 @@ import (
 2 	"github.com/yuin/goldmark/renderer/html"
 3 )
 4 
 5-func BuildTemplate(name string, fn string, output io.Writer, data any) error {
 6+func BuildTemplate(name string, fn string, output io.Writer, config Config) error {
 7 	masterTemplate, err := template.New(name).ParseFiles(fn)
 8 	if err != nil {
 9 		return err
10 	}
11 	var b bytes.Buffer
12-	if err := masterTemplate.ExecuteTemplate(&b, name, data); err != nil {
13+	if err := masterTemplate.ExecuteTemplate(&b, name, config); err != nil {
14 		return err
15 	}
16 	gm := goldmark.New(
+13, -0
 1@@ -0,0 +1,13 @@
 2+package ssg
 3+
 4+type Config struct {
 5+	Title string         `toml:"title"`
 6+	Extra map[string]any `toml:"extra"`
 7+}
 8+
 9+func DefaultConfig() Config {
10+	return Config{
11+		Title: "Default title",
12+		Extra: map[string]any{},
13+	}
14+}