diff options
author | Nolann <62215577+Nolann71@users.noreply.github.com> | 2022-11-11 18:02:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-11 18:02:50 +0100 |
commit | 9f8e7789187013ab6752c2635b24c9dd1d70cd90 (patch) | |
tree | fe0e6c933d866cebe2aa09d322ba5d18f0ccea99 /routers/web | |
parent | 9db221780f28e161c02c4106f63d0b6f185933d4 (diff) | |
download | gitea-9f8e7789187013ab6752c2635b24c9dd1d70cd90.tar.gz gitea-9f8e7789187013ab6752c2635b24c9dd1d70cd90.zip |
Copy citation file content, in APA and BibTex format, on repo home page (#19999)
Add feature to easily copy CITATION.cff content in APA and BibTex format.
Diffstat (limited to 'routers/web')
-rw-r--r-- | routers/web/repo/view.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index d35ec48df0..e7aca04819 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -730,6 +730,44 @@ func checkHomeCodeViewable(ctx *context.Context) { ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo"))) } +func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) { + if entry.Name() != "" { + return + } + tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath) + if err != nil { + ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err) + return + } + allEntries, err := tree.ListEntries() + if err != nil { + ctx.ServerError("ListEntries", err) + return + } + for _, entry := range allEntries { + if entry.Name() == "CITATION.cff" || entry.Name() == "CITATION.bib" { + ctx.Data["CitiationExist"] = true + // Read Citation file contents + blob := entry.Blob() + dataRc, err := blob.DataAsync() + if err != nil { + ctx.ServerError("DataAsync", err) + return + } + defer dataRc.Close() + buf := make([]byte, 1024) + n, err := util.ReadAtMost(dataRc, buf) + if err != nil { + ctx.ServerError("ReadAtMost", err) + return + } + buf = buf[:n] + ctx.PageData["citationFileContent"] = string(buf) + break + } + } +} + // Home render repository home page func Home(ctx *context.Context) { isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req) @@ -954,6 +992,13 @@ func renderCode(ctx *context.Context) { return } + if !ctx.Repo.Repository.IsEmpty { + checkCitationFile(ctx, entry) + if ctx.Written() { + return + } + } + renderLanguageStats(ctx) if ctx.Written() { return |