summaryrefslogtreecommitdiffstats
path: root/routers/repo/view.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-04-20 06:25:08 +0800
committerGitHub <noreply@github.com>2021-04-19 18:25:08 -0400
commit9d99f6ab19ac3f97af3ca126720e9075c127a652 (patch)
treeb817b4582a871f83b91ad7977fe772fc3501c1e8 /routers/repo/view.go
parentc9cc6698d2172625854cd063301e63602204a2a1 (diff)
downloadgitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.tar.gz
gitea-9d99f6ab19ac3f97af3ca126720e9075c127a652.zip
Refactor renders (#15175)
* Refactor renders * Some performance optimization * Fix comment * Transform reader * Fix csv test * Fix test * Fix tests * Improve optimaziation * Fix test * Fix test * Detect file encoding with reader * Improve optimaziation * reduce memory usage * improve code * fix build * Fix test * Fix for go1.15 * Fix render * Fix comment * Fix lint * Fix test * Don't use NormalEOF when unnecessary * revert change on util.go * Apply suggestions from code review Co-authored-by: zeripath <art27@cantab.net> * rename function * Take NormalEOF back Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'routers/repo/view.go')
-rw-r--r--routers/repo/view.go51
1 files changed, 41 insertions, 10 deletions
diff --git a/routers/repo/view.go b/routers/repo/view.go
index a03fd58c8a..10deb7065a 100644
--- a/routers/repo/view.go
+++ b/routers/repo/view.go
@@ -324,13 +324,26 @@ func renderDirectory(ctx *context.Context, treeLink string) {
ctx.Data["IsTextFile"] = true
ctx.Data["FileSize"] = fileSize
} else {
- d, _ := ioutil.ReadAll(dataRc)
- buf = charset.ToUTF8WithFallback(append(buf, d...))
+ rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
if markupType := markup.Type(readmeFile.name); markupType != "" {
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = string(markupType)
- ctx.Data["FileContent"] = string(markup.Render(readmeFile.name, buf, readmeTreelink, ctx.Repo.Repository.ComposeDocumentMetas()))
+ var result strings.Builder
+ err := markup.Render(&markup.RenderContext{
+ Filename: readmeFile.name,
+ URLPrefix: readmeTreelink,
+ Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
+ }, rd, &result)
+ if err != nil {
+ log.Error("Render failed: %v then fallback", err)
+ bs, _ := ioutil.ReadAll(rd)
+ ctx.Data["FileContent"] = strings.ReplaceAll(
+ gotemplate.HTMLEscapeString(string(bs)), "\n", `<br>`,
+ )
+ } else {
+ ctx.Data["FileContent"] = result.String()
+ }
} else {
ctx.Data["IsRenderedHTML"] = true
ctx.Data["FileContent"] = strings.ReplaceAll(
@@ -481,21 +494,30 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
break
}
- d, _ := ioutil.ReadAll(dataRc)
- buf = charset.ToUTF8WithFallback(append(buf, d...))
+ rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
readmeExist := markup.IsReadmeFile(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist
if markupType := markup.Type(blob.Name()); markupType != "" {
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
- ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeDocumentMetas()))
+ var result strings.Builder
+ err := markup.Render(&markup.RenderContext{
+ Filename: blob.Name(),
+ URLPrefix: path.Dir(treeLink),
+ Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
+ }, rd, &result)
+ if err != nil {
+ ctx.ServerError("Render", err)
+ return
+ }
+ ctx.Data["FileContent"] = result.String()
} else if readmeExist {
ctx.Data["IsRenderedHTML"] = true
ctx.Data["FileContent"] = strings.ReplaceAll(
gotemplate.HTMLEscapeString(string(buf)), "\n", `<br>`,
)
} else {
- buf = charset.ToUTF8WithFallback(buf)
+ buf, _ := ioutil.ReadAll(rd)
lineNums := linesBytesCount(buf)
ctx.Data["NumLines"] = strconv.Itoa(lineNums)
ctx.Data["NumLinesSet"] = true
@@ -532,11 +554,20 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
}
if markupType := markup.Type(blob.Name()); markupType != "" {
- d, _ := ioutil.ReadAll(dataRc)
- buf = append(buf, d...)
+ rd := io.MultiReader(bytes.NewReader(buf), dataRc)
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
- ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeDocumentMetas()))
+ var result strings.Builder
+ err := markup.Render(&markup.RenderContext{
+ Filename: blob.Name(),
+ URLPrefix: path.Dir(treeLink),
+ Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
+ }, rd, &result)
+ if err != nil {
+ ctx.ServerError("Render", err)
+ return
+ }
+ ctx.Data["FileContent"] = result.String()
}
}