Skip to content

Commit

Permalink
Fixes bug in post_process_doi_citations().
Browse files Browse the repository at this point in the history
  • Loading branch information
crsh committed Oct 16, 2024
1 parent c6b7b39 commit 6d780e7
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions R/replace_doi.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#' @export

post_process_doi_citations <- function(input_file, bib) {
if(!require("bibtex", quietly = TRUE)) {
if(!requireNamespace("bibtex", quietly = TRUE)) {
stop("The package `bibtex` is not avialable but required to replace DOI citations in a source document. Please install the package and try again.")
}
if(!require("stringr", quietly = TRUE)) {
if(!requireNamespace("stringr", quietly = TRUE)) {
stop("The package `stringr` is not avialable but required to replace DOI citations in a source document. Please install the package and try again.")
}

Expand Down Expand Up @@ -43,19 +43,19 @@ post_process_doi_citations <- function(input_file, bib) {
# Process bib files
entries <- lapply(bib[existant_bib & !empty_bib], bibtex::read.bib) |>
do.call("c", args = _) |>
(\(x) setNames(x$doi, names(x)))()
(\(x) stats::setNames(x$doi, names(x)))()

entries <- entries[!is.na(entries) & !duplicated(entries)]

rmd <- gsub("\\.knit\\.md$", ".Rmd", rmd)
rmd <- gsub("\\.knit\\.md$", ".Rmd", input_file)
if(!file.exists(rmd)) {
rmd <- gsub("Rmd$", "rmd", rmd)
if(!file.exists(rmd)) stop("Cannot locate source file at", rmd, "or", gsub("rmd$", "Rmd", rmd), ".")
}

stringr::str_replace_all(
readLines_utf8(con = rmd)
, setNames(
, stats::setNames(
paste0("@", names(entries))
, paste0("@(doi:|DOI:|(https://)*doi.org/)*((", toupper(entries), ")|(", tolower(entries), "))")
)
Expand Down

0 comments on commit 6d780e7

Please sign in to comment.