commit
message
Cleaning up syntax highlighting utils
author
Ben Vogt <[email protected]>
date
2023-04-04 20:47:09
stats
3 file(s) changed,
36 insertions(+),
47 deletions(-)
files
commit.go
file.go
main.go
1diff --git a/commit.go b/commit.go
2index 45a6717..da1354e 100644
3--- a/commit.go
4+++ b/commit.go
5@@ -1,15 +1,11 @@
6 package main
7
8 import (
9- "bytes"
10 "errors"
11 "html/template"
12 "os"
13 "path"
14
15- "github.com/alecthomas/chroma/formatters/html"
16- "github.com/alecthomas/chroma/lexers"
17- "github.com/alecthomas/chroma/styles"
18 "github.com/go-git/go-git/v5"
19 "github.com/go-git/go-git/v5/plumbing/object"
20 )
21@@ -58,28 +54,13 @@ func RenderAllCommitPages(r *git.Repository) {
22 filesChangedMap := make(map[string]bool)
23 filesChanged := []string{}
24 if parent != nil {
25- lexer := lexers.Match("x.diff")
26- if lexer == nil {
27- lexer = lexers.Fallback
28- }
29- style := styles.Get("borland")
30- if style == nil {
31- style = styles.Fallback
32- }
33+
34 patch, err := c.Patch(parent)
35 checkErr(err)
36 patchString := patch.String()
37- iterator, err := lexer.Tokenise(nil, patchString)
38- formatter := html.New(
39- html.WithClasses(true),
40- html.WithLineNumbers(true),
41- html.LinkableLineNumbers(true, ""),
42- )
43- s := ""
44- buf := bytes.NewBufferString(s)
45- err = formatter.Format(buf, style, iterator)
46+ highlighted := highlight("x.diff", &patchString)
47 checkErr(err)
48- diffContent = template.HTML(buf.String())
49+ diffContent = template.HTML(highlighted)
50 for _, fp := range patch.FilePatches() {
51 from, to := fp.Files()
52 if from != nil {
53diff --git a/file.go b/file.go
54index 14c75bf..a31b6e8 100644
55--- a/file.go
56+++ b/file.go
57@@ -1,17 +1,12 @@
58 package main
59
60 import (
61- "bytes"
62 "html/template"
63 "io/fs"
64 "os"
65 "path"
66 "path/filepath"
67 "strings"
68-
69- "github.com/alecthomas/chroma/formatters/html"
70- "github.com/alecthomas/chroma/lexers"
71- "github.com/alecthomas/chroma/styles"
72 )
73
74 type FilePage struct {
75@@ -28,31 +23,15 @@ type FilePage struct {
76 }
77
78 func (f *FilePage) Render(t *template.Template) {
79- lexer := lexers.Match(f.DestinationDir)
80- if lexer == nil {
81- lexer = lexers.Fallback
82- }
83- style := styles.Get("borland")
84- if style == nil {
85- style = styles.Fallback
86- }
87 err := os.MkdirAll(f.DestinationDir, 0775)
88 checkErr(err)
89 if f.CanRender {
90 fileBytes, err := os.ReadFile(f.Origin)
91 checkErr(err)
92 fileStr := string(fileBytes)
93- iterator, err := lexer.Tokenise(nil, fileStr)
94- formatter := html.New(
95- html.WithClasses(true),
96- html.WithLineNumbers(true),
97- html.LinkableLineNumbers(true, ""),
98- )
99- s := ""
100- buf := bytes.NewBufferString(s)
101- err = formatter.Format(buf, style, iterator)
102+ highlighted := highlight(f.DestinationDir, &fileStr)
103 checkErr(err)
104- f.Content = template.HTML(buf.String())
105+ f.Content = template.HTML(highlighted)
106 }
107 err = os.MkdirAll(filepath.Dir(f.Destination), 0775)
108 checkErr(err)
109diff --git a/main.go b/main.go
110index 5647f56..8997465 100644
111--- a/main.go
112+++ b/main.go
113@@ -1,6 +1,7 @@
114 package main
115
116 import (
117+ "bytes"
118 "embed"
119 _ "embed"
120 "errors"
121@@ -10,6 +11,10 @@ import (
122 "os"
123 "path"
124
125+ "github.com/alecthomas/chroma"
126+ "github.com/alecthomas/chroma/formatters/html"
127+ "github.com/alecthomas/chroma/lexers"
128+ "github.com/alecthomas/chroma/styles"
129 "github.com/go-git/go-git/v5"
130 )
131
132@@ -154,3 +159,29 @@ func debug(format string, a ...any) {
133 fmt.Print("\n")
134 }
135 }
136+
137+func syntaxHighlightTools(pathOrExtension string) (chroma.Lexer, *chroma.Style, *html.Formatter) {
138+ lexer := lexers.Match(pathOrExtension)
139+ if lexer == nil {
140+ lexer = lexers.Fallback
141+ }
142+ style := styles.Get("borland")
143+ if style == nil {
144+ style = styles.Fallback
145+ }
146+ formatter := html.New(
147+ html.WithClasses(true),
148+ html.WithLineNumbers(true),
149+ html.LinkableLineNumbers(true, ""),
150+ )
151+ return lexer, style, formatter
152+}
153+
154+func highlight(pathOrExtension string, data *string) string {
155+ lexer, style, formatter := syntaxHighlightTools(pathOrExtension)
156+ iterator, err := lexer.Tokenise(nil, *data)
157+ buf := bytes.NewBufferString("")
158+ err = formatter.Format(buf, style, iterator)
159+ checkErr(err)
160+ return buf.String()
161+}