Skip to content

Commit

Permalink
Escape Rd comments in markdown
Browse files Browse the repository at this point in the history
Fixes #879
  • Loading branch information
hadley committed Sep 10, 2019
1 parent 803c466 commit 5d56ec2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# roxygen2 (development version)

* Rd comments (`%`) are automatically escaped in markdown text. Existing
code that uses `\%` is automatically repaired; if you're generating
more exotic sequences like `\\%` you'll need to put them inside
backticks (#879)

* As well as storing roxygen options in the `Roxygen` field of the
`DESCRIPTION` you can now also store them in `man/roxygen/meta.R` (#889).

Expand Down
2 changes: 1 addition & 1 deletion R/markdown-link.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ parse_link <- function(destination, contents) {
)

} else {
contents <- gsub("%", "\\\\%", mdxml_link_text(contents))
contents <- mdxml_link_text(contents)

list(
paste0(
Expand Down
20 changes: 15 additions & 5 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mdxml_node_to_rd <- function(xml, tag) {

code_block = paste0("\\preformatted{", gsub("%", "\\\\%", xml_text(xml)), "}"),
paragraph = paste0("\n\n", mdxml_children_to_rd(xml, tag)),
text = xml_text(xml),
text = escape_comment(xml_text(xml)),
code = paste0("\\code{", gsub("%", "\\\\%", xml_text(xml)), "}"),
emph = paste0("\\emph{", mdxml_children_to_rd(xml, tag), "}"),
strong = paste0("\\strong{", mdxml_children_to_rd(xml, tag), "}"),
Expand All @@ -63,11 +63,11 @@ mdxml_node_to_rd <- function(xml, tag) {

mdxml_unknown <- function(xml, tag) {
roxy_tag_warning(tag, "Unknown xml node: ", xml_name(xml))
xml_text(xml)
escape_comment(xml_text(xml))
}
mdxml_unsupported <- function(xml, tag, feature) {
roxy_tag_warning(tag, "Use of ", feature, " is not currently supported")
xml_text(xml)
escape_comment(xml_text(xml))
}


Expand Down Expand Up @@ -108,7 +108,7 @@ mdxml_link <- function(xml) {
if (!is.null(link)) {
paste0(link, collapse = "")
} else if (dest == "" || dest == xml_text(xml)) {
paste0("\\url{", xml_text(xml), "}")
paste0("\\url{", escape_comment(xml_text(xml)), "}")
} else {
paste0("\\href{", dest, "}{", mdxml_link_text(contents), "}")
}
Expand All @@ -120,11 +120,21 @@ mdxml_link <- function(xml) {
mdxml_link_text <- function(xml_contents) {
text <- xml_text(xml_contents)
text[xml_name(xml_contents) %in% c("linebreak", "softbreak")] <- " "
paste0(text, collapse = "")
escape_comment(paste0(text, collapse = ""))
}

mdxml_image = function(xml) {
dest <- xml_attr(xml, "destination")
title <- xml_attr(xml, "title")
paste0("\\figure{", dest, "}{", title, "}")
}


escape_comment <- function(x) {
# Strip existing escaping - this is admittedly a hack and makes the
# state of % escaping more confusing, but it only affects markdown, and
# I think if you do want a literal \% you can do `\%`
x <- gsub("\\%", "%", x, fixed = TRUE)
x <- gsub("%", "\\%", x, fixed = TRUE)
x
}
7 changes: 7 additions & 0 deletions tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ So far so good. \\preformatted{ *these are not
expect_equal(get_tag(out1, "description")[[2]], desc1)
})

test_that("% is automatically escaped", {
expect_equal(markdown("5%"), "5\\%")

# Even if it was escaped before
expect_equal(markdown("5\\%"), "5\\%")
})

test_that("% and $ and _ are not unescaped", {
out1 <- roc_proc_text(rd_roclet(), "
#' Title
Expand Down

0 comments on commit 5d56ec2

Please sign in to comment.