R/stackjs_presentation.R

#' Convert to a stack.js presentation
#'
#' Format for converting from R Markdown to a stack.js presentation.
#'
#' @param fig_width Default width (in inches) for figures
#' @param fig_height Default width (in inches) for figures
#' @param fig_retina Scaling to perform for retina displays (defaults to 2 when
#'   \code{fig_caption} is \code{FALSE}, which currently works for all widely
#'   used retina displays). Set to \code{NULL} to prevent retina scaling. Note
#'   that this will always be \code{NULL} when \code{keep_md} is specified (this
#'   is because \code{fig_retina} relies on outputting HTML directly into the
#'   markdown document).
#' @param fig_caption \code{TRUE} to render figures with captions
#' @param smart Produce typographically correct output, converting straight
#'   quotes to curly quotes, --- to em-dashes, -- to en-dashes, and ... to
#'   ellipses.
#' @param self_contained Produce a standalone HTML file with no external
#'   dependencies, using data: URIs to incorporate the contents of linked
#'   scripts, stylesheets, images, and videos. Note that even for self
#'   contained documents MathJax is still loaded externally (this is
#'   necessary because of it's size).
#' @param theme "light" or "dark" for black text on white background and white text on black background.
#' @param highlight Syntax highlighting style. Supported styles include
#'   "default", "tango", "pygments", "kate", "monochrome", "espresso",
#'   "zenburn", "haddock", and "textmate". Pass \code{NULL} to prevent syntax
#'   highlighting.
#' @param mathjax Include mathjax. The "default" option uses an https URL from
#'   the official MathJax CDN. The "local" option uses a local version of
#'   MathJax (which is copied into the output directory). You can pass an
#'   alternate URL or pass \code{NULL} to exclude MathJax entirely.
#' @param template Pandoc template to use for rendering. Pass "default"
#'   to use the rmarkdown package default template; pass \code{NULL}
#'   to use pandoc's built-in template; pass a path to use a custom template
#'   that you've created. Note that if you don't use the "default" template
#'   then some features of \code{html_document} won't be available (see the
#'   Templates section below for more details).
#' @param css One or more css files to include
#' @param keep_md Keep intermediate markdown generated by knitr
#' @param lib_dir Directory to copy dependent HTML libraries (e.g. jquery,
#'   bootstrap, etc.) into. By default this will be the name of the document
#'   with \code{_files} appended to it.
#' @param pandoc_args Additional command line options to pass to pandoc
#' @param ... Additional function arguments to pass to the base R Markdown HTML
#'   output formatter
#'
#' @return R Markdown output format to pass to \code{\link{render}}
#'
#' @details
#'
#' In stack.js presentations you can use headers for
#' slides. 
#'
#' For more information on markdown syntax for presentations see
#' \href{http://johnmacfarlane.net/pandoc/demo/example9/producing-slide-shows-with-pandoc.html}{producing
#' slide shows with pandoc}.
#'
#'
#' @export
stackjs_presentation <- function(
                                  fig_width = 8,
                                  fig_height = 6,
                                  fig_retina = if (!fig_caption) 2,
                                  fig_caption = FALSE,
                                  smart = TRUE,
                                  self_contained = TRUE,
                                  theme = "light",
                                  highlight = "default",
                                  mathjax = "default",
                                  template = "default",
                                  keep_md = FALSE,
                                  lib_dir = NULL,
                                  pandoc_args = NULL,
                                  ...) {
  
  # base pandoc options for all reveal.js output
  args <- c()
  
  # template path and assets
  if (identical(template, "default"))
    args <- c(args, "--template",
              system.file("default.html", package = "stackrdown"))
  else if (!is.null(template))
    args <- c(args, "--template", template)
   
  # pre-processor for arguments that may depend on the name of the
  # the input file (e.g. ones that need to copy supporting files)
  pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
                            output_dir) {
    
    # use files_dir as lib_dir if not explicitly specified
    if (is.null(lib_dir))
      lib_dir <- files_dir
    
    # extra args
    args <- c()
    
    if(theme == "light"){
      
      args <- c(args, "--variable", "back=#fff", "--variable", "front=#000")
      
    } else {
      
      args <- c(args, "--variable", "back=#000", "--variable", "front=#fff")
      
    }
    
    # reveal.js
    stack_path <- gsub("default.html", "", system.file("default.html", package = "stackrdown"), fixed = TRUE)
    args <- c(args, "--section-divs", "--variable", paste("stack-url=",
                                        rmarkdown:::pandoc_path_arg(stack_path), sep=""))
    
    # highlight
    args <- c(args, rmarkdown:::pandoc_highlight_args(highlight, default = "pygments"))
    
    # return additional args
    args
  }
  
  # return format
  rmarkdown:::output_format(
    knitr = rmarkdown:::knitr_options_html(fig_width, fig_height, fig_retina, keep_md),
    pandoc = rmarkdown:::pandoc_options(to = "revealjs",
                            from = rmarkdown:::from_rmarkdown(fig_caption),
                            args = args),
    keep_md = keep_md,
    clean_supporting = self_contained,
    pre_processor = pre_processor,
    base_format = rmarkdown:::html_document_base(smart = smart, lib_dir = lib_dir,
                                     self_contained = self_contained,
                                     mathjax = mathjax,
                                     pandoc_args = pandoc_args, ...))
}
sachsmc/stackrdown documentation built on May 29, 2019, 12:56 p.m.