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