1package handler
2
3import (
4 "io"
5 "log/slog"
6 "net/http"
7 "os"
8
9 "github.com/gomarkdown/markdown"
10 "github.com/gomarkdown/markdown/html"
11 "github.com/gomarkdown/markdown/parser"
12
13 "git.gabrielgio.me/cerrado/templates"
14)
15
16type AboutHandler struct {
17 readmePath string
18}
19
20func NewAboutHandler(readmePath string) *AboutHandler {
21 return &AboutHandler{readmePath}
22}
23
24func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) {
25 f, err := os.Open(g.readmePath)
26 if err != nil {
27 slog.Error("Error loading readme file", "error", err)
28 return
29 }
30
31 bs, err := io.ReadAll(f)
32 if err != nil {
33 slog.Error("Error reading readme file bytes", "error", err)
34 return
35 }
36
37 extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
38 p := parser.NewWithExtensions(extensions)
39 doc := p.Parse(bs)
40
41 htmlFlags := html.CommonFlags | html.HrefTargetBlank
42 opts := html.RendererOptions{Flags: htmlFlags}
43 renderer := html.NewRenderer(opts)
44
45 bs = markdown.Render(doc, renderer)
46
47 gitList := &templates.HelloPage{
48 Body: bs,
49 }
50 templates.WritePageTemplate(w, gitList)
51}