@@ -67,7 +67,7 @@ func linesBytesCount(s []byte) int {
67
67
}
68
68
69
69
// FIXME: There has to be a more efficient way of doing this
70
- func getReadmeFileFromPath (commit * git.Commit , treePath string ) (* namedBlob , error ) {
70
+ func getReadmeFileFromPath (ctx * context. Context , commit * git.Commit , treePath string ) (* namedBlob , error ) {
71
71
tree , err := commit .SubTree (treePath )
72
72
if err != nil {
73
73
return nil , err
@@ -78,50 +78,33 @@ func getReadmeFileFromPath(commit *git.Commit, treePath string) (*namedBlob, err
78
78
return nil , err
79
79
}
80
80
81
- var readmeFiles [4 ]* namedBlob
82
- exts := []string {".md" , ".txt" , "" } // sorted by priority
81
+ // Create a list of extensions in priority order
82
+ // 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
83
+ // 2. Txt files - e.g. README.txt
84
+ // 3. No extension - e.g. README
85
+ exts := append (localizedExtensions (".md" , ctx .Language ()), ".txt" , "" ) // sorted by priority
86
+ extCount := len (exts )
87
+ readmeFiles := make ([]* namedBlob , extCount + 1 )
83
88
for _ , entry := range entries {
84
89
if entry .IsDir () {
85
90
continue
86
91
}
87
- for i , ext := range exts {
88
- if markup .IsReadmeFile (entry .Name (), ext ) {
89
- if readmeFiles [i ] == nil || base .NaturalSortLess (readmeFiles [i ].name , entry .Blob ().Name ()) {
90
- name := entry .Name ()
91
- isSymlink := entry .IsLink ()
92
- target := entry
93
- if isSymlink {
94
- target , err = entry .FollowLinks ()
95
- if err != nil && ! git .IsErrBadLink (err ) {
96
- return nil , err
97
- }
98
- }
99
- if target != nil && (target .IsExecutable () || target .IsRegular ()) {
100
- readmeFiles [i ] = & namedBlob {
101
- name ,
102
- isSymlink ,
103
- target .Blob (),
104
- }
105
- }
106
- }
107
- }
108
- }
109
-
110
- if markup .IsReadmeFile (entry .Name ()) {
111
- if readmeFiles [3 ] == nil || base .NaturalSortLess (readmeFiles [3 ].name , entry .Blob ().Name ()) {
92
+ if i , ok := markup .IsReadmeFileExtension (entry .Name (), exts ... ); ok {
93
+ if readmeFiles [i ] == nil || base .NaturalSortLess (readmeFiles [i ].name , entry .Blob ().Name ()) {
112
94
name := entry .Name ()
113
95
isSymlink := entry .IsLink ()
96
+ target := entry
114
97
if isSymlink {
115
- entry , err = entry .FollowLinks ()
98
+ target , err = entry .FollowLinks ()
116
99
if err != nil && ! git .IsErrBadLink (err ) {
117
100
return nil , err
118
101
}
119
102
}
120
- if entry != nil && (entry .IsExecutable () || entry .IsRegular ()) {
121
- readmeFiles [3 ] = & namedBlob {
103
+ if target != nil && (target .IsExecutable () || target .IsRegular ()) {
104
+ readmeFiles [i ] = & namedBlob {
122
105
name ,
123
106
isSymlink ,
124
- entry .Blob (),
107
+ target .Blob (),
125
108
}
126
109
}
127
110
}
@@ -161,8 +144,11 @@ func renderDirectory(ctx *context.Context, treeLink string) {
161
144
renderReadmeFile (ctx , readmeFile , readmeTreelink )
162
145
}
163
146
164
- // Note: This will always return lower-case strings
165
- func localizedExtensions (ext string , languageCode string ) (localizedExts []string ) {
147
+ // localizedExtensions prepends the provided language code with and without a
148
+ // regional identifier to the provided extenstion.
149
+ // Note: the language code will always be lower-cased, if a region is present it must be separated with a `-`
150
+ // Note: ext should be prefixed with a `.`
151
+ func localizedExtensions (ext , languageCode string ) (localizedExts []string ) {
166
152
if len (languageCode ) < 1 {
167
153
return []string {ext }
168
154
}
@@ -181,14 +167,15 @@ func localizedExtensions(ext string, languageCode string) (localizedExts []strin
181
167
}
182
168
183
169
func findReadmeFile (ctx * context.Context , entries git.Entries , treeLink string ) (* namedBlob , string ) {
184
- // 3 kinds of extensions in exts[] in order
185
- // the last one is for a readme that doesn't
186
- // strictly match an extension
170
+ // Create a list of extensions in priority order
171
+ // 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
172
+ // 2. Txt files - e.g. README.txt
173
+ // 3. No extension - e.g. README
187
174
exts := append (localizedExtensions (".md" , ctx .Language ()), ".txt" , "" ) // sorted by priority
188
175
extCount := len (exts )
189
176
readmeFiles := make ([]* namedBlob , extCount + 1 )
190
- docsEntries := make ([]* git.TreeEntry , extCount )
191
177
178
+ docsEntries := make ([]* git.TreeEntry , 3 ) // (one of docs/, .gitea/ or .github/)
192
179
for _ , entry := range entries {
193
180
if entry .IsDir () {
194
181
lowerName := strings .ToLower (entry .Name ())
@@ -209,47 +196,24 @@ func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string)
209
196
continue
210
197
}
211
198
212
- for i , ext := range exts {
213
- if markup .IsReadmeFile (entry .Name (), ext ) {
214
- log .Debug ("%s" , entry .Name ())
215
- name := entry .Name ()
216
- isSymlink := entry .IsLink ()
217
- target := entry
218
- if isSymlink {
219
- var err error
220
- target , err = entry .FollowLinks ()
221
- if err != nil && ! git .IsErrBadLink (err ) {
222
- ctx .ServerError ("FollowLinks" , err )
223
- return nil , ""
224
- }
225
- }
226
- log .Debug ("%t" , target == nil )
227
- if target != nil && (target .IsExecutable () || target .IsRegular ()) {
228
- readmeFiles [i ] = & namedBlob {
229
- name ,
230
- isSymlink ,
231
- target .Blob (),
232
- }
233
- }
234
- }
235
- }
236
-
237
- if markup .IsReadmeFile (entry .Name ()) {
199
+ if i , ok := markup .IsReadmeFileExtension (entry .Name (), exts ... ); ok {
200
+ log .Debug ("Potential readme file: %s" , entry .Name ())
238
201
name := entry .Name ()
239
202
isSymlink := entry .IsLink ()
203
+ target := entry
240
204
if isSymlink {
241
205
var err error
242
- entry , err = entry .FollowLinks ()
206
+ target , err = entry .FollowLinks ()
243
207
if err != nil && ! git .IsErrBadLink (err ) {
244
208
ctx .ServerError ("FollowLinks" , err )
245
209
return nil , ""
246
210
}
247
211
}
248
- if entry != nil && (entry .IsExecutable () || entry .IsRegular ()) {
249
- readmeFiles [extCount ] = & namedBlob {
212
+ if target != nil && (target .IsExecutable () || target .IsRegular ()) {
213
+ readmeFiles [i ] = & namedBlob {
250
214
name ,
251
215
isSymlink ,
252
- entry .Blob (),
216
+ target .Blob (),
253
217
}
254
218
}
255
219
}
@@ -270,7 +234,7 @@ func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string)
270
234
continue
271
235
}
272
236
var err error
273
- readmeFile , err = getReadmeFileFromPath (ctx .Repo .Commit , entry .GetSubJumpablePathName ())
237
+ readmeFile , err = getReadmeFileFromPath (ctx , ctx .Repo .Commit , entry .GetSubJumpablePathName ())
274
238
if err != nil {
275
239
ctx .ServerError ("getReadmeFileFromPath" , err )
276
240
return nil , ""
0 commit comments