From ac4c897cc8c080a8968730258ba6a47f52dcdb93 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 4 Aug 2026 16:16:48 -0500 Subject: [PATCH 1/3] Don't double-escape an already-escaped % in Rd output (#31) escape_rd() and the three examples paths escaped % unconditionally, so a pre-escaped \% from roxygen2-style sources became \\%, which Rd reads as a literal backslash plus a comment start and mangles the generated file with warnings that never mention %. Centralize the escape in escape_percent(), which uses a negative lookbehind so bare % is escaped exactly once and \% passes through untouched. Both the bare % tinyrox always accepted and the pre-escaped \% a roxygen2 author brings along now produce identical, valid Rd. --- R/rd.R | 27 +++++++++++++++++++-------- inst/tinytest/test_rd.R | 30 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/R/rd.R b/R/rd.R index 10d4961..542ea1b 100644 --- a/R/rd.R +++ b/R/rd.R @@ -139,7 +139,7 @@ generate_rd <- function(tags, formals = NULL, source_file = NULL, if (!is.null(tags$examples) && nchar(trimws(tags$examples)) > 0) { lines <- c(lines, "\\examples{") # Escape % in examples (Rd comment character), but leave other content verbatim - examples_escaped <- gsub("%", "\\\\%", tags$examples) + examples_escaped <- escape_percent(tags$examples) lines <- c(lines, examples_escaped) lines <- c(lines, "}") } @@ -229,7 +229,7 @@ generate_data_rd <- function(tags, source_file = NULL, format_string = NULL) { # Examples (before seealso like roxygen2) if (!is.null(tags$examples) && nchar(trimws(tags$examples)) > 0) { lines <- c(lines, "\\examples{") - examples_escaped <- gsub("%", "\\\\%", tags$examples) + examples_escaped <- escape_percent(tags$examples) lines <- c(lines, examples_escaped) lines <- c(lines, "}") } @@ -303,6 +303,20 @@ format_object_info <- function(name, pkg_path) { paste0("An object of class \\code{", obj_class, "} of length ", obj_len, ".") } +#' Escape Unescaped Percent Signs +#' +#' Escapes % as \\% for Rd, leaving an already-escaped \\% alone. roxygen2 +#' makes authors escape % themselves, so sources migrated from roxygen2 +#' carry \\% already; escaping unconditionally would double it into \\\\%, +#' which Rd reads as a literal backslash plus a comment start (#31). +#' +#' @param text Text to escape. +#' @return Text with every % escaped exactly once. +#' @keywords internal +escape_percent <- function(text) { + gsub("(? 0) { lines <- c(lines, "\\examples{") - lines <- c(lines, gsub("%", "\\\\%", paste(examples_parts, collapse = "\n\n"))) + lines <- c(lines, escape_percent(paste(examples_parts, collapse = "\n\n"))) lines <- c(lines, "}") } diff --git a/inst/tinytest/test_rd.R b/inst/tinytest/test_rd.R index 3002542..cb51d04 100644 --- a/inst/tinytest/test_rd.R +++ b/inst/tinytest/test_rd.R @@ -312,3 +312,33 @@ rd29 <- tinyrox:::generate_rd(tags29, list(names = c("provider", "access_token"), usage = long_args)) expect_true(all(nchar(strsplit(rd29, "\n")[[1]]) <= 90)) + +# --- Issue #31: already-escaped \% must not be double-escaped --- + +# Behavior table from the issue: only the pre-escaped rows change +expect_equal(tinyrox:::escape_rd("frame_%04d.png"), "frame_\\%04d.png") +expect_equal(tinyrox:::escape_rd("frame_\\%04d.png"), "frame_\\%04d.png") +expect_equal(tinyrox:::escape_rd("x %in% y"), "x \\%in\\% y") +expect_equal(tinyrox:::escape_rd("trailing \\%"), "trailing \\%") + +# Mixed bare and pre-escaped in one string +expect_equal(tinyrox:::escape_rd("50% and \\%"), "50\\% and \\%") + +# Markup-passthrough branch preserves pre-escaped % too +expect_equal(tinyrox:::escape_rd('\\code{"frame_\\%04d.png"}'), + '\\code{"frame_\\%04d.png"}') +expect_equal(tinyrox:::escape_rd('\\code{"frame_%04d.png"}'), + '\\code{"frame_\\%04d.png"}') + +# Examples path goes through the same escape +tags31 <- list( + title = "T", description = "D", details = NULL, + params = list(), return = NULL, + examples = 'sprintf("%d of \\%d", 1)', + seealso = NULL, references = NULL, + aliases = character(), keywords = character(), family = NULL, + name = "fmt", noRd = FALSE +) +rd31 <- tinyrox:::generate_rd(tags31, list(names = character(), + usage = character())) +expect_true(grepl('sprintf("\\%d of \\%d", 1)', rd31, fixed = TRUE)) From 499876fd371365d33ad9563ee35cb06f877db5e1 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 4 Aug 2026 16:17:11 -0500 Subject: [PATCH 2/3] rformat + document --- man/escape_percent.Rd | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 man/escape_percent.Rd diff --git a/man/escape_percent.Rd b/man/escape_percent.Rd new file mode 100644 index 0000000..948838f --- /dev/null +++ b/man/escape_percent.Rd @@ -0,0 +1,20 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{escape_percent} +\alias{escape_percent} +\title{Escape Unescaped Percent Signs} +\usage{ +escape_percent(text) +} +\arguments{ +\item{text}{Text to escape.} +} +\value{ +Text with every \% escaped exactly once. +} +\description{ +Escapes \% as \\% for Rd, leaving an already-escaped \\% alone. roxygen2 +makes authors escape \% themselves, so sources migrated from roxygen2 +carry \\% already; escaping unconditionally would double it into \\\\%, +which Rd reads as a literal backslash plus a comment start (#31). +} +\keyword{internal} From ac737707adaec89b4ee6e9c3227b1209fb68602c Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Tue, 4 Aug 2026 16:17:23 -0500 Subject: [PATCH 3/3] Bump version to 0.4.0.2 --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 866a8d9..c6e6375 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tinyrox Title: Minimal R Documentation Generator -Version: 0.4.0.1 +Version: 0.4.0.2 Date: 2026-06-24 Authors@R: c( person("Troy", "Hernandez", email = "troy@cornball.ai", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 8f09767..fd4ebf5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# tinyrox 0.4.0.2 + +* An already-escaped `\%` in roxygen source is no longer double-escaped into `\\%` (#31). Both bare `%` and roxygen2-style pre-escaped `\%` now produce a single `\%` in the generated Rd. + # tinyrox 0.4.0.1 * `\usage` lines now stay within the 90-character Rd limit (#29). Overlong signatures wrap roxygen2-style: one argument per line, breaking inside a long default when needed, so `R CMD check` no longer NOTEs "Rd line widths".