blob: 66fd5421195e2f39e4cc41e4101c38865ee8e180 [file] [log] [blame]
JJ Allaire2ec40242014-09-15 09:18:39 -04001#' 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 Allaire6da1bb62016-01-30 14:28:39 -050010#' @param theme Visual theme ("simple", "sky", "beige", "serif",
junkkad4b3a162015-03-16 07:49:11 +010011#' "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 Allaire5665d542016-01-30 17:38:06 -050016#' @param history \code{TRUE} to push each slide change to the browser history.
JJ Allaire2ec40242014-09-15 09:18:39 -040017#' @param template Pandoc template to use for rendering. Pass "default"
18#' to use the rmarkdown package default template; pass \code{NULL}
19#' to use pandoc's built-in template; pass a path to use a custom template
20#' that you've created. Note that if you don't use the "default" template
21#' then some features of \code{revealjs_presentation} won't be available
22#' (see the Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050023#' @param ... Ignored
JJ Allaire2ec40242014-09-15 09:18:39 -040024#'
25#' @return R Markdown output format to pass to \code{\link{render}}
26#'
27#' @details
28#'
29#' In reveal.js presentations you can use level 1 or level 2 headers for
30#' slides. If you use a mix of level 1 and level 2 headers then a
31#' two-dimensional layout will be produced, with level 1 headers building
32#' horizontally and level 2 headers building vertically.
33#'
34#' For more information on markdown syntax for presentations see
35#' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing
36#' slide shows with pandoc}.
37#'
38#' @section Templates:
39#'
40#' You can provide a custom HTML template to be used for rendering. The syntax
41#' for templates is described in the documentation on
42#' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc
43#' templates}. You can also use the basic pandoc template by passing
44#' \code{template = NULL}.
45#'
46#' Note however that if you choose not to use the "default" reveal.js template
47#' then several aspects of reveal.js presentation rendering will behave
48#' differently:
49#'
50#' \itemize{
51#' \item{The \code{center} parameter does not work (you'd need to
52#' set this directly in the template).
53#' }
54#' \item{The built-in template includes some additional tweaks to styles
55#' to optimize for output from R, these won't be present.
56#' }
57#' \item{MathJax will not work if \code{self_contained} is \code{TRUE}
58#' (these two options can't be used together in normal pandoc templates).
59#' }
60#' }
61#'
62#' @examples
63#' \dontrun{
64#'
65#' library(rmarkdown)
66#' library(revealjs)
67#'
68#' # simple invocation
69#' render("pres.Rmd", revealjs_presentation())
70#'
71#' # specify an option for incremental rendering
72#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
73#' }
74#'
75#'
76#' @export
77revealjs_presentation <- function(incremental = FALSE,
78 center = FALSE,
79 fig_width = 8,
80 fig_height = 6,
81 fig_retina = if (!fig_caption) 2,
82 fig_caption = FALSE,
83 smart = TRUE,
84 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050085 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040086 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010087 background_transition = "default",
JJ Allairee6fc8ac2016-01-30 17:12:13 -050088 history = TRUE,
JJ Allaire2ec40242014-09-15 09:18:39 -040089 highlight = "default",
90 mathjax = "default",
91 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040092 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040093 includes = NULL,
94 keep_md = FALSE,
95 lib_dir = NULL,
96 pandoc_args = NULL,
97 ...) {
98
99 # function to lookup reveal resource
100 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -0500101 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -0400102 package = "revealjs")
103 }
104
105 # base pandoc options for all reveal.js output
106 args <- c()
107
108 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500109 if (identical(template, "default")) {
110 default_template <- system.file(
111 "rmarkdown/templates/revealjs_presentation/resources/default.html",
112 package = "revealjs"
113 )
114 args <- c(args, "--template", pandoc_path_arg(default_template))
115 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400116 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500117 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400118
119 # incremental
120 if (incremental)
121 args <- c(args, "--incremental")
122
123 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500124 jsbool <- function(value) ifelse(value, "true", "false")
125 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400126
127 # theme
128 theme <- match.arg(theme, revealjs_themes())
129 if (identical(theme, "default"))
130 theme <- "simple"
131 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500132 theme <- "black"
133 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400134 args <- c(args, "--variable", "theme-dark")
135 args <- c(args, "--variable", paste("theme=", theme, sep=""))
136
137
138 # transition
139 transition <- match.arg(transition, revealjs_transitions())
140 args <- c(args, "--variable", paste("transition=", transition, sep=""))
141
junkkad55cff02015-03-15 22:27:03 +0100142 # background_transition
143 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500144 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
145
JJ Allairee6fc8ac2016-01-30 17:12:13 -0500146 # history
147 args <- c(args, pandoc_variable_arg("history", jsbool(history)))
junkkad55cff02015-03-15 22:27:03 +0100148
JJ Allaire2ec40242014-09-15 09:18:39 -0400149 # content includes
150 args <- c(args, includes_to_pandoc_args(includes))
151
JJ Allairefad55232015-10-19 07:47:26 -0400152 # additional css
153 for (css_file in css)
154 args <- c(args, "--css", pandoc_path_arg(css_file))
155
JJ Allaire2ec40242014-09-15 09:18:39 -0400156 # pre-processor for arguments that may depend on the name of the
157 # the input file (e.g. ones that need to copy supporting files)
158 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
159 output_dir) {
160
161 # use files_dir as lib_dir if not explicitly specified
162 if (is.null(lib_dir))
163 lib_dir <- files_dir
164
165 # extra args
166 args <- c()
167
168 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500169 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400170 if (!self_contained || identical(.Platform$OS.type, "windows"))
171 revealjs_path <- relative_to(
172 output_dir, render_supporting_files(revealjs_path, lib_dir))
173 args <- c(args, "--variable", paste("revealjs-url=",
174 pandoc_path_arg(revealjs_path), sep=""))
175
176 # highlight
177 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
178
179 # return additional args
180 args
181 }
182
183 # return format
184 output_format(
185 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
186 pandoc = pandoc_options(to = "revealjs",
187 from = rmarkdown_format(ifelse(fig_caption,
188 "",
189 "-implicit_figures")),
190 args = args),
191 keep_md = keep_md,
192 clean_supporting = self_contained,
193 pre_processor = pre_processor,
194 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
195 self_contained = self_contained,
196 mathjax = mathjax,
197 pandoc_args = pandoc_args, ...))
198}
199
200revealjs_themes <- function() {
201 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500202 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400203 "simple",
204 "sky",
205 "beige",
206 "serif",
207 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400208 "blood",
209 "moon",
junkka77fbf082015-03-15 22:25:47 +0100210 "night",
211 "black",
212 "league",
213 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400214}
215
216
217revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100218 c(
219 "default",
220 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400221 "fade",
junkka77fbf082015-03-15 22:25:47 +0100222 "slide",
223 "convex",
224 "concave",
225 "zoom"
226 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400227}
228
229