Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 42 additions & 19 deletions R/update.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,59 @@
#' 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_bib The path to the bibliography 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`.
#' @param encoding Passed to [rmarkdown::yaml_front_matter()]
#' @param quiet Use `TRUE` to suppress message on successful write.
#' @inheritParams bbt_write_bib
#'
#' @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
Expand Down
10 changes: 6 additions & 4 deletions man/bbt_update_bib.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rbbt.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 5ad6e129-6c23-4b55-902b-135693439039

RestoreWorkspace: No
SaveWorkspace: No
Expand Down