diff options
-rw-r--r-- | modules/highlight/highlight.go | 22 | ||||
-rw-r--r-- | modules/highlight/highlight_test.go | 58 | ||||
-rw-r--r-- | templates/repo/view_file.tmpl | 3 |
3 files changed, 41 insertions, 42 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>`, }, }, } diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index 8c73f1252b..b6198bd0e2 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -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> |