From 154f68bfd9ab4660dc764dbed6190289082b5c94 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 8 Feb 2025 22:26:39 +0100 Subject: feat: Add syntax highlight to diff --- pkg/handler/git/handler.go | 21 ++++++++++++++++++++- templates/gititemcommit.qtpl | 4 ++-- templates/gititemcommit.qtpl.go | 4 ++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 9c7ba5b..80f7de6 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -361,12 +361,31 @@ func (g *GitHandler) Commit(w http.ResponseWriter, r *http.Request) error { return err } + lexer := lexers.Get("diff") + style := styles.Get(g.config.GetSyntaxHighlight()) + + formatter := html.New( + html.WithLineNumbers(true), + html.WithLinkableLineNumbers(true, "L"), + ) + + iterator, err := lexer.Tokenise(nil, diff) + if err != nil { + return err + } + + var code bytes.Buffer + err = formatter.Format(&code, style, iterator) + if err != nil { + return err + } + gitList := &templates.GitItemPage{ Name: name, Ref: ref, GitItemBase: &templates.GitItemCommitPage{ Commit: commit, - Diff: diff, + Diff: code.Bytes(), }, } templates.WritePageTemplate(w, gitList, r.Context()) diff --git a/templates/gititemcommit.qtpl b/templates/gititemcommit.qtpl index d223315..ec67757 100644 --- a/templates/gititemcommit.qtpl +++ b/templates/gititemcommit.qtpl @@ -3,7 +3,7 @@ {% code type GitItemCommitPage struct { Commit *object.Commit - Diff string + Diff []byte } %} @@ -14,6 +14,6 @@ type GitItemCommitPage struct { {%= Commit(name, g.Commit, true) %}
-
{%s g.Diff %}
+
{%z= g.Diff %}
{% endfunc %} diff --git a/templates/gititemcommit.qtpl.go b/templates/gititemcommit.qtpl.go index 8048e58..7e458f3 100644 --- a/templates/gititemcommit.qtpl.go +++ b/templates/gititemcommit.qtpl.go @@ -23,7 +23,7 @@ var ( //line templates/gititemcommit.qtpl:4 type GitItemCommitPage struct { Commit *object.Commit - Diff string + Diff []byte } //line templates/gititemcommit.qtpl:10 @@ -73,7 +73,7 @@ func (g *GitItemCommitPage) StreamGitContent(qw422016 *qt422016.Writer, name, re
`)
 //line templates/gititemcommit.qtpl:17
-	qw422016.E().S(g.Diff)
+	qw422016.N().Z(g.Diff)
 //line templates/gititemcommit.qtpl:17
 	qw422016.N().S(`
-- cgit v1.2.3