Browse Source

Merge pull request #251 from lunny/lunny/golint_modules_template

Golint fixed for modules/template
tags/v1.0.0
Thomas Boerger 7 years ago
parent
commit
d39266228c

+ 1
- 0
modules/template/highlight/highlight.go View File

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 {

+ 25
- 14
modules/template/template.go View File

"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 {
}} }}
} }


// 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{})
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
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
for ; i < len(s) && strings.HasPrefix(s[i:], old); n += 1 {
i += old_len
oldLen, newLen, i, n := len(old), len(new), 0, 0
for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
i += oldLen
} }


// simple optimization // simple optimization
} }


// allocating space for the new string // allocating space for the new string
newLen := n*new_len + len(s[i:])
replacement := make([]byte, newLen, newLen)
curLen := n*newLen + len(s[i:])
replacement := make([]byte, curLen, curLen)


j := 0 j := 0
for ; j < n*new_len; j += new_len {
copy(replacement[j:j+new_len], new)
for ; j < n*newLen; j += newLen {
copy(replacement[j:j+newLen], new)
} }


copy(replacement[j:], s[i:]) copy(replacement[j:], s[i:])
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
} }
} }


// 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 {
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",
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:

+ 1
- 1
routers/repo/editor.go View File



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)
} }

+ 1
- 1
routers/repo/view.go View File

} 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)
} }

Loading…
Cancel
Save