Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame^] | 1 | ################################################ |
| 2 | # Script to update reveal.js resources # |
| 3 | # - To document steps so to execute manually # |
| 4 | ################################################ |
| 5 | |
| 6 | |
| 7 | # 01 - Update reveal.js library ------------------------------------------------ |
| 8 | |
| 9 | ## Main workflow: |
| 10 | ## * Download new release |
| 11 | ## * move plugins specific to revealjs package |
| 12 | ## * Check using version control the differences |
| 13 | ## * Adapt the code base if necessary |
| 14 | ## |
| 15 | ## Notes from updates |
| 16 | ## |
| 17 | ## 3.3 -> 4.1.2 (27/09/2021): |
| 18 | ## * Upgrade notes: https://revealjs.com/upgrading/ |
| 19 | ## * note-server and multiplex plugins where removed, but they weren't use in the package |
| 20 | ## * JS and CSS files were moved to dist/, including themes/ |
| 21 | ## * css/monokai moved to plugin/highlight |
| 22 | ## * print CSS were removed |
| 23 | ## * head.min.js deleted |
| 24 | ## * Plugin registration has changed |
| 25 | ## * html5shiv deleted |
| 26 | |
| 27 | |
| 28 | # download latest source |
| 29 | dir.create(tmp_dir <- tempfile()) |
| 30 | owd <- setwd(tmp_dir) |
| 31 | latest <- xfun::github_releases("hakimel/reveal.js", pattern = "([0-9.]+)")[1] |
| 32 | url <- sprintf("https://github.com/hakimel/reveal.js/archive/refs/tags/%s.zip", latest) |
| 33 | xfun::download_file(url) |
| 34 | fs::dir_ls() |
| 35 | unzip(basename(url)) |
| 36 | reveal_folder <- fs::path_abs(fs::dir_ls(glob = "reveal.js-*")) |
| 37 | setwd(owd) |
| 38 | |
| 39 | # Replace the library in the package |
| 40 | current <- fs::dir_ls("inst", glob = "*/reveal.js-*") |
| 41 | new <- fs::path("inst", fs::path_file(reveal_folder), "/") |
| 42 | fs::dir_copy(reveal_folder, fs::path("inst", fs::path_file(reveal_folder)), overwrite = TRUE) |
| 43 | |
| 44 | # move non-core plugins to new library folder |
| 45 | plugins <- c("chalkboard", "menu") |
| 46 | purrr::walk(plugins, ~{ |
| 47 | fs::dir_copy(fs::path(current, "plugin", .x), fs::path(new, "plugin", .x)) |
| 48 | }) |
| 49 | |
| 50 | # Delete old version |
| 51 | fs::dir_delete(current) |
| 52 | |
| 53 | # Stage file to look at differences |
| 54 | gert::git_add("inst/") |
| 55 | |
| 56 | fs::dir_delete(tmp_dir) |
| 57 | |
| 58 | # Update .Rbuildignore |
| 59 | current <- fs::dir_ls("inst", glob = "*/reveal.js-*") |
| 60 | buildignore <- xfun::read_utf8(".Rbuildignore") |
| 61 | i <- grep("inst/reveal", buildignore) |
| 62 | buildignore <- buildignore[-i] |
| 63 | ignore <- fs::dir_ls(current, regexp = ".*(dist|plugin|LICENSE|README.md).*", invert = TRUE, all = TRUE) |
| 64 | ignore_reg <- gsub("reveal\\.js-[^/]*", "reveal\\.js-[^/]+", ignore) |
| 65 | xfun::write_utf8(c(buildignore, ignore_reg), ".Rbuildignore") |
| 66 | |
| 67 | # Make fonts local ------------------------------------------------------- |
| 68 | |
| 69 | current <- fs::dir_ls("inst", glob = "*/reveal.js-*") |
| 70 | themes <- fs::dir_ls(fs::path(current, "dist", "theme"), glob = "*.css") |
| 71 | themes <- purrr::set_names(themes, nm = fs::path_file(fs::path_ext_remove(themes))) |
| 72 | url_fonts <- purrr::map(themes, ~ { |
| 73 | css_theme <- xfun::read_utf8(.x) |
| 74 | fonts <- stringr::str_extract(css_theme, "(?<=@import url\\()https://[^)]+") |
| 75 | as.character(na.omit(fonts)) |
| 76 | }) |
| 77 | fonts <- unique(purrr::simplify(url_fonts)) |
| 78 | sort(fonts) |
| 79 | |
| 80 | # is there duplicate font ? |
| 81 | dup <- duplicated(purrr::map_chr(stringr::str_match_all(fonts, "(?<=family=)([^:]+)"), ~ .x[1,1])) |
| 82 | fonts[dup] |
| 83 | |
| 84 | # if this is ok download theme |
| 85 | get_fonts <- purrr::map(fonts, ~ { |
| 86 | font_url <- .x |
| 87 | if (!grepl("https://fonts.googleapis.com", font_url, fixed = TRUE)) stop("Not a good font. Handle manuallly.") |
| 88 | # from sass:::font_dep_google_local |
| 89 | tmpdir <- tempfile() |
| 90 | dir.create(tmpdir, recursive = TRUE) |
| 91 | css_file <- file.path(tmpdir, "font.css") |
| 92 | css <- sass:::read_gfont_url(font_url, css_file) |
| 93 | urls <- sass:::extract_group(css, "url\\(([^)]+)") |
| 94 | family <- stringr::str_match_all(font_url, "(?<=family=)([^:]+)")[[1]][2] |
| 95 | family <- sub("\\s+", "_", sass:::trim_ws(family)) |
| 96 | family <- sub("\\+", "-", family) |
| 97 | basenames <- paste(family, seq_along(urls), sep = "-") |
| 98 | basenames <- fs::path_ext_set(basenames, fs::path_ext(fs::path_file(urls))) |
| 99 | Map(function(url, nm) { |
| 100 | f <- file.path(tmpdir, nm) |
| 101 | xfun::download_file(url, f, mode = "wb") |
| 102 | css <<- sub(url, nm, css, fixed = TRUE) |
| 103 | }, urls, basenames) |
| 104 | xfun::write_utf8(css, css_file) |
| 105 | font <- list(name = family, dir = dirname(css_file), css = basename(css_file)) |
| 106 | fs::dir_create(font_folder <- fs::path(current, "dist", "theme", "fonts", font$name)) |
| 107 | fs::file_copy(fs::dir_ls(font$dir), font_folder, overwrite = TRUE) |
| 108 | unlink(font$dir, recursive = TRUE) |
| 109 | font |
| 110 | }) |
| 111 | |
| 112 | get_fonts <- purrr::set_names(get_fonts, fonts) |
| 113 | local_fonts <- purrr::map(get_fonts, ~ { |
| 114 | font_folder <- fs::path(current, "dist", "theme", "fonts", .x$name) |
| 115 | fs::path(".", fs::path_rel(fs::path(font_folder, .x$css), fs::path_dir(themes[1]))) |
| 116 | }) |
| 117 | |
| 118 | for(theme in themes) { |
| 119 | purrr::iwalk(local_fonts, ~ xfun::gsub_file(theme, pattern = .y, replacement = .x, fixed = TRUE)) |
| 120 | } |
| 121 | |
| 122 | gert::git_add(fs::path(current, "dist", "theme")) |
| 123 | |
| 124 | |
| 125 | # Update plugins ---------------------------------------------------------- |
| 126 | |
| 127 | revealjs_lib <- fs::dir_ls("inst", glob = "*/reveal.js-*") |
| 128 | stopifnot(length(revealjs_lib) == 1) |
| 129 | |
| 130 | ## MENU PLUGGINS |
| 131 | ## https://github.com/denehyg/reveal.js-menu |
| 132 | dir.create(tmp_dir <- tempfile()) |
| 133 | owd <- setwd(tmp_dir) |
| 134 | latest <- xfun::github_releases("denehyg/reveal.js-menu", pattern = "([0-9.]+)")[1] |
| 135 | url <- sprintf("https://github.com/denehyg/reveal.js-menu/archive/refs/tags/%s.zip", latest) |
| 136 | xfun::download_file(url) |
| 137 | fs::dir_ls() |
| 138 | unzip(basename(url)) |
| 139 | new_plugin <- fs::path_abs(fs::dir_ls(glob = "reveal.js-*")) |
| 140 | setwd(owd) |
| 141 | |
| 142 | ### keep only necessary resources |
| 143 | plugin_folder <- fs::path(revealjs_lib, "plugin", "menu") |
| 144 | fs::dir_delete(plugin_folder) |
| 145 | to_keep <- c("menu.css", "menu.js", "LICENSE") |
| 146 | fs::dir_create(plugin_folder) |
| 147 | fs::file_copy(fs::path(new_plugin, to_keep), fs::path(plugin_folder, to_keep), overwrite = TRUE) |
| 148 | |
| 149 | ### Create VERSION file |
| 150 | writeLines(latest, fs::path(plugin_folder, "VERSION")) |
| 151 | |
| 152 | gert::git_add(plugin_folder) |
| 153 | |
| 154 | ## reveal.js-plugins repo |
| 155 | ## https://github.com/rajgoel/reveal.js-plugins/ |
| 156 | |
| 157 | dir.create(tmp_dir <- tempfile()) |
| 158 | owd <- setwd(tmp_dir) |
| 159 | latest <- xfun::github_releases("rajgoel/reveal.js-plugins", pattern = "([0-9.]+)")[1] |
| 160 | url <- sprintf("https://github.com/rajgoel/reveal.js-plugins/archive/refs/tags/%s.zip", latest) |
| 161 | xfun::download_file(url) |
| 162 | fs::dir_ls() |
| 163 | unzip(basename(url)) |
| 164 | new_plugin <- fs::path_abs(fs::dir_ls(glob = "reveal.js-*")) |
| 165 | setwd(owd) |
| 166 | |
| 167 | ### keep only necessary resources |
| 168 | plugins_to_keep <- c("chalkboard", "customcontrols") |
| 169 | plugin_folders <- fs::path(revealjs_lib, "plugin", plugins_to_keep) |
| 170 | fs::dir_delete(plugin_folders[fs::dir_exists(plugin_folders)]) |
| 171 | purrr::walk(plugin_folders, ~ { |
| 172 | fs::dir_copy(fs::path(new_plugin, fs::path_file(.x)), .x, overwrite = TRUE) |
| 173 | fs::file_copy(fs::path(new_plugin, "LICENSE"), .x, overwrite = TRUE) |
| 174 | writeLines(latest, fs::path(.x, "VERSION")) |
| 175 | }) |
| 176 | |
| 177 | gert::git_add(plugin_folders) |