blob: 3be680344ea0f24ecaab16019d895d7c2a008447 [file] [log] [blame]
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001################################################
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
29dir.create(tmp_dir <- tempfile())
30owd <- setwd(tmp_dir)
31latest <- xfun::github_releases("hakimel/reveal.js", pattern = "([0-9.]+)")[1]
32url <- sprintf("https://github.com/hakimel/reveal.js/archive/refs/tags/%s.zip", latest)
33xfun::download_file(url)
34fs::dir_ls()
35unzip(basename(url))
36reveal_folder <- fs::path_abs(fs::dir_ls(glob = "reveal.js-*"))
37setwd(owd)
38
39# Replace the library in the package
40current <- fs::dir_ls("inst", glob = "*/reveal.js-*")
41new <- fs::path("inst", fs::path_file(reveal_folder), "/")
42fs::dir_copy(reveal_folder, fs::path("inst", fs::path_file(reveal_folder)), overwrite = TRUE)
43
44# move non-core plugins to new library folder
45plugins <- c("chalkboard", "menu")
46purrr::walk(plugins, ~{
47 fs::dir_copy(fs::path(current, "plugin", .x), fs::path(new, "plugin", .x))
48})
49
50# Delete old version
51fs::dir_delete(current)
52
53# Stage file to look at differences
54gert::git_add("inst/")
55
56fs::dir_delete(tmp_dir)
57
58# Update .Rbuildignore
59current <- fs::dir_ls("inst", glob = "*/reveal.js-*")
60buildignore <- xfun::read_utf8(".Rbuildignore")
61i <- grep("inst/reveal", buildignore)
62buildignore <- buildignore[-i]
63ignore <- fs::dir_ls(current, regexp = ".*(dist|plugin|LICENSE|README.md).*", invert = TRUE, all = TRUE)
64ignore_reg <- gsub("reveal\\.js-[^/]*", "reveal\\.js-[^/]+", ignore)
65xfun::write_utf8(c(buildignore, ignore_reg), ".Rbuildignore")
66
67# Make fonts local -------------------------------------------------------
68
69current <- fs::dir_ls("inst", glob = "*/reveal.js-*")
70themes <- fs::dir_ls(fs::path(current, "dist", "theme"), glob = "*.css")
71themes <- purrr::set_names(themes, nm = fs::path_file(fs::path_ext_remove(themes)))
72url_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})
77fonts <- unique(purrr::simplify(url_fonts))
78sort(fonts)
79
80# is there duplicate font ?
81dup <- duplicated(purrr::map_chr(stringr::str_match_all(fonts, "(?<=family=)([^:]+)"), ~ .x[1,1]))
82fonts[dup]
83
84# if this is ok download theme
85get_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
112get_fonts <- purrr::set_names(get_fonts, fonts)
113local_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
118for(theme in themes) {
119 purrr::iwalk(local_fonts, ~ xfun::gsub_file(theme, pattern = .y, replacement = .x, fixed = TRUE))
120}
121
122gert::git_add(fs::path(current, "dist", "theme"))
123
124
125# Update plugins ----------------------------------------------------------
126
127revealjs_lib <- fs::dir_ls("inst", glob = "*/reveal.js-*")
128stopifnot(length(revealjs_lib) == 1)
129
130## MENU PLUGGINS
131## https://github.com/denehyg/reveal.js-menu
132dir.create(tmp_dir <- tempfile())
133owd <- setwd(tmp_dir)
134latest <- xfun::github_releases("denehyg/reveal.js-menu", pattern = "([0-9.]+)")[1]
135url <- sprintf("https://github.com/denehyg/reveal.js-menu/archive/refs/tags/%s.zip", latest)
136xfun::download_file(url)
137fs::dir_ls()
138unzip(basename(url))
139new_plugin <- fs::path_abs(fs::dir_ls(glob = "reveal.js-*"))
140setwd(owd)
141
142### keep only necessary resources
143plugin_folder <- fs::path(revealjs_lib, "plugin", "menu")
144fs::dir_delete(plugin_folder)
145to_keep <- c("menu.css", "menu.js", "LICENSE")
146fs::dir_create(plugin_folder)
147fs::file_copy(fs::path(new_plugin, to_keep), fs::path(plugin_folder, to_keep), overwrite = TRUE)
148
149### Create VERSION file
150writeLines(latest, fs::path(plugin_folder, "VERSION"))
151
152gert::git_add(plugin_folder)
153
154## reveal.js-plugins repo
155## https://github.com/rajgoel/reveal.js-plugins/
156
157dir.create(tmp_dir <- tempfile())
158owd <- setwd(tmp_dir)
159latest <- xfun::github_releases("rajgoel/reveal.js-plugins", pattern = "([0-9.]+)")[1]
160url <- sprintf("https://github.com/rajgoel/reveal.js-plugins/archive/refs/tags/%s.zip", latest)
161xfun::download_file(url)
162fs::dir_ls()
163unzip(basename(url))
164new_plugin <- fs::path_abs(fs::dir_ls(glob = "reveal.js-*"))
165setwd(owd)
166
167### keep only necessary resources
168plugins_to_keep <- c("chalkboard", "customcontrols")
169plugin_folders <- fs::path(revealjs_lib, "plugin", plugins_to_keep)
170fs::dir_delete(plugin_folders[fs::dir_exists(plugin_folders)])
171purrr::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
177gert::git_add(plugin_folders)