mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-29 16:03:59 +01:00
Adjust line detection in highlight.go (#20612)
The code for detection of lines in highlight.go is somewhat too complex and doesn't take account of how Chroma is actually splitting things into lines for us. Remove both the .line and .cl classes from Chroma's HTML which made the old conditional work again. This fixed Copy of YAML files while also reducing the amount of rendered HTML nodes. Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
07d140625e
commit
0747592865
2 changed files with 68 additions and 49 deletions
|
@ -26,7 +26,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// don't index files larger than this many bytes for performance purposes
|
// don't index files larger than this many bytes for performance purposes
|
||||||
const sizeLimit = 1000000
|
const sizeLimit = 1024 * 1024
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// For custom user mapping
|
// For custom user mapping
|
||||||
|
@ -58,7 +58,7 @@ func NewContext() {
|
||||||
func Code(fileName, language, code string) string {
|
func Code(fileName, language, code string) string {
|
||||||
NewContext()
|
NewContext()
|
||||||
|
|
||||||
// diff view newline will be passed as empty, change to literal \n so it can be copied
|
// diff view newline will be passed as empty, change to literal '\n' so it can be copied
|
||||||
// preserve literal newline in blame view
|
// preserve literal newline in blame view
|
||||||
if code == "" || code == "\n" {
|
if code == "" || code == "\n" {
|
||||||
return "\n"
|
return "\n"
|
||||||
|
@ -104,6 +104,11 @@ func Code(fileName, language, code string) string {
|
||||||
return CodeFromLexer(lexer, code)
|
return CodeFromLexer(lexer, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type nopPreWrapper struct{}
|
||||||
|
|
||||||
|
func (nopPreWrapper) Start(code bool, styleAttr string) string { return "" }
|
||||||
|
func (nopPreWrapper) End(code bool) string { return "" }
|
||||||
|
|
||||||
// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
|
// CodeFromLexer returns a HTML version of code string with chroma syntax highlighting classes
|
||||||
func CodeFromLexer(lexer chroma.Lexer, code string) string {
|
func CodeFromLexer(lexer chroma.Lexer, code string) string {
|
||||||
formatter := html.New(html.WithClasses(true),
|
formatter := html.New(html.WithClasses(true),
|
||||||
|
@ -126,9 +131,9 @@ func CodeFromLexer(lexer chroma.Lexer, code string) string {
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlw.Flush()
|
_ = htmlw.Flush()
|
||||||
// Chroma will add newlines for certain lexers in order to highlight them properly
|
// Chroma will add newlines for certain lexers in order to highlight them properly
|
||||||
// Once highlighted, strip them here so they don't cause copy/paste trouble in HTML output
|
// Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output
|
||||||
return strings.TrimSuffix(htmlbuf.String(), "\n")
|
return strings.TrimSuffix(htmlbuf.String(), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +146,7 @@ func File(numLines int, fileName, language string, code []byte) []string {
|
||||||
}
|
}
|
||||||
formatter := html.New(html.WithClasses(true),
|
formatter := html.New(html.WithClasses(true),
|
||||||
html.WithLineNumbers(false),
|
html.WithLineNumbers(false),
|
||||||
html.PreventSurroundingPre(true),
|
html.WithPreWrapper(nopPreWrapper{}),
|
||||||
)
|
)
|
||||||
|
|
||||||
if formatter == nil {
|
if formatter == nil {
|
||||||
|
@ -189,27 +194,19 @@ func File(numLines int, fileName, language string, code []byte) []string {
|
||||||
return plainText(string(code), numLines)
|
return plainText(string(code), numLines)
|
||||||
}
|
}
|
||||||
|
|
||||||
htmlw.Flush()
|
_ = htmlw.Flush()
|
||||||
finalNewLine := false
|
finalNewLine := false
|
||||||
if len(code) > 0 {
|
if len(code) > 0 {
|
||||||
finalNewLine = code[len(code)-1] == '\n'
|
finalNewLine = code[len(code)-1] == '\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
m := make([]string, 0, numLines)
|
m := strings.SplitN(htmlbuf.String(), `</span></span><span class="line"><span class="cl">`, numLines)
|
||||||
for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
|
if len(m) > 0 {
|
||||||
content := string(v)
|
m[0] = m[0][len(`<span class="line"><span class="cl">`):]
|
||||||
// need to keep lines that are only \n so copy/paste works properly in browser
|
last := m[len(m)-1]
|
||||||
if content == "" {
|
m[len(m)-1] = last[:len(last)-len(`</span></span>`)]
|
||||||
content = "\n"
|
|
||||||
} else if content == `</span><span class="w">` {
|
|
||||||
content += "\n</span>"
|
|
||||||
} else if content == `</span></span><span class="line"><span class="cl">` {
|
|
||||||
content += "\n"
|
|
||||||
}
|
|
||||||
content = strings.TrimSuffix(content, `<span class="w">`)
|
|
||||||
content = strings.TrimPrefix(content, `</span>`)
|
|
||||||
m = append(m, content)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if finalNewLine {
|
if finalNewLine {
|
||||||
m = append(m, "<span class=\"w\">\n</span>")
|
m = append(m, "<span class=\"w\">\n</span>")
|
||||||
}
|
}
|
||||||
|
@ -219,14 +216,14 @@ func File(numLines int, fileName, language string, code []byte) []string {
|
||||||
|
|
||||||
// return unhiglighted map
|
// return unhiglighted map
|
||||||
func plainText(code string, numLines int) []string {
|
func plainText(code string, numLines int) []string {
|
||||||
m := make([]string, 0, numLines)
|
m := strings.SplitN(code, "\n", numLines)
|
||||||
for _, v := range strings.SplitN(string(code), "\n", numLines) {
|
|
||||||
content := string(v)
|
for i, content := range m {
|
||||||
// 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 = append(m, gohtml.EscapeString(content))
|
m[i] = gohtml.EscapeString(content)
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,18 +43,29 @@ func TestFile(t *testing.T) {
|
||||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||||
`),
|
`),
|
||||||
want: util.Dedent(`
|
want: util.Dedent(`
|
||||||
<span class="line"><span class="cl"><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><span class="w">
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>
|
</span>
|
||||||
</span></span><span class="line"><span class="cl">
|
<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><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
|
</span>
|
||||||
</span></span><span class="line"><span class="cl"><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><span class="line"><span class="cl"><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>
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
|
<span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
|
||||||
</span></span><span class="line"><span class="cl"><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>
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</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><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
|
</span>
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</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><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span></span></span>
|
</span>
|
||||||
|
<span class="w"> </span><span class="nt">environment</span><span class="p">:</span><span class="w">
|
||||||
|
</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="w"> </span><span class="nt">commands</span><span class="p">:</span><span class="w">
|
||||||
|
</span>
|
||||||
|
<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 build -v</span><span class="w">
|
||||||
|
</span>
|
||||||
|
<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
|
||||||
`),
|
`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -76,19 +87,30 @@ func TestFile(t *testing.T) {
|
||||||
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
|
||||||
`)+"\n", "name: default", "name: default ", 1),
|
`)+"\n", "name: default", "name: default ", 1),
|
||||||
want: util.Dedent(`
|
want: util.Dedent(`
|
||||||
<span class="line"><span class="cl"><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><span class="w">
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>
|
</span>
|
||||||
</span></span><span class="line"><span class="cl">
|
<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><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
|
</span>
|
||||||
</span></span><span class="line"><span class="cl"><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><span class="line"><span class="cl"><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>
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
|
<span class="w"></span><span class="nt">steps</span><span class="p">:</span><span class="w">
|
||||||
</span></span><span class="line"><span class="cl"><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>
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</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><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
|
</span>
|
||||||
</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</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><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
|
</span>
|
||||||
</span></span>
|
<span class="w"> </span><span class="nt">environment</span><span class="p">:</span><span class="w">
|
||||||
|
</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="w"> </span><span class="nt">commands</span><span class="p">:</span><span class="w">
|
||||||
|
</span>
|
||||||
|
<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 build -v</span><span class="w">
|
||||||
|
</span>
|
||||||
|
<span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
|
||||||
|
</span>
|
||||||
<span class="w">
|
<span class="w">
|
||||||
</span>
|
</span>
|
||||||
`),
|
`),
|
||||||
|
|
Loading…
Reference in a new issue