blob: 9a41437993149365646e8c2a3ae221b536424d4b [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
junkkad4b3a162015-03-16 07:49:11 +010010#' @param theme Visual theme ("default", "simple", "sky", "beige", "serif",
11#' "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,
83 theme = "default",
84 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",
89 includes = NULL,
90 keep_md = FALSE,
91 lib_dir = NULL,
92 pandoc_args = NULL,
93 ...) {
94
95 # function to lookup reveal resource
96 reveal_resources <- function() {
97 system.file("rmarkdown/templates/revealjs_presentation",
98 package = "revealjs")
99 }
100
101 # base pandoc options for all reveal.js output
102 args <- c()
103
104 # template path and assets
105 if (identical(template, "default"))
106 args <- c(args, "--template",
107 pandoc_path_arg(file.path(reveal_resources(), "default.html")))
108 else if (!is.null(template))
109 args <- c(args, "--template", pandoc_path_arg(template))
110
111 # incremental
112 if (incremental)
113 args <- c(args, "--incremental")
114
115 # centering
116 if (center)
117 args <- c(args, "--variable", "center")
118
119 # theme
120 theme <- match.arg(theme, revealjs_themes())
121 if (identical(theme, "default"))
122 theme <- "simple"
123 else if (identical(theme, "dark"))
124 theme <- "default"
125 if (theme %in% c("default", "blood", "moon", "night"))
126 args <- c(args, "--variable", "theme-dark")
127 args <- c(args, "--variable", paste("theme=", theme, sep=""))
128
129
130 # transition
131 transition <- match.arg(transition, revealjs_transitions())
132 args <- c(args, "--variable", paste("transition=", transition, sep=""))
133
junkkad55cff02015-03-15 22:27:03 +0100134 # background_transition
135 background_transition <- match.arg(background_transition, revealjs_transitions())
136 args <- c(args, "--variable", paste("background_transition=", background_transition, sep=""))
137
JJ Allaire2ec40242014-09-15 09:18:39 -0400138 # content includes
139 args <- c(args, includes_to_pandoc_args(includes))
140
141 # pre-processor for arguments that may depend on the name of the
142 # the input file (e.g. ones that need to copy supporting files)
143 pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
144 output_dir) {
145
146 # use files_dir as lib_dir if not explicitly specified
147 if (is.null(lib_dir))
148 lib_dir <- files_dir
149
150 # extra args
151 args <- c()
152
153 # reveal.js
154 revealjs_path <- reveal_resources()
155 if (!self_contained || identical(.Platform$OS.type, "windows"))
156 revealjs_path <- relative_to(
157 output_dir, render_supporting_files(revealjs_path, lib_dir))
158 args <- c(args, "--variable", paste("revealjs-url=",
159 pandoc_path_arg(revealjs_path), sep=""))
160
161 # highlight
162 args <- c(args, pandoc_highlight_args(highlight, default = "pygments"))
163
164 # return additional args
165 args
166 }
167
168 # return format
169 output_format(
170 knitr = knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
171 pandoc = pandoc_options(to = "revealjs",
172 from = rmarkdown_format(ifelse(fig_caption,
173 "",
174 "-implicit_figures")),
175 args = args),
176 keep_md = keep_md,
177 clean_supporting = self_contained,
178 pre_processor = pre_processor,
179 base_format = html_document_base(smart = smart, lib_dir = lib_dir,
180 self_contained = self_contained,
181 mathjax = mathjax,
182 pandoc_args = pandoc_args, ...))
183}
184
185revealjs_themes <- function() {
186 c("default",
187 "simple",
188 "sky",
189 "beige",
190 "serif",
191 "solarized",
JJ Allaire2ec40242014-09-15 09:18:39 -0400192 "blood",
193 "moon",
junkka77fbf082015-03-15 22:25:47 +0100194 "night",
195 "black",
196 "league",
197 "white")
JJ Allaire2ec40242014-09-15 09:18:39 -0400198}
199
200
201revealjs_transitions <- function() {
junkka77fbf082015-03-15 22:25:47 +0100202 c(
203 "default",
204 "none",
JJ Allaire2ec40242014-09-15 09:18:39 -0400205 "fade",
junkka77fbf082015-03-15 22:25:47 +0100206 "slide",
207 "convex",
208 "concave",
209 "zoom"
210 )
JJ Allaire2ec40242014-09-15 09:18:39 -0400211}
212
213