Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize "readme.md" #5691

Merged
merged 5 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions modules/markup/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ func IsMarkupFile(name, markup string) bool {
}

// IsReadmeFile reports whether name looks like a README file
wewark marked this conversation as resolved.
Show resolved Hide resolved
// 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 {
Expand Down
22 changes: 22 additions & 0 deletions modules/markup/markup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestMisc_IsReadmeFile(t *testing.T) {
"README",
"readME.mdown",
"README.md",
"readme.i18n.md",
}
falseTestCases := []string{
"test.md",
Expand All @@ -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{
wewark marked this conversation as resolved.
Show resolved Hide resolved
{"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]))
}
}
23 changes: 18 additions & 5 deletions routers/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
wewark marked this conversation as resolved.
Show resolved Hide resolved
var exts = []string{".md", ".txt", ""} // sorted by priority
wewark marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
Expand Down