|
| 1 | +#' fixes link-check problem |
| 2 | +#' Finds whether any files use the string: "(.mailto:" for linking email adresses |
| 3 | +#' then just replaces with: "(mailto:" as is markdown standard |
| 4 | +#' |
| 5 | +#' @param file_list vector of filenames |
| 6 | +#' |
| 7 | +#' @return messages only. side effect is: changes files. |
| 8 | +modify_files <- function(file_list) { |
| 9 | + # Create an empty vector to store the file names that contain the string |
| 10 | + matching_files <- c() |
| 11 | + |
| 12 | + # Iterate over each file in the directory |
| 13 | + for (file in file_list) { |
| 14 | + # Read the contents of the file |
| 15 | + file_contents <- readLines(file) |
| 16 | + |
| 17 | + # Check if the file contains the string "(.mailto:" |
| 18 | + if (any(grepl("\\(\\.mailto:", file_contents))) { |
| 19 | + # Add the file name to the vector |
| 20 | + matching_files <- c(matching_files, file) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + # Iterate over the matching files |
| 25 | + for (file in matching_files) { |
| 26 | + # Read the contents of the file |
| 27 | + file_contents <- readLines(file) |
| 28 | + |
| 29 | + # Remove the "." from each line that contains the string |
| 30 | + modified_contents <- gsub("\\(\\.mailto:", "(mailto:", file_contents) |
| 31 | + |
| 32 | + # Write the modified contents back to the file |
| 33 | + writeLines(modified_contents, file) |
| 34 | + |
| 35 | + # Print a message indicating the modification has been made |
| 36 | + message("Modified file:", file, "\n") |
| 37 | + } |
| 38 | + |
| 39 | + # Print the list of matching files |
| 40 | + message("Matching files:", matching_files, "\n") |
| 41 | +} |
| 42 | + |
| 43 | +# get all qmd files |
| 44 | +all_qmd <- list.files(full.names = FALSE, all.files = FALSE, pattern = ".qmd$", recursive = TRUE) |
| 45 | + |
| 46 | +# modify if needed the link to email problem for link checker |
| 47 | +modify_files(all_qmd) |
| 48 | +# get filenames ending with .md |
| 49 | +all_md <- gsub(".qmd$", ".md", all_qmd) |
| 50 | +# rename all files |
| 51 | +file.rename(all_qmd, all_md) |
0 commit comments