From 4573ae4a12f4b927ec6a23930a292ac8dd926447 Mon Sep 17 00:00:00 2001 From: Romain Lesur Date: Wed, 27 Nov 2019 23:48:21 +0100 Subject: [PATCH] add support for PDF generation of reveal.js presentations in chrome_print() --- DESCRIPTION | 4 ++-- NEWS.md | 2 ++ inst/resources/js/chrome_print.js | 30 ++++++++++++++++++++++++++--- tests/test-travis/test-chrome.R | 11 +++++++++++ tests/test-travis/test-revealjs.Rmd | 29 ++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 tests/test-travis/test-revealjs.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 9888f0a6..45aff20b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: pagedown Type: Package Title: Paginate the HTML Output of R Markdown with CSS for Print -Version: 0.6.1 +Version: 0.6.2 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Romain", "Lesur", role = c("aut", "cph"), comment = c(ORCID = "0000-0002-0721-5595")), @@ -21,7 +21,7 @@ Description: Use the paged media properties in CSS and the JavaScript reports, papers, business cards, resumes, and posters. Imports: rmarkdown (>= 1.16), bookdown (>= 0.8), htmltools, jsonlite, later (>= 1.0.0), processx, servr (>= 0.13), httpuv, xfun, websocket -Suggests: promises, testit, xaringan, pdftools +Suggests: promises, testit, xaringan, pdftools, revealjs License: MIT + file LICENSE URL: https://github.com/rstudio/pagedown BugReports: https://github.com/rstudio/pagedown/issues diff --git a/NEWS.md b/NEWS.md index 4ae18bbb..05a86e1f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ - Added support for pagebreaks: in output formats which use **paged.js**, a pagebreak can be forced using the LaTeX commands `\newpage` and `\pagebreak` or using the CSS classes `page-break-before` and `page-break-after`. +- **reveal.js** presentations can be printed to PDF using `chrome_print()`. + # CHANGES IN pagedown VERSION 0.6 ## MINOR CHANGES diff --git a/inst/resources/js/chrome_print.js b/inst/resources/js/chrome_print.js index e3329afa..581687b8 100644 --- a/inst/resources/js/chrome_print.js +++ b/inst/resources/js/chrome_print.js @@ -1,4 +1,23 @@ -(() => { +{ + let RevealReady = new Promise((resolve) => { + window.addEventListener( + 'DOMContentLoaded', + () => { + if (window.Reveal) { + if (!window.location.search.match( /print-pdf/gi )) { + window.location.search = 'print-pdf'; + return; + } + Reveal.addEventListener('ready', resolve); + if (Reveal.isReady()) resolve(); + } else { + resolve(); + } + }, + {capture: true, once: true} + ); + }); + let HTMLWidgetsReady = new Promise((resolve) => { window.addEventListener( 'DOMContentLoaded', @@ -40,5 +59,10 @@ ); }); - window.pagedownReady = Promise.all([MathJaxReady, HTMLWidgetsReady, document.fonts.ready]); -})(); + window.pagedownReady = Promise.all([ + RevealReady, + MathJaxReady, + HTMLWidgetsReady, + document.fonts.ready + ]); +} diff --git a/tests/test-travis/test-chrome.R b/tests/test-travis/test-chrome.R index 18b8dfcb..a6c90905 100644 --- a/tests/test-travis/test-chrome.R +++ b/tests/test-travis/test-chrome.R @@ -21,3 +21,14 @@ assert('chrome_print() works with a local file path', { assert('chrome_print() works with html_paged format', { (is_pdf(print_pdf('test-chrome.Rmd'))) }) + +assert('chrome_print() works with reveal.js presentations', { + f = print_pdf('test-revealjs.Rmd') + + (is_pdf(f)) + + (identical(pdftools::pdf_info(f)$pages, 5L)) + + first_page_text_content = pdftools::pdf_text('reveal.pdf')[1] + (identical(first_page_text_content, 'Test reveal.js')) +}) diff --git a/tests/test-travis/test-revealjs.Rmd b/tests/test-travis/test-revealjs.Rmd new file mode 100644 index 00000000..f78d4133 --- /dev/null +++ b/tests/test-travis/test-revealjs.Rmd @@ -0,0 +1,29 @@ +--- +title: "Test reveal.js" +output: revealjs::revealjs_presentation +--- + +## R Markdown + +This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . + +When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. + +## Slide with Bullets + +- Bullet 1 +- Bullet 2 +- Bullet 3 + +## Slide with R Code and Output + +```{r} +summary(cars) +``` + +## Slide with Plot + +```{r, echo=FALSE} +plot(cars) +``` +