| import { IContributorCredit, IInfoPayload, ISoftwareCredit } from "./PluginInterfaces"; |
|
|
| export interface ICitation { |
| title: string; |
| authors: string[]; |
| journal: string; |
| volume: number; |
| issue?: number | string; |
| pages: string; |
| year: number; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function formatCitation(citation: ICitation): string { |
| |
| for (const author of citation.authors) { |
| if (author.indexOf(",") === -1) { |
| throw new Error( |
| `Author name does not contain a comma: ${author}. Format should be LAST NAME, GIVEN NAMES.` |
| ); |
| } |
| } |
|
|
| |
| for (let i = 0; i < citation.authors.length; i++) { |
| const author = citation.authors[i]; |
| const commaIndex = author.indexOf(","); |
| const lastName = author.substring(0, commaIndex); |
| const givenNames = author.substring(commaIndex + 1).trim(); |
| const givenNamesArray = givenNames.split(" "); |
| const newGivenNamesArray = []; |
| for (const givenName of givenNamesArray) { |
| if (givenName.length > 0) { |
| newGivenNamesArray.push(givenName[0] + "."); |
| } |
| } |
| citation.authors[i] = `${lastName}, ${newGivenNamesArray.join(" ")}`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| const lastChar = citation.title[citation.title.length - 1]; |
| if (lastChar !== "." && lastChar !== "!" && lastChar !== "?") { |
| citation.title += "."; |
| } |
|
|
| |
| let volumeString = citation.volume.toString(); |
| if (citation.issue) { |
| volumeString += `(${citation.issue})`; |
| } |
|
|
| const searchStr = `"${citation.authors[0]}" "${citation.title}" "${citation.journal}" ${citation.year};${volumeString};${citation.pages}.`; |
|
|
| |
| const urlEncodedSearchStr = encodeURIComponent(searchStr); |
| const url = `https://scholar.google.com/scholar?q=${urlEncodedSearchStr}`; |
|
|
| |
| |
| return `<a href="${url}" target="_blank"><i>${citation.journal}</i> ${citation.year};${volumeString};${citation.pages}</a>`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function formatCredit(credit: ISoftwareCredit | IContributorCredit): string[] { |
| if (credit.citations && credit.citations.length > 0) { |
| |
| const softwareCredits: string[] = []; |
| for (const citation of credit.citations) { |
| softwareCredits.push(formatCitation(citation)) |
| } |
|
|
| return softwareCredits; |
| } |
|
|
| |
| if (credit.name) { |
| if (credit.url) { |
| return [`<a href="${credit.url}" target="_blank">${credit.name}</a>`] |
| } else { |
| return [credit.name]; |
| } |
| } |
|
|
| console.warn("SHOULD NEVER GET HERE!") |
| return []; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function citationsTxt( |
| infoPayload: IInfoPayload, |
| extraFormatting = true |
| ): string { |
| const credits = [ |
| ...infoPayload.contributorCredits, |
| ...infoPayload.softwareCredits, |
| ] |
| |
| |
| |
| |
| |
| |
| |
| if (credits.length === 0) { |
| return ""; |
| } |
|
|
| const allCredits: string[] = []; |
| for (const citation of credits) { |
| allCredits.push(...formatCredit(citation)); |
| } |
| |
| let creditStr = extraFormatting ? "<p class='mb-4'><small><b>" : ""; |
| creditStr += (credits.length === 1 ? "Credit" : "Credits") + ":"; |
| creditStr += extraFormatting ? "</b> " : " "; |
|
|
| creditStr += allCredits.join("; ") + "."; |
|
|
| if (extraFormatting) { |
| creditStr += "</small></p>"; |
| } |
|
|
| return creditStr; |
| } |
|
|