Added syntax highlight

このコミットが含まれているのは:
Shiny Nematoda 2022-12-24 19:08:07 +00:00
コミット f772a4c2ae
5個のファイルの変更57行の追加8行の削除

4
go.mod
ファイルの表示

@ -3,17 +3,20 @@ module codeberg.org/gothub/gothub
go 1.19
require (
github.com/alecthomas/chroma/v2 v2.4.0
github.com/carlmjohnson/requests v0.22.3
github.com/gofiber/fiber/v2 v2.40.1
github.com/gofiber/template v1.7.3
github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c
github.com/microcosm-cc/bluemonday v1.0.21
github.com/spf13/cobra v1.6.1
github.com/tidwall/gjson v1.14.4
)
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.15.12 // indirect
@ -21,7 +24,6 @@ require (
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect

7
go.sum
ファイルの表示

@ -54,6 +54,10 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/assert/v2 v2.2.0 h1:f6L/b7KE2bfA+9O4FL3CM/xJccDEwPVYd5fALBiuwvw=
github.com/alecthomas/chroma/v2 v2.4.0 h1:Loe2ZjT5x3q1bcWwemqyqEi8p11/IV/ncFCeLYDpWC4=
github.com/alecthomas/chroma/v2 v2.4.0/go.mod h1:6kHzqF5O6FUSJzBXW7fXELjb+e+7OXW4UpoPqMO7IBQ=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@ -104,6 +108,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -243,6 +249,7 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn
github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=

ファイルの表示

@ -187,12 +187,12 @@ a:hover {
}
/* URI: /file/:user/:repo/:file */
.filePre {
.userReadme pre {
background-color: var(--background-darker);
color: var(--text);
padding: 8px;
border-radius: 8px;
white-space: pre-wrap;
/*white-space: pre-wrap;*/
overflow-x: auto;
}
@ -251,4 +251,4 @@ a:hover {
margin-bottom: 8px;
margin-right: 0;
}
}
}

ファイルの表示

@ -6,9 +6,16 @@ import (
"log"
"os"
"time"
"bytes"
"bufio"
"codeberg.org/gothub/gothub/pages"
"github.com/carlmjohnson/requests"
"github.com/alecthomas/chroma/v2/lexers"
htmlfmt "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/compress"
@ -105,8 +112,40 @@ func Serve() {
"error": err,
})
}
lexer := lexers.Match(c.Params("file"))
if lexer == nil {
lexer = lexers.Fallback
}
formatter := htmlfmt.New(
htmlfmt.WithClasses(true),
htmlfmt.WithLineNumbers(true),
htmlfmt.PreventSurroundingPre(false),
)
buf := bytes.Buffer{}
writer := bufio.NewWriter(&buf)
style := styles.Get("native")
tokens, err := lexer.Tokenise(nil, file)
if err != nil {
return c.Status(500).Render("error", fiber.Map{
"error": err,
})
}
formatter.Format(writer, style, tokens)
cssbuf := bytes.Buffer{}
cssw := bufio.NewWriter(&cssbuf)
formatter.WriteCSS(cssw, style)
writer.Flush()
cssw.Flush()
return c.Render("file", fiber.Map{
"file": file,
"file": template.HTML(buf.String()),
"css": template.CSS(cssbuf.String()),
"fullname": c.Params("user") + "/" + c.Params("repo"),
"name": c.Params("file"),
"branch": c.Params("branch"),

ファイルの表示

@ -2,16 +2,17 @@
<main>
{{ if .file }}
<style>{{ .css }}</style>
<div class="downloadParent">
<a href="/{{.fullname}}" class="downloadButton">Back to {{.fullname}}</a>
</div>
<div class="userReadme">
<h3>{{.name}}</h3>
<pre class="filePre">{{ .file}}</pre>
{{ .file}}
</div>
<div class="downloadParent">
<a href="/raw/{{.fullname}}/{{.branch}}/{{.name}}" class="downloadButton">Download</a>
</div>
{{end}}
</main>
</main>