You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And, as consequence, produces unsavory results with pandoc >= 2.0
m<-DT::datatable(mtcars)
htmlwidgets::saveWidget(m, "index.html")
#> [WARNING] This document format requires a nonempty <title> element.#> Please specify either 'title' or 'pagetitle' in the metadata.#> Falling back to 'index'
browseURL("index.html")
htmlwidgets:::.pandoc$version#> ‘2.0.0.1’
I noticed the same thing with jjallaire/sigma. I installed pandoc-1.19.2.1 and pandoc-2.0.1 under C:\Pandoc and switched between them by fiddling with htmlwidgets:::.pandoc. When using
saveWidget(selfcontained=TRUE) with pandoc-2.0.1 I got a warning and mangled html file.
> z <- sigma::sigma(system.file("examples/ediaspora.gexf.xml", package = "sigma"))
> htmlFilename <- tempfile(fileext=".html")
> w <- htmlwidgets:::saveWidget(z, file=htmlFilename, selfcontained=TRUE)
[WARNING] This document format requires a nonempty <title> element.
Please specify either 'title' or 'pagetitle' in the metadata.
Falling back to 'file2a643ec14d9f'
> writeLines(substring(readLines(htmlFilename, n=10), 1, 50))
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>(function() {
// If window.HTMLWidgets is already defined, the
// new object. This allows preceding code to set
// initialization process (though none currently
window.HTMLWidgets = window.HTMLWidgets || {};
Is this because htmlwidgets:::pandoc_self_contained_html claims to pandoc
that its html input file is a markdown file? Pandoc-2.0 seems to balk at this.
# 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)
pandoc_convert(
input = input,
from = "markdown",
output = output,
options = c(
"--self-contained",
"--template", template
)
)
Fixing the title requirement is straightforward. We can explicitly allow users to specify a title in saveWidget. What should we default it to? Any thoughts @cpsievert.
@BillDunlap based on the output you posted, it seems like pandoc is escaping html tags. I will try to reproduce this with pandoc 2.0.1 and the latest version of htmlwidgets.
With pandoc-2.0.1 you also do not get the base64 encoding of the
supporting library
scripts. My suspicion is that when you claim the input is markdown but it
is really
html, pandoc-2.0.1 will give you problems.
At this point, is pandoc only being used to incorporate the library scripts
into the main
file and to base64 encode them? If so, we could do that in R code.
I am not very familiar with all of what htmlwidgets can do, but the
following function makes a self-contained html file similar to the one
pandoc makes if all the required files are *.js files and if there is no
need to recursively process the required files. Apply it to the
readLInes() of the file
that htmlwidgets::saveWidget(selfcontained=FALSE,...) produces, after
setting the working directory to dirname() of that html file and then call
writeLines() on its output.
It uses regular expressions instead parsing the html so it is not robust,
but it helped me see what was going on.
replaceScriptSrcLines <- function(htmlText)
{
# replace lines of form
# <script src="dir/file.js"></script>
# by
# <script
src="data:application/x-javascript;base64,*BASE64_ENCODED_JSFILE*"></script>"
# where *BASE64_ENCODED_JSFILE* is the string returned by
base64enc::base64encode("dir/file.js").
# Apply this to readLines() of the file that
srcPattern <- "(^.*<script src=\")([^:\"]+.js)(\"></script>.*)"
srcLineNos <- grep(srcPattern, htmlText)
cap <- strcapture(srcPattern, htmlText[srcLineNos],
proto=list(start="", script="", end=""))
for(j in seq_along(srcLineNos)) {
htmlText[srcLineNos[j]] <- paste0(cap$start[j],
"data:application/x-javascript;base64,",
base64enc::base64encode(cap$script[j]), cap$end[j])
}
htmlText
}
I seem to have circumvented the issue via htmltools::save_html(). My issue seems less involve than what others are getting into. Still interesting to dive deeper on some of these functions.
I'm going to submit a PR for this (the original issue) today. My proposal is that saveWidget will get a new title parameter that defaults to class(widget)[[1]]. Any objections to that?
Turns out this is significantly more annoying than we originally thought.
The htmlwidgets::pandoc_self_contained_html function tells pandoc that the input format is markdown. But the actual file is a well-formed .html file, generated by htmltools::save_html. This has two problems.
Any <title> in the input document is ignored (you still get the warning); instead Pandoc wants you to add a yaml header to the input HTML file with a non-empty title or pagetitle field, which then is not actually used in our pandoc template (because its entire contents is $body$).
The <!DOCTYPE html> at the top of the input HTML file (rightfully put there by htmltools::save_html) becomes escaped in the output HTML: <!DOCTYPE html>. This behavior is different than pandoc 1.x.
Rather than try to feed pandoc a well-formed HTML document, I think it's a better idea to fork the body of htmltools::save_html, which isn't much anyway, and more carefully generate an input file and template that both pandoc 1.x and 2.0 can be happy with.
Activity
BillDunlap commentedon Nov 2, 2017
I noticed the same thing with jjallaire/sigma. I installed pandoc-1.19.2.1 and pandoc-2.0.1 under C:\Pandoc and switched between them by fiddling with htmlwidgets:::.pandoc. When using
saveWidget(selfcontained=TRUE) with pandoc-2.0.1 I got a warning and mangled html file.
Is this because htmlwidgets:::pandoc_self_contained_html claims to pandoc
that its html input file is a markdown file? Pandoc-2.0 seems to balk at this.
ramnathv commentedon Nov 3, 2017
Fixing the
titlerequirement is straightforward. We can explicitly allow users to specify a title insaveWidget. What should we default it to? Any thoughts @cpsievert.@BillDunlap based on the output you posted, it seems like pandoc is escaping html tags. I will try to reproduce this with pandoc 2.0.1 and the latest version of
htmlwidgets.BillDunlap commentedon Nov 3, 2017
ramnathv commentedon Nov 3, 2017
@BillDunlap
pandocis mainly used to inline all dependencies recursively. Doing this in R will take some effort.kiefersmith commentedon Nov 13, 2017
I am also having this issue. Any updates?
BillDunlap commentedon Nov 13, 2017
kiefersmith commentedon Nov 14, 2017
I seem to have circumvented the issue via
htmltools::save_html(). My issue seems less involve than what others are getting into. Still interesting to dive deeper on some of these functions.jcheng5 commentedon Nov 14, 2017
I'm going to submit a PR for this (the original issue) today. My proposal is that
saveWidgetwill get a newtitleparameter that defaults toclass(widget)[[1]]. Any objections to that?jjallaire commentedon Nov 14, 2017
jcheng5 commentedon Nov 15, 2017
Turns out this is significantly more annoying than we originally thought.
The
htmlwidgets::pandoc_self_contained_htmlfunction tells pandoc that the input format is markdown. But the actual file is a well-formed .html file, generated byhtmltools::save_html. This has two problems.<title>in the input document is ignored (you still get the warning); instead Pandoc wants you to add a yaml header to the input HTML file with a non-emptytitleorpagetitlefield, which then is not actually used in our pandoc template (because its entire contents is$body$).<!DOCTYPE html>at the top of the input HTML file (rightfully put there byhtmltools::save_html) becomes escaped in the output HTML:<!DOCTYPE html>. This behavior is different than pandoc 1.x.Rather than try to feed pandoc a well-formed HTML document, I think it's a better idea to fork the body of
htmltools::save_html, which isn't much anyway, and more carefully generate an input file and template that both pandoc 1.x and 2.0 can be happy with.cpsievert commentedon Feb 8, 2023
This was fixed by #292