From b677b697f093735831b35593225095077add618e Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 7 Jun 2017 22:45:12 +0200 Subject: [PATCH 1/5] Support CRLF when splitting code lines for display --- public/css/index.css | 2 +- public/less/_repository.less | 2 +- routers/repo/view.go | 21 +++++++++++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/public/css/index.css b/public/css/index.css index 0d6112a168eaa..35e56413b11e8 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -1435,7 +1435,7 @@ footer .ui.language .menu { .repository.file.list #file-content .code-view .lines-code ol li, .repository.file.list #file-content .code-view .lines-num .hljs li, .repository.file.list #file-content .code-view .lines-code .hljs li { - display: inline-block; + display: block; width: 100%; } .repository.file.list #file-content .code-view .lines-num pre li.active, diff --git a/public/less/_repository.less b/public/less/_repository.less index 56249f3a9a83e..c6f4527634409 100644 --- a/public/less/_repository.less +++ b/public/less/_repository.less @@ -296,7 +296,7 @@ margin: 0; padding: 0 !important; li { - display: inline-block; + display: block; width: 100%; &.active { background: #ffffdd; diff --git a/routers/repo/view.go b/routers/repo/view.go index a3c42a0fdd2bf..aa6eea379be7a 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -11,6 +11,7 @@ import ( gotemplate "html/template" "io/ioutil" "path" + "regexp" "strconv" "strings" @@ -210,10 +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 + + // test for bare linefeeds in case of mixed terminators + lf, _ := regexp.MatchString("[^\r]\n", str) + + if strings.Index(fileContent, "\r\n") != -1 && !lf { + terminator = "\r\n" + } else { + terminator = "\n" + } + lines := strings.Split(fileContent, terminator) + for index, line := range lines { - output.WriteString(fmt.Sprintf(`
  • %s
  • `, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n") + line = gotemplate.HTMLEscapeString(line) + if index != len(lines) - 1 { + line += terminator + } + output.WriteString(fmt.Sprintf(`
  • %s
  • `, index+1, index+1, line)) } + ctx.Data["FileContent"] = gotemplate.HTML(output.String()) output.Reset() From 8e8385a437d59ec0574322f79cb037b68d434781 Mon Sep 17 00:00:00 2001 From: silverwind Date: Wed, 7 Jun 2017 23:04:23 +0200 Subject: [PATCH 2/5] refactor, fix mixed match --- routers/repo/view.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index aa6eea379be7a..6f684cb811983 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -213,11 +213,13 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st var output bytes.Buffer var terminator string - // test for bare linefeeds in case of mixed terminators - lf, _ := regexp.MatchString("[^\r]\n", str) - - if strings.Index(fileContent, "\r\n") != -1 && !lf { - terminator = "\r\n" + if strings.Index(fileContent, "\r\n") != -1 { + lf, _ := regexp.MatchString("[^\r]\n", fileContent) + if lf { + terminator = "\n" + } else { + terminator = "\r\n" + } } else { terminator = "\n" } @@ -230,7 +232,6 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st } output.WriteString(fmt.Sprintf(`
  • %s
  • `, index+1, index+1, line)) } - ctx.Data["FileContent"] = gotemplate.HTML(output.String()) output.Reset() From 00763ed31bc5c0aecb80475454c997fd673a1043 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 8 Jun 2017 10:21:31 +0200 Subject: [PATCH 3/5] fmt --- routers/repo/view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index 6f684cb811983..141637dc2a1a1 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -227,7 +227,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st for index, line := range lines { line = gotemplate.HTMLEscapeString(line) - if index != len(lines) - 1 { + if index != len(lines)-1 { line += terminator } output.WriteString(fmt.Sprintf(`
  • %s
  • `, index+1, index+1, line)) From 3c3c8eb3320eb90169430ce0ca797bbb7a0e65a7 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 8 Jun 2017 10:30:26 +0200 Subject: [PATCH 4/5] split on both LF and CRLF, use raw literals in regexes --- routers/repo/view.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index 141637dc2a1a1..d79184a2e69c1 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -214,7 +214,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st var terminator string if strings.Index(fileContent, "\r\n") != -1 { - lf, _ := regexp.MatchString("[^\r]\n", fileContent) + lf, _ := regexp.MatchString(`[^\r]\n`, fileContent) if lf { terminator = "\n" } else { @@ -223,7 +223,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st } else { terminator = "\n" } - lines := strings.Split(fileContent, terminator) + lines := regexp.MustCompile(`\r?\n`).Split(fileContent, -1) for index, line := range lines { line = gotemplate.HTMLEscapeString(line) From 623e1a3637991f68441ed58c52f401046f1d285b Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 8 Jun 2017 20:26:08 +0200 Subject: [PATCH 5/5] simplify --- routers/repo/view.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index d79184a2e69c1..84e5ba85ce877 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -11,7 +11,6 @@ import ( gotemplate "html/template" "io/ioutil" "path" - "regexp" "strconv" "strings" @@ -211,24 +210,11 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st } var output bytes.Buffer - var terminator string - - if strings.Index(fileContent, "\r\n") != -1 { - lf, _ := regexp.MatchString(`[^\r]\n`, fileContent) - if lf { - terminator = "\n" - } else { - terminator = "\r\n" - } - } else { - terminator = "\n" - } - lines := regexp.MustCompile(`\r?\n`).Split(fileContent, -1) - + lines := strings.Split(fileContent, "\n") for index, line := range lines { line = gotemplate.HTMLEscapeString(line) if index != len(lines)-1 { - line += terminator + line += "\n" } output.WriteString(fmt.Sprintf(`
  • %s
  • `, index+1, index+1, line)) }