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 --- go.mod | 6 +++++- go.sum | 10 ++++++++++ main.go | 26 +++++++++++++++++++++++++- templates/helloworld.qtpl | 2 +- templates/helloworld.qtpl.go | 2 +- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index b8b36b4..ff4f2dd 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,13 @@ go 1.22.2 require ( git.sr.ht/~emersion/go-scfg v0.0.0-20240128091534-2ae16e782082 + github.com/alecthomas/chroma/v2 v2.13.0 github.com/google/go-cmp v0.6.0 github.com/valyala/quicktemplate v1.7.0 golang.org/x/sync v0.7.0 ) -require github.com/valyala/bytebufferpool v1.0.0 // indirect +require ( + github.com/dlclark/regexp2 v1.11.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect +) diff --git a/go.sum b/go.sum index 0ba6fdb..15e574a 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,22 @@ git.sr.ht/~emersion/go-scfg v0.0.0-20240128091534-2ae16e782082 h1:9Udx5fm4vRtmgDIBjy2ef5QioHbzpw5oHabbhpAUyEw= git.sr.ht/~emersion/go-scfg v0.0.0-20240128091534-2ae16e782082/go.mod h1:ybgvEJTIx5XbaspSviB3KNa6OdPmAZqDoSud7z8fFlw= +github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU= +github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= +github.com/alecthomas/chroma/v2 v2.13.0 h1:VP72+99Fb2zEcYM0MeaWJmV+xQvz5v5cxRHd+ooU1lI= +github.com/alecthomas/chroma/v2 v2.13.0/go.mod h1:BUGjjsD+ndS6eX37YgTchSEG+Jg9Jv1GiZs9sqPqztk= +github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= +github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 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) diff --git a/templates/helloworld.qtpl b/templates/helloworld.qtpl index 02c0968..66287ea 100644 --- a/templates/helloworld.qtpl +++ b/templates/helloworld.qtpl @@ -9,7 +9,7 @@ type HelloPage struct { {% func (p *HelloPage) Content() %} HelloWorld -{%s p.Body %} +{%s= p.Body %} {% endfunc %} {% func (p *HelloPage) Script() %} diff --git a/templates/helloworld.qtpl.go b/templates/helloworld.qtpl.go index 9ef78f5..a12455f 100644 --- a/templates/helloworld.qtpl.go +++ b/templates/helloworld.qtpl.go @@ -63,7 +63,7 @@ HelloWorld `) //line helloworld.qtpl:12 - qw422016.E().S(p.Body) + qw422016.N().S(p.Body) //line helloworld.qtpl:12 qw422016.N().S(` `) -- cgit v1.2.3