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
This commit is contained in:
Rory McNamara 2016-08-09 20:35:20 +01:00 committed by 无闻
parent 9e8a8867ea
commit c8b45ecc27
5 changed files with 24 additions and 20 deletions

View File

@ -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-num .hljs li,
.repository.file.list #file-content .code-view .lines-code .hljs li { .repository.file.list #file-content .code-view .lines-code .hljs li {
padding-left: 5px; 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-num pre li.active,
.repository.file.list #file-content .code-view .lines-code pre li.active, .repository.file.list #file-content .code-view .lines-code pre li.active,

View File

@ -1088,7 +1088,7 @@ $(window).load(function () {
b = c; b = c;
} }
var classes = []; var classes = [];
for (i = a; i <= b; i++) { for (var i = a; i <= b; i++) {
classes.push('.L' + i); classes.push('.L' + i);
} }
$list.filter(classes.join(',')).addClass('active'); $list.filter(classes.join(',')).addClass('active');
@ -1102,22 +1102,6 @@ $(window).load(function () {
// Code view. // Code view.
if ($('.code-view .linenums').length > 0) { 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('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>');
$toappendnum_list.push('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>');
}
$block.append($toappendblock.join(''));
$num_list.append($toappendnum_list.join(''));
$(document).on('click', '.lines-num span', function (e) { $(document).on('click', '.lines-num span', function (e) {
var $select = $(this); var $select = $(this);
var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li'); var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');

View File

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

View File

@ -5,11 +5,14 @@
package repo package repo
import ( import (
"fmt"
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"path" "path"
"strings" "strings"
htmltemplate "html/template"
"github.com/Unknwon/paginater" "github.com/Unknwon/paginater"
"github.com/gogits/git-module" "github.com/gogits/git-module"
@ -116,14 +119,27 @@ func Home(ctx *context.Context) {
if readmeExist { if readmeExist {
ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas())) ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
} else { } else {
filecontent := ""
if err, content := template.ToUtf8WithErr(buf); err != nil { if err, content := template.ToUtf8WithErr(buf); err != nil {
if err != nil { if err != nil {
log.Error(4, "Convert content encoding: %s", err) log.Error(4, "Convert content encoding: %s", err)
} }
ctx.Data["FileContent"] = string(buf) filecontent = string(buf)
} else { } 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(`<li class="L%d" rel="L%d">%s</li>`, 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(`<span id="L%d">%d</span>`, i+1, i+1))
}
ctx.Data["LineNums"] = htmltemplate.HTML(output.String())
} }
} }
} }

View File

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