Skip to content

Commit

Permalink
automatically parse url as id for cff
Browse files Browse the repository at this point in the history
  • Loading branch information
timlrx committed Oct 13, 2023
1 parent caf8979 commit e086caf
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
19 changes: 14 additions & 5 deletions src/citation-js/plugin-cff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,19 +642,28 @@ const mainTranslator = new util.Translator(MAIN_PROPS)
const refTranslator = new util.Translator(REF_PROPS)
const CFF_VERSION = '1.2.0'

/** Add doi or url as unique id if available to make citation easy */
function addId(entry) {
if ('DOI' in entry) {
entry.id = entry.DOI
} else if ('URL' in entry) {
entry.id = entry.URL.replace("http://", "").replace("https://", "")
}
}

function parse(input) {
const main = mainTranslator.convertToTarget(input)
if (input['cff-version'] <= '1.1.0') {
main.type = TYPES_TO_TARGET.software
}
main._cff_mainReference = true
// Use identifier as unique id to make citation easy
if ('DOI' in main) {
main.id = main.DOI
}
addId(main)

const output = [main]
if (input['preferred-citation']) {
output.push(refTranslator.convertToTarget(input['preferred-citation']))
const preferredCitation = refTranslator.convertToTarget(input['preferred-citation'])
addId(preferredCitation)
output.push(preferredCitation)
}

if (Array.isArray(input.references)) {
Expand Down
12 changes: 6 additions & 6 deletions src/gen-citation.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export const genCitation = (
const output = isComposite
? `<a href="#bib-${entries[0].id.toLowerCase()}">${citationText}</a>`
: `${citationText.slice(
0,
1
)}<a href="#bib-${entries[0].id.toLowerCase()}">${citationText.slice(
1,
-1
)}</a>${citationText.slice(-1)}`
0,
1
)}<a href="#bib-${entries[0].id.toLowerCase()}">${citationText.slice(
1,
-1
)}</a>${citationText.slice(-1)}`
return [
citationText,
htmlToHast(`<span class="${(inlineClass ?? []).join(' ')}" id=${ids}>${output}</span>`),
Expand Down
13 changes: 12 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import rehypeCitation from '../index.js'
const bibliography = './test/references-data.bib'
const cslJSON = './test/csl-json-data.json'
const cff = './test/CITATION.cff'
const urlCff = "./test/pytorch-CITATION.cff"

const processHtml = (html, options, input = bibliography) => {
return rehype()
Expand Down Expand Up @@ -277,7 +278,7 @@ rehypeCitationTest('generates multiple inline bibs', async () => {
assert.is(result, expected)
})

rehypeCitationTest('works with cff file', async () => {
rehypeCitationTest('works with cff file and add doi as id', async () => {
const result = await processHtml(
dedent`<div>[@10.5281/zenodo.1234]<</div>`,
{ suppressBibliography: true },
Expand All @@ -287,4 +288,14 @@ rehypeCitationTest('works with cff file', async () => {
assert.is(result, expected)
})

rehypeCitationTest('adds url as id for preferred citation', async () => {
const result = await processHtml(
dedent`<div>[@papers.neurips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library.pdf]<</div>`,
{ suppressBibliography: true },
urlCff
)
const expected = dedent`<div><span class="" id="citation--papers.neurips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library.pdf--1">(Paszke et al., 2019)</span>&#x3C;</div>`
assert.is(result, expected)
})

rehypeCitationTest.run()
73 changes: 73 additions & 0 deletions test/pytorch-CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cff-version: 1.2.0
message: If you use this software, please cite it as below.
title: PyTorch
authors:
- family-names: PyTorch Team
url: https://pytorch.org
preferred-citation:
type: conference-paper
title: 'PyTorch: An Imperative Style, High-Performance Deep Learning Library'
authors:
- family-names: Paszke
given-names: Adam
- family-names: Gross
given-names: Sam
- family-names: Massa
given-names: Francisco
- family-names: Lerer
given-names: Adam
- family-names: Bradbury
given-names: James
- family-names: Chanan
given-names: Gregory
- family-names: Killeen
given-names: Trevor
- family-names: Lin
given-names: Zeming
- family-names: Gimelshein
given-names: Natalia
- family-names: Antiga
given-names: Luca
- family-names: Desmaison
given-names: Alban
- family-names: Kopf
given-names: Andreas
- family-names: Yang
given-names: Edward
- family-names: DeVito
given-names: Zachary
- family-names: Raison
given-names: Martin
- family-names: Tejani
given-names: Alykhan
- family-names: Chilamkurthy
given-names: Sasank
- family-names: Steiner
given-names: Benoit
- family-names: Fang
given-names: Lu
- family-names: Bai
given-names: Junjie
- family-names: Chintala
given-names: Soumith
collection-title: Advances in Neural Information Processing Systems 32
collection-type: proceedings
editors:
- family-names: Wallach
given-names: H.
- family-names: Larochelle
given-names: H.
- family-names: Beygelzimer
given-names: A.
- family-names: "d'Alché-Buc"
given-names: F.
- family-names: Fox
given-names: E.
- family-names: Garnett
given-names: R.
start: 8024
end: 8035
year: 2019
publisher:
name: Curran Associates, Inc.
url: http://papers.neurips.cc/paper/9015-pytorch-an-imperative-style-high-performance-deep-learning-library.pdf

0 comments on commit e086caf

Please sign in to comment.