aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-05-05 19:35:41 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-05-05 19:35:41 +0200
commit3be25766027178489a0c5f1f82e65f9be518c79c (patch)
tree2a9d8b2e63ae40d571b0ea899baf4c1567c15d3e /main.go
parentf5b5f72f7eaf14692b8036c3698037c647b2423a (diff)
downloadcerrado-3be25766027178489a0c5f1f82e65f9be518c79c.tar.gz
cerrado-3be25766027178489a0c5f1f82e65f9be518c79c.tar.bz2
cerrado-3be25766027178489a0c5f1f82e65f9be518c79c.zip
feat: Add chroma support
Diffstat (limited to 'main.go')
-rw-r--r--main.go26
1 files changed, 25 insertions, 1 deletions
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)