Move processing of reveal_options out of main format (#126)
This includes the addition of unit tests
diff --git a/R/revealjs_presentation.R b/R/revealjs_presentation.R
index 8ddb818..360465b 100644
--- a/R/revealjs_presentation.R
+++ b/R/revealjs_presentation.R
@@ -136,40 +136,21 @@
# additional reveal options
if (is.list(reveal_options)) {
- add_reveal_option <- function(option, value) {
- if (is.logical(value)) {
- value <- jsbool(value)
- } else if (is.character(value)) {
- # Special handling for some chalkboard plugin options
- # e.g: color: [ 'rgba(0,0,255,1)', 'rgba(255,255,255,0.5)' ]
- if (grepl("chalkboard-(background|color|draw|pen)", option)) {
- value <- sprintf("[%s]", paste(paste0("'", value, "'"), collapse = ", "))
- }
- # Add quotes around some config that can be several type
- # like number or percent unit or slideNumber = true or slideNumber = 'c/t'
- if (
- option %in% c("slideNumber") ||
- (option %in% c("width", "height") && grepl("%$", value))) {
- value <- paste0("'", value, "'")
- }
- }
- args <<- c(args, pandoc_variable_arg(option, value))
- }
-
for (option in names(reveal_options)) {
# special handling for nested options
if (option %in% c("chalkboard", "menu")) {
nested_options <- reveal_options[[option]]
for (nested_option in names(nested_options)) {
- add_reveal_option(
- paste0(option, "-", nested_option),
- nested_options[[nested_option]]
+ args <- c(args,
+ process_reveal_option(
+ paste0(option, "-", nested_option),
+ nested_options[[nested_option]]
+ )
)
}
- }
- # standard top-level options
- else {
- add_reveal_option(option, reveal_options[[option]])
+ } else {
+ # standard top-level options
+ args <- c(args, process_reveal_option(option, reveal_options[[option]]))
}
}
}
diff --git a/R/utils.R b/R/utils.R
index f681e24..52381a2 100644
--- a/R/utils.R
+++ b/R/utils.R
@@ -6,4 +6,25 @@
}
# Convert boolean from R to JS boolean
-jsbool <- function(value) ifelse(value, "true", "false")
\ No newline at end of file
+jsbool <- function(value) ifelse(value, "true", "false")
+
+# transfrom reveal option as pandoc variable
+process_reveal_option <- function(option, value) {
+ if (is.logical(value)) {
+ value <- jsbool(value)
+ } else if (is.character(value)) {
+ # Special handling for some chalkboard plugin options
+ # e.g: color: [ 'rgba(0,0,255,1)', 'rgba(255,255,255,0.5)' ]
+ if (grepl("chalkboard-(background|color|draw|pen)", option)) {
+ value <- sprintf("[%s]", paste(paste0("'", value, "'"), collapse = ", "))
+ }
+ # Add quotes around some config that can be several type
+ # like number or percent unit or slideNumber = true or slideNumber = 'c/t'
+ if (
+ option %in% c("slideNumber") ||
+ (option %in% c("width", "height") && grepl("%$", value))) {
+ value <- paste0("'", value, "'")
+ }
+ }
+ pandoc_variable_arg(option, value)
+}
\ No newline at end of file
diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R
new file mode 100644
index 0000000..c53a50d
--- /dev/null
+++ b/tests/testthat/test-utils.R
@@ -0,0 +1,29 @@
+test_that("reveal options are passed as pandoc variables", {
+ expect_equal(process_reveal_option("a", "b"), pandoc_variable_arg("a", "b"))
+})
+
+test_that("reveal options with boolean are transformed to JS bool", {
+ expect_equal(process_reveal_option("a", TRUE), pandoc_variable_arg("a", "true"))
+ expect_equal(process_reveal_option("a", FALSE), pandoc_variable_arg("a", "false"))
+})
+
+test_that("reveal options slideNumbers is treated specifically", {
+ expect_equal(process_reveal_option("slideNumber", "c/t"), pandoc_variable_arg("slideNumber", "'c/t'"))
+ expect_equal(process_reveal_option("slideNumber", TRUE), pandoc_variable_arg("slideNumber", "true"))
+})
+
+test_that("reveal options width / heigh in % are quoted", {
+ expect_equal(process_reveal_option("width", "50%"), pandoc_variable_arg("width", "'50%'"))
+ expect_equal(process_reveal_option("height", "50%"), pandoc_variable_arg("height", "'50%'"))
+ expect_equal(process_reveal_option("width", 5), pandoc_variable_arg("width", "5"))
+ expect_equal(process_reveal_option("height", 5), pandoc_variable_arg("height", "5"))
+})
+
+test_that("reveal options for chalkboard plugins special handling", {
+ expect_equal(process_reveal_option("chalkboard-background", "rgba(255,255,255,0.5)"), pandoc_variable_arg("chalkboard-background", "['rgba(255,255,255,0.5)']"))
+ expect_equal(process_reveal_option("chalkboard-color", c("a", "b")), pandoc_variable_arg("chalkboard-color", "['a', 'b']"))
+ expect_equal(process_reveal_option("chalkboard-draw", c("a", "b")), pandoc_variable_arg("chalkboard-draw", "['a', 'b']"))
+ expect_equal(process_reveal_option("chalkboard-pen", c("a", "b")), pandoc_variable_arg("chalkboard-pen", "['a', 'b']"))
+ expect_equal(process_reveal_option("chalkboard-other", "dummy"), pandoc_variable_arg("chalkboard-other", "dummy"))
+})
+