blob: d1f5edb9816b0963b3b2305361034d356ba18f78 [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
122 if (center)
123 args <- c(args, "--variable", "center")
124
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())
142 args <- c(args, "--variable", paste("background_transition=", background_transition, sep=""))
143
JJ Allaire2ec40242014-09-15 09:18:39 -0400144 # content includes
145 args <- c(args, includes_to_pandoc_args(includes))
146
JJ Allairefad55232015-10-19 07:47:26 -0400147 # additional css
148 for (css_file in css)
149 args <- c(args, "--css", pandoc_path_arg(css_file))
150
JJ Allaire2ec40242014-09-15 09:18:39 -0400151 # pre-processor for arguments that may depend on the name of the
152 # the input file (e.g. ones that need to copy supporting files)
153 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
154 output_dir) {
155
156 # use files_dir as lib_dir if not explicitly specified
157 if (is.null(lib_dir))
158 lib_dir <- files_dir
159
160 # extra args
161 args <- c()
162
163 # reveal.js
JJ Allairea8c414b2016-01-30 14:36:53 -0500164 revealjs_path <- system.file("reveal.js-3.2.0", package = "revealjs")
JJ Allaire2ec40242014-09-15 09:18:39 -0400165 if (!self_contained || identical(.Platform$OS.type, "windows"))
166 revealjs_path <- relative_to(
167 output_dir, render_supporting_files(revealjs_path, lib_dir))
168 args <- c(args, "--variable", paste("revealjs-url=",
169 pandoc_path_arg(revealjs_path), sep=""))
170
171 # highlight
172 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
173
174 # return additional args
175 args
176 }
177
178 # return format
179 output_format(
180 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
181 pandoc = pandoc_options(to = "revealjs",
182 from = rmarkdown_format(ifelse(fig_caption,
183 "",
184 "-implicit_figures")),
185 args = args),
186 keep_md = keep_md,
187 clean_supporting = self_contained,
188 pre_processor = pre_processor,
189 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
190 self_contained = self_contained,
191 mathjax = mathjax,
192 pandoc_args = pandoc_args, ...))
193}
194
195revealjs_themes <- function() {
196 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500197 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400198 "simple",
199 "sky",
200 "beige",
201 "serif",
202 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400203 "blood",
204 "moon",
junkka77fbf082015-03-15 22:25:47 +0100205 "night",
206 "black",
207 "league",
208 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400209}
210
211
212revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100213 c(
214 "default",
215 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400216 "fade",
junkka77fbf082015-03-15 22:25:47 +0100217 "slide",
218 "convex",
219 "concave",
220 "zoom"
221 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400222}
223
224