-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Copy citation file content, in APA and BibTex format, on repo home page #19999
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
Changes from 52 commits
003f608
5dbffa0
711fcaa
c0c02a9
bdb76c1
344c926
6284a7c
8c69928
287c687
982f389
d07052e
10c9949
c0cf481
abcde25
586f9b5
04f1acd
15618d5
ce5a37a
e62420f
24841e7
b57fb0e
600b057
38dd4a6
134cdad
af4b319
be912ed
7e84d1f
b50ad8a
6583f38
ef0b8da
ae70a3f
d407e5c
1592af9
3180c00
4050352
9baf3ca
14c99d4
4df0438
7498f94
04d2bef
ea6360e
1047d5f
d71f0dd
d6e1db0
f2f0bfe
d14c968
649b745
e4f0287
6be7ba5
05cd7c6
99136ac
75e7324
a62b1d8
7f2cf5e
9991e9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we just need to check this in function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think. Here I want to handle citation no render directory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But when rendering a file, there is no download menu. |
||
checkCitationFile(ctx, entry) | ||
if ctx.Written() { | ||
return | ||
} | ||
} | ||
|
||
renderLanguageStats(ctx) | ||
if ctx.Written() { | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<button class="ui basic citation button" id="citation-copy-apa" data-text=""> | ||
APA | ||
</button> | ||
<button class="ui basic citation button" id="citation-copy-bibtex" data-text=""> | ||
BibTeX | ||
</button> | ||
<!-- the value will be updated by initCitationFileCopyContent, the code below is used to avoid UI flicking --> | ||
<input id="citation-copy-content" value="" size="1" readonly> | ||
<button class="ui basic icon button tooltip" id="citation-clipboard-btn" data-content="{{.locale.Tr "copy"}}" data-clipboard-text="" data-clipboard-target="#citation-copy-content"> | ||
{{svg "octicon-copy"}} | ||
</button> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="ui tiny modal" id="cite-repo-modal"> | ||
<div class="header"> | ||
{{.locale.Tr "repo.cite_this_repo"}} | ||
</div> | ||
<div class="content"> | ||
<div class="ui stackable secondary menu mobile--margin-between-items mobile--no-negative-margins no-vertical-tabs"> | ||
<div class="fitted item"> | ||
<div class="ui action input" id="citation-panel"> | ||
{{template "repo/cite/cite_buttons" .}} | ||
<a id="goto-citation-btn" class="ui basic jump icon button tooltip" href="{{$.RepoLink}}/src/{{$.BranchName}}/CITATION.cff" data-position="top right" data-content="{{.locale.Tr "repo.find_file.go_to_file"}}"> | ||
{{svg "octicon-file-moved"}} | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="actions"> | ||
<div class="ui black deny button"> | ||
{{.locale.Tr "cancel"}} | ||
</div> | ||
</div> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import $ from 'jquery'; | ||
|
||
const {pageData} = window.config; | ||
|
||
const initInputCitationValue = async ($citationCopyBibtex, $citationCopyApa) => { | ||
const [{Cite, plugins}] = await Promise.all([ | ||
import(/* webpackChunkName: "citation-js-core" */'@citation-js/core'), | ||
import(/* webpackChunkName: "citation-js-formats" */'@citation-js/plugin-software-formats'), | ||
import(/* webpackChunkName: "citation-js-bibtex" */'@citation-js/plugin-bibtex'), | ||
import(/* webpackChunkName: "citation-js-bibtex" */'@citation-js/plugin-csl'), | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]); | ||
const {citationFileContent} = pageData; | ||
const config = plugins.config.get('@bibtex'); | ||
config.constants.fieldTypes.doi = ['field', 'literal']; | ||
config.constants.fieldTypes.version = ['field', 'literal']; | ||
const citationFormatter = new Cite(citationFileContent); | ||
const lang = document.documentElement.lang || 'en-US'; | ||
const apaOutput = citationFormatter.format('bibliography', {template: 'apa', lang}); | ||
const bibtexOutput = citationFormatter.format('bibtex', {lang}); | ||
$citationCopyBibtex.attr('data-text', bibtexOutput); | ||
$citationCopyApa.attr('data-text', apaOutput); | ||
}; | ||
|
||
export function initCitationFileCopyContent() { | ||
const defaultCitationFormat = 'apa'; // apa or bibtex | ||
|
||
sarrooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!pageData.citationFileContent) return; | ||
|
||
const $citationCopyApa = $('#citation-copy-apa'); | ||
const $citationCopyBibtex = $('#citation-copy-bibtex'); | ||
sarrooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const $inputContent = $('#citation-copy-content'); | ||
|
||
if ((!$citationCopyApa.length && !$citationCopyBibtex.length) || !$inputContent.length) return; | ||
const updateUi = () => { | ||
const isBibtex = (localStorage.getItem('citation-copy-format') || defaultCitationFormat) === 'bibtex'; | ||
const copyContent = (isBibtex ? $citationCopyBibtex : $citationCopyApa).attr('data-text'); | ||
|
||
$inputContent.val(copyContent); | ||
$citationCopyBibtex.toggleClass('primary', isBibtex); | ||
$citationCopyApa.toggleClass('primary', !isBibtex); | ||
sarrooo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
initInputCitationValue($citationCopyApa, $citationCopyBibtex).then(updateUi); | ||
|
||
$citationCopyApa.on('click', () => { | ||
localStorage.setItem('citation-copy-format', 'apa'); | ||
updateUi(); | ||
}); | ||
$citationCopyBibtex.on('click', () => { | ||
localStorage.setItem('citation-copy-format', 'bibtex'); | ||
updateUi(); | ||
}); | ||
|
||
$inputContent.on('click', () => { | ||
$inputContent.select(); | ||
}); | ||
|
||
$('#cite-repo-button').on('click', () => { | ||
$('#cite-repo-modal').modal('show'); | ||
}); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.