diff --git a/Practice/Test_qmd.qmd b/Practice/Test_qmd.qmd new file mode 100644 index 0000000..274fc11 --- /dev/null +++ b/Practice/Test_qmd.qmd @@ -0,0 +1,6 @@ +--- +title: "Test QMD" +--- + + +This is just to see if the new function will pull this file into the zip file for student consumption. diff --git a/Student_Work/Practice/Test_qmd.qmd b/Student_Work/Practice/Test_qmd.qmd new file mode 100644 index 0000000..3448b25 --- /dev/null +++ b/Student_Work/Practice/Test_qmd.qmd @@ -0,0 +1,9 @@ +--- +title: Test QMD +format: + html: + self_contained: yes +--- + + +This is just to see if the new function will pull this file into the zip file for student consumption. diff --git a/Student_Work_zip.zip b/Student_Work_zip.zip index c67e4ea..62770c8 100644 Binary files a/Student_Work_zip.zip and b/Student_Work_zip.zip differ diff --git a/_Piping_qmd_files_for_download_zip.R b/_Piping_qmd_files_for_download_zip.R deleted file mode 100644 index b2af07f..0000000 --- a/_Piping_qmd_files_for_download_zip.R +++ /dev/null @@ -1,118 +0,0 @@ -library(fs) -library(yaml) -library(stringr) -library(zip) - -source_dirs <- c("./Workbooks", "./Practice", "./R_Resources", "./Application_Activities") -zip_file_name <- "Student_Work.zip" - -# Create a temporary directory to store processed files -temp_dir <- tempdir() - -add_yaml_features_and_remove_button <- function(file_path, new_features) { - # Read the file content - content <- readLines(file_path, warn = FALSE) - - # Find the YAML section - 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) - } - - # Extract and parse the existing YAML - existing_yaml <- yaml::yaml.load(paste(content[(yaml_start + 1):(yaml_end - 1)], collapse = "\n")) - - # Merge the new features with the existing YAML - updated_yaml <- modifyList(existing_yaml, new_features, keep.null = TRUE) - - # Convert the updated YAML back to string - updated_yaml_str <- as.yaml(updated_yaml) - - # Remove the download button - if (yaml_end < length(content)) { - content_without_button <- content[(yaml_end + 1):length(content)] - - # Find start and end indices of the button - button_start <- which(grepl("", content_without_button)) - - # Remove the button if found - if (length(button_start) > 0 && length(button_end) > 0) { - button_end <- button_end[button_end > button_start][1] # Find the first closing tag after the opening tag - if (!is.na(button_end)) { - content_without_button <- content_without_button[-(button_start:button_end)] - } - } - } else { - content_without_button <- character(0) # Empty vector if there's no content after YAML - } - - # Reconstruct the file content - new_content <- c( - "---", - strsplit(updated_yaml_str, "\n")[[1]], - "---", - content_without_button - ) - - return(new_content) -} - -for(i in 1:length(source_dirs)){ - - # Define source and destination directories - dest_dir <- path(temp_dir, "Student_Work", str_remove_all(source_dirs[i], "./")) - - # Create the destination directory if it doesn't exist - dir_create(dest_dir) - - yaml_to_add <- list( - format = list( - html = list( - self_contained = TRUE - # Add any other features you want here - ) - ) - ) - - # Get list of qmd files - qmd_files <- dir_ls(source_dirs[i], glob = "*.qmd") - - # Process each file - for (file in qmd_files) { - # Construct destination file path - dest_file <- path(dest_dir, path_file(file)) - - # Copy the file - file_copy(file, dest_file, overwrite = TRUE) - - # Add YAML features to the copied file and remove download button - new_content <- add_yaml_features_and_remove_button(dest_file, yaml_to_add) - - # Write the new content back to the file - writeLines(new_content, dest_file) - - cat("Processed:", path_file(file), "\n") - } - - cat("All files have been copied, modified, and had download buttons removed.\n") -} - -# Create zip file -zip_file_path <- path(temp_dir, zip_file_name) -zip::zip(zip_file_path, dir(path(temp_dir, "Student_Work"), recursive = TRUE, full.names = TRUE)) - -cat("Created zip file:", zip_file_path, "\n") - -# Move zip file to desired location (e.g., current working directory) -file_move(zip_file_path, path(getwd(), zip_file_name)) - -cat("Moved zip file to:", path(getwd(), zip_file_name), "\n") - -# Clean up temporary directory -dir_delete(temp_dir) - -cat("Cleaned up temporary directory.\n") \ No newline at end of file diff --git a/_create_qmd_workbook.R b/_create_qmd_workbook.R new file mode 100644 index 0000000..0f5df4e --- /dev/null +++ b/_create_qmd_workbook.R @@ -0,0 +1,112 @@ +library(fs) +library(yaml) +library(stringr) +library(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) + + if (yaml_end < length(content)) { + content_without_button <- content[(yaml_end + 1):length(content)] + button_start <- which(grepl("", 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) +} + +# Function to zip a folder +zip_folder <- function(folder_path, output_zip_name) { + if (!dir.exists(folder_path)) { + stop("The specified folder does not exist.") + } + + output_zip_path <- file.path(dirname(folder_path), output_zip_name) + base_dir <- basename(folder_path) + files_to_zip <- list.files(base_dir, recursive = TRUE, full.names = TRUE, all.files = TRUE) + relative_paths <- sub(paste0("^", base_dir, "/"), "", files_to_zip) + + cat("Files to zip:\n") + print(relative_paths) + + non_existent_files <- files_to_zip[!file.exists(files_to_zip)] + if (length(non_existent_files) > 0) { + cat("\nWarning: The following files do not exist:\n") + print(non_existent_files) + files_to_zip <- files_to_zip[file.exists(files_to_zip)] + relative_paths <- relative_paths[file.exists(files_to_zip)] + } + + files_for_zip <- setNames(files_to_zip, relative_paths) + + tryCatch({ + zip(zipfile = output_zip_name, files = files_for_zip) + cat("\nFolder successfully zipped to:", output_zip_path, "\n") + }, error = function(e) { + cat("\nError during zipping:", e$message, "\n") + }) +} + +# Main script +source_dirs <- c("./Workbooks", "./Practice", "./R_Resources", "./Application_Activities") +yaml_to_add <- list( + format = list( + html = list( + self_contained = TRUE + ) + ) +) + +for(i in 1:length(source_dirs)){ + dest_dir <- paste("./Student_Work", 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 Student_Work folder +folder_to_zip <- "./Student_Work" +zip_file_name <- "Student_Work_zip.zip" +tryCatch({ + zip_folder(folder_to_zip, zip_file_name) +}, error = function(e) { + cat("Error:", e$message, "\n") +}) \ No newline at end of file diff --git a/Student_Work/_Piping_qmd_files_for_download.R b/_qmds_to_folder.R similarity index 100% rename from Student_Work/_Piping_qmd_files_for_download.R rename to _qmds_to_folder.R diff --git a/_Creating_Zip_Folder.R b/_zip_student_work.R similarity index 100% rename from _Creating_Zip_Folder.R rename to _zip_student_work.R