Skip to content

Commit e1d3930

Browse files
committed
Monkeypatch as.character.Rd to fix \href{} rendering
Fixes #778
1 parent 7a8da92 commit e1d3930

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# roxygen2 (development version)
22

3+
* `\href{}` links in external inherited documentation are now inserted
4+
correctly (without additional `{}`) (#778).
5+
36
* White space is automatically trimmed off the `RoxygenNote` field when
47
comparing the installed version of roxygen2 to the version used to
58
generate the documentation (#802).

R/utils-rd.R

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,37 @@ get_tags <- function(rd, tag) {
7373
}
7474

7575
rd2text <- function(x) {
76-
chr <- as.character(structure(x, class = "Rd"), deparse = TRUE)
76+
chr <- as_character_rd(structure(x, class = "Rd"), deparse = TRUE)
7777
paste(chr, collapse = "")
7878
}
7979

80+
81+
# helpers -----------------------------------------------------------------
82+
83+
parse_rd <- function(x) {
84+
con <- textConnection(x)
85+
on.exit(close(con), add = TRUE)
86+
87+
tryCatch(
88+
tools::parse_Rd(con, fragment = TRUE, encoding = "UTF-8"),
89+
warning = function(cnd) NULL
90+
)
91+
}
92+
93+
# Generated in .onLoad()
94+
as_character_rd <- NULL
95+
make_as_character_rd <- function() {
96+
# "as.character.Rd" appears to be missing \href in TWOARGS
97+
# this code hacks the body of the function to add it
98+
fn <- internal_f("tools", "as.character.Rd")
99+
100+
body <- body(fn)
101+
idx <- purrr::detect_index(body, ~ is_call(.x, "<-", 2) && is_symbol(.x[[2]], "TWOARG"))
102+
if (idx == 0) {
103+
return(fn)
104+
}
105+
106+
body[[idx]][[3]] <- call_modify(body[[idx]][[3]], "\\href")
107+
body(fn) <- body
108+
fn
109+
}

R/zzz.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.onLoad <- function(...) {
2+
as_character_rd <<- make_as_character_rd()
3+
}

tests/testthat/test-utils-rd.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_that("href doesn't get extra parens", {
2+
expect_equal(rd2text(parse_rd("\\href{a}{b}")), "\\href{a}{b}\n")
3+
})

0 commit comments

Comments
 (0)