From 569ee0515fce00eac98ef45e642b805cb473ce1c Mon Sep 17 00:00:00 2001 From: Georgi Boshnakov Date: Mon, 30 Oct 2023 16:29:34 +0000 Subject: [PATCH] fix issue with 'lost bracket' NOTE from R-devdel check --- DESCRIPTION | 2 +- R/bib.R | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 184d076..d4553c4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: Rdpack Type: Package Title: Update and Manipulate Rd Documentation Objects -Version: 2.5 +Version: 2.5.9000 Authors@R: c( person(given = c("Georgi", "N."), family = "Boshnakov", role = c("aut", "cre"), diff --git a/R/bib.R b/R/bib.R index c552e8f..a33a279 100644 --- a/R/bib.R +++ b/R/bib.R @@ -1089,6 +1089,69 @@ deparseLatexToRd <- function(x, dropBraces = FALSE) paste(result, collapse="") } + + +`%notin%` <- +function(x, y) + is.na(match(x, y)) + +## This converts a latex object into a single element character vector +deparseLatexToRd <- function(x, dropBraces = FALSE) +{ + specials <- c("\\", "#", "$", "%", "&", "~", "_", "^", "{", "}") + result <- character() + lastTag <- "TEXT" + expectArg <- FALSE + for (i in seq_along(x)) { + a <- x[[i]] + tag <- attr(a, "latex_tag") + if (is.null(tag)) tag <- "NULL" + result <- c(result, + switch(tag, + VERB = , + COMMENT = a, + TEXT = c(if (lastTag == "MACRO" && expectArg && grepl("^[[:alpha:]]", a)) + ## restore space that the parser has eaten ('\item text') + " ", + a), + MACRO = { + ## see issue #26 + ## regex in r-devel/R/src/library/tools/R/RdConv2.R: + ## pat <- "([^\\]|^)\\\\[#$&_^~]" + ## here we add grouping for substitution + pat <- "([^\\]|^)(\\\\)([#$&_^~])" # with more grouping + if(grepl(pat, a)){ + a <- gsub(pat, "\\1\\3", a) + } + c(if (lastTag == "MACRO" && expectArg && grepl("^[[:alpha:]]", a)) + ## restore space that the parser has eaten ('\item text') + " ", + a) + }, + BLOCK = if (dropBraces && !expectArg) + Recall(a) + else + c("{", Recall(a), "}"), + ENVIRONMENT = c( + "\\begin{", a[[1L]], "}", + Recall(a[[2L]]), + "\\end{", a[[1L]], "}"), + ## enclose maths in \eqn{...}, not $ ... $; # \( and \) parse as MACRO + MATH = c("\\eqn{", Recall(a), "}"), + NULL = stop("Internal error, no tag", domain = NA) + )) + lastTag <- tag + expectArg <- + if (tag == "MACRO") + a %notin% paste0("\\", c(specials, "(", ")")) + else + expectArg && + tag %in% c("BLOCK", "COMMENT") # \cmd{}{}, \cmd% + ## currently ignoring \cmd {}, \cmd[]{}, \cmd*{} + } + paste(result, collapse="") +} + Rdpack_bibstyles <- local({ styles <- list() function(package, authors){