Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function 'decktape()' #177

Merged
merged 28 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xaringan
Type: Package
Title: Presentation Ninja
Version: 0.8.1
Version: 0.8.2
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Claus Thorn", "Ekstrøm", role = "ctb"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(decktape)
export(inf_mr)
export(infinite_moon_reader)
export(moon_reader)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGES IN xaringan VERSION 0.9

## NEW FEATURES

- Added a function `decktape()` to export slides to PDF via the DeckTape library (thanks, @pat-s, #177).

# CHANGES IN xaringan VERSION 0.8

Expand Down
45 changes: 43 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ moon_reader = function(

if (is.null(title_cls <- nature[['titleSlideClass']]))
title_cls = c('center', 'middle', 'inverse')
title_cls = paste(c(title_cls, 'title-slide'), collapse = ", ")
title_cls = paste(c(title_cls, 'title-slide'), collapse = ', ')

before = nature[['beforeInit']]
for (i in c('countdown', 'autoplay', 'beforeInit', "titleSlideClass")) nature[[i]] = NULL
for (i in c('countdown', 'autoplay', 'beforeInit', 'titleSlideClass')) nature[[i]] = NULL

write_utf8(as.character(tagList(
tags$script(src = chakra),
Expand Down Expand Up @@ -264,3 +264,44 @@ infinite_moon_reader = function(moon, cast_from = '.') {
#' @export
#' @rdname inf_mr
inf_mr = infinite_moon_reader


#' Convert HTML presentations to PDF via DeckTape
#'
#' This function can use either the \command{decktape} command or the hosted
#' docker image of the \pkg{decktape} library to convert HTML slides to PDF
#' (including slides produced by \pkg{xaringan}).
#' @param file The path to the HTML presentation file. When \code{docker =
#' FALSE}, this path could be a URL to online slides.
#' @param output The desired output path of the PDF file.
#' @param docker Whether to use Docker (\code{TRUE}) or use the
#' \command{decktape} command directly (\code{FALSE}). By default, if
#' \pkg{decktape} has been installed in your system and can be found via
#' \code{Sys.which('decktape')}, it will be uesd directly.
#' @param version The \pkg{decktape} version when you use Docker.
#' @param open Whether to open the resulting PDF with your system PDF viewer.
#' @note For some operating systems you may need to
#' \href{https://stackoverflow.com/questions/48957195}{add yourself to the
#' \command{docker} group} and restart your machine if you use DeckTape via
#' Docker. By default, the latest version of the \pkg{decktape} Docker image
#' is used. In case of errors, you may want to try older versions (e.g.,
#' \code{version = '2.8.0'}).
#' @references DeckTape: \url{https://github.com/astefanutti/decktape}. Docker:
#' \url{https://www.docker.com}.
#' @return The output file path (invisibly).
#' @export
#' @examples if (interactive()) {
#' xaringan::decktape('https://slides.yihui.name/xaringan', 'xaringan.pdf', docker = FALSE)
#' }
decktape = function(
file, output, docker = Sys.which('decktape') == '', version = '', open = FALSE
) {
args = shQuote(c(file, output))
res = if (docker) system2('docker', c(
'run', '--rm', '-t', '-v', '`pwd`:/slides', '-v', '$HOME:$HOME',
paste0('astefanutti/decktape', if (version != '') ':', version), args
)) else system2('decktape', args)
if (res != 0) stop('Failed to convert ', file, ' to PDF')
if (open) open_file(output)
invisible(output)
}
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,11 @@ file_content = function(file) {
}

pkg_file = function(file) file_content(pkg_resource(file))

open_file = function(path){
if (xfun::is_windows()) {
shell.exec(path)
} else {
system2(if (xfun::is_macos()) 'open' else 'xdg-open', shQuote(path))
}
}
49 changes: 49 additions & 0 deletions man/decktape.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.