Skip to content

Commit

Permalink
switch bib parser from bibtex to rbibutils
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoBosh committed Oct 3, 2020
1 parent bbd0f50 commit aa46920
Show file tree
Hide file tree
Showing 90 changed files with 451 additions and 317 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
auto/
/vignettes/.RData
/vignettes/Inserting_figures_and_evaluated_examples.tex
/vignettes/Inserting_bibtex_references.tex
Expand Down
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: Rdpack
Type: Package
Title: Update and Manipulate Rd Documentation Objects
Version: 1.0.1
Date: 2020-07-02
Version: 2.0.0
Date: 2020-10-02
Authors@R: c( person(given = c("Georgi", "N."),
family = "Boshnakov",
role = c("aut", "cre"),
Expand All @@ -26,13 +26,13 @@ Depends:
Imports:
tools,
utils,
bibtex (>= 0.4.0),
gbRd
gbRd,
rbibutils (>= 1.3)
Suggests:
grDevices,
testthat,
rstudioapi,
rprojroot
License: GPL (>= 2)
LazyLoad: yes
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ importFrom(tools, parse_Rd, toRd, checkRd, Rd2txt,
#importFrom(grDevices, dev.off, png)

importFrom(gbRd, Rd_help2txt)
importFrom(bibtex, read.bib)
#importFrom(bibtex, read.bib)
importFrom(rbibutils, readBib)

S3method(as.character, f_usage)
S3method(print, f_usage)
Expand Down
14 changes: 13 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# Rdpack 2.0.0 (CRAN)

- removed `bibtex` from `Suggests` and elsewhere.


# Rdpack 1.1.0

- imported `rbibutils` and made suitable adjustments to the code.

- moved `bibtex` to `Suggests` (since `bibtex` has been orphaned for several years).


# Rdpack 1.0.1

- removed a documentation link to a function in `gbutils`, which is not among
the dependencies of `Rdpack`. This was raising a NOTE on one of the CRAN
testing machines.


# Rdpack 1.0.0
# Rdpack 1.0.0 (CRAN)

- `viewRd()` now loads also Rd macros declared by the package to which the
rendered Rd file belongs. Previously only macros from `Rdpack` were loaded
Expand Down
158 changes: 125 additions & 33 deletions R/bib.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ get_bibentries <- function(..., package = NULL, bibfile = "REFERENCES.bib",
## This is really for the case when system.file() is the one from devtools,
## see the note above. TODO: check if this is the case?
fn <- system.file("inst", ..., bibfile, package = package)

if(length(fn) == 1 && fn == "")
## if system.file() didn't find the bib file, check if file package.bib is
## provided by package "bibtex" (it is for core R packages, such as "base")
fn <- system.file("bib", sprintf("%s.bib", package), package = "bibtex")

## 2020-09-27 removing this functionality since package 'bibtex' ca no longer be
## relied upon and was dropped from the dependencies.
##
## if(length(fn) == 1 && fn == "")
## ## if system.file() didn't find the bib file, check if file package.bib is
## ## provided by package "bibtex" (it is for core R packages, such as "base")
## fn <- system.file("bib", sprintf("%s.bib", package), package = "bibtex")
}

if(length(fn) > 1){
Expand Down Expand Up @@ -109,7 +112,17 @@ get_bibentries <- function(..., package = NULL, bibfile = "REFERENCES.bib",
else
"UTF-8"

res <- read.bib(file = fn, encoding = encoding)
## 2020-09-22 switching to 'rbibutils
## res <- read.bib(file = fn, encoding = encoding)
# rds <- tempfile(fileext = ".rds")
# if(encoding == "UTF-8")
# encoding = "utf8"
# be <- bibConvert(fn, rds, "bibtex",
# "bibentry", encoding = c(encoding, "utf8"), tex = "no_latex")
# res <- readRDS(rds)
# #print(res)
# unlink(rds)
res <- readBib(file = fn, encoding = encoding)

# 2018-03-10 commenting out
# since bibtex v. >= 0.4.0 has been required for a long time in DESCRIPTION
Expand All @@ -120,33 +133,71 @@ get_bibentries <- function(..., package = NULL, bibfile = "REFERENCES.bib",
# names(res) <- sapply(1:length(res), function(x) bibentry_key(res[[x]][[1]]))
# }

for(nam in names(res)){
## unconditionaly recode %'s in filed URL
if(!is.null(res[nam]$url))
res[nam]$url <- gsub("([^\\])%", "\\1\\\\%", res[nam]$url)

if(url_only){ # process also other fields
## TODO: currently all unescaped $'s in all fields are recoded;
## Maybe do it more selectively, e.g. only for %'s inside \url{},
## or matching something like http(s)://
fields <- names(unclass(res[nam])[[1]])

unclassed <- unclass(res[nam])
flag <- FALSE
for(field in fields){
wrk <- unclass(res[nam])[[1]][[field]]
if(is.character(wrk) && any(grepl("([^\\])%", wrk))){
flag <- TRUE
unclassed[[1]][[field]] <- gsub("([^\\])%", "\\1\\\\%", wrk)
}
## 2020-10-02 commenting out since taken care (hopefully) by readBib
##
# for(nam in names(res)){
# ## unconditionaly recode %'s in filed URL
# if(!is.null(res[nam]$url)) {
# res[nam]$url <- gsub("([^\\])%", "\\1\\\\%", res[nam]$url)
# }
#
# if(url_only){ # process also other fields
# ## TODO: currently all unescaped %'s in all fields are recoded;
# ## Maybe do it more selectively, e.g. only for %'s inside \url{},
# ## or matching something like http(s)://
# fields <- names(unclass(res[nam])[[1]])
#
# unclassed <- unclass(res[nam])
# flag <- FALSE
# for(field in fields){
# wrk <- unclass(res[nam])[[1]][[field]]
# if(is.character(wrk) && any(grepl("([^\\])%", wrk))){
# flag <- TRUE
# unclassed[[1]][[field]] <- gsub("([^\\])%", "\\1\\\\%", wrk)
# }
# }
# if(flag){
# class(unclassed) <- class(res[nam])
# res[nam] <- unclassed
# }
# }
# }

## new 2020-10-02 - allow \% in url's and doi's in the bib file
for(nam in names(res)){
#print(res[nam], style = "R")
## unconditionaly recode %'s in filed URL
if(!is.null(res[nam]$doi)) {
res[nam]$doi <- gsub("([^\\\\])[\\\\]%", "\\1%", res[nam]$doi)
}
if(flag){
class(unclassed) <- class(res[nam])
res[nam] <- unclassed

if(!is.null(res[nam]$url)) {
res[nam]$url <- gsub("([^\\\\])[\\\\]%", "\\1%", res[nam]$url)
}

# if(url_only){ # process also other fields
# ## TODO: currently all unescaped %'s in all fields are recoded;
# ## Maybe do it more selectively, e.g. only for %'s inside \url{},
# ## or matching something like http(s)://
# fields <- names(unclass(res[nam])[[1]])
#
# unclassed <- unclass(res[nam])
# flag <- FALSE
# for(field in fields){
# wrk <- unclass(res[nam])[[1]][[field]]
# if(is.character(wrk) && any(grepl("([^\\])%", wrk))){
# flag <- TRUE
# unclassed[[1]][[field]] <- gsub("([^\\])%", "\\1\\\\%", wrk)
# }
# }
# if(flag){
# class(unclassed) <- class(res[nam])
# res[nam] <- unclassed
# }
# }
}
}


## 2018-03-03 new:
class(res) <- c("bibentryRd", class(res))

Expand Down Expand Up @@ -315,12 +366,53 @@ Rdo_flatinsert <- function(rdo, val, pos, before = TRUE){
res
}

## TODO: auto-deduce 'package'?
insert_ref <- function(key, package = NULL, ...) { # bibfile = "REFERENCES.bib"
if(is.null(package))

.get_bibs0 <- function(package, ..., cached_env) {
if(is.null(package))
stop("argument 'package' must be provided")

bibs <- get_bibentries(package = package, ..., stop_on_error = FALSE)
if(!is.null(cached_env)){
if(is.null(cached_env$refsmat))
cached_env$refsmat <- matrix(character(0), nrow = 0, ncol = 2)
if(is.null(cached_env$allbibs))
cached_env$allbibs <- list()
}

## if(length(keys) > 1)
## stop("`keys' must be a character string")
##
## if(!cite_only)
## cached_env$refsmat <- rbind(cached_env$refsmat, c(keys, package))
##
## if(dont_cite)
## return(character(0))


if(is.null(cached_env)){
bibs <- get_bibentries(package = package, ..., stop_on_error = FALSE)
}else{
bibs <- cached_env$allbibs[[package]]
if(is.null(bibs)){
bibs <- get_bibentries(package = package, ..., stop_on_error = FALSE)
cached_env$allbibs[[package]] <- bibs
}
}

bibs
}


## TODO: auto-deduce 'package'?
## 2020-09-30: changing to cache bib as \insertCite does (new arg. cached_env, etc)
insert_ref <- function(key, package = NULL, ..., cached_env = NULL) { # bibfile = "REFERENCES.bib"

# 2020-09-30: replaced by a single call
# if(is.null(package))
# stop("argument 'package' must be provided")
#
# bibs <- get_bibentries(package = package, ..., stop_on_error = FALSE)
#
bibs <- .get_bibs0(package, ..., cached_env = cached_env)

if(length(bibs) == 0){
note <- paste0("\"Failed to insert reference with key = ", key,
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

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

6 changes: 3 additions & 3 deletions docs/authors.html

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

2 changes: 1 addition & 1 deletion docs/index.html

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

23 changes: 20 additions & 3 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pandoc: '2.5'
pkgdown: 1.5.1
pkgdown_sha: ~
articles: {}
last_built: 2020-07-02T08:12Z
last_built: 2020-10-03T09:03Z

2 changes: 1 addition & 1 deletion docs/reference/RStudio_reprompt.html

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

2 changes: 1 addition & 1 deletion docs/reference/Rd_combo.html

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

6 changes: 3 additions & 3 deletions docs/reference/Rdapply.html

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

Loading

0 comments on commit aa46920

Please sign in to comment.