-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.R
103 lines (82 loc) · 2.71 KB
/
helper_functions.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
## Have the intro code ready
read_targets_intro <- function(){
targets_file <- file("_targets.R", open = "r")
targets_lines <- readLines(targets_file)
close(targets_file)
intro_end <- stringr::str_which(targets_lines, "^\\)$")[[1]]
c(targets_lines[seq_len(intro_end)], "")
}
## Read in list of chapters in order
get_chapters <- function(){
quarto_yml <- file("_quarto.yml", open = "r")
yml_lines <- readLines(quarto_yml)
close(quarto_yml)
## Selecting section containing chapter list
yaml_start <- stringr::str_which(yml_lines, "chapters")[2]
yml_lines <- yml_lines[-(seq_len(yaml_start))]
yaml_end <- stringr::str_which(yml_lines, "references.qmd")[1] - 1
yml_lines <- yml_lines[seq_len(yaml_end)]
yml_lines[stringr::str_which(yml_lines, "\\.qmd")] |>
stringr::str_remove("\\s+- ")
}
## Add missing comma and empty line to the last target of each element in the
## list except the last one.
combine_content <- function(x) {
last_index <- length(x)
last_x <- x[last_index]
x <- x[-last_index]
x <- x |>
purrr::map(\(y){
indx <- rev(stringr::str_which(y, "^ \\)$"))[1]
y[indx] <- paste0(y[indx], ",")
if (y[length(y)] != "") y <- c(y, "")
y
})
c(x, last_x) |> unlist()
}
## Read last chunk from each chapter
parse_chapter <- function(chapter){
chapter_content <- parsermd::parse_rmd(chapter)
chapter_title <- parsermd::as_tibble(chapter_content) |>
dplyr::filter(!is.na(sec_h1)) |>
dplyr::pull(sec_h1) |>
unique() |>
stringr::str_remove(" \\{.+")
chapter_title_line <- paste0("# ", chapter_title, " ") |>
stringr::str_pad(width = 80, side = "right", pad = "-")
lines <- chapter_content |>
parsermd::rmd_select(parsermd::has_label("*recap-targets-list")) |>
purrr::map(parsermd::as_document) |>
combine_content()
to_remove <- c(
stringr::str_which(lines, "```"),
stringr::str_which(lines, "^list\\("),
stringr::str_which(lines, "^\\)$")
)
c(chapter_title_line, "", lines[-to_remove])
}
## for each chapter, combine previous chapters' code
get_previous_content <- function(current_chapter) {
current_chapter <- stringr::str_replace(current_chapter, "rmarkdown", "qmd")
intro <- read_targets_intro()
chapters <- get_chapters()
curr_chap <- which(chapters == current_chapter)
chapters <- chapters[seq_len(curr_chap - 1)]
chapters_content <- purrr::map(chapters, parse_chapter) |>
combine_content()
if (chapters_content[length(chapters_content)] == "") {
chapters_content <- chapters_content[-length(chapters_content)]
}
c(
"::: {.cell}",
"",
"\`\`\`{.r .cell-code}",
intro,
"list(",
chapters_content,
")",
"\`\`\`",
":::"
) |>
paste0(collapse = "\n")
}