From 278301596f5448e9526980920d8f30e7cec85bf8 Mon Sep 17 00:00:00 2001 From: Lukas Wallrich Date: Sun, 11 Dec 2022 15:09:23 +0000 Subject: [PATCH] Allow to pass BibTex as character to drop_name() This allows users to copy-paste BibTex from (e.g.) Google Scholar ```r drop_name("@article{rungta2022geographic, title={Geographic Citation Gaps in NLP Research}, author={Rungta, Mukund and Singh, Janvijay and Mohammad, Saif M and Yang, Diyi}, journal={arXiv preprint arXiv:2210.14424}, year={2022} }") ``` In that context, it also deals with #49 - while we should prob not edit bib-files here, it seems fair to add a workaround for this interactive use until bib2df addresses the issue? --- R/drop_name.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/drop_name.R b/R/drop_name.R index 52e496e..f841479 100644 --- a/R/drop_name.R +++ b/R/drop_name.R @@ -5,6 +5,7 @@ #' @param bib Accepts one of the following: #' 1) A data.frame or tibble containing the columns YEAR, JOURNAL, AUTHOR, TITLE, BIBTEXKEY (all mandatory) and DOI, URL (optional). #' 2) A file path to a bibliography file in BibTeX/BibLaTeX format (usually *.bib file). +#' 3) A string containing one or more BibTeX citations (must start with `@`) #' @param cite_key If given, either a character string or a vector of strings are accepted. #' Specifies the reference items within the bibliography for which visual citations should be created. #' If no key is specified, a visual citation is created for ALL reference items within the bibliography. @@ -142,6 +143,14 @@ drop_name <- function(bib, cite_key, if (is.data.frame(bib)) { bib_data <- bib } else if (is.character(bib)) { + bib_file <- NULL #Track whether new file was created + if(substr(bib, 1, 1) == "@") { + bib <- gsub("(?<=\\w{1})\\=\\{", " \\= \\{", bib, perl = TRUE) # To deal with Google Scholar parsing issue - https://github.com/nucleic-acid/namedropR/issues/49 + bib_file <- tempfile(fileext = ".bib") + writeLines(bib, bib_file) + writeLines(bib, "test.bib") + bib <- bib_file + } if (file.exists(bib)) { bib_data <- suppressMessages(suppressWarnings(bib2df::bib2df(file = bib))) if ("YEAR" %in% colnames(bib_data)) { @@ -150,6 +159,9 @@ drop_name <- function(bib, cite_key, message("Years coerced to string format.") } } + if(!is.null(bib_file)) { + file.remove(bib_file) + } message("Bibliography file successfully read.") } else { stop("BibTeX file not found. Check file path or pass a suitable data.frame/tibble to the function.")