1package about
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 (
17 AboutHandler struct {
18 readmePath string
19 }
20
21 configurationRepository interface {
22 GetRootReadme() string
23 }
24)
25
26func NewAboutHandler(configRepo configurationRepository) *AboutHandler {
27 return &AboutHandler{configRepo.GetRootReadme()}
28}
29
30func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) {
31 f, err := os.Open(g.readmePath)
32 if err != nil {
33 slog.Error("Error loading readme file", "error", err)
34 return
35 }
36
37 bs, err := io.ReadAll(f)
38 if err != nil {
39 slog.Error("Error reading readme file bytes", "error", err)
40 return
41 }
42
43 extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
44 p := parser.NewWithExtensions(extensions)
45 doc := p.Parse(bs)
46
47 htmlFlags := html.CommonFlags | html.HrefTargetBlank
48 opts := html.RendererOptions{Flags: htmlFlags}
49 renderer := html.NewRenderer(opts)
50
51 bs = markdown.Render(doc, renderer)
52
53 gitList := &templates.AboutPage{
54 Body: bs,
55 }
56 templates.WritePageTemplate(w, gitList)
57}