Skip to content
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

Copy citation file content, in APA and BibTex format, on repo home page #19999

Merged
merged 55 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
003f608
add: add function to read and stock citation file content into PageData
sarrooo Jun 17, 2022
5dbffa0
add: add template to handle citation file on repo home page (same loo…
sarrooo Jun 17, 2022
711fcaa
fix: fix lint errors
sarrooo Jun 17, 2022
c0c02a9
fix: forgot to check if the repo is empty, cause test crash
sarrooo Jun 17, 2022
bdb76c1
Merge branch 'main' into feature/citation-file
sarrooo Jun 17, 2022
344c926
fix: fix test crash when server render file (add check if we are at /…
sarrooo Jun 18, 2022
6284a7c
Merge branch 'feature/citation-file' of github.com:Nolann71/gitea int…
sarrooo Jun 18, 2022
8c69928
Update routers/web/repo/view.go
sarrooo Jun 27, 2022
287c687
fix: gusted suggested fixes
sarrooo Jun 29, 2022
982f389
fix: roll back error handling that cause tests crash
sarrooo Jul 5, 2022
d07052e
Update templates/repo/citation_buttons.tmpl
sarrooo Jul 14, 2022
10c9949
Update templates/repo/home.tmpl
sarrooo Jul 14, 2022
c0cf481
Merge branch 'main' into feature/citation-file
sarrooo Jul 14, 2022
abcde25
fix: fix package-lock json
sarrooo Jul 14, 2022
586f9b5
add: change previous button, add button in download dropdown to open …
sarrooo Jul 18, 2022
04f1acd
Merge branch 'main' into feature/citation-file
sarrooo Jul 18, 2022
15618d5
fix: fix frontend lint
sarrooo Jul 18, 2022
ce5a37a
fix: fix package-lock.json
sarrooo Jul 18, 2022
e62420f
fix: fix backend lint
sarrooo Jul 18, 2022
24841e7
fix: move citation functions from repo-common.js to citation.js, add …
sarrooo Jul 19, 2022
b57fb0e
fix: remove citation-js and add @citation-js/core and @citation-js/csl
sarrooo Jul 19, 2022
600b057
fix: fix lazy-load
sarrooo Jul 19, 2022
38dd4a6
Merge branch 'main' into feature/citation-file
sarrooo Jul 19, 2022
134cdad
fix: remove string into locale-fr-FR to use https://crowdin.com/proje…
sarrooo Jul 19, 2022
af4b319
fix: fix package-lock.json
sarrooo Jul 19, 2022
be912ed
fix: revert empty line (local fr)
sarrooo Jul 21, 2022
7e84d1f
fix: remove no-transition class (useless), rename download-btn to too…
sarrooo Jul 21, 2022
b50ad8a
merge: merge main
sarrooo Jul 21, 2022
6583f38
revert: revert package-lock.json
sarrooo Jul 21, 2022
ef0b8da
npm install --package-lock-only
sarrooo Jul 21, 2022
ae70a3f
fix: change tooltip-btn to more-btn
sarrooo Jul 21, 2022
d407e5c
fix: move checkCitationFile to renderCode to avoid two calls to function
sarrooo Jul 21, 2022
1592af9
fix: check empty repo before call func
sarrooo Jul 21, 2022
3180c00
fix: remove inline script
sarrooo Jul 21, 2022
4050352
fix: css and add newline where I forgot
sarrooo Jul 23, 2022
9baf3ca
fix: style and add a check js side
sarrooo Nov 3, 2022
14c99d4
merge: master to feature/citation-file
sarrooo Nov 4, 2022
4df0438
feat: add bibtex input handling
sarrooo Nov 4, 2022
7498f94
fix: npm install --package-lock-only
sarrooo Nov 4, 2022
04d2bef
fix: fix lint-fronted
sarrooo Nov 4, 2022
ea6360e
Merge branch 'main' into feature/citation-file
sarrooo Nov 4, 2022
1047d5f
Merge branch 'main' into feature/citation-file
sarrooo Nov 5, 2022
d71f0dd
Merge branch 'main' into feature/citation-file
6543 Nov 8, 2022
d6e1db0
Merge branch 'main' into feature/citation-file
6543 Nov 8, 2022
f2f0bfe
feat: replace download button by a more button (home page)
sarrooo Nov 9, 2022
d14c968
Merge branch 'main' into feature/citation-file
sarrooo Nov 9, 2022
649b745
Merge branch 'main' into feature/citation-file
6543 Nov 9, 2022
e4f0287
Merge branch 'main' into feature/citation-file
6543 Nov 10, 2022
6be7ba5
Merge branch 'main' into feature/citation-file
6543 Nov 10, 2022
05cd7c6
Merge branch 'main' into feature/citation-file
6543 Nov 10, 2022
99136ac
Merge branch 'main' into feature/citation-file
6543 Nov 10, 2022
75e7324
fix: fix typo, move cite file template into own folder
sarrooo Nov 11, 2022
a62b1d8
fix: change space to tab
sarrooo Nov 11, 2022
7f2cf5e
Merge branch 'main' into feature/citation-file
sarrooo Nov 11, 2022
9991e9a
Merge branch 'main' into feature/citation-file
6543 Nov 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1013,10 +1013,12 @@ unstar = Unstar
star = Star
fork = Fork
download_archive = Download Repository
more_actions = More Actions

no_desc = No Description
quick_guide = Quick Guide
clone_this_repo = Clone this repository
cite_this_repo = Cite this repository
create_new_repo_command = Creating a new repository on the command line
push_exist_repo = Pushing an existing repository from the command line
empty_message = This repository does not contain any content.
Expand Down
289 changes: 271 additions & 18 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"node": ">= 14.0.0"
},
"dependencies": {
"@citation-js/core": "0.6.1",
"@citation-js/plugin-bibtex": "0.6.1",
"@citation-js/plugin-csl": "0.6.3",
"@citation-js/plugin-software-formats": "0.6.0",
"@claviska/jquery-minicolors": "2.3.6",
"@mcaptcha/vanilla-glue": "0.1.0-alpha-3",
"@primer/octicons": "17.7.0",
Expand Down
45 changes: 45 additions & 0 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, _ := util.ReadAtMost(dataRc, buf)
sarrooo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
ctx.ServerError("ReadAtMost", err)
return
}
buf = buf[:n]
ctx.PageData["citiationFileContent"] = string(buf)
break
}
}
}

// Home render repository home page
func Home(ctx *context.Context) {
isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req)
Expand Down Expand Up @@ -954,6 +992,13 @@ func renderCode(ctx *context.Context) {
return
}
sarrooo marked this conversation as resolved.
Show resolved Hide resolved

if !ctx.Repo.Repository.IsEmpty {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we just need to check this in function renderDirectory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
11 changes: 11 additions & 0 deletions templates/repo/citation_buttons.tmpl
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>
29 changes: 27 additions & 2 deletions templates/repo/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,44 @@
{{if eq $n 0}}
<div class="ui action tiny input" id="clone-panel">
{{template "repo/clone_buttons" .}}
<button id="download-btn" class="ui basic small compact jump dropdown icon button tooltip" data-content="{{.locale.Tr "repo.download_archive"}}" data-position="top right">
{{svg "octicon-download"}}
<button id="more-btn" class="ui basic small compact jump dropdown icon button tooltip" data-content="{{.locale.Tr "repo.more_actions"}}" data-position="top right">
{{svg "octicon-kebab-horizontal"}}
<div class="menu">
{{if not $.DisableDownloadSourceArchives}}
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.RefName}}.zip" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.locale.Tr "repo.download_zip"}}</a>
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.RefName}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip" 16 "mr-3"}}{{.locale.Tr "repo.download_tar"}}</a>
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.RefName}}.bundle" rel="nofollow">{{svg "octicon-package" 16 "mr-3"}}{{.locale.Tr "repo.download_bundle"}}</a>
{{if .CitiationExist}}
<a class="item" id="cite-repo-button">{{svg "octicon-cross-reference" 16 "mr-3"}}{{.locale.Tr "repo.cite_this_repo"}}</a>
{{end}}
{{end}}
<a class="item js-clone-url-vsc" href="vscode://vscode.git/clone?url={{.CloneButtonOriginLink.HTTPS}}">{{svg "gitea-vscode" 16 "mr-3"}}{{.locale.Tr "repo.clone_in_vsc"}}</a>
</div>
</button>
{{template "repo/clone_script" .}}{{/* the script will update `.js-clone-url` and related elements */}}
</div>
<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/citation_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>
sarrooo marked this conversation as resolved.
Show resolved Hide resolved
{{end}}
{{if and (ne $n 0) (not .IsViewFile) (not .IsBlame)}}
<a class="ui button" href="{{.RepoLink}}/commits/{{.BranchNameSubURL}}/{{.TreePath | PathEscapeSegments}}">
Expand Down
62 changes: 62 additions & 0 deletions web_src/js/features/citation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import $ from 'jquery';

const {pageData} = window.config;

const initInputCitationValue = async () => {
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 {citiationFileContent} = pageData;
sarrooo marked this conversation as resolved.
Show resolved Hide resolved
const $citationCopyApa = $('#citation-copy-apa');
const $citationCopyBibtex = $('#citation-copy-bibtex');
const config = plugins.config.get('@bibtex');
config.constants.fieldTypes.doi = ['field', 'literal'];
config.constants.fieldTypes.version = ['field', 'literal'];
const citationFormatter = new Cite(citiationFileContent);
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.citiationFileContent) 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().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');
});
}
2 changes: 2 additions & 0 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
initRepoCommonFilterSearchDropdown,
initRepoCommonLanguageStats,
} from './repo-common.js';
import {initCitationFileCopyContent} from './citation.js';
import {initCompLabelEdit} from './comp/LabelEdit.js';
import {initRepoDiffConversationNav} from './repo-diff.js';
import attachTribute from './tribute.js';
Expand Down Expand Up @@ -505,6 +506,7 @@ export function initRepository() {
}

initRepoCloneLink();
initCitationFileCopyContent();
initRepoCommonLanguageStats();
initRepoSettingBranches();

Expand Down
52 changes: 51 additions & 1 deletion web_src/less/_repository.less
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
border-right: none;
}

#download-btn {
#more-btn {
border-left: none;
}

Expand Down Expand Up @@ -2468,6 +2468,56 @@

// End of .repository

#cite-repo-modal {
#citation-panel {
width: 500px;

@media @mediaSm {
width: 100%;
}

input {
border-radius: 0;
padding: 5px 10px;
width: 50%;
line-height: 1.4;
}

.citation.button {
font-size: 13px;
padding: 7.5px 5px;
}

#citation-copy-content {
border-radius: 0;
padding: 5px 10px;
font-size: 1.2em;
line-height: 1.4;
}

#citation-copy-apa,
#citation-copy-bibtex {
border-right: none;
}

#goto-citation-btn {
border-left: none;
}

>:first-child {
border-radius: var(--border-radius) 0 0 var(--border-radius) !important;
}

>:last-child {
border-radius: 0 var(--border-radius) var(--border-radius) 0 !important;
}

.icon.button {
padding: 0 10px;
}
}
}

&.user-cards {
.list {
padding: 0;
Expand Down