diff options
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | handle_category_index.go | 2 | ||||
-rw-r--r-- | handle_repo_tree.go | 21 | ||||
-rw-r--r-- | static/style.css | 2 | ||||
-rw-r--r-- | templates/repo_tree_file.html | 2 |
6 files changed, 29 insertions, 4 deletions
@@ -13,9 +13,11 @@ require ( dario.cat/mergo v1.0.0 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect github.com/ProtonMail/go-crypto v1.1.5 // indirect + github.com/alecthomas/chroma/v2 v2.15.0 // indirect github.com/aymerick/douceur v0.2.0 // indirect github.com/cloudflare/circl v1.3.7 // indirect github.com/cyphar/filepath-securejoin v0.3.6 // indirect + github.com/dlclark/regexp2 v1.11.4 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.6.2 // indirect @@ -5,6 +5,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v1.1.5 h1:eoAQfK2dwL+tFSFpr7TbOaPNUbPiJj4fLYwwGE1FQO4= github.com/ProtonMail/go-crypto v1.1.5/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE= +github.com/alecthomas/chroma/v2 v2.15.0 h1:LxXTQHFoYrstG2nnV9y2X5O94sOBzf0CIUpSTbpxvMc= +github.com/alecthomas/chroma/v2 v2.15.0/go.mod h1:gUhVLrPDXPtp/f+L1jo9xepo9gL4eLwRuGAunSZMkio= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -18,6 +20,8 @@ github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo= +github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/elazarl/goproxy v1.4.0 h1:4GyuSbFa+s26+3rmYNSuUVsx+HgPrV1bk1jXI0l9wjM= github.com/elazarl/goproxy v1.4.0/go.mod h1:X/5W/t+gzDyLfHW4DrMdpjqYjpXsURlBt9lpBDxZZZQ= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= diff --git a/handle_category_index.go b/handle_category_index.go index 953557a..d81899a 100644 --- a/handle_category_index.go +++ b/handle_category_index.go @@ -2,8 +2,8 @@ package main import ( "net/http" - "path/filepath" "os" + "path/filepath" "strings" ) diff --git a/handle_repo_tree.go b/handle_repo_tree.go index cc79202..c2810d9 100644 --- a/handle_repo_tree.go +++ b/handle_repo_tree.go @@ -7,6 +7,9 @@ import ( "path/filepath" "strings" + chroma_formatters_html "github.com/alecthomas/chroma/v2/formatters/html" + chroma_lexers "github.com/alecthomas/chroma/v2/lexers" + chroma_styles "github.com/alecthomas/chroma/v2/styles" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/microcosm-cc/bluemonday" @@ -63,11 +66,27 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("Error retrieving path: " + err.Error())) return } - data["file_contents"], err = file.Contents() + file_contents, err := file.Contents() if err != nil { _, _ = w.Write([]byte("Error reading file: " + err.Error())) return } + lexer := chroma_lexers.Match(path_spec) + if lexer == nil { + lexer = chroma_lexers.Fallback + } + iterator, err := lexer.Tokenise(nil, file_contents) + if err != nil { + _, _ = w.Write([]byte("Error rendering code: " + err.Error())) + return + } + var formatted_unencapsulated bytes.Buffer + style := chroma_styles.Get("emacs") + formatter := chroma_formatters_html.New(chroma_formatters_html.WithClasses(true), chroma_formatters_html.TabWidth(8)) + formatter.Format(&formatted_unencapsulated, style, iterator) + formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) + data["file_contents"] = formatted_encapsulated + err = templates.ExecuteTemplate(w, "repo_tree_file", data) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) diff --git a/static/style.css b/static/style.css index e816df1..bde80a8 100644 --- a/static/style.css +++ b/static/style.css @@ -20,7 +20,7 @@ a:link, a:visited { text-decoration-color: var(--text-decoration-color); color: var(--link-color); } -code { +code:not(pre > code) { background-color: var(--box-background-color); border-radius: 2px; padding: 2px; diff --git a/templates/repo_tree_file.html b/templates/repo_tree_file.html index 72bf42b..d561485 100644 --- a/templates/repo_tree_file.html +++ b/templates/repo_tree_file.html @@ -9,7 +9,7 @@ <p> /{{ .path_spec }} </p> -<pre>{{ .file_contents }}</pre> +{{ .file_contents }} </body> </html> {{- end -}} |