Created
September 26, 2021 18:05
-
-
Save n3ssuno/835a1aa2e27e886ed3740158b8adc3f4 to your computer and use it in GitHub Desktop.
JavaScript functions to transform a JSON containing bibliographic information, in an HTML string thought to be included in a personal website.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: Carlo Bottai - carlo.bottai@gmail.com | |
Copyright (c) 2020 - Carlo Bottai | |
Licence: MIT | |
Date: 2020-01-21 | |
This group of functions transform a JSON (generated by the bibtexParse function | |
used by ORCID from a BibTeX file) that contains bibliographic information, | |
in an HTML string thought to be included in a personal website. | |
The function pubAny is the main function, and is then called by pubArticle and | |
pubInCollection. The function pubAny generates all the common patterns, while | |
the other two generate specific patterns for papers or book chapters, | |
respectively. Lastly, the function pubAuthor is used both to transform the list | |
of authors and the list of editors (if any) in a proper string. | |
*/ | |
var pubAuthor = function(author, ego = null) { | |
/* ego is the name of the website owner. Her name will be excluded from the publications. | |
If it's a solo publication, it will have no author. | |
Otherwise, a "with Other Author" will be added. */ | |
var authors = author.split(" and "); | |
/* Split the BibTeX string and create an array with the | |
full name of each author */ | |
if (ego) { | |
authors = authors.filter(item => !ego.includes(item)) | |
} | |
/* Exclude First Last from the list of authors (useful for authors list, | |
while not applied to editors list) */ | |
var author = ""; | |
if (authors.length > 0) { | |
for (var j = 0; j < authors.length; j++) { | |
var author_ = authors[j].split(", "); | |
/* If the full name is written like Last, F., revert the name | |
in F. Last */ | |
var name = author_[1]; | |
var surname = author_[0]; | |
if (typeof name != "undefined") { | |
var name_ = name.split(" "); | |
if (!name.endsWith('.')) { | |
/* If the first name is not just the initial, like "Last, First", | |
shorten the first name(s) */ | |
for (var k = 0; k < name_.length; k++) { | |
name_[k] = `${name_[k].substring(0,1)}.`; | |
} | |
} | |
name = name_.join(' '); | |
author_ = `${name} ${surname}`; | |
} | |
if (j == 0) { | |
author += author_ | |
} else if (j == authors.length - 1) { | |
author += ` and ${author_}`; | |
/* The last name is preceded by an "and" */ | |
} else if (authors.length > 6 && j == 2) { | |
author += " <em>et al.</em>" | |
break; | |
/* If there are more than 6 names, add the first two, than "et al." | |
and exit the loop */ | |
} else { | |
/* Each other name is separated by a comma */ | |
author += `, ${author_}`; | |
} | |
} | |
} | |
return author; | |
} | |
var pubAny = function(pub, ego = null) { | |
var author = pubAuthor(pub.author, ego) | |
if (author != "") { | |
var author = `(with ${author}) `; | |
} | |
var title = pub.title; | |
title = title.replace(/^{/i, '').replace(/}$/i, ''); | |
/* Remove the extra {} that sometimes is around the title | |
in the BibTeX format */ | |
title = title.replace(/{?\\&}?/i,"&").replace("--","–"); | |
/* replace {\&} or \& with the HTML code for the ampersand | |
and -- with the HTML sign for the n-dash */ | |
title = `'${title}' `; | |
if ("journal" in pub) { | |
var journal = `<em>${pub.journal}</em>, `; | |
} else { | |
var journal = ""; | |
} | |
if ("volume" in pub) { | |
var volume = `Vol. ${pub.volume}, `; | |
} else { | |
var volume = ""; | |
} | |
if ("editor" in pub) { | |
var editor = pubAuthor(pub.editor); | |
if (pub.editor.split(" and ").length > 1) { | |
editor = `in ${editor} (eds.) `; | |
} else { | |
editor = `in ${editor} (ed.) `; | |
} | |
} else { | |
var editor = "" | |
} | |
if ("booktitle" in pub) { | |
var book = pub.booktitle; | |
book = book.replace(/^{/i, '').replace(/}$/i, ''); | |
book = book.replace(/{?\\&}?/i,"&").replace("--","–"); | |
book = `<em>${book}</em>, `; | |
} else { | |
var book = ""; | |
} | |
if ("publisher" in pub) { | |
var publisher = `${pub.publisher}, `; | |
} else { | |
var publisher = ""; | |
} | |
try { | |
var pages = pub.pages.replace("--","–").replace("-","–"); | |
var pages = `pp. ${pages}, `; | |
} catch { | |
var pages = ""; | |
} | |
var year = `${pub.year}. `; | |
if ("file" in pub && "url" in pub) { | |
var access = `Access <a href="publications/${pub.file}" | |
target="_blank" class="publications-link" rel="noopener">here</a> or | |
<a href="${pub.url}" class="publications-link" target="_blank" | |
rel="noopener">here</a>.`; | |
/* If there is a file and an URL in the BibTeX file, add these two | |
sources */ | |
} else if ("file" in pub && "doi" in pub) { | |
var access = `Access <a href="publications/${pub.file}" | |
class="publications-link" target="_blank" rel="noopener">here</a> or <a | |
href="https://doi.org/${pub.doi}" class="publications-link" | |
target="_blank" rel="noopener">here</a>.`; | |
/* If, instead, there is a file and a DOI, but not an URL, in the BibTeX | |
file, add these two sources, transforming the DOI in a web URL */ | |
} else if ("file" in pub) { | |
var access = `Access <a href="publications/${pub.file}" | |
class="publications-link" target="_blank" rel="noopener">here</a>.`; | |
/* If there is only a file in the BibTeX file, add this sources */ | |
} else if ("url" in pub) { | |
var access = `Access <a href="${pub.url}" class="publications-link" | |
target="_blank" rel="noopener">here</a>.`; | |
/* If there is only an URL in the BibTeX file, add this sources */ | |
} else if ("doi" in pub) { | |
var access = `Access <a href="https://doi.org/${pub.doi}" | |
class="publications-link" target="_blank" rel="noopener">here</a>`; | |
/* If there is only a DOI in the BibTeX file, transform it in a web URL and | |
add this sources */ | |
} else { | |
var access = ""; | |
/* If none of these conditions apply, add nothing as a source */ | |
} | |
if ("press_coverage" in pub) { | |
var press = `<br><em>Press coverage</em>: | |
<a href="${pub.press_coverage_url}" class="publications-link" | |
target="_blank" rel="noopener">${pub.press_coverage}</a>`; | |
} else { | |
var press = ""; | |
} | |
if ("note" in pub) { | |
var note = `<br>${pub.note}.`; | |
} else { | |
var note = ""; | |
} | |
return [{author:author, | |
title:title, | |
journal:journal, | |
editor:editor, | |
booktitle:book, | |
publisher:publisher, | |
volume:volume, | |
pages:pages, | |
year:year, | |
access:access, | |
note:note, | |
press_coverage:press}] | |
} | |
var pubArticle = function(pub, ego = null) { | |
pubData = pubAny(pub, ego)[0] | |
return pubData["author"] + pubData["title"] + pubData["journal"] + | |
pubData["volume"] + pubData["pages"] + pubData["year"] + | |
pubData["access"] + pubData["note"] + pubData["press_coverage"]; | |
} | |
var pubInCollection = function(pub, ego = null) { | |
pubData = pubAny(pub, ego)[0] | |
return pubData["author"] + pubData["title"] + pubData["editor"] + | |
pubData["booktitle"] + pubData["publisher"] + pubData["volume"] + | |
pubData["pages"] + pubData["year"] + pubData["access"] + pubData["note"] + | |
pubData["press_coverage"]; | |
} | |
var writeBib = function(pubs, ego = null) { | |
var pubs_div = document.getElementById("pubs_div"); | |
var ul = document.createElement("ul"); | |
ul.className = "list-unstyled bib" | |
pubs_div.appendChild(ul); | |
for(var i = 0; i < pubs.length; i++) { | |
var pub_type = pubs[i].entryType | |
var pub = pubs[i].entryTags | |
var li = document.createElement("li"); | |
li.className = "bib-item" | |
if (pub_type.toLowerCase() == "article") { | |
var pub_ = pubArticle(pub, ego); | |
} else if (["incollection","inbook"].includes(pub_type.toLowerCase())) { | |
var pub_ = pubInCollection(pub, ego); | |
} | |
li.innerHTML = pub_ | |
ul.appendChild(li); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment