1
0
Fork 0
forked from forgejo/forgejo

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

@ -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(`<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())
}
}
}