-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support CRLF when splitting code lines for display #1862
Conversation
routers/repo/view.go
Outdated
@@ -212,7 +212,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st | |||
var output bytes.Buffer | |||
lines := strings.Split(fileContent, "\n") | |||
for index, line := range lines { | |||
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n") | |||
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line) + "\n")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we instead do
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s\n</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly, but I think I'd rather avoid newlines in HTML entirely.
I'll have to revise this, I think it should be possible to avoid newlines in HTML (line termination should be done through block-level elements). Notably this issue only affects files without syntax highlighting. |
39d9b78
to
69284f0
Compare
153ece3
to
3d21d95
Compare
Updated. The actual issue with CRLF copying was that lines were always split only on I've now updated the line splitting to detect files containing For files that contain both |
@silverwind The CI build failed, run |
routers/repo/view.go
Outdated
} else { | ||
terminator = "\n" | ||
} | ||
lines := strings.Split(fileContent, terminator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To deal with mixed newlines it would be better to split using:
lines := regexp.MustCompile("\\r?\\n").Split(fileContent, -1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but how do I know whether to join the lines with LF or CRLF afterwards in the loop? I'd like to preserve CRLF if it's there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave detection code, just replace strings split with regexp split
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, actually I'll try your suggestion. I can keep the detection code above to know.
routers/repo/view.go
Outdated
@@ -210,9 +211,26 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st | |||
} | |||
|
|||
var output bytes.Buffer | |||
lines := strings.Split(fileContent, "\n") | |||
var terminator string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By using a single terminator, we aren't able to correctly render files that sometime use \r\n
and in other places use \n
? Is there a reason we can't just do
lines := strings.Split(fileContent, "\n")
for index, line := range lines {
if index < len(lines) - 1 {
line += "\n"
}
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, line))
}
Since both \r\n
and \n
end in \n
, it seems to me this would work, and it would handle the case where a file mixes \r\n
and \n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, yes, this looks to work. My first approach was similar to this one, but for some reason I stopped pursuing it. Anyways, this copies fine, preserves newlines in mixed files and also highlights fine, looking good!
LGTM |
LGTM |
make L-G-T-M work |
This removes the whitspace between
<li>
elements and adds them to the tag content, where it was originally removed.display:block
because with the previousdisplay:inline-block
it would all end up on one line.Fixes: #1857