Skip to content

Commit

Permalink
Forward compatibility with evaluate 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jul 3, 2024
1 parent 5c1d34d commit f45ffdc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 16 additions & 2 deletions R/highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ highlight_examples <- function(code, topic, env = globalenv()) {
do.call(fig_save, c(list(plot, name), fig_settings()))
}

hide_dontshow <- function(src, call) {
if (is_call(call, c("DONTSHOW", "TESTONLY"))) NULL else src
hide_dontshow <- function(src, expr) {
if (is.expression(expr)) {
# evaluate 1.0.0
if (length(expr) > 0) {
hide <- is_call(expr[[1]], c("DONTSHOW", "TESTONLY"))
} else {
hide <- FALSE
}
} else if (is.call(expr)) {
# evaluate 0.24.0
hide <- is_call(expr, c("DONTSHOW", "TESTONLY"))
} else {
hide <- FALSE
}

if (hide) NULL else src
}
handler <- evaluate::new_output_handler(
value = pkgdown_print,
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-highlight.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("highlight_examples captures dependencies", {
withr::defer(file_delete(test_path("Rplot001.png")))
withr::defer(try(file_delete(test_path("Rplot001.png")), TRUE))

dummy_dep <- htmltools::htmlDependency("dummy", "1.0.0", "dummy.js")
widget <- htmlwidgets::createWidget("test", list(), dependencies = dummy_dep)
Expand All @@ -9,14 +9,13 @@ test_that("highlight_examples captures dependencies", {
expect_equal(attr(out, "dependencies")[-1], list(dummy_dep))
})


test_that("highlight_examples runs and hides DONTSHOW calls()", {
out <- highlight_examples("DONTSHOW(x <- 1)\nx")
expect_snapshot(cat(strip_html_tags(out)))
})

test_that("highlight_text & highlight_examples include sourceCode div", {
withr::defer(file_delete(test_path("Rplot001.png")))
withr::defer(try(file_delete(test_path("Rplot001.png")), TRUE))

html <- xml2::read_html(highlight_examples("a + a", "x"))
expect_equal(xpath_attr(html, "./body/div", "class"), "sourceCode")
Expand Down

0 comments on commit f45ffdc

Please sign in to comment.