Skip to content

Commit

Permalink
Refactor as html_document() derived formats
Browse files Browse the repository at this point in the history
  • Loading branch information
juba committed Nov 3, 2014
1 parent fc3136b commit 101411f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 262 deletions.
153 changes: 21 additions & 132 deletions R/html_clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,14 @@
#'
#' Format for converting from R Markdown to an HTML document.
#'
#' @param number_sections \code{TRUE} to number section headings
#' @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 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 css One or more css files to include
#' @param includes Named list of additional content to include within the
#' document (typically created using the \code{\link{includes}} function).
#' @param keep_md Keep the markdown file generated by knitting.
#' @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
#' @param ... Additional function arguments passed to R Markdown \code{\link{html_document}}
#'
#' @return R Markdown output format to pass to \code{\link{render}}
#'
Expand All @@ -54,116 +26,33 @@
#' @import rmarkdown
#' @import htmltools

html_clean <- function(number_sections = FALSE,
fig_width = 6,

html_clean <- function(fig_width = 6,
fig_height = 6,
fig_retina = if (!fig_caption) 2,
fig_caption = TRUE,
smart = TRUE,
self_contained = TRUE,
highlight = "pygments",
mathjax = "default",
css = NULL,
includes = NULL,
keep_md = FALSE,
lib_dir = NULL,
pandoc_args = NULL,
...) {

## build pandoc args
args <- c("--standalone")
## use section divs
args <- c(args, "--section-divs")

## template
args <- c(args, "--template",
rmarkdown::pandoc_path_arg(system.file("templates/html_clean/default.html", package="rmdformats")))
## numbered sections
if (number_sections)
args <- c(args, "--number-sections")
## additional css
for (css_file in css)
args <- c(args, "--css", rmarkdown::pandoc_path_arg(css_file))

# 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()

# highlight
args <- c(args, rmarkdown:::pandoc_html_highlight_args(highlight,
"default",
self_contained,
lib_dir,
output_dir))

# content includes (we do this here so that user include-in-header content
# goes after dependency generated content)
args <- c(args, rmarkdown:::includes_to_pandoc_args(includes))

# return additional args
args
}

## Added js and css dependencies
extra_dependencies <- list(rmarkdown:::html_dependency_jquery(),
rmarkdown:::html_dependency_bootstrap("bootstrap"),
html_dependency_jquery_ui(),
html_dependency_tocify(),
html_dependency_magnific_popup(),
html_dependency_clean())
## knitr options
knitr_opts <- list(dev = 'png',
dpi = 96,
fig.width = fig_width,
fig.height = fig_height,
fig.retina = fig_retina)
if (keep_md)
knitr_opts$fig.retina <- NULL

## knitr hooks for plots
knitr_hooks <- list(plot=function(x, options) {
name <- paste(options$fig.path, options$label,".png", sep = '')
if(!is.null(options$fig.cap)){
caption <- paste('<p class="caption">', options$fig.cap, '</p>', sep = "")
} else {
caption <- ""
}
out <- "<div class='figure'>"
out <- paste0(out, "<a class='image-link' href='#' title='",options$fig.cap,"'>")
out <- paste0(out, "<img src='",name,"' class='img-polaroid image-thumb' />")
out <- paste0(out, "</a>")
out <- paste0(out, caption)
out <- paste0(out, "</div>")
return(out)
})

## return format
rmarkdown::output_format(
knitr = rmarkdown::knitr_options(opts_chunk=knitr_opts, knit_hooks=knitr_hooks),
pandoc = rmarkdown::pandoc_options(to = "html",
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, theme = "default",
self_contained = self_contained,
lib_dir = lib_dir, mathjax = mathjax,
pandoc_args = pandoc_args,
extra_dependencies = extra_dependencies,
...)

## js and css dependencies
extra_dependencies <- list(rmarkdown:::html_dependency_jquery(),
rmarkdown:::html_dependency_bootstrap("bootstrap"),
html_dependency_jquery_ui(),
html_dependency_tocify(),
html_dependency_magnific_popup(),
html_dependency_clean())

rmarkdown::html_document(
template=system.file("templates/html_clean/default.html", package="rmdformats"),
extra_dependencies = extra_dependencies,
fig_width = fig_width,
fig_height = fig_height,
fig_caption = fig_caption,
highlight = highlight,
...
)

}


# create an html dependency for jquery-ui
html_dependency_jquery_ui <- function() {
htmltools::htmlDependency(name = "jquery-ui",
Expand Down
149 changes: 19 additions & 130 deletions R/html_docco.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,14 @@
#'
#' Format for converting from R Markdown to an HTML document.
#'
#' @param number_sections \code{TRUE} to number section headings
#' @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 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 css One or more css files to include
#' @param includes Named list of additional content to include within the
#' document (typically created using the \code{\link{includes}} function).
#' @param keep_md Keep the markdown file generated by knitting.
#' @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
#' @param ... Additional function arguments passed to R Markdown \code{\link{html_document}}
#'
#' @return R Markdown output format to pass to \code{\link{render}}
#'
Expand All @@ -54,111 +26,28 @@
#' @import rmarkdown
#' @import htmltools

html_docco <- function(number_sections = FALSE,
fig_width = 6,
html_docco <- function(fig_width = 6,
fig_height = 6,
fig_retina = if (!fig_caption) 2,
fig_caption = TRUE,
smart = TRUE,
self_contained = TRUE,
highlight = "pygments",
mathjax = "default",
css = NULL,
includes = NULL,
keep_md = FALSE,
lib_dir = NULL,
pandoc_args = NULL,
...) {

## build pandoc args
args <- c("--standalone")
## use section divs
args <- c(args, "--section-divs")

## template
args <- c(args, "--template",
rmarkdown::pandoc_path_arg(system.file("templates/html_docco/default.html", package="rmdformats")))
## numbered sections
if (number_sections)
args <- c(args, "--number-sections")
## additional css
for (css_file in css)
args <- c(args, "--css", rmarkdown::pandoc_path_arg(css_file))

# 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()

# highlight
args <- c(args, rmarkdown:::pandoc_html_highlight_args(highlight,
"default",
self_contained,
lib_dir,
output_dir))

# content includes (we do this here so that user include-in-header content
# goes after dependency generated content)
args <- c(args, rmarkdown:::includes_to_pandoc_args(includes))

# return additional args
args
}

## Added js and css dependencies
extra_dependencies <- list(rmarkdown:::html_dependency_jquery(),
rmarkdown:::html_dependency_bootstrap("bootstrap"),
html_dependency_magnific_popup(),
html_dependency_docco())
## knitr options
knitr_opts <- list(dev = 'png',
dpi = 96,
fig.width = fig_width,
fig.height = fig_height,
fig.retina = fig_retina)
if (keep_md)
knitr_opts$fig.retina <- NULL

## knitr hooks for plots
knitr_hooks <- list(plot=function(x, options) {
name <- paste(options$fig.path, options$label,".png", sep = '')
if(!is.null(options$fig.cap)){
caption <- paste('<p class="caption">', options$fig.cap, '</p>', sep = "")
} else {
caption <- ""
}
out <- "<div class='figure'>"
out <- paste0(out, "<a class='image-link' href='#' title='",options$fig.cap,"'>")
out <- paste0(out, "<img src='",name,"' class='img-polaroid image-thumb' />")
out <- paste0(out, "</a>")
out <- paste0(out, caption)
out <- paste0(out, "</div>")
return(out)
})

## return format
rmarkdown::output_format(
knitr = rmarkdown::knitr_options(opts_chunk=knitr_opts, knit_hooks=knitr_hooks),
pandoc = rmarkdown::pandoc_options(to = "html",
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, theme = "default",
self_contained = self_contained,
lib_dir = lib_dir, mathjax = mathjax,
pandoc_args = pandoc_args,
extra_dependencies = extra_dependencies,
...)
)

## js and css dependencies
extra_dependencies <- list(rmarkdown:::html_dependency_jquery(),
rmarkdown:::html_dependency_bootstrap("bootstrap"),
html_dependency_magnific_popup(),
html_dependency_docco())

rmarkdown::html_document(
template=system.file("templates/html_docco/default.html", package="rmdformats"),
extra_dependencies = extra_dependencies,
fig_width = fig_width,
fig_height = fig_height,
fig_caption = fig_caption,
highlight = highlight,
...
)

}


Expand Down

0 comments on commit 101411f

Please sign in to comment.