@@ -73,11 +73,14 @@ get_bibentries <- function(..., package = NULL, bibfile = "REFERENCES.bib",
7373 # # This is really for the case when system.file() is the one from devtools,
7474 # # see the note above. TODO: check if this is the case?
7575 fn <- system.file(" inst" , ... , bibfile , package = package )
76-
77- if (length(fn ) == 1 && fn == " " )
78- # # if system.file() didn't find the bib file, check if file package.bib is
79- # # provided by package "bibtex" (it is for core R packages, such as "base")
80- fn <- system.file(" bib" , sprintf(" %s.bib" , package ), package = " bibtex" )
76+
77+ # # 2020-09-27 removing this functionality since package 'bibtex' ca no longer be
78+ # # relied upon and was dropped from the dependencies.
79+ # #
80+ # # if(length(fn) == 1 && fn == "")
81+ # # ## if system.file() didn't find the bib file, check if file package.bib is
82+ # # ## provided by package "bibtex" (it is for core R packages, such as "base")
83+ # # fn <- system.file("bib", sprintf("%s.bib", package), package = "bibtex")
8184 }
8285
8386 if (length(fn ) > 1 ){
@@ -109,7 +112,17 @@ get_bibentries <- function(..., package = NULL, bibfile = "REFERENCES.bib",
109112 else
110113 " UTF-8"
111114
112- res <- read.bib(file = fn , encoding = encoding )
115+ # # 2020-09-22 switching to 'rbibutils
116+ # # res <- read.bib(file = fn, encoding = encoding)
117+ # rds <- tempfile(fileext = ".rds")
118+ # if(encoding == "UTF-8")
119+ # encoding = "utf8"
120+ # be <- bibConvert(fn, rds, "bibtex",
121+ # "bibentry", encoding = c(encoding, "utf8"), tex = "no_latex")
122+ # res <- readRDS(rds)
123+ # #print(res)
124+ # unlink(rds)
125+ res <- readBib(file = fn , encoding = encoding )
113126
114127 # 2018-03-10 commenting out
115128 # since bibtex v. >= 0.4.0 has been required for a long time in DESCRIPTION
@@ -120,33 +133,71 @@ get_bibentries <- function(..., package = NULL, bibfile = "REFERENCES.bib",
120133 # names(res) <- sapply(1:length(res), function(x) bibentry_key(res[[x]][[1]]))
121134 # }
122135
123- for (nam in names(res )){
124- # # unconditionaly recode %'s in filed URL
125- if (! is.null(res [nam ]$ url ))
126- res [nam ]$ url <- gsub(" ([^\\ ])%" , " \\ 1\\\\ %" , res [nam ]$ url )
127-
128- if (url_only ){ # process also other fields
129- # # TODO: currently all unescaped $'s in all fields are recoded;
130- # # Maybe do it more selectively, e.g. only for %'s inside \url{},
131- # # or matching something like http(s)://
132- fields <- names(unclass(res [nam ])[[1 ]])
133-
134- unclassed <- unclass(res [nam ])
135- flag <- FALSE
136- for (field in fields ){
137- wrk <- unclass(res [nam ])[[1 ]][[field ]]
138- if (is.character(wrk ) && any(grepl(" ([^\\ ])%" , wrk ))){
139- flag <- TRUE
140- unclassed [[1 ]][[field ]] <- gsub(" ([^\\ ])%" , " \\ 1\\\\ %" , wrk )
141- }
136+ # # 2020-10-02 commenting out since taken care (hopefully) by readBib
137+ # #
138+ # for(nam in names(res)){
139+ # ## unconditionaly recode %'s in filed URL
140+ # if(!is.null(res[nam]$url)) {
141+ # res[nam]$url <- gsub("([^\\])%", "\\1\\\\%", res[nam]$url)
142+ # }
143+ #
144+ # if(url_only){ # process also other fields
145+ # ## TODO: currently all unescaped %'s in all fields are recoded;
146+ # ## Maybe do it more selectively, e.g. only for %'s inside \url{},
147+ # ## or matching something like http(s)://
148+ # fields <- names(unclass(res[nam])[[1]])
149+ #
150+ # unclassed <- unclass(res[nam])
151+ # flag <- FALSE
152+ # for(field in fields){
153+ # wrk <- unclass(res[nam])[[1]][[field]]
154+ # if(is.character(wrk) && any(grepl("([^\\])%", wrk))){
155+ # flag <- TRUE
156+ # unclassed[[1]][[field]] <- gsub("([^\\])%", "\\1\\\\%", wrk)
157+ # }
158+ # }
159+ # if(flag){
160+ # class(unclassed) <- class(res[nam])
161+ # res[nam] <- unclassed
162+ # }
163+ # }
164+ # }
165+
166+ # # new 2020-10-02 - allow \% in url's and doi's in the bib file
167+ for (nam in names(res )){
168+ # print(res[nam], style = "R")
169+ # # unconditionaly recode %'s in filed URL
170+ if (! is.null(res [nam ]$ doi )) {
171+ res [nam ]$ doi <- gsub(" ([^\\\\ ])[\\\\ ]%" , " \\ 1%" , res [nam ]$ doi )
142172 }
143- if ( flag ){
144- class( unclassed ) <- class (res [nam ])
145- res [nam ] <- unclassed
173+
174+ if ( ! is.null (res [nam ]$ url )) {
175+ res [nam ]$ url <- gsub( " ([^ \\\\ ])[ \\\\ ]% " , " \\ 1% " , res [ nam ] $ url )
146176 }
177+
178+ # if(url_only){ # process also other fields
179+ # ## TODO: currently all unescaped %'s in all fields are recoded;
180+ # ## Maybe do it more selectively, e.g. only for %'s inside \url{},
181+ # ## or matching something like http(s)://
182+ # fields <- names(unclass(res[nam])[[1]])
183+ #
184+ # unclassed <- unclass(res[nam])
185+ # flag <- FALSE
186+ # for(field in fields){
187+ # wrk <- unclass(res[nam])[[1]][[field]]
188+ # if(is.character(wrk) && any(grepl("([^\\])%", wrk))){
189+ # flag <- TRUE
190+ # unclassed[[1]][[field]] <- gsub("([^\\])%", "\\1\\\\%", wrk)
191+ # }
192+ # }
193+ # if(flag){
194+ # class(unclassed) <- class(res[nam])
195+ # res[nam] <- unclassed
196+ # }
197+ # }
147198 }
148- }
149199
200+
150201 # # 2018-03-03 new:
151202 class(res ) <- c(" bibentryRd" , class(res ))
152203
@@ -315,12 +366,53 @@ Rdo_flatinsert <- function(rdo, val, pos, before = TRUE){
315366 res
316367}
317368
318- # # TODO: auto-deduce 'package'?
319- insert_ref <- function (key , package = NULL , ... ) { # bibfile = "REFERENCES.bib"
320- if (is.null(package ))
369+
370+ .get_bibs0 <- function (package , ... , cached_env ) {
371+ if (is.null(package ))
321372 stop(" argument 'package' must be provided" )
322373
323- bibs <- get_bibentries(package = package , ... , stop_on_error = FALSE )
374+ if (! is.null(cached_env )){
375+ if (is.null(cached_env $ refsmat ))
376+ cached_env $ refsmat <- matrix (character (0 ), nrow = 0 , ncol = 2 )
377+ if (is.null(cached_env $ allbibs ))
378+ cached_env $ allbibs <- list ()
379+ }
380+
381+ # # if(length(keys) > 1)
382+ # # stop("`keys' must be a character string")
383+ # #
384+ # # if(!cite_only)
385+ # # cached_env$refsmat <- rbind(cached_env$refsmat, c(keys, package))
386+ # #
387+ # # if(dont_cite)
388+ # # return(character(0))
389+
390+
391+ if (is.null(cached_env )){
392+ bibs <- get_bibentries(package = package , ... , stop_on_error = FALSE )
393+ }else {
394+ bibs <- cached_env $ allbibs [[package ]]
395+ if (is.null(bibs )){
396+ bibs <- get_bibentries(package = package , ... , stop_on_error = FALSE )
397+ cached_env $ allbibs [[package ]] <- bibs
398+ }
399+ }
400+
401+ bibs
402+ }
403+
404+
405+ # # TODO: auto-deduce 'package'?
406+ # # 2020-09-30: changing to cache bib as \insertCite does (new arg. cached_env, etc)
407+ insert_ref <- function (key , package = NULL , ... , cached_env = NULL ) { # bibfile = "REFERENCES.bib"
408+
409+ # 2020-09-30: replaced by a single call
410+ # if(is.null(package))
411+ # stop("argument 'package' must be provided")
412+ #
413+ # bibs <- get_bibentries(package = package, ..., stop_on_error = FALSE)
414+ #
415+ bibs <- .get_bibs0(package , ... , cached_env = cached_env )
324416
325417 if (length(bibs ) == 0 ){
326418 note <- paste0(" \" Failed to insert reference with key = " , key ,
0 commit comments