blob: df9f34d34cb7001b03c57664d6aeffeacb5233e2 [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).
22#'
23#' @return R Markdown output format to pass to \code{\link{render}}
24#'
25#' @details
26#'
27#' In reveal.js presentations you can use level 1 or level 2 headers for
28#' slides. If you use a mix of level 1 and level 2 headers then a
29#' two-dimensional layout will be produced, with level 1 headers building
30#' horizontally and level 2 headers building vertically.
31#'
32#' For more information on markdown syntax for presentations see
33#' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing
34#' slide shows with pandoc}.
35#'
36#' @section Templates:
37#'
38#' You can provide a custom HTML template to be used for rendering. The syntax
39#' for templates is described in the documentation on
40#' \href{http://johnmacfarlane.net/pandoc/demo/example9/templates.html}{pandoc
41#' templates}. You can also use the basic pandoc template by passing
42#' \code{template = NULL}.
43#'
44#' Note however that if you choose not to use the "default" reveal.js template
45#' then several aspects of reveal.js presentation rendering will behave
46#' differently:
47#'
48#' \itemize{
49#' \item{The \code{center} parameter does not work (you'd need to
50#' set this directly in the template).
51#' }
52#' \item{The built-in template includes some additional tweaks to styles
53#' to optimize for output from R, these won't be present.
54#' }
55#' \item{MathJax will not work if \code{self_contained} is \code{TRUE}
56#' (these two options can't be used together in normal pandoc templates).
57#' }
58#' }
59#'
60#' @examples
61#' \dontrun{
62#'
63#' library(rmarkdown)
64#' library(revealjs)
65#'
66#' # simple invocation
67#' render("pres.Rmd", revealjs_presentation())
68#'
69#' # specify an option for incremental rendering
70#' render("pres.Rmd", revealjs_presentation(incremental = TRUE))
71#' }
72#'
73#'
74#' @export
75revealjs_presentation <- function(incremental = FALSE,
76 center = FALSE,
77 fig_width = 8,
78 fig_height = 6,
79 fig_retina = if (!fig_caption) 2,
80 fig_caption = FALSE,
81 smart = TRUE,
82 self_contained = TRUE,
JJ Allaire6da1bb62016-01-30 14:28:39 -050083 theme = "simple",
JJ Allaire2ec40242014-09-15 09:18:39 -040084 transition = "default",
junkkad55cff02015-03-15 22:27:03 +010085 background_transition = "default",
JJ Allaire2ec40242014-09-15 09:18:39 -040086 highlight = "default",
87 mathjax = "default",
88 template = "default",
JJ Allairefad55232015-10-19 07:47:26 -040089 css = NULL,
JJ Allaire2ec40242014-09-15 09:18:39 -040090 includes = NULL,
91 keep_md = FALSE,
92 lib_dir = NULL,
93 pandoc_args = NULL,
94 ...) {
95
96 # function to lookup reveal resource
97 reveal_resources <- function() {
JJ Allaire2d8f3f22016-01-30 13:08:52 -050098 system.file("rmarkdown/templates/revealjs_presentation/resources",
JJ Allaire2ec40242014-09-15 09:18:39 -040099 package = "revealjs")
100 }
101
102 # base pandoc options for all reveal.js output
103 args <- c()
104
105 # template path and assets
JJ Allairea8c414b2016-01-30 14:36:53 -0500106 if (identical(template, "default")) {
107 default_template <- system.file(
108 "rmarkdown/templates/revealjs_presentation/resources/default.html",
109 package = "revealjs"
110 )
111 args <- c(args, "--template", pandoc_path_arg(default_template))
112 } else if (!is.null(template)) {
JJ Allaire2ec40242014-09-15 09:18:39 -0400113 args <- c(args, "--template", pandoc_path_arg(template))
JJ Allairea8c414b2016-01-30 14:36:53 -0500114 }
JJ Allaire2ec40242014-09-15 09:18:39 -0400115
116 # incremental
117 if (incremental)
118 args <- c(args, "--incremental")
119
120 # centering
121 if (center)
122 args <- c(args, "--variable", "center")
123
124 # theme
125 theme <- match.arg(theme, revealjs_themes())
126 if (identical(theme, "default"))
127 theme <- "simple"
128 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500129 theme <- "black"
130 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400131 args <- c(args, "--variable", "theme-dark")
132 args <- c(args, "--variable", paste("theme=", theme, sep=""))
133
134
135 # transition
136 transition <- match.arg(transition, revealjs_transitions())
137 args <- c(args, "--variable", paste("transition=", transition, sep=""))
138
junkkad55cff02015-03-15 22:27:03 +0100139 # background_transition
140 background_transition <- match.arg(background_transition, revealjs_transitions())
141 args <- c(args, "--variable", paste("background_transition=", background_transition, sep=""))
142
JJ Allaire2ec40242014-09-15 09:18:39 -0400143 # content includes
144 args <- c(args, includes_to_pandoc_args(includes))
145
JJ Allairefad55232015-10-19 07:47:26 -0400146 # additional css
147 for (css_file in css)
148 args <- c(args, "--css", pandoc_path_arg(css_file))
149
JJ Allaire2ec40242014-09-15 09:18:39 -0400150 # pre-processor for arguments that may depend on the name of the
151 # the input file (e.g. ones that need to copy supporting files)
152 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
153 output_dir) {
154
155 # use files_dir as lib_dir if not explicitly specified
156 if (is.null(lib_dir))
157 lib_dir <- files_dir
158
159 # extra args
160 args <- c()
161
162 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500163 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400164 if (!self_contained || identical(.Platform$OS.type, "windows"))
165 revealjs_path <- relative_to(
166 output_dir, render_supporting_files(revealjs_path, lib_dir))
167 args <- c(args, "--variable", paste("revealjs-url=",
168 pandoc_path_arg(revealjs_path), sep=""))
169
170 # highlight
171 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
172
173 # return additional args
174 args
175 }
176
177 # return format
178 output_format(
179 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
180 pandoc = pandoc_options(to = "revealjs",
181 from = rmarkdown_format(ifelse(fig_caption,
182 "",
183 "-implicit_figures")),
184 args = args),
185 keep_md = keep_md,
186 clean_supporting = self_contained,
187 pre_processor = pre_processor,
188 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
189 self_contained = self_contained,
190 mathjax = mathjax,
191 pandoc_args = pandoc_args, ...))
192}
193
194revealjs_themes <- function() {
195 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500196 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400197 "simple",
198 "sky",
199 "beige",
200 "serif",
201 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400202 "blood",
203 "moon",
junkka77fbf082015-03-15 22:25:47 +0100204 "night",
205 "black",
206 "league",
207 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400208}
209
210
211revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100212 c(
213 "default",
214 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400215 "fade",
junkka77fbf082015-03-15 22:25:47 +0100216 "slide",
217 "convex",
218 "concave",
219 "zoom"
220 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400221}
222
223