Refactor readme file renderer (#19502)
* Refactor readme file renderer * improve
This commit is contained in:
parent
e4274f640c
commit
d71df01077
1 changed files with 123 additions and 110 deletions
|
@ -146,6 +146,21 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
|
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check permission to add or upload new file.
|
||||||
|
if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch {
|
||||||
|
ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
|
||||||
|
ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
|
||||||
|
}
|
||||||
|
|
||||||
|
readmeFile, readmeTreelink := findReadmeFile(ctx, entries, treeLink)
|
||||||
|
if ctx.Written() || readmeFile == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
renderReadmeFile(ctx, readmeFile, readmeTreelink)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string) (*namedBlob, string) {
|
||||||
// 3 for the extensions in exts[] in order
|
// 3 for the extensions in exts[] in order
|
||||||
// the last one is for a readme that doesn't
|
// the last one is for a readme that doesn't
|
||||||
// strictly match an extension
|
// strictly match an extension
|
||||||
|
@ -183,7 +198,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
target, err = entry.FollowLinks()
|
target, err = entry.FollowLinks()
|
||||||
if err != nil && !git.IsErrBadLink(err) {
|
if err != nil && !git.IsErrBadLink(err) {
|
||||||
ctx.ServerError("FollowLinks", err)
|
ctx.ServerError("FollowLinks", err)
|
||||||
return
|
return nil, ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Debug("%t", target == nil)
|
log.Debug("%t", target == nil)
|
||||||
|
@ -205,7 +220,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
entry, err = entry.FollowLinks()
|
entry, err = entry.FollowLinks()
|
||||||
if err != nil && !git.IsErrBadLink(err) {
|
if err != nil && !git.IsErrBadLink(err) {
|
||||||
ctx.ServerError("FollowLinks", err)
|
ctx.ServerError("FollowLinks", err)
|
||||||
return
|
return nil, ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
|
if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
|
||||||
|
@ -236,7 +251,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
readmeFile, err = getReadmeFileFromPath(ctx.Repo.Commit, entry.GetSubJumpablePathName())
|
readmeFile, err = getReadmeFileFromPath(ctx.Repo.Commit, entry.GetSubJumpablePathName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("getReadmeFileFromPath", err)
|
ctx.ServerError("getReadmeFileFromPath", err)
|
||||||
return
|
return nil, ""
|
||||||
}
|
}
|
||||||
if readmeFile != nil {
|
if readmeFile != nil {
|
||||||
readmeFile.name = entry.Name() + "/" + readmeFile.name
|
readmeFile.name = entry.Name() + "/" + readmeFile.name
|
||||||
|
@ -245,8 +260,10 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return readmeFile, readmeTreelink
|
||||||
|
}
|
||||||
|
|
||||||
if readmeFile != nil {
|
func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelink string) {
|
||||||
ctx.Data["RawFileLink"] = ""
|
ctx.Data["RawFileLink"] = ""
|
||||||
ctx.Data["ReadmeInList"] = true
|
ctx.Data["ReadmeInList"] = true
|
||||||
ctx.Data["ReadmeExist"] = true
|
ctx.Data["ReadmeExist"] = true
|
||||||
|
@ -314,17 +331,22 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isTextFile {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !isLFSFile {
|
if !isLFSFile {
|
||||||
fileSize = readmeFile.blob.Size()
|
fileSize = readmeFile.blob.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
if isTextFile {
|
|
||||||
if fileSize >= setting.UI.MaxDisplayFileSize {
|
if fileSize >= setting.UI.MaxDisplayFileSize {
|
||||||
// Pretend that this is a normal text file to display 'This file is too large to be shown'
|
// Pretend that this is a normal text file to display 'This file is too large to be shown'
|
||||||
ctx.Data["IsFileTooLarge"] = true
|
ctx.Data["IsFileTooLarge"] = true
|
||||||
ctx.Data["IsTextFile"] = true
|
ctx.Data["IsTextFile"] = true
|
||||||
ctx.Data["FileSize"] = fileSize
|
ctx.Data["FileSize"] = fileSize
|
||||||
} else {
|
return
|
||||||
|
}
|
||||||
|
|
||||||
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
|
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
|
||||||
|
|
||||||
if markupType := markup.Type(readmeFile.name); markupType != "" {
|
if markupType := markup.Type(readmeFile.name); markupType != "" {
|
||||||
|
@ -360,15 +382,6 @@ func renderDirectory(ctx *context.Context, treeLink string) {
|
||||||
gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
|
gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check permission to add or upload new file.
|
|
||||||
if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch {
|
|
||||||
ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
|
|
||||||
ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
|
func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
|
||||||
|
|
Loading…
Reference in a new issue