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>
This commit is contained in:
parent
73defbbd1c
commit
f31e7a67cf
3 changed files with 41 additions and 42 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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>`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -99,7 +99,8 @@
|
|||
{{else}}
|
||||
<table>
|
||||
<tbody>
|
||||
{{range $line, $code := .FileContent}}
|
||||
{{range $idx, $code := .FileContent}}
|
||||
{{$line := Add $idx 1}}
|
||||
<tr>
|
||||
<td id="L{{$line}}" class="lines-num">
|
||||
<span id="L{{$line}}" data-line-number="{{$line}}"></span>
|
||||
|
|
Loading…
Reference in a new issue