From 7244b97a965569806c18b8390198be12eb3f582f Mon Sep 17 00:00:00 2001 From: yourname Date: Sat, 8 Mar 2025 08:02:17 +0100 Subject: [PATCH 1/2] update 2025-03-08 08:02:17.902535 --- DESCRIPTION | 2 +- R/update.R | 59 ++++++++++++++++++++++++++++++------------- man/bbt_update_bib.Rd | 8 +++--- rbbt.Rproj | 1 + 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a838c44..691b5cf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Depends: R (>= 3.4.0) License: GPL-3 Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 Imports: httr, rstudioapi, diff --git a/R/update.R b/R/update.R index db0b13f..d0f06ee 100644 --- a/R/update.R +++ b/R/update.R @@ -5,7 +5,9 @@ #' as inputs and updates the bibliography file. #' #' @param path_rmd The path to the RMarkdown file. -#' @param path_bib The path to the bibliography file. +#' @param path_bib Optionally, the path to the bibliography file. +#' The default value `NULL`uses `bbt_guess_bib_file()` to detect the path from +#' the 'YAML' front matter of `path_rmd`. #' @param encoding Passed to [rmarkdown::yaml_front_matter()] #' @param quiet Use `TRUE` to suppress message on successful write. #' @inheritParams bbt_write_bib @@ -13,27 +15,48 @@ #' @return `path_bib`, invisibly #' @export bbt_update_bib <- function(path_rmd, - path_bib = bbt_guess_bib_file(path_rmd), + path_bib = NULL, ignore = character(0), - translator = bbt_guess_translator(path_bib), + translator = NULL, library_id = getOption("rbbt.default.library_id", 1), overwrite = TRUE, filter = identity, quiet = FALSE) { - keys <- bbt_detect_citations(path_rmd) - bbt_write_bib( - path = path_bib, - keys = keys, - ignore = ignore, - translator = translator, - overwrite = overwrite, - filter = filter - ) - - if (!quiet) { - references <- if (length(keys) != 1) "references" else "reference" - message(sprintf("Wrote %d %s to '%s'", length(keys), references, path_bib)) + # Extract citations from all rmds + keys_list <- lapply(path_rmd, bbt_detect_citations) + # If no bib path given, guess from files. Otherwise, repeat given bib path + if(is.null(path_bib)){ + path_bib <- sapply(path_rmd, bbt_guess_bib_file) + } else { + path_bib <- rep(path_bib, length(keys_list)) } - - invisible(path_bib) + # Preallocate list and name it + keys_per_bib <- vector("list", length = length(unique(path_bib))) + names(keys_per_bib) <- unique(path_bib) + # Get keys from all documents per unique bib file + for(thisb in names(keys_per_bib)){ + keys_per_bib[[thisb]] <- unique(do.call(c, keys_list[path_bib == thisb])) + } + # If no translator given, guess from files. Otherwise, repeat given translator + if(is.null(translator)){ + translator <- sapply(names(keys_per_bib), bbt_guess_translator) + } else { + translator <- rep(translator, length(keys_list)) + } + # Write files and report status messages + out_file <- sapply(seq_along(keys_per_bib), function(i){ + fl <- bbt_write_bib( + path = names(keys_per_bib)[i], + keys = keys_per_bib[[i]], + ignore = ignore, + translator = translator[i], + overwrite = overwrite, + filter = filter + ) + if (!quiet) { + message(sprintf("Wrote %d %s to '%s'", length(keys_per_bib[[1]]), c("reference", "references")[(length(keys_per_bib[[1]]) > 1)+1L], names(keys_per_bib)[1])) + } + fl + }) + return(invisible(out_file)) } #' @rdname bbt_update_bib diff --git a/man/bbt_update_bib.Rd b/man/bbt_update_bib.Rd index d088477..232eb71 100644 --- a/man/bbt_update_bib.Rd +++ b/man/bbt_update_bib.Rd @@ -7,9 +7,9 @@ \usage{ bbt_update_bib( path_rmd, - path_bib = bbt_guess_bib_file(path_rmd), + path_bib = NULL, ignore = character(0), - translator = bbt_guess_translator(path_bib), + translator = NULL, library_id = getOption("rbbt.default.library_id", 1), overwrite = TRUE, filter = identity, @@ -21,7 +21,9 @@ bbt_guess_bib_file(path_rmd, encoding = "UTF-8") \arguments{ \item{path_rmd}{The path to the RMarkdown file.} -\item{path_bib}{The path to the bibliography file.} +\item{path_bib}{Optionally, the path to the bibliography file. +The default value \code{NULL}uses \code{bbt_guess_bib_file()} to detect the path from +the 'YAML' front matter of \code{path_rmd}.} \item{ignore}{A character vector of keys to disregard (useful if \code{\link[=bbt_detect_citations]{bbt_detect_citations()}} gives spurious output).} diff --git a/rbbt.Rproj b/rbbt.Rproj index cba1b6b..472125f 100644 --- a/rbbt.Rproj +++ b/rbbt.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 5ad6e129-6c23-4b55-902b-135693439039 RestoreWorkspace: No SaveWorkspace: No From 5eb2cce2a0077c211bf860618403aff9100fc346 Mon Sep 17 00:00:00 2001 From: yourname Date: Sat, 8 Mar 2025 08:02:41 +0100 Subject: [PATCH 2/2] update 2025-03-08 08:02:41.504833 --- R/update.R | 2 +- man/bbt_update_bib.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/update.R b/R/update.R index d0f06ee..a521b64 100644 --- a/R/update.R +++ b/R/update.R @@ -4,7 +4,7 @@ #' This function takes an RMarkdown file location and a bibliography file #' as inputs and updates the bibliography file. #' -#' @param path_rmd The path to the RMarkdown file. +#' @param path_rmd The path to the RMarkdown file(s). #' @param path_bib Optionally, the path to the bibliography file. #' The default value `NULL`uses `bbt_guess_bib_file()` to detect the path from #' the 'YAML' front matter of `path_rmd`. diff --git a/man/bbt_update_bib.Rd b/man/bbt_update_bib.Rd index 232eb71..63214b8 100644 --- a/man/bbt_update_bib.Rd +++ b/man/bbt_update_bib.Rd @@ -19,7 +19,7 @@ bbt_update_bib( bbt_guess_bib_file(path_rmd, encoding = "UTF-8") } \arguments{ -\item{path_rmd}{The path to the RMarkdown file.} +\item{path_rmd}{The path to the RMarkdown file(s).} \item{path_bib}{Optionally, the path to the bibliography file. The default value \code{NULL}uses \code{bbt_guess_bib_file()} to detect the path from