From 7b5b739a2f9d9c84ec216ff629dbf0d1dd8a4322 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 13 Feb 2023 15:01:09 -0500 Subject: [PATCH] Move `IsReadmeFile*` from `modules/markup/` to `modules/util` (#22877) These functions don't examine contents, just filenames, so they don't fit in well in a markup module. This was originally part of https://github.com/go-gitea/gitea/pull/22177. Signed-off-by: Nick Guenther --- modules/markup/renderer.go | 38 -------------- modules/markup/renderer_test.go | 91 --------------------------------- modules/util/path.go | 39 ++++++++++++++ modules/util/path_test.go | 81 +++++++++++++++++++++++++++++ routers/web/repo/view.go | 4 +- 5 files changed, 122 insertions(+), 131 deletions(-) diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index 5a31e961fb588..df2c9ebfc6156 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -317,41 +317,3 @@ func IsMarkupFile(name, markup string) bool { } return false } - -// IsReadmeFile reports whether name looks like a README file -// based on its name. -func IsReadmeFile(name string) bool { - name = strings.ToLower(name) - if len(name) < 6 { - return false - } else if len(name) == 6 { - return name == "readme" - } - return name[:7] == "readme." -} - -// IsReadmeFileExtension reports whether name looks like a README file -// based on its name. It will look through the provided extensions and check if the file matches -// one of the extensions and provide the index in the extension list. -// If the filename is `readme.` with an unmatched extension it will match with the index equaling -// the length of the provided extension list. -// Note that the '.' should be provided in ext, e.g ".md" -func IsReadmeFileExtension(name string, ext ...string) (int, bool) { - name = strings.ToLower(name) - if len(name) < 6 || name[:6] != "readme" { - return 0, false - } - - for i, extension := range ext { - extension = strings.ToLower(extension) - if name[6:] == extension { - return i, true - } - } - - if name[6] == '.' { - return len(ext), true - } - - return 0, false -} diff --git a/modules/markup/renderer_test.go b/modules/markup/renderer_test.go index 624558c3f7342..0791081f94e8f 100644 --- a/modules/markup/renderer_test.go +++ b/modules/markup/renderer_test.go @@ -2,94 +2,3 @@ // SPDX-License-Identifier: MIT package markup_test - -import ( - "testing" - - . "code.gitea.io/gitea/modules/markup" - - _ "code.gitea.io/gitea/modules/markup/markdown" - - "github.com/stretchr/testify/assert" -) - -func TestMisc_IsReadmeFile(t *testing.T) { - trueTestCases := []string{ - "readme", - "README", - "readME.mdown", - "README.md", - "readme.i18n.md", - } - falseTestCases := []string{ - "test.md", - "wow.MARKDOWN", - "LOL.mDoWn", - "test", - "abcdefg", - "abcdefghijklmnopqrstuvwxyz", - "test.md.test", - "readmf", - } - - for _, testCase := range trueTestCases { - assert.True(t, IsReadmeFile(testCase)) - } - for _, testCase := range falseTestCases { - assert.False(t, IsReadmeFile(testCase)) - } - - type extensionTestcase struct { - name string - expected bool - idx int - } - - exts := []string{".md", ".txt", ""} - testCasesExtensions := []extensionTestcase{ - { - name: "readme", - expected: true, - idx: 2, - }, - { - name: "readme.md", - expected: true, - idx: 0, - }, - { - name: "README.md", - expected: true, - idx: 0, - }, - { - name: "ReAdMe.Md", - expected: true, - idx: 0, - }, - { - name: "readme.txt", - expected: true, - idx: 1, - }, - { - name: "readme.doc", - expected: true, - idx: 3, - }, - { - name: "readmee.md", - }, - { - name: "readme..", - expected: true, - idx: 3, - }, - } - - for _, testCase := range testCasesExtensions { - idx, ok := IsReadmeFileExtension(testCase.name, exts...) - assert.Equal(t, testCase.expected, ok) - assert.Equal(t, testCase.idx, idx) - } -} diff --git a/modules/util/path.go b/modules/util/path.go index e060b527f3f1e..74acb7a85fd87 100644 --- a/modules/util/path.go +++ b/modules/util/path.go @@ -11,6 +11,7 @@ import ( "path/filepath" "regexp" "runtime" + "strings" ) // EnsureAbsolutePath ensure that a path is absolute, making it @@ -201,3 +202,41 @@ func CommonSkip(name string) bool { return false } + +// IsReadmeFileName reports whether name looks like a README file +// based on its name. +func IsReadmeFileName(name string) bool { + name = strings.ToLower(name) + if len(name) < 6 { + return false + } else if len(name) == 6 { + return name == "readme" + } + return name[:7] == "readme." +} + +// IsReadmeFileExtension reports whether name looks like a README file +// based on its name. It will look through the provided extensions and check if the file matches +// one of the extensions and provide the index in the extension list. +// If the filename is `readme.` with an unmatched extension it will match with the index equaling +// the length of the provided extension list. +// Note that the '.' should be provided in ext, e.g ".md" +func IsReadmeFileExtension(name string, ext ...string) (int, bool) { + name = strings.ToLower(name) + if len(name) < 6 || name[:6] != "readme" { + return 0, false + } + + for i, extension := range ext { + extension = strings.ToLower(extension) + if name[6:] == extension { + return i, true + } + } + + if name[6] == '.' { + return len(ext), true + } + + return 0, false +} diff --git a/modules/util/path_test.go b/modules/util/path_test.go index 5794df1ebf33d..93f4f67cf6486 100644 --- a/modules/util/path_test.go +++ b/modules/util/path_test.go @@ -55,3 +55,84 @@ func TestFileURLToPath(t *testing.T) { } } } + +func TestMisc_IsReadmeFileName(t *testing.T) { + trueTestCases := []string{ + "readme", + "README", + "readME.mdown", + "README.md", + "readme.i18n.md", + } + falseTestCases := []string{ + "test.md", + "wow.MARKDOWN", + "LOL.mDoWn", + "test", + "abcdefg", + "abcdefghijklmnopqrstuvwxyz", + "test.md.test", + "readmf", + } + + for _, testCase := range trueTestCases { + assert.True(t, IsReadmeFileName(testCase)) + } + for _, testCase := range falseTestCases { + assert.False(t, IsReadmeFileName(testCase)) + } + + type extensionTestcase struct { + name string + expected bool + idx int + } + + exts := []string{".md", ".txt", ""} + testCasesExtensions := []extensionTestcase{ + { + name: "readme", + expected: true, + idx: 2, + }, + { + name: "readme.md", + expected: true, + idx: 0, + }, + { + name: "README.md", + expected: true, + idx: 0, + }, + { + name: "ReAdMe.Md", + expected: true, + idx: 0, + }, + { + name: "readme.txt", + expected: true, + idx: 1, + }, + { + name: "readme.doc", + expected: true, + idx: 3, + }, + { + name: "readmee.md", + }, + { + name: "readme..", + expected: true, + idx: 3, + }, + } + + for _, testCase := range testCasesExtensions { + idx, ok := IsReadmeFileExtension(testCase.name, exts...) + assert.Equal(t, testCase.expected, ok) + assert.Equal(t, testCase.idx, idx) + } +} diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index ca86c744d115c..8f2213a68cd38 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -96,7 +96,7 @@ func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry) (*n } continue } - if i, ok := markup.IsReadmeFileExtension(entry.Name(), exts...); ok { + if i, ok := util.IsReadmeFileExtension(entry.Name(), exts...); ok { log.Debug("Potential readme file: %s", entry.Name()) if readmeFiles[i] == nil || base.NaturalSortLess(readmeFiles[i].name, entry.Blob().Name()) { name := entry.Name() @@ -423,7 +423,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc)) shouldRenderSource := ctx.FormString("display") == "source" - readmeExist := markup.IsReadmeFile(blob.Name()) + readmeExist := util.IsReadmeFileName(blob.Name()) ctx.Data["ReadmeExist"] = readmeExist markupType := markup.Type(blob.Name())