Skip to content

Instantly share code, notes, and snippets.

@singerng
Last active March 28, 2024 19:56
Show Gist options
  • Save singerng/b4ec2d2e2f4ea613fc572ad00b497353 to your computer and use it in GitHub Desktop.
Save singerng/b4ec2d2e2f4ea613fc572ad00b497353 to your computer and use it in GitHub Desktop.
Export config script for Better BibTeX/LaTeX
function protectMath(text) {
if (text) return text.replace(/(\\\(.*?\\\))/g, '<script>{$1}</script>');
return "";
}
// wrap all latex in <script></script>
if (Translator.BetterTeX) {
if (reference.has.title) reference.add({ name: 'title', value: protectMath(item.title) });
if (reference.has.abstract) reference.add({ name: 'abstract', value: protectMath(item.abstract) });
const hasNote = ['annotation', 'note'].find(field => reference.has[field]);
if (hasNote) {
Zotero.debug(item.notes);
let notes = item.notes.map(entry => `<div>${entry.note}</div>`).join('');
reference.add({ name: 'note', value: protectMath(notes), html: true });
}
}
// put arxiv as misc in bibtex
if (Translator.BetterBibTeX && reference.referencetype === 'article' && item.arXiv) {
if (reference.has.journal) {
reference.remove('journal');
}
if (!reference.has.journal) reference.referencetype = 'misc';
}
function formatDate(date, options) {
options.timeZone = 'UTC';
return new Intl.DateTimeFormat('en-US', options).format(date);
}
function getDay(date) {
return formatDate(date, { day: 'numeric' });
}
function getMonth(date) {
return formatDate(date, { month: 'long' });
}
function getYear(date) {
return formatDate(date, { year: 'numeric' });
}
function parseDateRange(dates) {
if (dates.indexOf("/") == -1) return "";
var startDate = new Date(dates.split("/")[0]);
var endDate = new Date(dates.split("/")[1]);
if (!startDate || !endDate) return "";
var output = "";
if (startDate.getFullYear() == endDate.getFullYear()) {
if (startDate.getMonth() == endDate.getMonth()) {
return `${getMonth(startDate)} ${getDay(startDate)}-${getDay(endDate)}, ` +
`${getYear(endDate)}`;
} else {
return `${getMonth(startDate)} ${getDay(startDate)}-${getMonth(endDate)} ` +
`${getDay(endDate)}, ${getYear(startDate)}`;
}
} else {
return `${getMonth(startDate)} ${getDay(startDate)}, ${getYear(startDate)}-` +
`${getMonth(endDate)} ${getDay(endDate)}, ${getYear(endDate)}`;
}
}
if (Translator.BetterBibTeX) {
if (item.itemType == 'conferencePaper') {
var conferenceInfo = "";
var conferenceLoc = "";
if (item.conferenceName) {
conferenceInfo = " (" + item.conferenceName;
var conferencePlace = item.extraFields.kv['event-place'];
Zotero.debug(`Parsing date range: ${item.conferenceDate}`);
var conferenceDate = parseDateRange(item.conferenceDate);
Zotero.debug(`Parsed as: ${conferenceDate}`);
if (conferencePlace) conferenceLoc += `${conferencePlace}`;
if (conferenceDate) conferenceLoc += `, ${conferenceDate}`;
if (conferenceLoc) conferenceInfo += `, ${conferenceLoc}`;
conferenceInfo += ")";
}
reference.add({ name: 'booktitle', value: item.publicationTitle + conferenceInfo });
reference.add({ name: 'address', value: conferenceLoc });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment