forked from byuistats/Math221D_Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_create_qmd_workbook.R
More file actions
109 lines (84 loc) · 3.18 KB
/
_create_qmd_workbook.R
File metadata and controls
109 lines (84 loc) · 3.18 KB
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
104
105
106
107
108
pacman::p_load(fs, yaml, stringr, zip)
# Function to add YAML features and remove download button
add_yaml_features_and_remove_button <- function(file_path, new_features) {
content <- readLines(file_path, warn = FALSE)
yaml_start <- which(content == "---")[1]
yaml_end <- which(content == "---")[2]
if (is.na(yaml_end)) {
warning(paste("No YAML found in", file_path))
return(content)
}
existing_yaml <- yaml.load(paste(content[(yaml_start + 1):(yaml_end - 1)], collapse = "\n"))
updated_yaml <- modifyList(existing_yaml, new_features, keep.null = TRUE)
updated_yaml_str <- as.yaml(updated_yaml, handlers = list(logical=verbatim_logical))
if (yaml_end < length(content)) {
content_without_button <- content[(yaml_end + 1):length(content)]
button_start <- which(grepl("<a.*class=\"button-download\"", content_without_button))
button_end <- which(grepl("</a>", content_without_button))
if (length(button_start) > 0 && length(button_end) > 0) {
button_end <- button_end[button_end > button_start][1]
if (!is.na(button_end)) {
content_without_button <- content_without_button[-(button_start:button_end)]
}
}
} else {
content_without_button <- character(0)
}
new_content <- c(
"---",
strsplit(updated_yaml_str, "\n")[[1]],
"---",
content_without_button
)
return(new_content)
}
source_dirs <- c("./1-Getting_Started", "./2-Descriptive_Statistics", "./3-Data_Wrangling_Visualization", "./7-Semester_Project", "4-Foundations_Statistical_Inference", "5-Statistical_Tests_Part1", "6-Statistical_Tests_Part2")
yaml_to_add <- list(
format = list(
html = list(
`self-contained` = TRUE,
`embed-resources` = TRUE
)
)
)
for(i in 1:length(source_dirs)){
dest_dir <- paste("./Student_Work_Part2", str_remove_all(source_dirs[i], "./"), sep='/')
dir_create(dest_dir)
qmd_files <- dir_ls(source_dirs[i], glob = "*.qmd")
for (file in qmd_files) {
dest_file <- path(dest_dir, path_file(file))
file_copy(file, dest_file, overwrite = TRUE)
new_content <- add_yaml_features_and_remove_button(dest_file, yaml_to_add)
writeLines(new_content, dest_file)
cat("Processed:", path_file(file), "\n")
}
cat("All files have been copied, modified, and had download buttons removed.\n")
}
############# Zip the folder
zip_directory <- function(dir_path, zip_name = NULL) {
# Normalize directory path
dir_path <- path_norm(dir_path)
# Check if directory exists
if (!dir.exists(dir_path)) {
stop("Directory does not exist:", dir_path)
}
# If no zip name provided, use directory name
if (is.null(zip_name)) {
zip_name <- paste0(path_file(dir_path), ".zip")
}
# Create zip file path in parent directory
zip_path <- path_norm(file.path(path_dir(dir_path), zip_name))
# Change working directory to parent directory
old_wd <- getwd()
on.exit(setwd(old_wd)) # Ensure we return to original directory
setwd(path_dir(dir_path))
# Create zip file using relative path
zip(
zipfile = zip_name,
files = path_file(dir_path),
mode = "mirror",
recurse = TRUE
)
cat("Created zip file:", zip_path, "\n")
}
zip_directory("Student_Work")