Skip to content
Merged
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
@@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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".
Expand Down
27 changes: 19 additions & 8 deletions R/rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, "}")
}
Expand Down Expand Up @@ -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, "}")
}
Expand Down Expand Up @@ -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("(?<!\\\\)%", "\\\\%", text, perl = TRUE)
}

#' Escape Special Characters for Rd
#'
#' Escapes special characters for Rd format, but detects and preserves
Expand All @@ -319,17 +333,14 @@ escape_rd <- function(text) {
# Check if text contains Rd markup (backslash commands like \describe, \item,
# \Sexpr[...]{}, etc.). If so, pass through with minimal escaping (just %).
if (grepl("\\\\[a-zA-Z]+(\\[.*\\])?\\{", text)) {
text <- gsub("%", "\\\\%", text)
return(text)
return(escape_percent(text))
}

# No Rd markup - escape braces and percent only
# Don't escape backslashes - unknown sequences pass through in Rd (like roxygen2)
text <- gsub("\\{", "\\\\{", text)
text <- gsub("\\}", "\\\\}", text)
text <- gsub("%", "\\\\%", text)

text
escape_percent(text)
}

#' Format Usage Line
Expand Down Expand Up @@ -763,7 +774,7 @@ generate_rd_grouped <- function(topic, entries, all_tags,
}
if (length(examples_parts) > 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, "}")
}

Expand Down
30 changes: 30 additions & 0 deletions inst/tinytest/test_rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
20 changes: 20 additions & 0 deletions man/escape_percent.Rd
Original file line number Diff line number Diff line change
@@ -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}
Loading