Skip to content

Commit

Permalink
Add @examplesIf tag
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 25, 2019
1 parent 4d131e7 commit 551c94c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE, load = "installed")
RoxygenNote: 7.0.0.9000
RoxygenNote: 7.0.1.9000
Language: en-GB
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ S3method(roxy_tag_parse,roxy_tag_evalNamespace)
S3method(roxy_tag_parse,roxy_tag_evalRd)
S3method(roxy_tag_parse,roxy_tag_example)
S3method(roxy_tag_parse,roxy_tag_examples)
S3method(roxy_tag_parse,roxy_tag_examplesIf)
S3method(roxy_tag_parse,roxy_tag_export)
S3method(roxy_tag_parse,roxy_tag_exportClass)
S3method(roxy_tag_parse,roxy_tag_exportMethod)
Expand Down Expand Up @@ -165,6 +166,7 @@ S3method(roxy_tag_rd,roxy_tag_encoding)
S3method(roxy_tag_rd,roxy_tag_evalRd)
S3method(roxy_tag_rd,roxy_tag_example)
S3method(roxy_tag_rd,roxy_tag_examples)
S3method(roxy_tag_rd,roxy_tag_examplesIf)
S3method(roxy_tag_rd,roxy_tag_family)
S3method(roxy_tag_rd,roxy_tag_field)
S3method(roxy_tag_rd,roxy_tag_format)
Expand Down
29 changes: 29 additions & 0 deletions R/rd-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ roxy_tag_parse.roxy_tag_examples <- function(x) {
tag_examples(x)
}
#' @export
roxy_tag_parse.roxy_tag_examplesIf <- function(x) {
lines <- unlist(strsplit(x$raw, "\r?\n"))

condition <- lines[1]
tryCatch(
suppressWarnings(parse(text = condition)),
error = function(err) {
roxy_tag_warning(x, "failed to parse condition of @examplesIf")
}
)

dontshow <- paste0(
"\\dontshow{if (",
condition,
") (if (getRversion() >= \"3.4\") withAutoprint else force)(\\{ # examplesIf}"
)

x$raw <- paste(
c(dontshow, lines[-1], "\\dontshow{\\}) # examplesIf}"),
collapse = "\n"
)

x <- tag_examples(x)
}
#' @export
roxy_tag_parse.roxy_tag_example <- function(x) {
x <- tag_value(x)

Expand All @@ -20,6 +45,10 @@ roxy_tag_rd.roxy_tag_examples <- function(x, base_path, env) {
rd_section("examples", x$val)
}
#' @export
roxy_tag_rd.roxy_tag_examplesIf <- function(x, base_path, env) {
rd_section("examples", x$val)
}
#' @export
roxy_tag_rd.roxy_tag_example <- function(x, base_path, env) {
path <- file.path(base_path, x$val)
if (!file.exists(path)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-rd-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ test_that("warns if path doesn't exist", {
)
})

test_that("@examplesIf", {
out <- roc_proc_text(rd_roclet(), "
#' @name a
#' @title a
#' @examplesIf foo::bar()
#' maybe-run-this-code
#' @examplesIf foobar()
#' and-this
NULL")[[1]]

verify_output(test_path("test-rd-examplesIf.txt"), {
out$get_section("examples")
})
})

test_that("% in @examples escaped before matching braces test (#213)", {
out <- roc_proc_text(rd_roclet(), "
#' @name a
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-rd-examplesIf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
> out$get_section("examples")
\examples{
\dontshow{if (foo::bar()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
maybe-run-this-code
\dontshow{\}) # examplesIf}
\dontshow{if (foobar()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
and-this
\dontshow{\}) # examplesIf}
}

0 comments on commit 551c94c

Please sign in to comment.