From 3be25766027178489a0c5f1f82e65f9be518c79c Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 5 May 2024 19:35:41 +0200 Subject: feat: Add chroma support --- main.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 55f866e..3d6411d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "encoding/json" "flag" @@ -10,6 +11,10 @@ import ( "os/signal" "time" + "github.com/alecthomas/chroma/v2/formatters/html" + "github.com/alecthomas/chroma/v2/lexers" + "github.com/alecthomas/chroma/v2/styles" + "git.gabrielgio.me/cerrado/pkg/config" "git.gabrielgio.me/cerrado/pkg/worker" "git.gabrielgio.me/cerrado/templates" @@ -33,6 +38,7 @@ func run(ctx context.Context) error { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + slog.Info("Handling index") f, err := os.Open(*configPath) if err != nil { @@ -52,8 +58,26 @@ func run(ctx context.Context) error { return } + lexer := lexers.Get("json") + style := styles.Get("monokailight") + formatter := html.New( + html.WithLineNumbers(true), + ) + iterator, err := lexer.Tokenise(nil, string(b)) + if err != nil { + slog.Error("Error tokenise", "error", err) + return + } + + var code bytes.Buffer + err = formatter.Format(&code, style, iterator) + if err != nil { + slog.Error("Error format", "error", err) + return + } + hello := &templates.HelloPage{ - Body: string(b), + Body: code.String(), } templates.WritePageTemplate(w, hello) -- cgit v1.2.3