Browse Source

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
Rory McNamara 3 years ago
parent
commit
c8b45ecc27
5 changed files with 24 additions and 20 deletions
  1. 2 0
      public/css/gogs.css
  2. 1 17
      public/js/gogs.js
  3. 2 0
      public/less/_repository.less
  4. 18 2
      routers/repo/view.go
  5. 1 1
      templates/repo/view_file.tmpl

+ 2 - 0
public/css/gogs.css

@@ -1316,6 +1316,8 @@ footer .ui.language .menu {
1316 1316
 .repository.file.list #file-content .code-view .lines-num .hljs li,
1317 1317
 .repository.file.list #file-content .code-view .lines-code .hljs li {
1318 1318
   padding-left: 5px;
1319
+  display: inline-block;
1320
+  width: 100%;
1319 1321
 }
1320 1322
 .repository.file.list #file-content .code-view .lines-num pre li.active,
1321 1323
 .repository.file.list #file-content .code-view .lines-code pre li.active,

+ 1 - 17
public/js/gogs.js

@@ -1088,7 +1088,7 @@ $(window).load(function () {
1088 1088
                     b = c;
1089 1089
                 }
1090 1090
                 var classes = [];
1091
-                for (i = a; i <= b; i++) {
1091
+                for (var i = a; i <= b; i++) {
1092 1092
                     classes.push('.L' + i);
1093 1093
                 }
1094 1094
                 $list.filter(classes.join(',')).addClass('active');
@@ -1102,22 +1102,6 @@ $(window).load(function () {
1102 1102
 
1103 1103
     // Code view.
1104 1104
     if ($('.code-view .linenums').length > 0) {
1105
-        var $block = $('.code-view .linenums');
1106
-        var lines = $block.html().split("\n");
1107
-        $block.html('');
1108
-
1109
-        var $num_list = $('.code-view .lines-num');
1110
-
1111
-        // Building blocks.
1112
-        var $toappendblock = [];
1113
-        var $toappendnum_list = [];
1114
-        for (var i = 0; i < lines.length; i++) {
1115
-            $toappendblock.push('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>');
1116
-            $toappendnum_list.push('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>');
1117
-        }
1118
-        $block.append($toappendblock.join(''));
1119
-        $num_list.append($toappendnum_list.join(''));
1120
-
1121 1105
         $(document).on('click', '.lines-num span', function (e) {
1122 1106
             var $select = $(this);
1123 1107
             var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');

+ 2 - 0
public/less/_repository.less

@@ -251,6 +251,8 @@
251 251
 						padding: 0 !important;
252 252
 						li {
253 253
 							padding-left: 5px;
254
+							display: inline-block;
255
+							width: 100%;
254 256
 							&.active {
255 257
 								background: #ffffdd;
256 258
 							}

+ 18 - 2
routers/repo/view.go

@@ -5,11 +5,14 @@
5 5
 package repo
6 6
 
7 7
 import (
8
+	"fmt"
8 9
 	"bytes"
9 10
 	"io/ioutil"
10 11
 	"path"
11 12
 	"strings"
12 13
 
14
+	htmltemplate "html/template"
15
+
13 16
 	"github.com/Unknwon/paginater"
14 17
 
15 18
 	"github.com/gogits/git-module"
@@ -116,14 +119,27 @@ func Home(ctx *context.Context) {
116 119
 					if readmeExist {
117 120
 						ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
118 121
 					} else {
122
+						filecontent := ""
119 123
 						if err, content := template.ToUtf8WithErr(buf); err != nil {
120 124
 							if err != nil {
121 125
 								log.Error(4, "Convert content encoding: %s", err)
122 126
 							}
123
-							ctx.Data["FileContent"] = string(buf)
127
+							filecontent = string(buf)
124 128
 						} else {
125
-							ctx.Data["FileContent"] = content
129
+							filecontent = content
130
+						}
131
+						var output bytes.Buffer
132
+						lines := strings.Split(filecontent, "\n")
133
+						for index, line := range lines {
134
+							output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, htmltemplate.HTMLEscapeString(line)) + "\n")
135
+						}
136
+						ctx.Data["FileContent"] = htmltemplate.HTML(output.String())
137
+
138
+						output.Reset()
139
+						for i := 0; i < len(lines); i++ {
140
+							output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
126 141
 						}
142
+						ctx.Data["LineNums"] = htmltemplate.HTML(output.String())
127 143
 					}
128 144
 				}
129 145
 			}

+ 1 - 1
templates/repo/view_file.tmpl

@@ -44,7 +44,7 @@
44 44
 						{{if .IsFileTooLarge}}
45 45
 							<td><strong>{{.i18n.Tr "repo.file_too_large"}}</strong></td>
46 46
 						{{else}}
47
-							<td class="lines-num"></td>
47
+							<td class="lines-num">{{.LineNums}}</td>
48 48
 							<td class="lines-code"><pre><code class="{{.HighlightClass}}"><ol class="linenums">{{.FileContent}}</ol></code></pre></td>
49 49
 						{{end}}
50 50
 						</tr>