blob: 027597c59391955e4fd925a8483126d5dc800954 [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
106 if (identical(template, "default"))
107 args <- c(args, "--template",
108 pandoc_path_arg(file.path(reveal_resources(), "default.html")))
109 else if (!is.null(template))
110 args <- c(args, "--template", pandoc_path_arg(template))
111
112 # incremental
113 if (incremental)
114 args <- c(args, "--incremental")
115
116 # centering
117 if (center)
118 args <- c(args, "--variable", "center")
119
120 # theme
121 theme <- match.arg(theme, revealjs_themes())
122 if (identical(theme, "default"))
123 theme <- "simple"
124 else if (identical(theme, "dark"))
JJ Allaire6da1bb62016-01-30 14:28:39 -0500125 theme <- "black"
126 if (theme %in% c("blood", "moon", "night", "black"))
JJ Allaire2ec40242014-09-15 09:18:39 -0400127 args <- c(args, "--variable", "theme-dark")
128 args <- c(args, "--variable", paste("theme=", theme, sep=""))
129
130
131 # transition
132 transition <- match.arg(transition, revealjs_transitions())
133 args <- c(args, "--variable", paste("transition=", transition, sep=""))
134
junkkad55cff02015-03-15 22:27:03 +0100135 # background_transition
136 background_transition <- match.arg(background_transition, revealjs_transitions())
137 args <- c(args, "--variable", paste("background_transition=", background_transition, sep=""))
138
JJ Allaire2ec40242014-09-15 09:18:39 -0400139 # content includes
140 args <- c(args, includes_to_pandoc_args(includes))
141
JJ Allairefad55232015-10-19 07:47:26 -0400142 # additional css
143 for (css_file in css)
144 args <- c(args, "--css", pandoc_path_arg(css_file))
145
JJ Allaire2ec40242014-09-15 09:18:39 -0400146 # pre-processor for arguments that may depend on the name of the
147 # the input file (e.g. ones that need to copy supporting files)
148 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
149 output_dir) {
150
151 # use files_dir as lib_dir if not explicitly specified
152 if (is.null(lib_dir))
153 lib_dir <- files_dir
154
155 # extra args
156 args <- c()
157
158 # reveal.js
JJ Allaire6da1bb62016-01-30 14:28:39 -0500159 revealjs_path <- file.path(reveal_resources(), "reveal.js-3.2.0")
JJ Allaire2ec40242014-09-15 09:18:39 -0400160 if (!self_contained || identical(.Platform$OS.type, "windows"))
161 revealjs_path <- relative_to(
162 output_dir, render_supporting_files(revealjs_path, lib_dir))
163 args <- c(args, "--variable", paste("revealjs-url=",
164 pandoc_path_arg(revealjs_path), sep=""))
165
166 # highlight
167 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
168
169 # return additional args
170 args
171 }
172
173 # return format
174 output_format(
175 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
176 pandoc = pandoc_options(to = "revealjs",
177 from = rmarkdown_format(ifelse(fig_caption,
178 "",
179 "-implicit_figures")),
180 args = args),
181 keep_md = keep_md,
182 clean_supporting = self_contained,
183 pre_processor = pre_processor,
184 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
185 self_contained = self_contained,
186 mathjax = mathjax,
187 pandoc_args = pandoc_args, ...))
188}
189
190revealjs_themes <- function() {
191 c("default",
JJ Allaire6da1bb62016-01-30 14:28:39 -0500192 "dark",
JJ Allaire2ec40242014-09-15 09:18:39 -0400193 "simple",
194 "sky",
195 "beige",
196 "serif",
197 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400198 "blood",
199 "moon",
junkka77fbf082015-03-15 22:25:47 +0100200 "night",
201 "black",
202 "league",
203 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400204}
205
206
207revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100208 c(
209 "default",
210 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400211 "fade",
junkka77fbf082015-03-15 22:25:47 +0100212 "slide",
213 "convex",
214 "concave",
215 "zoom"
216 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400217}
218
219