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

More resilient pandoc self-contained mechanism #292

Merged
merged 1 commit into from Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 55 additions & 2 deletions R/pandoc.R
Expand Up @@ -14,6 +14,42 @@ pandoc_available <- function(version = NULL) {
FALSE
}

pandoc_save_markdown <- function(html, file, background = "white", title, libdir = "lib") {
# Forked from htmltools::save_html to work better with pandoc_self_contained_html

# ensure that the paths to dependencies are relative to the base
# directory where the webpage is being built.
dir <- dirname(file)
oldwd <- setwd(dir)
on.exit(setwd(oldwd), add = TRUE)

rendered <- renderTags(html)

deps <- lapply(rendered$dependencies, function(dep) {
dep <- htmltools::copyDependencyToDir(dep, libdir, FALSE)
dep <- htmltools::makeDependencyRelative(dep, dir, FALSE)
dep
})

# Build the markdown page. Anything that goes into the eventual <head> goes in
# the yaml header, and will be rendered using the pandoc template.
html <- c(
"---",
yaml::as.yaml(list(
title = htmltools::htmlEscape(title),
"header-include" = renderDependencies(deps, c("href", "file")),
"head" = rendered$head,
"background-color" = htmltools::htmlEscape(background, attribute = TRUE)
)),
"---",
rendered$html
)

# write it
writeLines(html, file, useBytes = TRUE)
}

# The input should be the path to a file that was created using pandoc_save_markdown
pandoc_self_contained_html <- function(input, output) {

# make input file path absolute
Expand All @@ -24,9 +60,26 @@ pandoc_self_contained_html <- function(input, output) {
file.create(output)
output <- normalizePath(output)

# create a simple body-only template
# create a template
template <- tempfile(fileext = ".html")
writeLines("$body$", template)
writeLines(c(
"<!DOCTYPE html>",
"<html>",
"<head>",
"<meta charset=\"utf-8\"/>",
"<title>$title$</title>",
"$for(header-include)$",
"$header-include$",
"$endfor$",
"$for(head)$",
"$head$",
"$endfor$",
"</head>",
"<body style=\"background-color: $background-color$;\">",
"$body$",
"</body>",
"</html>"
), template)

# convert from markdown to html to get base64 encoding
# (note there is no markdown in the source document but
Expand Down
10 changes: 7 additions & 3 deletions R/savewidget.R
Expand Up @@ -11,10 +11,12 @@
#' filename_files).
#' @param background Text string giving the html background color of the widget.
#' Defaults to white.
#' @param title Text to use as the title of the generated page.
#' @param knitrOptions A list of \pkg{knitr} chunk options.
#' @export
saveWidget <- function(widget, file, selfcontained = TRUE, libdir = NULL,
background = "white", knitrOptions = list()) {
background = "white", title = class(widget)[[1]],
knitrOptions = list()) {

# convert to HTML tags
html <- toHTML(widget, standalone = TRUE, knitrOptions = knitrOptions)
Expand All @@ -25,8 +27,10 @@ saveWidget <- function(widget, file, selfcontained = TRUE, libdir = NULL,
sep = "")
}

# save the file
htmltools::save_html(html, file = file, libdir = libdir, background=background)
# Save the file
# Include a title; pandoc 2.0 complains if you don't have one
pandoc_save_markdown(html, file = file, libdir = libdir,
background = background, title = title)

# make it self-contained if requested
if (selfcontained) {
Expand Down