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")
|
return strings.TrimSuffix(htmlbuf.String(), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// File returns map with line lumbers and HTML version of code with chroma syntax highlighting classes
|
// File returns a slice of chroma syntax highlighted lines of code
|
||||||
func File(numLines int, fileName string, code []byte) map[int]string {
|
func File(numLines int, fileName string, code []byte) []string {
|
||||||
NewContext()
|
NewContext()
|
||||||
|
|
||||||
if len(code) > sizeLimit {
|
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'
|
finalNewLine = code[len(code)-1] == '\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
m := make(map[int]string, numLines)
|
m := make([]string, 0, numLines)
|
||||||
for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
|
for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
|
||||||
line := k + 1
|
|
||||||
content := string(v)
|
content := string(v)
|
||||||
//need to keep lines that are only \n so copy/paste works properly in browser
|
//need to keep lines that are only \n so copy/paste works properly in browser
|
||||||
if content == "" {
|
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.TrimSuffix(content, `<span class="w">`)
|
||||||
content = strings.TrimPrefix(content, `</span>`)
|
content = strings.TrimPrefix(content, `</span>`)
|
||||||
m[line] = content
|
m = append(m, content)
|
||||||
}
|
}
|
||||||
if finalNewLine {
|
if finalNewLine {
|
||||||
m[numLines+1] = "<span class=\"w\">\n</span>"
|
m = append(m, "<span class=\"w\">\n</span>")
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// return unhiglighted map
|
// return unhiglighted map
|
||||||
func plainText(code string, numLines int) map[int]string {
|
func plainText(code string, numLines int) []string {
|
||||||
m := make(map[int]string, numLines)
|
m := make([]string, 0, numLines)
|
||||||
for k, v := range strings.SplitN(string(code), "\n", numLines) {
|
for _, v := range strings.SplitN(string(code), "\n", numLines) {
|
||||||
line := k + 1
|
|
||||||
content := string(v)
|
content := string(v)
|
||||||
//need to keep lines that are only \n so copy/paste works properly in browser
|
//need to keep lines that are only \n so copy/paste works properly in browser
|
||||||
if content == "" {
|
if content == "" {
|
||||||
content = "\n"
|
content = "\n"
|
||||||
}
|
}
|
||||||
m[line] = gohtml.EscapeString(content)
|
m = append(m, gohtml.EscapeString(content))
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ func TestFile(t *testing.T) {
|
||||||
numLines int
|
numLines int
|
||||||
fileName string
|
fileName string
|
||||||
code string
|
code string
|
||||||
want map[int]string
|
want []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: ".drone.yml",
|
name: ".drone.yml",
|
||||||
|
@ -38,22 +38,22 @@ steps:
|
||||||
- go build -v
|
- go build -v
|
||||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||||
`,
|
`,
|
||||||
want: map[int]string{
|
want: []string{
|
||||||
1: `<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
|
`<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>`,
|
`<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">
|
`<span class="w">
|
||||||
</span>`,
|
</span>`,
|
||||||
4: `<span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
|
||||||
</span>`,
|
</span>`,
|
||||||
13: `<span class="w">
|
`<span class="w">
|
||||||
</span>`,
|
</span>`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -74,21 +74,21 @@ steps:
|
||||||
- go build -v
|
- go build -v
|
||||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||||
`,
|
`,
|
||||||
want: map[int]string{
|
want: []string{
|
||||||
1: `<span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
|
`<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>`,
|
`<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">
|
`<span class="w">
|
||||||
</span>`,
|
</span>`,
|
||||||
4: `<span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>`,
|
`<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>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>`,
|
||||||
13: `<span class="w"> </span>`,
|
`<span class="w"> </span>`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,7 +99,8 @@
|
||||||
{{else}}
|
{{else}}
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
{{range $line, $code := .FileContent}}
|
{{range $idx, $code := .FileContent}}
|
||||||
|
{{$line := Add $idx 1}}
|
||||||
<tr>
|
<tr>
|
||||||
<td id="L{{$line}}" class="lines-num">
|
<td id="L{{$line}}" class="lines-num">
|
||||||
<span id="L{{$line}}" data-line-number="{{$line}}"></span>
|
<span id="L{{$line}}" data-line-number="{{$line}}"></span>
|
||||||
|
|
Reference in a new issue