From c8b45ecc2762a9d64dbe83cafd68b137661448a5 Mon Sep 17 00:00:00 2001 From: Rory McNamara Date: Tue, 9 Aug 2016 20:35:20 +0100 Subject: [PATCH] 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 --- public/css/gogs.css | 2 ++ public/js/gogs.js | 18 +----------------- public/less/_repository.less | 2 ++ routers/repo/view.go | 20 ++++++++++++++++++-- templates/repo/view_file.tmpl | 2 +- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/public/css/gogs.css b/public/css/gogs.css index ae58c7c54e..c39c52ca22 100644 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -1316,6 +1316,8 @@ footer .ui.language .menu { .repository.file.list #file-content .code-view .lines-num .hljs li, .repository.file.list #file-content .code-view .lines-code .hljs li { padding-left: 5px; + display: inline-block; + width: 100%; } .repository.file.list #file-content .code-view .lines-num pre li.active, .repository.file.list #file-content .code-view .lines-code pre li.active, diff --git a/public/js/gogs.js b/public/js/gogs.js index 226bb6bae5..c6a1b6b624 100644 --- a/public/js/gogs.js +++ b/public/js/gogs.js @@ -1088,7 +1088,7 @@ $(window).load(function () { b = c; } var classes = []; - for (i = a; i <= b; i++) { + for (var i = a; i <= b; i++) { classes.push('.L' + i); } $list.filter(classes.join(',')).addClass('active'); @@ -1102,22 +1102,6 @@ $(window).load(function () { // Code view. if ($('.code-view .linenums').length > 0) { - var $block = $('.code-view .linenums'); - var lines = $block.html().split("\n"); - $block.html(''); - - var $num_list = $('.code-view .lines-num'); - - // Building blocks. - var $toappendblock = []; - var $toappendnum_list = []; - for (var i = 0; i < lines.length; i++) { - $toappendblock.push('
  • ' + lines[i] + '
  • '); - $toappendnum_list.push('' + (i + 1) + ''); - } - $block.append($toappendblock.join('')); - $num_list.append($toappendnum_list.join('')); - $(document).on('click', '.lines-num span', function (e) { var $select = $(this); var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li'); diff --git a/public/less/_repository.less b/public/less/_repository.less index 474b8ade6d..cb1cc121ce 100644 --- a/public/less/_repository.less +++ b/public/less/_repository.less @@ -251,6 +251,8 @@ padding: 0 !important; li { padding-left: 5px; + display: inline-block; + width: 100%; &.active { background: #ffffdd; } 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()) } } } diff --git a/templates/repo/view_file.tmpl b/templates/repo/view_file.tmpl index cd0caf10f9..0f42df4925 100644 --- a/templates/repo/view_file.tmpl +++ b/templates/repo/view_file.tmpl @@ -44,7 +44,7 @@ {{if .IsFileTooLarge}} {{.i18n.Tr "repo.file_too_large"}} {{else}} - + {{.LineNums}}
      {{.FileContent}}
    {{end}}