Skip to content

Commit

Permalink
Merge pull request #190 from vict0rsch/oup
Browse files Browse the repository at this point in the history
  • Loading branch information
vict0rsch authored Dec 1, 2023
2 parents 6c2258f + b3b2c27 commit 58ab638
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/popup/js/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ const hideTitleTooltip = (id, isPopup) => {
};

const getHandleTitleTooltip = (func, delay, isPopup) => {
console.log("delay: ", delay);
return (e) => {
const id = isPopup ? global.state.currentId : eventId(e);
let timerId = global.state.timerIdMap.get(e.target) ?? 0;
Expand Down
2 changes: 1 addition & 1 deletion src/popup/min/popup.min.js

Large diffs are not rendered by default.

34 changes: 33 additions & 1 deletion src/shared/js/utils/bibtexParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,30 @@ function BibtexParser() {
};
}

/**
* Removes surrounding braces of `{some title is wrapped}`
* but not of `{some} title is {wrapped}`
* @param {string} str
* @returns {string} str without surrounding braces
*/
const safeRemoveSurroundingBraces = (str) => {
let opened = 0;
let closed = 0;
let remove = true;
for (const c of str.slice(1, -1)) {
if (c === "{") opened++;
if (c === "}") closed++;
if (closed > opened) {
remove = false;
break;
}
}
if (remove) {
return str.slice(1, -1);
}
return str;
};

const bibtexToObject = (bibtex) => {
var b = new BibtexParser();
b.setInput(bibtex);
Expand All @@ -339,6 +363,11 @@ const bibtexToObject = (bibtex) => {
entryType: entry.entryType,
citationKey: entry.citationKey,
};
for (const [key, value] of Object.entries(obj)) {
if (value.startsWith("{") && value.endsWith("}")) {
obj[key] = safeRemoveSurroundingBraces(value);
}
}
return obj;
};

Expand All @@ -361,7 +390,10 @@ const bibtexToString = (bibtex) => {
const keyLen = Math.max(...Object.keys(bibtex).map((k) => k.length));
for (const key in bibtex) {
if (bibtex.hasOwnProperty(key) && bibtex[key]) {
const value = bibtex[key].replaceAll(/\s+/g, " ").trim();
let value = bibtex[key].replaceAll(/\s+/g, " ").trim();
if (value.startsWith("{") && value.endsWith("}")) {
value = safeRemoveSurroundingBraces(value);
}
const bkey = key + " ".repeat(keyLen - key.length);
bstr += `\t${bkey} = {${value}},\n`;
}
Expand Down
7 changes: 7 additions & 0 deletions src/shared/js/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ global.knownPaperPages = {
"openreview.net/pdf",
"openreview.net/attachment",
],
oup: [
(url) =>
(url
.split("https://academic.oup.com/")[1]
?.split("/")[1]
?.indexOf("article") ?? -1) >= 0,
],
plos: [(url) => /journals\.plos\.org\/.+\/article.+id=/gi.test(url)],
pmc: ["ncbi.nlm.nih.gov/pmc/articles/PMC"],
pmlr: ["proceedings.mlr.press/"],
Expand Down
13 changes: 13 additions & 0 deletions src/shared/js/utils/paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ const paperToAbs = (paper) => {
case "mdpi":
abs = paper.pdfLink.split("/pdf")[0];
break;
case "oup":
abs = `https://doi.org/${paper.doi}`;
break;

default:
abs = "https://xkcd.com/1969/";
Expand Down Expand Up @@ -288,6 +291,9 @@ const paperToPDF = (paper) => {
case "mdpi":
break;

case "oup":
break;

case "website":
abs = paper.pdfLink;
break;
Expand Down Expand Up @@ -895,6 +901,13 @@ const parseIdFromUrl = async (url, tab = null) => {
.split("/notes")[0]
);
idForUrl = findPaperForProperty(papers, "mdpi", miniHash(mdpiId));
} else if (is.oup) {
url = noParamUrl(url).split("https://academic.oup.com/").last();
if (isPdfUrl(url)) {
url = url.split("/").slice(0, -1).join("/");
}
const num = url.split("/").slice(2).join("");
idForUrl = findPaperForProperty(papers, "oup", miniHash(num));
} else if (is.localFile) {
idForUrl = is.localFile;
} else if (is.parsedWebsite) {
Expand Down
25 changes: 25 additions & 0 deletions src/shared/js/utils/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,26 @@ const makeMDPIPaper = async (url) => {
return { author, bibtex, id, key, note, pdfLink, title, venue, year, doi };
};

const makeOUPPaper = async (url) => {
url = noParamUrl(url);
const resourceId = url.split("/").last();
let bibtex = await fetchText(
`https://academic.oup.com/Citation/Download?resourceId=${resourceId}&resourceType=3&citationFormat=2`
);
const paper = bibtexToObject(bibtex);
delete paper.abstract;
bibtex = bibtexToString(paper);
let { title, year, author, journal, doi, citationKey, eprint } = paper;
author = flipAndAuthors(author);
const venue = journal;
const note = `Published @ ${venue} (${year})`;
const key = citationKey;
const num = url.split("https://academic.oup.com/")[1].split("/").slice(2).join("");
const id = `OUP-${year}_${miniHash(num)}`;
const pdfLink = eprint?.replaceAll("\\", "") ?? url;

return { author, bibtex, id, key, note, pdfLink, title, venue, year, doi };
};
// -------------------------------
// ----- PREPRINT MATCHING -----
// -------------------------------
Expand Down Expand Up @@ -1898,6 +1918,11 @@ const makePaper = async (is, url, tab = false) => {
if (paper) {
paper.source = "mdpi";
}
} else if (is.oup) {
paper = await makeOUPPaper(url);
if (paper) {
paper.source = "oup";
}
} else {
throw new Error("Unknown paper source: " + JSON.stringify({ is, url }));
}
Expand Down
12 changes: 6 additions & 6 deletions src/shared/min/utils.min.js

Large diffs are not rendered by default.

0 comments on commit 58ab638

Please sign in to comment.