From 360143f1876e9a29b330793c1e2f23673c69dbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Sun, 10 Jul 2022 22:43:26 +0200 Subject: [PATCH] Allow alternative knitr engines (#1392) Fixes #1149. --- NEWS.md | 2 ++ R/markdown.R | 10 ++++++++-- man/markdown_pass1.Rd | 7 +++++++ tests/testthat/_snaps/markdown.md | 25 +++++++++++++++++++++++++ tests/testthat/example.Rmd | 5 +++++ tests/testthat/test-markdown.R | 17 +++++++++++++++++ 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/example.Rmd diff --git a/NEWS.md b/NEWS.md index 732765eb..341e840b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # roxygen2 (development version) +* You can now use alternative knitr engines in markdown code blocks (#1149). + * Fix bug interpolating the results of indented inline RMarkdown (#1353). * R6 documentation no longer shows inherited methods if there aren't any diff --git a/R/markdown.R b/R/markdown.R index 8286efff..0362492f 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -52,6 +52,12 @@ markdown <- function(text, tag = NULL, sections = FALSE) { #' plot(1:10) #' ``` #' +#' Alternative knitr engines: +#' +#' ```{verbatim} +#' #| file = "tests/testthat/example.Rmd" +#' ``` +#' #' Also see `vignette("rd-formatting")`. #' #' @param text Input text. @@ -113,9 +119,9 @@ work_around_cmark_sourcepos_bug <- function(text, rcode_pos) { } is_markdown_code_node <- function(x) { - info <- str_sub(xml_attr(x, "info"), 1, 3) + info <- xml_attr(x, "info") str_sub(xml_text(x), 1, 2) == "r " || - (!is.na(info) && info %in% c("{r ", "{r}", "{r,")) + (!is.na(info) && grepl("^[{][a-zA-z]+[}, ]", info)) } parse_md_pos <- function(text) { diff --git a/man/markdown_pass1.Rd b/man/markdown_pass1.Rd index 63ef0540..e7319ebc 100644 --- a/man/markdown_pass1.Rd +++ b/man/markdown_pass1.Rd @@ -53,5 +53,12 @@ Plots: \figure{test-figure-1.png} Also see \code{vignette("rd-formatting")}. + +\if{html}{\out{
}}\preformatted{```\{r\} +# comment +this <- 10 +is <- this + 10 +good <- this + is +}\if{html}{\out{
}} } \keyword{internal} diff --git a/tests/testthat/_snaps/markdown.md b/tests/testthat/_snaps/markdown.md index 18ba8e09..fc260b22 100644 --- a/tests/testthat/_snaps/markdown.md +++ b/tests/testthat/_snaps/markdown.md @@ -57,3 +57,28 @@ [:4] @description markdown translation failed x block quotes are not currently supported +# alternative knitr engines + + Code + print(out1 <- roc_proc_text(rd_roclet(), + "\n #' Title\n #'\n #' Description.\n #'\n #' ```{verbatim}\n #' #| file = testthat::test_path(\"example.Rmd\")\n #' ```\n #' @md\n #' @name x\n NULL\n ")) + Output + $x.Rd + % Generated by roxygen2: do not edit by hand + % Please edit documentation in ./ + \name{x} + \alias{x} + \title{Title} + \description{ + Description. + } + \details{ + \if{html}{\out{
}}\preformatted{```\{r\} + # comment + this <- 10 + is <- this + 10 + good <- this + is + }\if{html}{\out{
}} + } + + diff --git a/tests/testthat/example.Rmd b/tests/testthat/example.Rmd new file mode 100644 index 00000000..b1b80d86 --- /dev/null +++ b/tests/testthat/example.Rmd @@ -0,0 +1,5 @@ +```{r} +# comment +this <- 10 +is <- this + 10 +good <- this + is diff --git a/tests/testthat/test-markdown.R b/tests/testthat/test-markdown.R index ae8c613a..5806b0ab 100644 --- a/tests/testthat/test-markdown.R +++ b/tests/testthat/test-markdown.R @@ -619,3 +619,20 @@ test_that("markup in headings", { ) ) }) + +test_that("alternative knitr engines", { + expect_snapshot( + print(out1 <- roc_proc_text(rd_roclet(), " + #' Title + #' + #' Description. + #' + #' ```{verbatim} + #' #| file = testthat::test_path(\"example.Rmd\") + #' ``` + #' @md + #' @name x + NULL + ")) + ) +})