From 1eb44ba9d1b469570d60245afb9a66186ee17ca7 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 13:16:34 +0200 Subject: [PATCH 1/8] Add missing variable in templates --- .../templates/revealjs_presentation/resources/default.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/inst/rmarkdown/templates/revealjs_presentation/resources/default.html b/inst/rmarkdown/templates/revealjs_presentation/resources/default.html index 4a7ef29..9729ed4 100644 --- a/inst/rmarkdown/templates/revealjs_presentation/resources/default.html +++ b/inst/rmarkdown/templates/revealjs_presentation/resources/default.html @@ -549,7 +549,12 @@

$date$

$endif$ $if(toc)$
+
$endif$ From f5580a25afe1a7b970b39db686aa75d9445cb13d Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 13:16:50 +0200 Subject: [PATCH 2/8] expose them through output format function --- R/revealjs_presentation.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/revealjs_presentation.R b/R/revealjs_presentation.R index 12edafc..94e21e6 100644 --- a/R/revealjs_presentation.R +++ b/R/revealjs_presentation.R @@ -64,6 +64,8 @@ revealjs_presentation <- function(incremental = FALSE, center = FALSE, slide_level = 2, + toc = FALSE, + toc_depth = 3, fig_width = 8, fig_height = 6, fig_retina = if (!fig_caption) 2, @@ -88,6 +90,9 @@ revealjs_presentation <- function(incremental = FALSE, # base pandoc options for all reveal.js output args <- c() + + # table of contents + args <- c(args, pandoc_toc_args(toc, toc_depth)) # template path and assets if (identical(template, "default")) { From 7c9a4f120b3d9ee88dd258bc9aedb526e69ef804 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 14:52:04 +0200 Subject: [PATCH 3/8] Add some tests using xml2 --- R/utils.R | 2 +- tests/testthat/helpers.R | 49 +++++++++++++++++++++ tests/testthat/test-revealjs_presentation.R | 16 +++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/helpers.R create mode 100644 tests/testthat/test-revealjs_presentation.R diff --git a/R/utils.R b/R/utils.R index 52381a2..7251cff 100644 --- a/R/utils.R +++ b/R/utils.R @@ -27,4 +27,4 @@ process_reveal_option <- function(option, value) { } } pandoc_variable_arg(option, value) -} \ No newline at end of file +} diff --git a/tests/testthat/helpers.R b/tests/testthat/helpers.R new file mode 100644 index 0000000..ce5d6c4 --- /dev/null +++ b/tests/testthat/helpers.R @@ -0,0 +1,49 @@ +local_temp_rmd_file <- function(..., .env = parent.frame()) { + path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd") + xfun::write_utf8(c(...), path) + path +} + +local_temp_draft <- function(.env = parent.frame()) { + path <- withr::local_tempfile(.local_envir = .env, fileext = ".Rmd") + # TODO: Use `rmarkdown::draft()` when rmarkdown 2.12 is out. + pkg_file <- getFromNamespace("pkg_file", "rmarkdown") + template_path <- pkg_file("rmarkdown", "templates", "revealjs_presentation", + package = "revealjs") + rmarkdown::draft(path, template_path, edit = FALSE) +} + +.render_and_read <- function(input, xml = TRUE, ...) { + skip_if_not_pandoc() + output_file <- withr::local_tempfile(fileext = ".html") + res <- rmarkdown::render(input, output_file = output_file, quiet = TRUE, ...) + if (xml) { + xml2::read_html(res) + } else { + xfun::read_utf8(res) + } +} + +# Use to test pandoc availability or version lower than +skip_if_not_pandoc <- function(ver = NULL) { + if (!pandoc_available(ver)) { + msg <- if (is.null(ver)) { + "Pandoc is not available" + } else { + sprintf("Version of Pandoc is lower than %s.", ver) + } + skip(msg) + } +} + +# Use to test version greater than +skip_if_pandoc <- function(ver = NULL) { + if (pandoc_available(ver)) { + msg <- if (is.null(ver)) { + "Pandoc is available" + } else { + sprintf("Version of Pandoc is greater than %s.", ver) + } + skip(msg) + } +} \ No newline at end of file diff --git a/tests/testthat/test-revealjs_presentation.R b/tests/testthat/test-revealjs_presentation.R new file mode 100644 index 0000000..a4eb93f --- /dev/null +++ b/tests/testthat/test-revealjs_presentation.R @@ -0,0 +1,16 @@ +test_that("toc argument works", { + skip_if_not_pandoc() + skip_if_not_installed("xml2") + rmd <- local_temp_draft() + html <- .render_and_read( + rmd, + output_options = list( + toc = TRUE, + pandoc_args = c(pandoc_variable_arg("toc-title", "TOC")) + ) + ) + toc <- xml2::xml_find_all(html, "//section[@id='TOC']") + expect_length(toc, 1) + xml2::xml_find_all(toc, "./nav/*[contains(@id, 'toc-title')]") + expect_equal(xml2::xml_name(xml2::xml_child(toc)), "nav") +}) From ca1b7401f2704190e92dd59ff13deea7101478ca Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 14:57:27 +0200 Subject: [PATCH 4/8] test toc-title too --- tests/testthat/test-revealjs_presentation.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-revealjs_presentation.R b/tests/testthat/test-revealjs_presentation.R index a4eb93f..ade1c9a 100644 --- a/tests/testthat/test-revealjs_presentation.R +++ b/tests/testthat/test-revealjs_presentation.R @@ -3,7 +3,7 @@ test_that("toc argument works", { skip_if_not_installed("xml2") rmd <- local_temp_draft() html <- .render_and_read( - rmd, + rmd, output_options = list( toc = TRUE, pandoc_args = c(pandoc_variable_arg("toc-title", "TOC")) @@ -11,6 +11,10 @@ test_that("toc argument works", { ) toc <- xml2::xml_find_all(html, "//section[@id='TOC']") expect_length(toc, 1) - xml2::xml_find_all(toc, "./nav/*[contains(@id, 'toc-title')]") - expect_equal(xml2::xml_name(xml2::xml_child(toc)), "nav") + expect_equal( + xml2::xml_text( + xml2::xml_find_all(toc, "./nav/*[contains(@id, 'toc-title')]") + ), + "TOC" + ) }) From af7047d593c0000afea4a6f212ddf5d558389d58 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 14:57:35 +0200 Subject: [PATCH 5/8] Add xml2 in suggest --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 797f81c..9395d18 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Depends: Imports: rmarkdown (>= 1.7) Suggests: + xml2, bslib, testthat (>= 3.0.0) Encoding: UTF-8 From 91af6f1c147947d46c853cb1584c35bb13c0b186 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 15:04:01 +0200 Subject: [PATCH 6/8] devtools::document() --- man/revealjs_presentation.Rd | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/man/revealjs_presentation.Rd b/man/revealjs_presentation.Rd index e89753d..4aed888 100644 --- a/man/revealjs_presentation.Rd +++ b/man/revealjs_presentation.Rd @@ -8,6 +8,8 @@ revealjs_presentation( incremental = FALSE, center = FALSE, slide_level = 2, + toc = FALSE, + toc_depth = 3, fig_width = 8, fig_height = 6, fig_retina = if (!fig_caption) 2, @@ -45,6 +47,11 @@ produced, with level 1 headers building horizontally and level 2 headers building vertically. It is not recommended that you use deeper nesting of section levels with reveal.js.} +\item{toc}{\code{TRUE} to include a table of contents in the output (only +level 1 headers will be included in the table of contents).} + +\item{toc_depth}{Depth of headers to include in table of contents} + \item{fig_width}{Default width (in inches) for figures} \item{fig_height}{Default height (in inches) for figures} From a7363907f860a858e2ececb9ad19e05f9a6abda2 Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 15:09:52 +0200 Subject: [PATCH 7/8] Add xfun in Suggest as used in tests --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 9395d18..df965d0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Depends: Imports: rmarkdown (>= 1.7) Suggests: + xfun, xml2, bslib, testthat (>= 3.0.0) From 2b4d8e2e2a6f8140f26066a8333419d80d93c18b Mon Sep 17 00:00:00 2001 From: christophe dervieux Date: Thu, 23 Sep 2021 15:11:21 +0200 Subject: [PATCH 8/8] Add withr too --- DESCRIPTION | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index df965d0..b780b5a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,10 +33,11 @@ Depends: Imports: rmarkdown (>= 1.7) Suggests: - xfun, + xfun (>= 0.21), xml2, bslib, - testthat (>= 3.0.0) + testthat (>= 3.0.0), + withr (>= 2.4.2) Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.2