@@ -57,7 +57,7 @@ type namedBlob struct {
57
57
}
58
58
59
59
// FIXME: There has to be a more efficient way of doing this
60
- func getReadmeFileFromPath (commit * git.Commit , treePath string ) (* namedBlob , error ) {
60
+ func getReadmeFileFromPath (ctx * context. Context , commit * git.Commit , treePath string ) (* namedBlob , error ) {
61
61
tree , err := commit .SubTree (treePath )
62
62
if err != nil {
63
63
return nil , err
@@ -68,50 +68,33 @@ func getReadmeFileFromPath(commit *git.Commit, treePath string) (*namedBlob, err
68
68
return nil , err
69
69
}
70
70
71
- var readmeFiles [4 ]* namedBlob
72
- exts := []string {".md" , ".txt" , "" } // sorted by priority
71
+ // Create a list of extensions in priority order
72
+ // 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
73
+ // 2. Txt files - e.g. README.txt
74
+ // 3. No extension - e.g. README
75
+ exts := append (localizedExtensions (".md" , ctx .Language ()), ".txt" , "" ) // sorted by priority
76
+ extCount := len (exts )
77
+ readmeFiles := make ([]* namedBlob , extCount + 1 )
73
78
for _ , entry := range entries {
74
79
if entry .IsDir () {
75
80
continue
76
81
}
77
- for i , ext := range exts {
78
- if markup .IsReadmeFile (entry .Name (), ext ) {
79
- if readmeFiles [i ] == nil || base .NaturalSortLess (readmeFiles [i ].name , entry .Blob ().Name ()) {
80
- name := entry .Name ()
81
- isSymlink := entry .IsLink ()
82
- target := entry
83
- if isSymlink {
84
- target , err = entry .FollowLinks ()
85
- if err != nil && ! git .IsErrBadLink (err ) {
86
- return nil , err
87
- }
88
- }
89
- if target != nil && (target .IsExecutable () || target .IsRegular ()) {
90
- readmeFiles [i ] = & namedBlob {
91
- name ,
92
- isSymlink ,
93
- target .Blob (),
94
- }
95
- }
96
- }
97
- }
98
- }
99
-
100
- if markup .IsReadmeFile (entry .Name ()) {
101
- if readmeFiles [3 ] == nil || base .NaturalSortLess (readmeFiles [3 ].name , entry .Blob ().Name ()) {
82
+ if i , ok := markup .IsReadmeFileExtension (entry .Name (), exts ... ); ok {
83
+ if readmeFiles [i ] == nil || base .NaturalSortLess (readmeFiles [i ].name , entry .Blob ().Name ()) {
102
84
name := entry .Name ()
103
85
isSymlink := entry .IsLink ()
86
+ target := entry
104
87
if isSymlink {
105
- entry , err = entry .FollowLinks ()
88
+ target , err = entry .FollowLinks ()
106
89
if err != nil && ! git .IsErrBadLink (err ) {
107
90
return nil , err
108
91
}
109
92
}
110
- if entry != nil && (entry .IsExecutable () || entry .IsRegular ()) {
111
- readmeFiles [3 ] = & namedBlob {
93
+ if target != nil && (target .IsExecutable () || target .IsRegular ()) {
94
+ readmeFiles [i ] = & namedBlob {
112
95
name ,
113
96
isSymlink ,
114
- entry .Blob (),
97
+ target .Blob (),
115
98
}
116
99
}
117
100
}
@@ -151,13 +134,38 @@ func renderDirectory(ctx *context.Context, treeLink string) {
151
134
renderReadmeFile (ctx , readmeFile , readmeTreelink )
152
135
}
153
136
137
+ // localizedExtensions prepends the provided language code with and without a
138
+ // regional identifier to the provided extenstion.
139
+ // Note: the language code will always be lower-cased, if a region is present it must be separated with a `-`
140
+ // Note: ext should be prefixed with a `.`
141
+ func localizedExtensions (ext , languageCode string ) (localizedExts []string ) {
142
+ if len (languageCode ) < 1 {
143
+ return []string {ext }
144
+ }
145
+
146
+ lowerLangCode := "." + strings .ToLower (languageCode )
147
+
148
+ if strings .Contains (lowerLangCode , "-" ) {
149
+ underscoreLangCode := strings .ReplaceAll (lowerLangCode , "-" , "_" )
150
+ indexOfDash := strings .Index (lowerLangCode , "-" )
151
+ // e.g. [.zh-cn.md, .zh_cn.md, .zh.md, .md]
152
+ return []string {lowerLangCode + ext , underscoreLangCode + ext , lowerLangCode [:indexOfDash ] + ext , ext }
153
+ }
154
+
155
+ // e.g. [.en.md, .md]
156
+ return []string {lowerLangCode + ext , ext }
157
+ }
158
+
154
159
func findReadmeFile (ctx * context.Context , entries git.Entries , treeLink string ) (* namedBlob , string ) {
155
- // 3 for the extensions in exts[] in order
156
- // the last one is for a readme that doesn't
157
- // strictly match an extension
158
- var readmeFiles [4 ]* namedBlob
159
- var docsEntries [3 ]* git.TreeEntry
160
- exts := []string {".md" , ".txt" , "" } // sorted by priority
160
+ // Create a list of extensions in priority order
161
+ // 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
162
+ // 2. Txt files - e.g. README.txt
163
+ // 3. No extension - e.g. README
164
+ exts := append (localizedExtensions (".md" , ctx .Language ()), ".txt" , "" ) // sorted by priority
165
+ extCount := len (exts )
166
+ readmeFiles := make ([]* namedBlob , extCount + 1 )
167
+
168
+ docsEntries := make ([]* git.TreeEntry , 3 ) // (one of docs/, .gitea/ or .github/)
161
169
for _ , entry := range entries {
162
170
if entry .IsDir () {
163
171
lowerName := strings .ToLower (entry .Name ())
@@ -178,47 +186,24 @@ func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string)
178
186
continue
179
187
}
180
188
181
- for i , ext := range exts {
182
- if markup .IsReadmeFile (entry .Name (), ext ) {
183
- log .Debug ("%s" , entry .Name ())
184
- name := entry .Name ()
185
- isSymlink := entry .IsLink ()
186
- target := entry
187
- if isSymlink {
188
- var err error
189
- target , err = entry .FollowLinks ()
190
- if err != nil && ! git .IsErrBadLink (err ) {
191
- ctx .ServerError ("FollowLinks" , err )
192
- return nil , ""
193
- }
194
- }
195
- log .Debug ("%t" , target == nil )
196
- if target != nil && (target .IsExecutable () || target .IsRegular ()) {
197
- readmeFiles [i ] = & namedBlob {
198
- name ,
199
- isSymlink ,
200
- target .Blob (),
201
- }
202
- }
203
- }
204
- }
205
-
206
- if markup .IsReadmeFile (entry .Name ()) {
189
+ if i , ok := markup .IsReadmeFileExtension (entry .Name (), exts ... ); ok {
190
+ log .Debug ("Potential readme file: %s" , entry .Name ())
207
191
name := entry .Name ()
208
192
isSymlink := entry .IsLink ()
193
+ target := entry
209
194
if isSymlink {
210
195
var err error
211
- entry , err = entry .FollowLinks ()
196
+ target , err = entry .FollowLinks ()
212
197
if err != nil && ! git .IsErrBadLink (err ) {
213
198
ctx .ServerError ("FollowLinks" , err )
214
199
return nil , ""
215
200
}
216
201
}
217
- if entry != nil && (entry .IsExecutable () || entry .IsRegular ()) {
218
- readmeFiles [3 ] = & namedBlob {
202
+ if target != nil && (target .IsExecutable () || target .IsRegular ()) {
203
+ readmeFiles [i ] = & namedBlob {
219
204
name ,
220
205
isSymlink ,
221
- entry .Blob (),
206
+ target .Blob (),
222
207
}
223
208
}
224
209
}
@@ -239,7 +224,7 @@ func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string)
239
224
continue
240
225
}
241
226
var err error
242
- readmeFile , err = getReadmeFileFromPath (ctx .Repo .Commit , entry .GetSubJumpablePathName ())
227
+ readmeFile , err = getReadmeFileFromPath (ctx , ctx .Repo .Commit , entry .GetSubJumpablePathName ())
243
228
if err != nil {
244
229
ctx .ServerError ("getReadmeFileFromPath" , err )
245
230
return nil , ""
0 commit comments