diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-07 23:02:54 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-07 23:02:54 +0200 |
commit | e9098e00fb6339b759df5b0df2e086cef8a7ce83 (patch) | |
tree | 97ada52833cf152cf347f418e24ccb5bc05fd5e4 /pkg/handler/git | |
parent | c7a8aa113a914e70e027fea93265c7232b865b5e (diff) | |
download | cerrado-e9098e00fb6339b759df5b0df2e086cef8a7ce83.tar.gz cerrado-e9098e00fb6339b759df5b0df2e086cef8a7ce83.tar.bz2 cerrado-e9098e00fb6339b759df5b0df2e086cef8a7ce83.zip |
feat: Rework some pages
Diffstat (limited to 'pkg/handler/git')
-rw-r--r-- | pkg/handler/git/handler.go | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 28cc99e..7bdf372 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -2,8 +2,10 @@ package git import ( "bytes" + "io" "log/slog" "net/http" + "os" "path/filepath" "git.gabrielgio.me/cerrado/pkg/ext" @@ -15,11 +17,15 @@ import ( "github.com/alecthomas/chroma/v2/styles" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" + "github.com/gomarkdown/markdown" + markdownhtml "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" ) type ( GitHandler struct { gitService gitService + readmePath string } gitService interface { @@ -31,11 +37,16 @@ type ( ListTags(name string) ([]*object.Tag, error) ListBranches(name string) ([]*plumbing.Reference, error) } + + configurationRepository interface { + GetRootReadme() string + } ) -func NewGitHandler(gitService gitService) *GitHandler { +func NewGitHandler(gitService gitService, confRepo configurationRepository) *GitHandler { return &GitHandler{ gitService: gitService, + readmePath: confRepo.GetRootReadme(), } } @@ -46,7 +57,32 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { return } - gitList := &templates.GitListPage{repos} + 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) + + htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank + opts := markdownhtml.RendererOptions{Flags: htmlFlag} + renderer := markdownhtml.NewRenderer(opts) + + bs = markdown.Render(doc, renderer) + + gitList := &templates.GitListPage{ + Respositories: repos, + About: bs, + } templates.WritePageTemplate(w, gitList) } |