0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-24 16:12:46 +01:00

Merge pull request #251 from lunny/lunny/golint_modules_template

Golint fixed for modules/template
This commit is contained in:
Thomas Boerger 2016-11-25 10:08:37 +01:00 committed by GitHub
commit d39266228c
4 changed files with 28 additions and 16 deletions

View file

@ -64,6 +64,7 @@ var (
highlightMapping = map[string]string{} highlightMapping = map[string]string{}
) )
// NewContext loads highlight map
func NewContext() { func NewContext() {
keys := setting.Cfg.Section("highlight.mapping").Keys() keys := setting.Cfg.Section("highlight.mapping").Keys()
for i := range keys { for i := range keys {

View file

@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
) )
// NewFuncMap returns functions for injecting to templates
func NewFuncMap() []template.FuncMap { func NewFuncMap() []template.FuncMap {
return []template.FuncMap{map[string]interface{}{ return []template.FuncMap{map[string]interface{}{
"GoVer": func() string { "GoVer": func() string {
@ -119,14 +120,17 @@ func NewFuncMap() []template.FuncMap {
}} }}
} }
// Safe render raw as HTML
func Safe(raw string) template.HTML { func Safe(raw string) template.HTML {
return template.HTML(raw) return template.HTML(raw)
} }
// Str2html render Markdown text to HTML
func Str2html(raw string) template.HTML { func Str2html(raw string) template.HTML {
return template.HTML(markdown.Sanitizer.Sanitize(raw)) return template.HTML(markdown.Sanitizer.Sanitize(raw))
} }
// List traversings the list
func List(l *list.List) chan interface{} { func List(l *list.List) chan interface{} {
e := l.Front() e := l.Front()
c := make(chan interface{}) c := make(chan interface{})
@ -140,21 +144,23 @@ func List(l *list.List) chan interface{} {
return c return c
} }
// Sha1 returns sha1 sum of string
func Sha1(str string) string { func Sha1(str string) string {
return base.EncodeSha1(str) return base.EncodeSha1(str)
} }
func ToUTF8WithErr(content []byte) (error, string) { // ToUTF8WithErr converts content to UTF8 encoding
func ToUTF8WithErr(content []byte) (string, error) {
charsetLabel, err := base.DetectEncoding(content) charsetLabel, err := base.DetectEncoding(content)
if err != nil { if err != nil {
return err, "" return "", err
} else if charsetLabel == "UTF-8" { } else if charsetLabel == "UTF-8" {
return nil, string(content) return string(content), nil
} }
encoding, _ := charset.Lookup(charsetLabel) encoding, _ := charset.Lookup(charsetLabel)
if encoding == nil { if encoding == nil {
return fmt.Errorf("Unknown encoding: %s", charsetLabel), string(content) return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
} }
// If there is an error, we concatenate the nicely decoded part and the // If there is an error, we concatenate the nicely decoded part and the
@ -164,19 +170,20 @@ func ToUTF8WithErr(content []byte) (error, string) {
result = result + string(content[n:]) result = result + string(content[n:])
} }
return err, result return result, err
} }
// ToUTF8 converts content to UTF8 encoding and ignore error
func ToUTF8(content string) string { func ToUTF8(content string) string {
_, res := ToUTF8WithErr([]byte(content)) res, _ := ToUTF8WithErr([]byte(content))
return res return res
} }
// Replaces all prefixes 'old' in 's' with 'new'. // ReplaceLeft replaces all prefixes 'old' in 's' with 'new'.
func ReplaceLeft(s, old, new string) string { func ReplaceLeft(s, old, new string) string {
old_len, new_len, i, n := len(old), len(new), 0, 0 oldLen, newLen, i, n := len(old), len(new), 0, 0
for ; i < len(s) && strings.HasPrefix(s[i:], old); n += 1 { for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
i += old_len i += oldLen
} }
// simple optimization // simple optimization
@ -185,12 +192,12 @@ func ReplaceLeft(s, old, new string) string {
} }
// allocating space for the new string // allocating space for the new string
newLen := n*new_len + len(s[i:]) curLen := n*newLen + len(s[i:])
replacement := make([]byte, newLen, newLen) replacement := make([]byte, curLen, curLen)
j := 0 j := 0
for ; j < n*new_len; j += new_len { for ; j < n*newLen; j += newLen {
copy(replacement[j:j+new_len], new) copy(replacement[j:j+newLen], new)
} }
copy(replacement[j:], s[i:]) copy(replacement[j:], s[i:])
@ -222,6 +229,7 @@ func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]stri
return template.HTML(fullMessage) return template.HTML(fullMessage)
} }
// Actioner describes an action
type Actioner interface { type Actioner interface {
GetOpType() int GetOpType() int
GetActUserName() string GetActUserName() string
@ -260,6 +268,7 @@ func ActionIcon(opType int) string {
} }
} }
// ActionContent2Commits converts action content to push commits
func ActionContent2Commits(act Actioner) *models.PushCommits { func ActionContent2Commits(act Actioner) *models.PushCommits {
push := models.NewPushCommits() push := models.NewPushCommits()
if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil { if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
@ -268,6 +277,7 @@ func ActionContent2Commits(act Actioner) *models.PushCommits {
return push return push
} }
// DiffTypeToStr returns diff type name
func DiffTypeToStr(diffType int) string { func DiffTypeToStr(diffType int) string {
diffTypes := map[int]string{ diffTypes := map[int]string{
1: "add", 2: "modify", 3: "del", 4: "rename", 1: "add", 2: "modify", 3: "del", 4: "rename",
@ -275,6 +285,7 @@ func DiffTypeToStr(diffType int) string {
return diffTypes[diffType] return diffTypes[diffType]
} }
// DiffLineTypeToStr returns diff line type name
func DiffLineTypeToStr(diffType int) string { func DiffLineTypeToStr(diffType int) string {
switch diffType { switch diffType {
case 2: case 2:

View file

@ -74,7 +74,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
d, _ := ioutil.ReadAll(dataRc) d, _ := ioutil.ReadAll(dataRc)
buf = append(buf, d...) buf = append(buf, d...)
if err, content := template.ToUTF8WithErr(buf); err != nil { if content, err := template.ToUTF8WithErr(buf); err != nil {
if err != nil { if err != nil {
log.Error(4, "ToUTF8WithErr: %v", err) log.Error(4, "ToUTF8WithErr: %v", err)
} }

View file

@ -164,7 +164,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
} else { } else {
// Building code view blocks with line number on server side. // Building code view blocks with line number on server side.
var fileContent string var fileContent string
if err, content := template.ToUTF8WithErr(buf); err != nil { if content, err := template.ToUTF8WithErr(buf); err != nil {
if err != nil { if err != nil {
log.Error(4, "ToUTF8WithErr: %s", err) log.Error(4, "ToUTF8WithErr: %s", err)
} }