blob: 29628dfa3167477081a0c8127f64c7b8d91c3489 [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 Allaire40fec332016-01-30 16:54:51 -050087 history = TRUE,
JJ Allaire2ec40242014-09-15 09:18:39 -040088 highlight = "default",
89 mathjax = "default",
90 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040091 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040092 includes = NULL,
93 keep_md = FALSE,
94 lib_dir = NULL,
95 pandoc_args = NULL,
96 ...) {
97
98 # function to lookup reveal resource
99 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -0500100 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -0400101 package = "revealjs")
102 }
103
104 # base pandoc options for all reveal.js output
105 args <- c()
106
107 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500108 if (identical(template, "default")) {
109 default_template <- system.file(
110 "rmarkdown/templates/revealjs_presentation/resources/default.html",
111 package = "revealjs"
112 )
113 args <- c(args, "--template", pandoc_path_arg(default_template))
114 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400115 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500116 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400117
118 # incremental
119 if (incremental)
120 args <- c(args, "--incremental")
121
122 # centering
JJ Allaire40fec332016-01-30 16:54:51 -0500123 jsbool <- function(value) ifelse(value, "true", "false")
124 args <- c(args, pandoc_variable_arg("center", jsbool(center)))
JJ Allaire2ec40242014-09-15 09:18:39 -0400125
126 # theme
127 theme <- match.arg(theme, revealjs_themes())
128 if (identical(theme, "default"))
129 theme <- "simple"
130 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500131 theme <- "black"
132 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400133 args <- c(args, "--variable", "theme-dark")
134 args <- c(args, "--variable", paste("theme=", theme, sep=""))
135
136
137 # transition
138 transition <- match.arg(transition, revealjs_transitions())
139 args <- c(args, "--variable", paste("transition=", transition, sep=""))
140
junkkad55cff02015-03-15 22:27:03 +0100141 # background_transition
142 background_transition <- match.arg(background_transition, revealjs_transitions())
JJ Allaire40fec332016-01-30 16:54:51 -0500143 args <- c(args, "--variable", paste("backgroundTransition=", background_transition, sep=""))
144
145 # history
146 args <- c(args, pandoc_variable_arg("history", jsbool(history)))
junkkad55cff02015-03-15 22:27:03 +0100147
JJ Allaire2ec40242014-09-15 09:18:39 -0400148 # content includes
149 args <- c(args, includes_to_pandoc_args(includes))
150
JJ Allairefad55232015-10-19 07:47:26 -0400151 # additional css
152 for (css_file in css)
153 args <- c(args, "--css", pandoc_path_arg(css_file))
154
JJ Allaire2ec40242014-09-15 09:18:39 -0400155 # pre-processor for arguments that may depend on the name of the
156 # the input file (e.g. ones that need to copy supporting files)
157 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
158 output_dir) {
159
160 # use files_dir as lib_dir if not explicitly specified
161 if (is.null(lib_dir))
162 lib_dir <- files_dir
163
164 # extra args
165 args <- c()
166
167 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500168 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400169 if (!self_contained || identical(.Platform$OS.type, "windows"))
170 revealjs_path <- relative_to(
171 output_dir, render_supporting_files(revealjs_path, lib_dir))
172 args <- c(args, "--variable", paste("revealjs-url=",
173 pandoc_path_arg(revealjs_path), sep=""))
174
175 # highlight
176 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
177
178 # return additional args
179 args
180 }
181
182 # return format
183 output_format(
184 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
185 pandoc = pandoc_options(to = "revealjs",
186 from = rmarkdown_format(ifelse(fig_caption,
187 "",
188 "-implicit_figures")),
189 args = args),
190 keep_md = keep_md,
191 clean_supporting = self_contained,
192 pre_processor = pre_processor,
193 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
194 self_contained = self_contained,
195 mathjax = mathjax,
196 pandoc_args = pandoc_args, ...))
197}
198
199revealjs_themes <- function() {
200 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500201 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400202 "simple",
203 "sky",
204 "beige",
205 "serif",
206 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400207 "blood",
208 "moon",
junkka77fbf082015-03-15 22:25:47 +0100209 "night",
210 "black",
211 "league",
212 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400213}
214
215
216revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100217 c(
218 "default",
219 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400220 "fade",
junkka77fbf082015-03-15 22:25:47 +0100221 "slide",
222 "convex",
223 "concave",
224 "zoom"
225 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400226}
227
228