diff options
author | Gary Wang <wzc782970009@gmail.com> | 2022-08-01 06:36:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-01 01:36:58 +0300 |
commit | c35535ce071c221fa0e529b89c3df462df769377 (patch) | |
tree | a52e3dd8847da9553bba5b27d7518ae9005d629a /modules/markup/renderer.go | |
parent | 335e918b1129ef6e0dd03a4713de90e68fbb23e3 (diff) | |
download | gitea-c35535ce071c221fa0e529b89c3df462df769377.tar.gz gitea-c35535ce071c221fa0e529b89c3df462df769377.zip |
Support localized README (#20508)
* Support localized README
* Slightly simplify getting the readme file and add some tests. Ensure that i18n also
works for docs/ etc.
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Update modules/markup/renderer.go
* Update modules/markup/renderer.go
* Update modules/markup/renderer.go
Co-authored-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/markup/renderer.go')
-rw-r--r-- | modules/markup/renderer.go | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index e88fa31187..9278d4c65d 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -310,14 +310,9 @@ func IsMarkupFile(name, markup string) bool { } // IsReadmeFile reports whether name looks like a README file -// 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 { +// based on its name. +func IsReadmeFile(name 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 { @@ -325,3 +320,27 @@ func IsReadmeFile(name string, ext ...string) bool { } 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) { + if len(name) < 6 || name[:6] != "readme" { + return 0, false + } + + for i, extension := range ext { + if name[6:] == extension { + return i, true + } + } + + if name[6] == '.' { + return len(ext), true + } + + return 0, false +} |