| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 1 | #' Convert to a reveal.js presentation | 
 | 2 | #' | 
 | 3 | #' Format for converting from R Markdown to a reveal.js presentation. | 
 | 4 | #' | 
 | 5 | #' @inheritParams rmarkdown::beamer_presentation | 
 | 6 | #' @inheritParams rmarkdown::pdf_document | 
 | 7 | #' @inheritParams rmarkdown::html_document | 
 | 8 | #' | 
 | 9 | #' @param center \code{TRUE} to vertically center content on slides | 
| JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 10 | #' @param theme Visual theme ("simple", "sky", "beige", "serif",  | 
| junkka | d4b3a16 | 2015-03-16 07:49:11 +0100 | [diff] [blame] | 11 | #'   "solarized", "blood", "moon", "night", "black", "league" or "white"). | 
 | 12 | #' @param transition Slide transition ("default", "none", "fade", "slide",  | 
 | 13 | #'   "convex", "concave" or "zoom") | 
 | 14 | #' @param background_transition Slide background-transition ("default", "none", "fade", "slide",  | 
 | 15 | #'   "convex", "concave" or "zoom") | 
| JJ Allaire | 5665d54 | 2016-01-30 17:38:06 -0500 | [diff] [blame] | 16 | #' @param history \code{TRUE} to push each slide change to the browser history. | 
| JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame^] | 17 | #' @param reveal_options Additional options to specify for reveal.js (see | 
 | 18 | #'   \href{https://github.com/hakimel/reveal.js#configuration}{https://github.com/hakimel/reveal.js#configuration} for details).  | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 19 | #' @param template Pandoc template to use for rendering. Pass "default" | 
 | 20 | #'   to use the rmarkdown package default template; pass \code{NULL} | 
 | 21 | #'   to use pandoc's built-in template; pass a path to use a custom template | 
 | 22 | #'   that you've created. Note that if you don't use the "default" template | 
 | 23 | #'   then some features of \code{revealjs_presentation} won't be available | 
 | 24 | #'   (see the Templates section below for more details). | 
| JJ Allaire | 8d1c2f4 | 2016-01-30 14:56:45 -0500 | [diff] [blame] | 25 | #' @param ... Ignored | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 26 | #' | 
 | 27 | #' @return R Markdown output format to pass to \code{\link{render}} | 
 | 28 | #' | 
 | 29 | #' @details | 
 | 30 | #' | 
 | 31 | #' In reveal.js presentations you can use level 1 or level 2 headers for | 
 | 32 | #' slides. If you use a mix of level 1 and level 2 headers then a | 
 | 33 | #' two-dimensional layout will be produced, with level 1 headers building | 
 | 34 | #' horizontally and level 2 headers building vertically. | 
 | 35 | #' | 
 | 36 | #' For more information on markdown syntax for presentations see | 
 | 37 | #' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing | 
 | 38 | #' slide shows with pandoc}. | 
 | 39 | #' | 
 | 40 | #' @section Templates: | 
 | 41 | #' | 
 | 42 | #' You can provide a custom HTML template to be used for rendering. The syntax | 
 | 43 | #' for templates is described in the documentation on | 
 | 44 | #' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc | 
 | 45 | #' templates}. You can also use the basic pandoc template by passing | 
 | 46 | #' \code{template = NULL}. | 
 | 47 | #' | 
 | 48 | #' Note however that if you choose not to use the "default" reveal.js template | 
 | 49 | #' then several aspects of reveal.js presentation rendering will behave | 
 | 50 | #' differently: | 
 | 51 | #' | 
 | 52 | #' \itemize{ | 
 | 53 | #'   \item{The \code{center} parameter does not work (you'd need to | 
 | 54 | #'      set this directly in the template). | 
 | 55 | #'   } | 
 | 56 | #'   \item{The built-in template includes some additional tweaks to styles | 
 | 57 | #'      to optimize for output from R, these won't be present. | 
 | 58 | #'   } | 
 | 59 | #'   \item{MathJax will not work if \code{self_contained} is \code{TRUE} | 
 | 60 | #'      (these two options can't be used together in normal pandoc templates). | 
 | 61 | #'   } | 
 | 62 | #' } | 
 | 63 | #' | 
 | 64 | #' @examples | 
 | 65 | #' \dontrun{ | 
 | 66 | #' | 
 | 67 | #' library(rmarkdown) | 
 | 68 | #' library(revealjs) | 
 | 69 | #' | 
 | 70 | #' # simple invocation | 
 | 71 | #' render("pres.Rmd", revealjs_presentation()) | 
 | 72 | #' | 
 | 73 | #' # specify an option for incremental rendering | 
 | 74 | #' render("pres.Rmd", revealjs_presentation(incremental = TRUE)) | 
 | 75 | #' } | 
 | 76 | #' | 
 | 77 | #' | 
 | 78 | #' @export | 
 | 79 | revealjs_presentation <- function(incremental = FALSE, | 
 | 80 |                                   center = FALSE, | 
 | 81 |                                   fig_width = 8, | 
 | 82 |                                   fig_height = 6, | 
 | 83 |                                   fig_retina = if (!fig_caption) 2, | 
 | 84 |                                   fig_caption = FALSE, | 
 | 85 |                                   smart = TRUE, | 
 | 86 |                                   self_contained = TRUE, | 
| JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 87 |                                   theme = "simple", | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 88 |                                   transition = "default", | 
| junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 89 |                                   background_transition = "default", | 
| JJ Allaire | e6fc8ac | 2016-01-30 17:12:13 -0500 | [diff] [blame] | 90 |                                   history = TRUE, | 
| JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame^] | 91 |                                   reveal_options = NULL, | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 92 |                                   highlight = "default", | 
 | 93 |                                   mathjax = "default", | 
 | 94 |                                   template = "default", | 
| JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 95 |                                   css = NULL, | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 96 |                                   includes = NULL, | 
 | 97 |                                   keep_md = FALSE, | 
 | 98 |                                   lib_dir = NULL, | 
 | 99 |                                   pandoc_args = NULL, | 
 | 100 |                                   ...) { | 
 | 101 |    | 
 | 102 |   # function to lookup reveal resource | 
 | 103 |   reveal_resources <- function() { | 
| JJ Allaire | 2d8f3f2 | 2016-01-30 13:08:52 -0500 | [diff] [blame] | 104 |     system.file("rmarkdown/templates/revealjs_presentation/resources", | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 105 |                 package = "revealjs") | 
 | 106 |   } | 
 | 107 |    | 
 | 108 |   # base pandoc options for all reveal.js output | 
 | 109 |   args <- c() | 
 | 110 |    | 
 | 111 |   # template path and assets | 
| JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 112 |   if (identical(template, "default")) { | 
 | 113 |     default_template <- system.file( | 
 | 114 |       "rmarkdown/templates/revealjs_presentation/resources/default.html", | 
 | 115 |       package = "revealjs" | 
 | 116 |     ) | 
 | 117 |     args <- c(args, "--template", pandoc_path_arg(default_template)) | 
 | 118 |   } else if (!is.null(template)) { | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 119 |     args <- c(args, "--template", pandoc_path_arg(template)) | 
| JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 120 |   } | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 121 |    | 
 | 122 |   # incremental | 
 | 123 |   if (incremental) | 
 | 124 |     args <- c(args, "--incremental") | 
 | 125 |    | 
 | 126 |   # centering | 
| JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 127 |   jsbool <- function(value) ifelse(value, "true", "false") | 
 | 128 |   args <- c(args, pandoc_variable_arg("center", jsbool(center))) | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 129 |    | 
 | 130 |   # theme | 
 | 131 |   theme <- match.arg(theme, revealjs_themes()) | 
 | 132 |   if (identical(theme, "default")) | 
 | 133 |     theme <- "simple" | 
 | 134 |   else if (identical(theme, "dark")) | 
| JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 135 |     theme <- "black" | 
 | 136 |   if (theme %in% c("blood", "moon", "night", "black")) | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 137 |     args <- c(args, "--variable", "theme-dark") | 
 | 138 |   args <- c(args, "--variable", paste("theme=", theme, sep="")) | 
 | 139 |    | 
 | 140 |    | 
 | 141 |   # transition | 
 | 142 |   transition <- match.arg(transition, revealjs_transitions()) | 
 | 143 |   args <- c(args, "--variable", paste("transition=", transition, sep="")) | 
 | 144 |    | 
| junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 145 |   # background_transition | 
 | 146 |   background_transition <- match.arg(background_transition, revealjs_transitions()) | 
| JJ Allaire | 40fec33 | 2016-01-30 16:54:51 -0500 | [diff] [blame] | 147 |   args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep="")) | 
 | 148 |    | 
| JJ Allaire | e6fc8ac | 2016-01-30 17:12:13 -0500 | [diff] [blame] | 149 |   # history | 
 | 150 |   args <- c(args, pandoc_variable_arg("history", jsbool(history))) | 
| junkka | d55cff0 | 2015-03-15 22:27:03 +0100 | [diff] [blame] | 151 |    | 
| JJ Allaire | 37f45b7 | 2016-01-30 18:17:45 -0500 | [diff] [blame^] | 152 |   # additional reveal options | 
 | 153 |   if (is.list(reveal_options)) { | 
 | 154 |     for (option in names(reveal_options)) { | 
 | 155 |       value <- reveal_options[[option]] | 
 | 156 |       if (is.logical(value)) | 
 | 157 |         value <- jsbool(value) | 
 | 158 |       args <- c(args, pandoc_variable_arg(option, value)) | 
 | 159 |     } | 
 | 160 |   } | 
 | 161 |    | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 162 |   # content includes | 
 | 163 |   args <- c(args, includes_to_pandoc_args(includes)) | 
 | 164 |    | 
| JJ Allaire | fad5523 | 2015-10-19 07:47:26 -0400 | [diff] [blame] | 165 |   # additional css | 
 | 166 |   for (css_file in css) | 
 | 167 |     args <- c(args, "--css", pandoc_path_arg(css_file)) | 
 | 168 |    | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 169 |   # pre-processor for arguments that may depend on the name of the | 
 | 170 |   # the input file (e.g. ones that need to copy supporting files) | 
 | 171 |   pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, | 
 | 172 |                             output_dir) { | 
 | 173 |      | 
 | 174 |     # use files_dir as lib_dir if not explicitly specified | 
 | 175 |     if (is.null(lib_dir)) | 
 | 176 |       lib_dir <- files_dir | 
 | 177 |      | 
 | 178 |     # extra args | 
 | 179 |     args <- c() | 
 | 180 |      | 
 | 181 |     # reveal.js | 
| JJ Allaire | a8c414b | 2016-01-30 14:36:53 -0500 | [diff] [blame] | 182 |     revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs") | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 183 |     if (!self_contained || identical(.Platform$OS.type, "windows")) | 
 | 184 |       revealjs_path <- relative_to( | 
 | 185 |         output_dir, render_supporting_files(revealjs_path, lib_dir)) | 
 | 186 |     args <- c(args, "--variable", paste("revealjs-url=", | 
 | 187 |                                         pandoc_path_arg(revealjs_path), sep="")) | 
 | 188 |      | 
 | 189 |     # highlight | 
 | 190 |     args <- c(args, pandoc_highlight_args(highlight, default = "pygments")) | 
 | 191 |      | 
 | 192 |     # return additional args | 
 | 193 |     args | 
 | 194 |   } | 
 | 195 |    | 
 | 196 |   # return format | 
 | 197 |   output_format( | 
 | 198 |     knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md), | 
 | 199 |     pandoc = pandoc_options(to = "revealjs", | 
 | 200 |                             from = rmarkdown_format(ifelse(fig_caption,  | 
 | 201 |                                                            "",  | 
 | 202 |                                                            "-implicit_figures")), | 
 | 203 |                             args = args), | 
 | 204 |     keep_md = keep_md, | 
 | 205 |     clean_supporting = self_contained, | 
 | 206 |     pre_processor = pre_processor, | 
 | 207 |     base_format = html_document_base(smart = smart, lib_dir = lib_dir, | 
 | 208 |                                      self_contained = self_contained, | 
 | 209 |                                      mathjax = mathjax, | 
 | 210 |                                      pandoc_args = pandoc_args, ...)) | 
 | 211 | } | 
 | 212 |  | 
 | 213 | revealjs_themes <- function() { | 
 | 214 |   c("default", | 
| JJ Allaire | 6da1bb6 | 2016-01-30 14:28:39 -0500 | [diff] [blame] | 215 |     "dark", | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 216 |     "simple", | 
 | 217 |     "sky", | 
 | 218 |     "beige", | 
 | 219 |     "serif", | 
 | 220 |     "solarized", | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 221 |     "blood", | 
 | 222 |     "moon", | 
| junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 223 |     "night", | 
 | 224 |     "black", | 
 | 225 |     "league", | 
 | 226 |     "white") | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 227 | } | 
 | 228 |  | 
 | 229 |  | 
 | 230 | revealjs_transitions <- function() { | 
| junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 231 |   c( | 
 | 232 |     "default", | 
 | 233 |     "none", | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 234 |     "fade", | 
| junkka | 77fbf08 | 2015-03-15 22:25:47 +0100 | [diff] [blame] | 235 |     "slide", | 
 | 236 |     "convex", | 
 | 237 |     "concave", | 
 | 238 |     "zoom" | 
 | 239 |     ) | 
| JJ Allaire | 2ec4024 | 2014-09-15 09:18:39 -0400 | [diff] [blame] | 240 | } | 
 | 241 |  | 
 | 242 |  |