From c8b45ecc2762a9d64dbe83cafd68b137661448a5 Mon Sep 17 00:00:00 2001 From: Rory McNamara Date: Tue, 9 Aug 2016 20:35:20 +0100 Subject: Render the Code view on the server (minus syntax highlighting) (#2942) * render code view server side * remove debug print * fix multiline selection bug * change string concatenation to bytes.Buffer for efficiency * Fix newlines added by previous for hljs * fix selection highlighting * make css changes in .less --- routers/repo/view.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'routers/repo') diff --git a/routers/repo/view.go b/routers/repo/view.go index 7bcdc1293c..c13f57f426 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -5,11 +5,14 @@ package repo import ( + "fmt" "bytes" "io/ioutil" "path" "strings" + htmltemplate "html/template" + "github.com/Unknwon/paginater" "github.com/gogits/git-module" @@ -116,14 +119,27 @@ func Home(ctx *context.Context) { if readmeExist { ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) } else { + filecontent := "" if err, content := template.ToUtf8WithErr(buf); err != nil { if err != nil { log.Error(4, "Convert content encoding: %s", err) } - ctx.Data["FileContent"] = string(buf) + filecontent = string(buf) } else { - ctx.Data["FileContent"] = content + filecontent = content + } + var output bytes.Buffer + lines := strings.Split(filecontent, "\n") + for index, line := range lines { + output.WriteString(fmt.Sprintf(`
  • %s
  • `, index+1, index+1, htmltemplate.HTMLEscapeString(line)) + "\n") + } + ctx.Data["FileContent"] = htmltemplate.HTML(output.String()) + + output.Reset() + for i := 0; i < len(lines); i++ { + output.WriteString(fmt.Sprintf(`%d`, i+1, i+1)) } + ctx.Data["LineNums"] = htmltemplate.HTML(output.String()) } } } -- cgit v1.2.3