Skip to content

Commit b677b69

Browse files
committed
Support CRLF when splitting code lines for display
1 parent d9a8eff commit b677b69

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

public/css/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ footer .ui.language .menu {
14351435
.repository.file.list #file-content .code-view .lines-code ol li,
14361436
.repository.file.list #file-content .code-view .lines-num .hljs li,
14371437
.repository.file.list #file-content .code-view .lines-code .hljs li {
1438-
display: inline-block;
1438+
display: block;
14391439
width: 100%;
14401440
}
14411441
.repository.file.list #file-content .code-view .lines-num pre li.active,

public/less/_repository.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
margin: 0;
297297
padding: 0 !important;
298298
li {
299-
display: inline-block;
299+
display: block;
300300
width: 100%;
301301
&.active {
302302
background: #ffffdd;

routers/repo/view.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
gotemplate "html/template"
1212
"io/ioutil"
1313
"path"
14+
"regexp"
1415
"strconv"
1516
"strings"
1617

@@ -210,10 +211,26 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
210211
}
211212

212213
var output bytes.Buffer
213-
lines := strings.Split(fileContent, "\n")
214+
var terminator string
215+
216+
// test for bare linefeeds in case of mixed terminators
217+
lf, _ := regexp.MatchString("[^\r]\n", str)
218+
219+
if strings.Index(fileContent, "\r\n") != -1 && !lf {
220+
terminator = "\r\n"
221+
} else {
222+
terminator = "\n"
223+
}
224+
lines := strings.Split(fileContent, terminator)
225+
214226
for index, line := range lines {
215-
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n")
227+
line = gotemplate.HTMLEscapeString(line)
228+
if index != len(lines) - 1 {
229+
line += terminator
230+
}
231+
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, line))
216232
}
233+
217234
ctx.Data["FileContent"] = gotemplate.HTML(output.String())
218235

219236
output.Reset()

0 commit comments

Comments
 (0)