]> source.dussan.org Git - gitea.git/commitdiff
Prioritize "readme.md" (#5691)
authorKhaled Hamed <khaled.hamedt@gmail.com>
Mon, 14 Jan 2019 19:15:06 +0000 (21:15 +0200)
committertechknowlogick <hello@techknowlogick.com>
Mon, 14 Jan 2019 19:15:06 +0000 (14:15 -0500)
* prioritize readme.md

* Improve IsReadmeFile

* Add more tests

modules/markup/markup.go
modules/markup/markup_test.go
routers/repo/view.go

index d17270fb011f6228a9a0626cd3ca980d31ab59e5..c5060077236750fbfb59b26425750f2c34d6a9f1 100644 (file)
@@ -111,9 +111,14 @@ func IsMarkupFile(name, markup string) bool {
 }
 
 // IsReadmeFile reports whether name looks like a README file
-// based on its name.
-func IsReadmeFile(name string) bool {
+// based on its name. If an extension is provided, it will strictly
+// match that extension.
+// Note that the '.' should be provided in ext, e.g ".md"
+func IsReadmeFile(name string, ext ...string) bool {
        name = strings.ToLower(name)
+       if len(ext) > 0 {
+               return name == "readme"+ext[0]
+       }
        if len(name) < 6 {
                return false
        } else if len(name) == 6 {
index b0ebfae57d725f18818c04a04bc1b6ba4c24c92b..118fa2632ba5829368b105d748b54ea6636239e2 100644 (file)
@@ -19,6 +19,7 @@ func TestMisc_IsReadmeFile(t *testing.T) {
                "README",
                "readME.mdown",
                "README.md",
+               "readme.i18n.md",
        }
        falseTestCases := []string{
                "test.md",
@@ -37,4 +38,25 @@ func TestMisc_IsReadmeFile(t *testing.T) {
        for _, testCase := range falseTestCases {
                assert.False(t, IsReadmeFile(testCase))
        }
+
+       trueTestCasesStrict := [][]string{
+               {"readme", ""},
+               {"readme.md", ".md"},
+               {"readme.txt", ".txt"},
+       }
+       falseTestCasesStrict := [][]string{
+               {"readme", ".md"},
+               {"readme.md", ""},
+               {"readme.md", ".txt"},
+               {"readme.md", "md"},
+               {"readmee.md", ".md"},
+               {"readme.i18n.md", ".md"},
+       }
+
+       for _, testCase := range trueTestCasesStrict {
+               assert.True(t, IsReadmeFile(testCase[0], testCase[1]))
+       }
+       for _, testCase := range falseTestCasesStrict {
+               assert.False(t, IsReadmeFile(testCase[0], testCase[1]))
+       }
 }
index 78a305aa28aa379fc72a9134dec2f8a43c75325d..8739c139d975c3d65c5bce02ea56a43c915f2bc1 100644 (file)
@@ -56,18 +56,31 @@ func renderDirectory(ctx *context.Context, treeLink string) {
                return
        }
 
-       var readmeFile *git.Blob
+       // 3 for the extensions in exts[] in order
+       // the last one is for a readme that doesn't
+       // strictly match an extension
+       var readmeFiles [4]*git.Blob
+       var exts = []string{".md", ".txt", ""} // sorted by priority
        for _, entry := range entries {
                if entry.IsDir() {
                        continue
                }
 
-               if !markup.IsReadmeFile(entry.Name()) {
-                       continue
+               for i, ext := range exts {
+                       if markup.IsReadmeFile(entry.Name(), ext) {
+                               readmeFiles[i] = entry.Blob()
+                       }
                }
 
-               readmeFile = entry.Blob()
-               if markup.Type(entry.Name()) != "" {
+               if markup.IsReadmeFile(entry.Name()) {
+                       readmeFiles[3] = entry.Blob()
+               }
+       }
+
+       var readmeFile *git.Blob
+       for _, f := range readmeFiles {
+               if f != nil {
+                       readmeFile = f
                        break
                }
        }