aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2022-07-30 21:17:43 +0200
committerGitHub <noreply@github.com>2022-07-30 21:17:43 +0200
commitae3dde1c873320abba49c0cf58c6c87c78cfe334 (patch)
treeddd3daded843a229e23f0cabbee244da7a4ed249 /routers
parent0e61a74e5a3fc14ff26d1c85065ba336d3f3994b (diff)
downloadgitea-ae3dde1c873320abba49c0cf58c6c87c78cfe334.tar.gz
gitea-ae3dde1c873320abba49c0cf58c6c87c78cfe334.zip
Rework file highlight rendering and fix yaml copy-paste (#19967)
* Rework file highlight rendering and fix yaml copy-paste * use Split+Trim to replace tag parser * remove unnecessary bytes.Count * remove newLineInHTML = "&#10;" Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'routers')
-rw-r--r--routers/web/repo/view.go26
1 files changed, 13 insertions, 13 deletions
diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go
index 1a39b21c6f..c5657aa675 100644
--- a/routers/web/repo/view.go
+++ b/routers/web/repo/view.go
@@ -15,7 +15,6 @@ import (
"net/http"
"net/url"
"path"
- "strconv"
"strings"
"time"
@@ -57,15 +56,6 @@ type namedBlob struct {
blob *git.Blob
}
-func linesBytesCount(s []byte) int {
- nl := []byte{'\n'}
- n := bytes.Count(s, nl)
- if len(s) > 0 && !bytes.HasSuffix(s, nl) {
- n++
- }
- return n
-}
-
// FIXME: There has to be a more efficient way of doing this
func getReadmeFileFromPath(commit *git.Commit, treePath string) (*namedBlob, error) {
tree, err := commit.SubTree(treePath)
@@ -555,8 +545,14 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
)
} else {
buf, _ := io.ReadAll(rd)
- lineNums := linesBytesCount(buf)
- ctx.Data["NumLines"] = strconv.Itoa(lineNums)
+
+ // empty: 0 lines; "a": one line; "a\n": two lines; "a\nb": two lines;
+ // the NumLines is only used for the display on the UI: "xxx lines"
+ if len(buf) == 0 {
+ ctx.Data["NumLines"] = 0
+ } else {
+ ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
+ }
ctx.Data["NumLinesSet"] = true
language := ""
@@ -584,7 +580,11 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
language = ""
}
}
- fileContent := highlight.File(lineNums, blob.Name(), language, buf)
+ fileContent, err := highlight.File(blob.Name(), language, buf)
+ if err != nil {
+ log.Error("highlight.File failed, fallback to plain text: %v", err)
+ fileContent = highlight.PlainText(buf)
+ }
status, _ := charset.EscapeControlReader(bytes.NewReader(buf), io.Discard)
ctx.Data["EscapeStatus"] = status
statuses := make([]charset.EscapeStatus, len(fileContent))