Skip to content

Commit 95b4a2e

Browse files
authored
Merge branch 'main' into fix-14110-if-no-certs-no-certs
2 parents de8fc18 + 52f8dcd commit 95b4a2e

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

modules/base/tool.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222
"time"
2323
"unicode"
24+
"unicode/utf8"
2425

2526
"code.gitea.io/gitea/modules/git"
2627
"code.gitea.io/gitea/modules/log"
@@ -213,19 +214,19 @@ func EllipsisString(str string, length int) string {
213214
if length <= 3 {
214215
return "..."
215216
}
216-
if len(str) <= length {
217+
if utf8.RuneCountInString(str) <= length {
217218
return str
218219
}
219-
return str[:length-3] + "..."
220+
return string([]rune(str)[:length-3]) + "..."
220221
}
221222

222223
// TruncateString returns a truncated string with given limit,
223224
// it returns input string if length is not reached limit.
224225
func TruncateString(str string, limit int) string {
225-
if len(str) < limit {
226+
if utf8.RuneCountInString(str) < limit {
226227
return str
227228
}
228-
return str[:limit]
229+
return string([]rune(str)[:limit])
229230
}
230231

231232
// StringsToInt64s converts a slice of string to a slice of int64.

modules/base/tool_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ func TestEllipsisString(t *testing.T) {
170170
assert.Equal(t, "fo...", EllipsisString("foobar", 5))
171171
assert.Equal(t, "foobar", EllipsisString("foobar", 6))
172172
assert.Equal(t, "foobar", EllipsisString("foobar", 10))
173+
assert.Equal(t, "测...", EllipsisString("测试文本一二三四", 4))
174+
assert.Equal(t, "测试...", EllipsisString("测试文本一二三四", 5))
175+
assert.Equal(t, "测试文...", EllipsisString("测试文本一二三四", 6))
176+
assert.Equal(t, "测试文本一二三四", EllipsisString("测试文本一二三四", 10))
173177
}
174178

175179
func TestTruncateString(t *testing.T) {
@@ -181,6 +185,10 @@ func TestTruncateString(t *testing.T) {
181185
assert.Equal(t, "fooba", TruncateString("foobar", 5))
182186
assert.Equal(t, "foobar", TruncateString("foobar", 6))
183187
assert.Equal(t, "foobar", TruncateString("foobar", 7))
188+
assert.Equal(t, "测试文本", TruncateString("测试文本一二三四", 4))
189+
assert.Equal(t, "测试文本一", TruncateString("测试文本一二三四", 5))
190+
assert.Equal(t, "测试文本一二", TruncateString("测试文本一二三四", 6))
191+
assert.Equal(t, "测试文本一二三", TruncateString("测试文本一二三四", 7))
184192
}
185193

186194
func TestStringsToInt64s(t *testing.T) {

modules/highlight/highlight.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/analyze"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/setting"
19+
"github.com/alecthomas/chroma"
1920
"github.com/alecthomas/chroma/formatters/html"
2021
"github.com/alecthomas/chroma/lexers"
2122
"github.com/alecthomas/chroma/styles"
@@ -66,14 +67,17 @@ func Code(fileName, code string) string {
6667
htmlbuf := bytes.Buffer{}
6768
htmlw := bufio.NewWriter(&htmlbuf)
6869

70+
var lexer chroma.Lexer
6971
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
70-
//change file name to one with mapped extension so we look that up instead
71-
fileName = "mapped." + val
72+
//use mapped value to find lexer
73+
lexer = lexers.Get(val)
7274
}
7375

74-
lexer := lexers.Match(fileName)
7576
if lexer == nil {
76-
lexer = lexers.Fallback
77+
lexer = lexers.Match(fileName)
78+
if lexer == nil {
79+
lexer = lexers.Fallback
80+
}
7781
}
7882

7983
iterator, err := lexer.Tokenise(nil, string(code))
@@ -114,17 +118,20 @@ func File(numLines int, fileName string, code []byte) map[int]string {
114118
htmlbuf := bytes.Buffer{}
115119
htmlw := bufio.NewWriter(&htmlbuf)
116120

121+
var lexer chroma.Lexer
117122
if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
118-
fileName = "test." + val
123+
lexer = lexers.Get(val)
119124
}
120125

121-
language := analyze.GetCodeLanguage(fileName, code)
122-
123-
lexer := lexers.Get(language)
124126
if lexer == nil {
125-
lexer = lexers.Match(fileName)
127+
language := analyze.GetCodeLanguage(fileName, code)
128+
129+
lexer = lexers.Get(language)
126130
if lexer == nil {
127-
lexer = lexers.Fallback
131+
lexer = lexers.Match(fileName)
132+
if lexer == nil {
133+
lexer = lexers.Fallback
134+
}
128135
}
129136
}
130137

0 commit comments

Comments
 (0)