forked from rstudio/distill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure_template.R
executable file
·58 lines (48 loc) · 2.12 KB
/
configure_template.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env Rscript
library(git2r)
library(stringr)
library(readr)
path <- file.path(tempfile(pattern = "git2r-"), "template")
dir.create(path, recursive = TRUE)
repo <- clone("https://github.com/rstudio/template", branch = "radix", path)
system2("npm", args = c("--prefix", path, "install"))
system2("npm", args = c("--prefix", path, "run", "build"))
transform_for_distill <- function(script) {
# Expect `Prism.languages.clike` and `n.languages.clike`
count_match <- str_count(script, "(?<=\\.languages.clike=\\{)")
if (!identical(2L, num_matches <- sum(count_match)))
stop(paste0(num_matches, " matches for pattern ", p, " found, but two expected."), call. = FALSE)
line_to_modify <- which(count_match == 2L)
# Add comment patterns, being explicit about keys
comment_patterns <- c(
"(?<=Prism\\.languages\\.clike=\\{comment:\\[)",
"(?<=n\\.languages\\.clike=\\{comment:\\[)"
)
comment_pattern_to_insert <- "{pattern:/(^|[^\\\\])#.*/,lookbehind:!0},"
for (p in comment_patterns) {
if (!str_detect(script[[line_to_modify]], p))
stop("Comment pattern insertion failed: cannot find pattern `", p, "`.", call. = FALSE)
splitted <- script[[line_to_modify]] %>%
str_split(p) %>%
unlist()
script[[line_to_modify]] <- paste0(splitted[[1]], comment_pattern_to_insert, splitted[[2]])
}
# Replace function patterns
function_patterns <- c(
"(?<=Prism\\.languages\\.clike=\\{.{0,550},function:)(.+?)(?=,number)",
"(?<=n\\.languages\\.clike=\\{.{0,550},function:)(.+?)(?=,number)"
)
function_pattern_replacement <- "/[a-z\\\\.0-9_]+(?=\\\\()/i"
for (p in function_patterns) {
if (!str_detect(script[[line_to_modify]], p))
stop("Function pattern replacement failed: cannot find pattern `", p, "`.", call. = FALSE)
script[[line_to_modify]] <- script[[line_to_modify]] %>%
str_replace(p, function_pattern_replacement)
}
# Function wrapper
c("function load_distill_framework() {", script, "}")
}
read_lines(file.path(path, "dist", "template.v2.js")) %>%
transform_for_distill() %>%
write_lines("inst/www/distill/template.v2.js")
unlink(path, recursive = TRUE)