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

Make self-contained output compatible with Pandoc 2 #1857

Merged
merged 1 commit into from Dec 18, 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
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