Skip to content

Commit

Permalink
Avoid using pandoc to save the whole page self-contained
Browse files Browse the repository at this point in the history
  • Loading branch information
dipterix committed Sep 19, 2024
1 parent bb7ba96 commit b07713d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changes since last CRAN release
* `3c5eb84c (HEAD -> master)` [_`dipterix`_]: Fixed the depth issue in electrode material shader
* `583d267e (origin/master, origin/HEAD)` [_`dipterix`_]: Set maximum render length cut-off for electrode prototypes
* `cb5aed88 (HEAD -> master)` [_`dipterix`_]: Avoid using pandoc to save the whole page self-contained
* `bb7ba967 (origin/master, origin/HEAD)` [_`dipterix`_]: Fixed the depth issue in electrode material shader
* `583d267e` [_`dipterix`_]: Set maximum render length cut-off for electrode prototypes
* `4037eddb` [_`dipterix`_]: Added model tangent (usually the direction) to electrode shader so outlines are correctly visualized
* `da148714` [_`dipterix`_]: Fixed electrode prototype rendering issue (color) on Windows
* `eda02a82` [_`dipterix`_]: Fixed the color map for discrete values
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: threeBrain
Type: Package
Title: Your Advanced 3D Brain Visualization
Version: 1.1.1.9017
Version: 1.1.1.9018
Authors@R: c(
person("Zhengjia", "Wang", email = "[email protected]", role = c("aut", "cre", "cph")),
person("John", "Magnotti", email = "[email protected]", role = c("ctb", "res")),
Expand Down
67 changes: 59 additions & 8 deletions R/threejs_brain.R
Original file line number Diff line number Diff line change
Expand Up @@ -474,22 +474,73 @@ save_brain <- function(widget, path, title = '3D Viewer', as_zip = FALSE, ...){
widget$x$settings$cache_folder <- "#"

# selfcontained = FALSE to save all data information
temp_file <- file.path(wdir, "_tmp.html")
htmlwidgets::saveWidget(
widget,
file = file.path(wdir, "_tmp.html"),
file = temp_file,
selfcontained = FALSE,
title = title,
libdir = "_lib"
)

# Use htmlwidgets to save all js and css to html
htmlwidgets::saveWidget(
widget,
file = file.path(wdir, "_tmp.html"),
selfcontained = TRUE,
title = title,
libdir = "lib"
)
html_text <- readLines(temp_file)

# convert <script src=*> to <script>js file contents</script>
js_lines <- which(grepl(
x = html_text,
pattern = '(src=.*js)'
))

# convert link[rel=stylesheet] to <style>css file contents</style>
css_lines <- which(grepl(
x = html_text,
pattern = '(href=.*css)'
))

readlines_quiet <- function(path) {
suppressWarnings({
readLines(path)
})
}

# perform self-contained conversion/replacement of JS
if(length(js_lines) > 0) {
html_text[js_lines] <- lapply(js_lines, function(js_line) {
js_file <- sub(x = html_text[js_line],
pattern = '.*src=[":\'](.*\\.js).*',
replacement = "\\1")
js_content <- paste0("<script>",
paste0(readlines_quiet(file.path(wdir, js_file)), collapse = "\n"),
"</script>",
collapse = "\n")
})
}


# perform self-contained conversion/replacement of JS
if(length(css_lines) > 0) {
html_text[css_lines] <- lapply(css_lines, function(css_line) {
css_file <- sub(x=html_text[css_line], pattern='.*href=[":\'](.*\\.css).*', replacement="\\1")
css_content <- paste0(
"<style>",
paste0(readlines_quiet(file.path(wdir,css_file)), collapse="\n"),
"</style>",
collapse="\n"
)
})
}

# save self-contained html
write(paste0(html_text, collapse = "\n"), file = temp_file)

# htmlwidgets::saveWidget(
# widget,
# file = file.path(wdir, "_tmp.html"),
# selfcontained = TRUE,
# title = title,
# libdir = "lib"
# )

# modify the html so the data is injected as data URI
index <- file.path(wdir, "_tmp.html")
Expand Down

0 comments on commit b07713d

Please sign in to comment.