Skip to content

Commit

Permalink
Allow alternative knitr engines (#1392)
Browse files Browse the repository at this point in the history
Fixes #1149.
  • Loading branch information
gaborcsardi authored Jul 10, 2022
1 parent 5ead04a commit 360143f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions man/markdown_pass1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/testthat/_snaps/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,28 @@
[<text>: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 ./<text>
\name{x}
\alias{x}
\title{Title}
\description{
Description.
}
\details{
\if{html}{\out{<div class="sourceCode default">}}\preformatted{```\{r\}
# comment
this <- 10
is <- this + 10
good <- this + is
}\if{html}{\out{</div>}}
}

5 changes: 5 additions & 0 deletions tests/testthat/example.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
```{r}
# comment
this <- 10
is <- this + 10
good <- this + is
17 changes: 17 additions & 0 deletions tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
"))
)
})

0 comments on commit 360143f

Please sign in to comment.