From 3d8637838e9ccfcb56899842945e760f337428b0 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 26 May 2024 22:14:56 +0200 Subject: feat: Add about page --- pkg/config/config.go | 14 +++++++++++++- pkg/handler/about.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/handler/status.go | 2 +- pkg/u/util.go | 5 +++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 pkg/handler/about.go create mode 100644 pkg/u/util.go (limited to 'pkg') diff --git a/pkg/config/config.go b/pkg/config/config.go index 776c698..9b6acce 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -16,7 +16,8 @@ type ( } Configuration struct { - Scan *Scan + Scan *Scan + RootReadme string } ) @@ -33,6 +34,11 @@ func Parse(r io.Reader) (*Configuration, error) { return nil, err } + err = setRootReadme(block, &config.RootReadme) + if err != nil { + return nil, err + } + return config, nil } @@ -42,9 +48,15 @@ func defaultConfiguration() *Configuration { Public: true, Path: "", }, + RootReadme: "", } } +func setRootReadme(block scfg.Block, readme *string) error { + scanDir := block.Get("root-readme") + return setString(scanDir, readme) +} + func setScan(block scfg.Block, scan *Scan) error { scanDir := block.Get("scan") err := setString(scanDir, &scan.Path) diff --git a/pkg/handler/about.go b/pkg/handler/about.go new file mode 100644 index 0000000..3ab2de8 --- /dev/null +++ b/pkg/handler/about.go @@ -0,0 +1,51 @@ +package handler + +import ( + "io" + "log/slog" + "net/http" + "os" + + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" + + "git.gabrielgio.me/cerrado/templates" +) + +type AboutHandler struct { + readmePath string +} + +func NewAboutHandler(readmePath string) *AboutHandler { + return &AboutHandler{readmePath} +} + +func (g *AboutHandler) About(w http.ResponseWriter, _ *http.Request) { + f, err := os.Open(g.readmePath) + if err != nil { + slog.Error("Error loading readme file", "error", err) + return + } + + bs, err := io.ReadAll(f) + if err != nil { + slog.Error("Error reading readme file bytes", "error", err) + return + } + + extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock + p := parser.NewWithExtensions(extensions) + doc := p.Parse(bs) + + htmlFlags := html.CommonFlags | html.HrefTargetBlank + opts := html.RendererOptions{Flags: htmlFlags} + renderer := html.NewRenderer(opts) + + bs = markdown.Render(doc, renderer) + + gitList := &templates.HelloPage{ + Body: bs, + } + templates.WritePageTemplate(w, gitList) +} diff --git a/pkg/handler/status.go b/pkg/handler/status.go index 1ca7f70..9baac2c 100644 --- a/pkg/handler/status.go +++ b/pkg/handler/status.go @@ -54,7 +54,7 @@ func ConfigFile(configPath string) func(http.ResponseWriter, *http.Request) { } hello := &templates.HelloPage{ - Body: code.String(), + Body: code.Bytes(), } templates.WritePageTemplate(w, hello) diff --git a/pkg/u/util.go b/pkg/u/util.go new file mode 100644 index 0000000..9706539 --- /dev/null +++ b/pkg/u/util.go @@ -0,0 +1,5 @@ +package u + +func Ptr[T any](v T) *T { + return &v +} -- cgit v1.2.3