aboutsummaryrefslogtreecommitdiffstats
path: root/modules/highlight
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-08-23 22:23:39 +0100
committerGitHub <noreply@github.com>2021-08-23 22:23:39 +0100
commitf31e7a67cfc9662af2ae2dab3f4687457545f60c (patch)
tree532a5e37ba751e710e1ee6ce271e2ac49026c6e7 /modules/highlight
parent73defbbd1c070930563f17daa9e540c44a127928 (diff)
downloadgitea-f31e7a67cfc9662af2ae2dab3f4687457545f60c.tar.gz
gitea-f31e7a67cfc9662af2ae2dab3f4687457545f60c.zip
Just use a slice when rendering file (#16774)
Highlight currently uses a map which is memory inefficient. Switch to use a slice instead. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/highlight')
-rw-r--r--modules/highlight/highlight.go22
-rw-r--r--modules/highlight/highlight_test.go58
2 files changed, 39 insertions, 41 deletions
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go
index ed0548578a..079f7a44bd 100644
--- a/modules/highlight/highlight.go
+++ b/modules/highlight/highlight.go
@@ -116,8 +116,8 @@ func Code(fileName, code string) string {
return strings.TrimSuffix(htmlbuf.String(), "\n")
}
-// File returns map with line lumbers and HTML version of code with chroma syntax highlighting classes
-func File(numLines int, fileName string, code []byte) map[int]string {
+// File returns a slice of chroma syntax highlighted lines of code
+func File(numLines int, fileName string, code []byte) []string {
NewContext()
if len(code) > sizeLimit {
@@ -171,9 +171,8 @@ func File(numLines int, fileName string, code []byte) map[int]string {
finalNewLine = code[len(code)-1] == '\n'
}
- m := make(map[int]string, numLines)
- for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
- line := k + 1
+ m := make([]string, 0, numLines)
+ for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
content := string(v)
//need to keep lines that are only \n so copy/paste works properly in browser
if content == "" {
@@ -183,26 +182,25 @@ func File(numLines int, fileName string, code []byte) map[int]string {
}
content = strings.TrimSuffix(content, `<span class="w">`)
content = strings.TrimPrefix(content, `</span>`)
- m[line] = content
+ m = append(m, content)
}
if finalNewLine {
- m[numLines+1] = "<span class=\"w\">\n</span>"
+ m = append(m, "<span class=\"w\">\n</span>")
}
return m
}
// return unhiglighted map
-func plainText(code string, numLines int) map[int]string {
- m := make(map[int]string, numLines)
- for k, v := range strings.SplitN(string(code), "\n", numLines) {
- line := k + 1
+func plainText(code string, numLines int) []string {
+ m := make([]string, 0, numLines)
+ for _, v := range strings.SplitN(string(code), "\n", numLines) {
content := string(v)
//need to keep lines that are only \n so copy/paste works properly in browser
if content == "" {
content = "\n"
}
- m[line] = gohtml.EscapeString(content)
+ m = append(m, gohtml.EscapeString(content))
}
return m
}
diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go
index 7c5afaa52c..0a67e4c602 100644
--- a/modules/highlight/highlight_test.go
+++ b/modules/highlight/highlight_test.go
@@ -19,7 +19,7 @@ func TestFile(t *testing.T) {
numLines int
fileName string
code string
- want map[int]string
+ want []string
}{
{
name: ".drone.yml",
@@ -38,22 +38,22 @@ steps:
- go build -v
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
`,
- want: map[int]string{
- 1: `<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
- 2: `<span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>`,
- 3: `<span class="w">
+ want: []string{
+ `<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
+ `<span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>`,
+ `<span class="w">
</span>`,
- 4: `<span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
- 5: `<span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>`,
- 6: `<span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>`,
- 7: `<span class="w"> </span><span class="nt">environment</span><span class="p">:</span>`,
- 8: `<span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>`,
- 9: `<span class="w"> </span><span class="nt">commands</span><span class="p">:</span>`,
- 10: `<span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>`,
- 11: `<span class="w"> </span>- <span class="l">go build -v</span>`,
- 12: `<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
+ `<span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
+ `<span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>`,
+ `<span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>`,
+ `<span class="w"> </span><span class="nt">environment</span><span class="p">:</span>`,
+ `<span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>`,
+ `<span class="w"> </span><span class="nt">commands</span><span class="p">:</span>`,
+ `<span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>`,
+ `<span class="w"> </span>- <span class="l">go build -v</span>`,
+ `<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
</span>`,
- 13: `<span class="w">
+ `<span class="w">
</span>`,
},
},
@@ -74,21 +74,21 @@ steps:
- go build -v
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
`,
- want: map[int]string{
- 1: `<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
- 2: `<span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>`,
- 3: `<span class="w">
+ want: []string{
+ `<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
+ `<span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>`,
+ `<span class="w">
</span>`,
- 4: `<span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
- 5: `<span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>`,
- 6: `<span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>`,
- 7: `<span class="w"> </span><span class="nt">environment</span><span class="p">:</span>`,
- 8: `<span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>`,
- 9: `<span class="w"> </span><span class="nt">commands</span><span class="p">:</span>`,
- 10: `<span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>`,
- 11: `<span class="w"> </span>- <span class="l">go build -v</span>`,
- 12: `<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>`,
- 13: `<span class="w"> </span>`,
+ `<span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
+ `<span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>`,
+ `<span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>`,
+ `<span class="w"> </span><span class="nt">environment</span><span class="p">:</span>`,
+ `<span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>`,
+ `<span class="w"> </span><span class="nt">commands</span><span class="p">:</span>`,
+ `<span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>`,
+ `<span class="w"> </span>- <span class="l">go build -v</span>`,
+ `<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>`,
+ `<span class="w"> </span>`,
},
},
}