commit 8f20beb
Artur Manuel
·
2026-05-10 14:31:36 +0000 UTC
parent bfc7001
ssg: actually generate pages
4 files changed,
+35,
-18
+10,
-6
1@@ -2,9 +2,10 @@ package main
2
3 import (
4 "context"
5- "fmt"
6+ "log/slog"
7 "os"
8 "path/filepath"
9+ "strings"
10
11 "codeberg.org/dani-institute/pages/pkg/ssg"
12 "github.com/urfave/cli/v3"
13@@ -42,6 +43,7 @@ var cmd = cli.Command{
14 }
15
16 func mainAction(ctx context.Context, c *cli.Command) error {
17+ slog.Info("generating pages")
18 var config ssg.Config
19 baseUrl := c.String("base-url")
20 _ = baseUrl
21@@ -67,13 +69,15 @@ func mainAction(ctx context.Context, c *cli.Command) error {
22 goldmark.WithExtensions(meta.Meta),
23 goldmark.WithParserOptions(parser.WithAutoHeadingID()),
24 )
25- for name, page := range pages {
26- path := filepath.Join(outputDir, name) + ".html"
27- f, err := os.Create(path)
28+ for path, page := range pages {
29+ name := strings.TrimSuffix(filepath.Base(path), ".md")
30+ slog.Info("building page", "page", name)
31+ outputPath := filepath.Join(outputDir, name) + ".html"
32+ f, err := os.Create(outputPath)
33 if err != nil {
34 return err
35 }
36- if err := ssg.BuildPage(f, name, page, markdown, templates, config, baseUrl); err != nil {
37+ if err := ssg.BuildPage(f, name, path, page, markdown, templates, config, baseUrl); err != nil {
38 return err
39 }
40 }
41@@ -82,7 +86,7 @@ func mainAction(ctx context.Context, c *cli.Command) error {
42
43 func main() {
44 if err := cmd.Run(context.Background(), os.Args); err != nil {
45- fmt.Fprintf(os.Stderr, "%v\n", err)
46+ slog.Error("got err while generating pages", "err", err)
47 os.Exit(1)
48 }
49 }
+1,
-0
1@@ -1,4 +1,5 @@
2 ---
3+description: Test
4 ---
5
6 # {{ .Extra.test }} :)
+19,
-11
1@@ -37,13 +37,12 @@ func GetPages(dir string) (map[string]string, error) {
2 return filepath.SkipDir
3 }
4 if !d.IsDir() && strings.HasSuffix(path, ".md") {
5- name := strings.TrimSuffix(filepath.Base(path), ".md")
6 content, err := os.ReadFile(path)
7 if err != nil {
8 slog.Error("got err while reading file", "file", path, "err", err)
9 return filepath.SkipDir
10 }
11- res[name] = string(content)
12+ res[path] = string(content)
13 }
14 return nil
15 })
16@@ -53,31 +52,40 @@ func GetPages(dir string) (map[string]string, error) {
17 return res, nil
18 }
19
20-func BuildPage(w io.Writer, name string, page string, markdown goldmark.Markdown, templates Templates, config Config, baseUrl string) error {
21+func BuildPage(w io.Writer, name string, path string, page string, markdown goldmark.Markdown, templates Templates, config Config, baseUrl string) error {
22 var buff bytes.Buffer
23 ctx := parser.NewContext()
24- if err := goldmark.Convert([]byte(page), &buff, parser.WithContext(ctx)); err != nil {
25+ if err := markdown.Convert([]byte(page), &buff, parser.WithContext(ctx)); err != nil {
26 return err
27 }
28 frontmatter := meta.Get(ctx)
29- templateName, ok := frontmatter["template"].(string)
30- if !ok {
31+ var templateName string
32+ switch v := frontmatter["title"]; v {
33+ case nil:
34 if name == "index" {
35 templateName = "index"
36 } else {
37 templateName = "page"
38 }
39+ default:
40+ templateName = v.(string)
41 }
42 if baseUrl == "" {
43 baseUrl = config.BaseURL
44 }
45- title := frontmatter["title"].(string)
46- if !ok {
47+ var title string
48+ switch v := frontmatter["title"]; v {
49+ case nil:
50 title = config.Title
51+ default:
52+ title = v.(string)
53 }
54- description, ok := frontmatter["description"].(string)
55- if !ok {
56+ var description string
57+ switch v := frontmatter["description"]; v {
58+ case nil:
59 description = config.Description
60+ default:
61+ description = v.(string)
62 }
63 templateData := TemplateData{
64 Title: title,
65@@ -87,7 +95,7 @@ func BuildPage(w io.Writer, name string, page string, markdown goldmark.Markdown
66 Config: config,
67 Content: buff.String(),
68 }
69- if err := BuildTemplate(w, name, templates[templateName], templateData); err != nil {
70+ if err := BuildTemplate(w, path, templates[templateName], templateData); err != nil {
71 return err
72 }
73 return nil
+5,
-1
1@@ -1,11 +1,11 @@
2 package ssg
3
4 import (
5- "html/template"
6 "io"
7 "os"
8 "path/filepath"
9 "strings"
10+ "text/template"
11 )
12
13 type TemplateData struct {
14@@ -35,6 +35,10 @@ func BuildTemplate(w io.Writer, name string, t string, data TemplateData) error
15 if err != nil {
16 return err
17 }
18+ _, err = master.New("content").Parse(data.Content)
19+ if err != nil {
20+ return err
21+ }
22 if err := master.ExecuteTemplate(w, name, data); err != nil {
23 return err
24 }