blob: d8ea2e67099e98b96dcb7a3efdab443ca1686350 [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 Allaire2ec40242014-09-15 09:18:39 -040016#' @param template Pandoc template to use for rendering. Pass "default"
17#' to use the rmarkdown package default template; pass \code{NULL}
18#' to use pandoc's built-in template; pass a path to use a custom template
19#' that you've created. Note that if you don't use the "default" template
20#' then some features of \code{revealjs_presentation} won't be available
21#' (see the Templates section below for more details).
JJ Allaire8d1c2f42016-01-30 14:56:45 -050022#' @param ... Ignored
JJ Allaire2ec40242014-09-15 09:18:39 -040023#'
24#' @return R Markdown output format to pass to \code{\link{render}}
25#'
26#' @details
27#'
28#' In reveal.js presentations you can use level 1 or level 2 headers for
29#' slides. If you use a mix of level 1 and level 2 headers then a
30#' two-dimensional layout will be produced, with level 1 headers building
31#' horizontally and level 2 headers building vertically.
32#'
33#' For more information on markdown syntax for presentations see
34#' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing
35#' slide shows with pandoc}.
36#'
37#' @section Templates:
38#'
39#' You can provide a custom HTML template to be used for rendering. The syntax
40#' for templates is described in the documentation on
41#' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc
42#' templates}. You can also use the basic pandoc template by passing
43#' \code{template = NULL}.
44#'
45#' Note however that if you choose not to use the "default" reveal.js template
46#' then several aspects of reveal.js presentation rendering will behave
47#' differently:
48#'
49#' \itemize{
50#' \item{The \code{center} parameter does not work (you'd need to
51#' set this directly in the template).
52#' }
53#' \item{The built-in template includes some additional tweaks to styles
54#' to optimize for output from R, these won't be present.
55#' }
56#' \item{MathJax will not work if \code{self_contained} is \code{TRUE}
57#' (these two options can't be used together in normal pandoc templates).
58#' }
59#' }
60#'
61#' @examples
62#' \dontrun{
63#'
64#' library(rmarkdown)
65#' library(revealjs)
66#'
67#' # simple invocation
68#' render("pres.Rmd", revealjs_presentation())
69#'
70#' # specify an option for incremental rendering
71#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
72#' }
73#'
74#'
75#' @export
76revealjs_presentation <- function(incremental = FALSE,
77 center = FALSE,
78 fig_width = 8,
79 fig_height = 6,
80 fig_retina = if (!fig_caption) 2,
81 fig_caption = FALSE,
82 smart = TRUE,
83 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050084 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040085 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010086 background_transition = "default",
JJ Allaire2ec40242014-09-15 09:18:39 -040087 highlight = "default",
88 mathjax = "default",
89 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040090 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040091 includes = NULL,
92 keep_md = FALSE,
93 lib_dir = NULL,
94 pandoc_args = NULL,
95 ...) {
96
97 # function to lookup reveal resource
98 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -050099 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -0400100 package = "revealjs")
101 }
102
103 # base pandoc options for all reveal.js output
104 args <- c()
105
106 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500107 if (identical(template, "default")) {
108 default_template <- system.file(
109 "rmarkdown/templates/revealjs_presentation/resources/default.html",
110 package = "revealjs"
111 )
112 args <- c(args, "--template", pandoc_path_arg(default_template))
113 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400114 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500115 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400116
117 # incremental
118 if (incremental)
119 args <- c(args, "--incremental")
120
121 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500122 jsbool <- function(value) ifelse(value, "true", "false")
123 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400124
125 # theme
126 theme <- match.arg(theme, revealjs_themes())
127 if (identical(theme, "default"))
128 theme <- "simple"
129 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500130 theme <- "black"
131 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400132 args <- c(args, "--variable", "theme-dark")
133 args <- c(args, "--variable", paste("theme=", theme, sep=""))
134
135
136 # transition
137 transition <- match.arg(transition, revealjs_transitions())
138 args <- c(args, "--variable", paste("transition=", transition, sep=""))
139
junkkad55cff02015-03-15 22:27:03 +0100140 # background_transition
141 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500142 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
143
junkkad55cff02015-03-15 22:27:03 +0100144
JJ Allaire2ec40242014-09-15 09:18:39 -0400145 # content includes
146 args <- c(args, includes_to_pandoc_args(includes))
147
JJ Allairefad55232015-10-19 07:47:26 -0400148 # additional css
149 for (css_file in css)
150 args <- c(args, "--css", pandoc_path_arg(css_file))
151
JJ Allaire2ec40242014-09-15 09:18:39 -0400152 # pre-processor for arguments that may depend on the name of the
153 # the input file (e.g. ones that need to copy supporting files)
154 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
155 output_dir) {
156
157 # use files_dir as lib_dir if not explicitly specified
158 if (is.null(lib_dir))
159 lib_dir <- files_dir
160
161 # extra args
162 args <- c()
163
164 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500165 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400166 if (!self_contained || identical(.Platform$OS.type, "windows"))
167 revealjs_path <- relative_to(
168 output_dir, render_supporting_files(revealjs_path, lib_dir))
169 args <- c(args, "--variable", paste("revealjs-url=",
170 pandoc_path_arg(revealjs_path), sep=""))
171
172 # highlight
173 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
174
175 # return additional args
176 args
177 }
178
179 # return format
180 output_format(
181 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
182 pandoc = pandoc_options(to = "revealjs",
183 from = rmarkdown_format(ifelse(fig_caption,
184 "",
185 "-implicit_figures")),
186 args = args),
187 keep_md = keep_md,
188 clean_supporting = self_contained,
189 pre_processor = pre_processor,
190 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
191 self_contained = self_contained,
192 mathjax = mathjax,
193 pandoc_args = pandoc_args, ...))
194}
195
196revealjs_themes <- function() {
197 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500198 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400199 "simple",
200 "sky",
201 "beige",
202 "serif",
203 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400204 "blood",
205 "moon",
junkka77fbf082015-03-15 22:25:47 +0100206 "night",
207 "black",
208 "league",
209 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400210}
211
212
213revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100214 c(
215 "default",
216 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400217 "fade",
junkka77fbf082015-03-15 22:25:47 +0100218 "slide",
219 "convex",
220 "concave",
221 "zoom"
222 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400223}
224
225