File tree 3 files changed +47
-7
lines changed 3 files changed +47
-7
lines changed Original file line number Diff line number Diff line change @@ -111,9 +111,14 @@ func IsMarkupFile(name, markup string) bool {
111
111
}
112
112
113
113
// IsReadmeFile reports whether name looks like a README file
114
- // based on its name.
115
- func IsReadmeFile (name string ) bool {
114
+ // based on its name. If an extension is provided, it will strictly
115
+ // match that extension.
116
+ // Note that the '.' should be provided in ext, e.g ".md"
117
+ func IsReadmeFile (name string , ext ... string ) bool {
116
118
name = strings .ToLower (name )
119
+ if len (ext ) > 0 {
120
+ return name == "readme" + ext [0 ]
121
+ }
117
122
if len (name ) < 6 {
118
123
return false
119
124
} else if len (name ) == 6 {
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ func TestMisc_IsReadmeFile(t *testing.T) {
19
19
"README" ,
20
20
"readME.mdown" ,
21
21
"README.md" ,
22
+ "readme.i18n.md" ,
22
23
}
23
24
falseTestCases := []string {
24
25
"test.md" ,
@@ -37,4 +38,25 @@ func TestMisc_IsReadmeFile(t *testing.T) {
37
38
for _ , testCase := range falseTestCases {
38
39
assert .False (t , IsReadmeFile (testCase ))
39
40
}
41
+
42
+ trueTestCasesStrict := [][]string {
43
+ {"readme" , "" },
44
+ {"readme.md" , ".md" },
45
+ {"readme.txt" , ".txt" },
46
+ }
47
+ falseTestCasesStrict := [][]string {
48
+ {"readme" , ".md" },
49
+ {"readme.md" , "" },
50
+ {"readme.md" , ".txt" },
51
+ {"readme.md" , "md" },
52
+ {"readmee.md" , ".md" },
53
+ {"readme.i18n.md" , ".md" },
54
+ }
55
+
56
+ for _ , testCase := range trueTestCasesStrict {
57
+ assert .True (t , IsReadmeFile (testCase [0 ], testCase [1 ]))
58
+ }
59
+ for _ , testCase := range falseTestCasesStrict {
60
+ assert .False (t , IsReadmeFile (testCase [0 ], testCase [1 ]))
61
+ }
40
62
}
Original file line number Diff line number Diff line change @@ -56,18 +56,31 @@ func renderDirectory(ctx *context.Context, treeLink string) {
56
56
return
57
57
}
58
58
59
- var readmeFile * git.Blob
59
+ // 3 for the extensions in exts[] in order
60
+ // the last one is for a readme that doesn't
61
+ // strictly match an extension
62
+ var readmeFiles [4 ]* git.Blob
63
+ var exts = []string {".md" , ".txt" , "" } // sorted by priority
60
64
for _ , entry := range entries {
61
65
if entry .IsDir () {
62
66
continue
63
67
}
64
68
65
- if ! markup .IsReadmeFile (entry .Name ()) {
66
- continue
69
+ for i , ext := range exts {
70
+ if markup .IsReadmeFile (entry .Name (), ext ) {
71
+ readmeFiles [i ] = entry .Blob ()
72
+ }
67
73
}
68
74
69
- readmeFile = entry .Blob ()
70
- if markup .Type (entry .Name ()) != "" {
75
+ if markup .IsReadmeFile (entry .Name ()) {
76
+ readmeFiles [3 ] = entry .Blob ()
77
+ }
78
+ }
79
+
80
+ var readmeFile * git.Blob
81
+ for _ , f := range readmeFiles {
82
+ if f != nil {
83
+ readmeFile = f
71
84
break
72
85
}
73
86
}
You can’t perform that action at this time.
0 commit comments