Skip to content

Commit

Permalink
Merge pull request #1857 from rstudio/feature/pandoc-2-compat
Browse files Browse the repository at this point in the history
Make self-contained output compatible with Pandoc 2
  • Loading branch information
jmcphers committed Dec 18, 2017
2 parents bf2e02a + 3648196 commit 0df3ce4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/cpp/session/SessionModuleContext.cpp
Expand Up @@ -2236,6 +2236,8 @@ Error createSelfContainedHtml(const FilePath& sourceFilePath,
{
r::exec::RFunction func(".rs.pandocSelfContainedHtml");
func.addParam(string_utils::utf8ToSystem(sourceFilePath.absolutePath()));
func.addParam(string_utils::utf8ToSystem(
session::options().rResourcesPath().complete("pandoc_template.html").absolutePath()));
func.addParam(string_utils::utf8ToSystem(targetFilePath.absolutePath()));
return func.call();
}
Expand Down
35 changes: 22 additions & 13 deletions src/cpp/session/modules/ModuleTools.R
Expand Up @@ -292,27 +292,36 @@
})


.rs.addFunction("pandocSelfContainedHtml", function(input, output) {
.rs.addFunction("pandocSelfContainedHtml", function(input, template, output) {

# make input file path absolute
input <- normalizePath(input)

# ensure output file exists and make it's path absolute

# create a temporary copy of the file for conversion
inputFile <- tempfile(tmpdir = dirname(input), fileext = ".html")
inputLines <- readLines(con = input, warn = FALSE)

# write all the lines from the input except the DOCTYPE declaration, which pandoc will not treat
# as HTML (starting in pandoc 2)
writeLines(text = inputLines[!grepl("<!DOCTYPE", inputLines, fixed = TRUE)],
con = inputFile)

# ensure output file exists and make its path absolute
if (!file.exists(output))
file.create(output)
output <- normalizePath(output)

# create a simple body-only template
template <- tempfile(fileext = ".html")
writeLines("$body$", template)

# convert from markdown to html to get base64 encoding
# (note there is no markdown in the source document but
# we still need to do this "conversion" to get the
# base64 encoding)
args <- c(input)
# convert from markdown to html to get base64 encoding. note there is no markdown in the source
# document but we still need to do this "conversion" to get the base64 encoding; we also don't
# want to convert from HTML since that will cause pandoc to convert only the <body>
args <- c(inputFile)
args <- c(args, "--from", "markdown_strict")
args <- c(args, "--output", output)

# define a title for the document. this value is not actually consumed by the template, but
# pandoc requires it in metadata when converting to HTML, so supply a dummy value to keep the
# output clean.
args <- c(args, "--metadata", "title:RStudio")

# set stack size
stack_size <- getOption("pandoc.stack.size", default = "512m")
Expand All @@ -329,7 +338,7 @@
# setwd temporarily
wd <- getwd()
on.exit(setwd(wd), add = TRUE)
setwd(dirname(input))
setwd(dirname(inputFile))

# execute it
result <- system(command)
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/session/resources/pandoc_template.html
@@ -0,0 +1,2 @@
<!DOCTYPE html>
$body$
1 change: 0 additions & 1 deletion src/cpp/session/resources/plot_publish.html
@@ -1,4 +1,3 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
Expand Down

0 comments on commit 0df3ce4

Please sign in to comment.