Browse Source

Golint fixed for modules/template

Lunny Xiao 3 years ago
parent
commit
229ec927b9

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

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

+ 25 - 14
modules/template/template.go

@@ -26,6 +26,7 @@ import (
26 26
 	"code.gitea.io/gitea/modules/setting"
27 27
 )
28 28
 
29
+// NewFuncMap returns functions for injecting to templates
29 30
 func NewFuncMap() []template.FuncMap {
30 31
 	return []template.FuncMap{map[string]interface{}{
31 32
 		"GoVer": func() string {
@@ -119,14 +120,17 @@ func NewFuncMap() []template.FuncMap {
119 120
 	}}
120 121
 }
121 122
 
123
+// Safe render raw as HTML
122 124
 func Safe(raw string) template.HTML {
123 125
 	return template.HTML(raw)
124 126
 }
125 127
 
128
+// Str2html render Markdown text to HTML
126 129
 func Str2html(raw string) template.HTML {
127 130
 	return template.HTML(markdown.Sanitizer.Sanitize(raw))
128 131
 }
129 132
 
133
+// List traversings the list
130 134
 func List(l *list.List) chan interface{} {
131 135
 	e := l.Front()
132 136
 	c := make(chan interface{})
@@ -140,21 +144,23 @@ func List(l *list.List) chan interface{} {
140 144
 	return c
141 145
 }
142 146
 
147
+// Sha1 returns sha1 sum of string
143 148
 func Sha1(str string) string {
144 149
 	return base.EncodeSha1(str)
145 150
 }
146 151
 
147
-func ToUTF8WithErr(content []byte) (error, string) {
152
+// ToUTF8WithErr converts content to UTF8 encoding
153
+func ToUTF8WithErr(content []byte) (string, error) {
148 154
 	charsetLabel, err := base.DetectEncoding(content)
149 155
 	if err != nil {
150
-		return err, ""
156
+		return "", err
151 157
 	} else if charsetLabel == "UTF-8" {
152
-		return nil, string(content)
158
+		return string(content), nil
153 159
 	}
154 160
 
155 161
 	encoding, _ := charset.Lookup(charsetLabel)
156 162
 	if encoding == nil {
157
-		return fmt.Errorf("Unknown encoding: %s", charsetLabel), string(content)
163
+		return string(content), fmt.Errorf("Unknown encoding: %s", charsetLabel)
158 164
 	}
159 165
 
160 166
 	// If there is an error, we concatenate the nicely decoded part and the
@@ -164,19 +170,20 @@ func ToUTF8WithErr(content []byte) (error, string) {
164 170
 		result = result + string(content[n:])
165 171
 	}
166 172
 
167
-	return err, result
173
+	return result, err
168 174
 }
169 175
 
176
+// ToUTF8 converts content to UTF8 encoding and ignore error
170 177
 func ToUTF8(content string) string {
171
-	_, res := ToUTF8WithErr([]byte(content))
178
+	res, _ := ToUTF8WithErr([]byte(content))
172 179
 	return res
173 180
 }
174 181
 
175
-// Replaces all prefixes 'old' in 's' with 'new'.
182
+// ReplaceLeft replaces all prefixes 'old' in 's' with 'new'.
176 183
 func ReplaceLeft(s, old, new string) string {
177
-	old_len, new_len, i, n := len(old), len(new), 0, 0
178
-	for ; i < len(s) && strings.HasPrefix(s[i:], old); n += 1 {
179
-		i += old_len
184
+	oldLen, newLen, i, n := len(old), len(new), 0, 0
185
+	for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ {
186
+		i += oldLen
180 187
 	}
181 188
 
182 189
 	// simple optimization
@@ -185,12 +192,12 @@ func ReplaceLeft(s, old, new string) string {
185 192
 	}
186 193
 
187 194
 	// allocating space for the new string
188
-	newLen := n*new_len + len(s[i:])
189
-	replacement := make([]byte, newLen, newLen)
195
+	curLen := n*newLen + len(s[i:])
196
+	replacement := make([]byte, curLen, curLen)
190 197
 
191 198
 	j := 0
192
-	for ; j < n*new_len; j += new_len {
193
-		copy(replacement[j:j+new_len], new)
199
+	for ; j < n*newLen; j += newLen {
200
+		copy(replacement[j:j+newLen], new)
194 201
 	}
195 202
 
196 203
 	copy(replacement[j:], s[i:])
@@ -222,6 +229,7 @@ func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]stri
222 229
 	return template.HTML(fullMessage)
223 230
 }
224 231
 
232
+// Actioner describes an action
225 233
 type Actioner interface {
226 234
 	GetOpType() int
227 235
 	GetActUserName() string
@@ -260,6 +268,7 @@ func ActionIcon(opType int) string {
260 268
 	}
261 269
 }
262 270
 
271
+// ActionContent2Commits converts action content to push commits
263 272
 func ActionContent2Commits(act Actioner) *models.PushCommits {
264 273
 	push := models.NewPushCommits()
265 274
 	if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil {
@@ -268,6 +277,7 @@ func ActionContent2Commits(act Actioner) *models.PushCommits {
268 277
 	return push
269 278
 }
270 279
 
280
+// DiffTypeToStr returns diff type name
271 281
 func DiffTypeToStr(diffType int) string {
272 282
 	diffTypes := map[int]string{
273 283
 		1: "add", 2: "modify", 3: "del", 4: "rename",
@@ -275,6 +285,7 @@ func DiffTypeToStr(diffType int) string {
275 285
 	return diffTypes[diffType]
276 286
 }
277 287
 
288
+// DiffLineTypeToStr returns diff line type name
278 289
 func DiffLineTypeToStr(diffType int) string {
279 290
 	switch diffType {
280 291
 	case 2:

+ 1 - 1
routers/repo/editor.go

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

+ 1 - 1
routers/repo/view.go

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