Closed vincentarelbundock closed 7 months ago
Maybe with servr
to serve the page first.
library(chromote)
library(tinytable)
tt(mtcars[1:3, 1:4]) |>
style_tt(j = 1, color = "white", background = "teal") |>
save_tt("example.html", overwrite = TRUE)
html_js_to_plain <- function(filename) {
filename <- path.expand(filename)
if (!file.exists(filename)) {
msg <- sprintf("`%s` does not exist.", filename)
stop(msg, call. = FALSE)
}
filename <- paste0("file:", filename)
b <- ChromoteSession$new()
b$Page$navigate(filename)
b$Page$loadEventFired()
js_code <- "var clone = document.body.cloneNode(true);
clone.querySelectorAll('script').forEach(script => script.remove());
clone.outerHTML;"
body <- b$Runtime$evaluate(js_code)$result$value
head <- b$Runtime$evaluate("document.querySelector('head').outerHTML")$result$value
out <- paste('```{=html}', '<html lang="en">', head, body, '</html>', '```', sep = '\n')
# out <- b$Runtime$evaluate("document.querySelector('html').outerHTML")$result$value
b$close()
out <- knitr::asis_output(out)
return(out)
}
filename <- "~/Downloads/example.html"
html_js_to_plain(filename)
pandoc does not recognize CSS, so we're stuck. This is useless for now, but keeping it in the sandbox folder just in case we eventually want "plain" html without having to interpret JS, which may be faster for readers (but slower for writers).