|
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 |
| 1 | +#' Fixes link-check problem |
| 2 | +#' - Finds files that use the string "(.mailto:" and corrects to "(mailto:" |
| 3 | +#' - Updates links to `.qmd` files to point to `.md` files. |
4 | 4 | #' |
5 | 5 | #' @param file_list vector of filenames |
6 | 6 | #' |
7 | | -#' @return messages only. side effect is: changes files. |
| 7 | +#' @return messages. Side effect: modifies files. |
8 | 8 | modify_files <- function(file_list) { |
9 | | - # Create an empty vector to store the file names that contain the string |
| 9 | + # Create an empty vector to store file names that need modifications |
10 | 10 | matching_files <- c() |
11 | 11 |
|
12 | | - # Iterate over each file in the directory |
| 12 | + # Iterate over each file to check for issues |
13 | 13 | for (file in file_list) { |
14 | 14 | # Read the contents of the file |
15 | 15 | file_contents <- readLines(file) |
16 | 16 |
|
17 | | - # Check if the file contains the string "(.mailto:" |
18 | | - if (any(grepl("\\(\\.mailto:", file_contents))) { |
| 17 | + # Check if the file contains the string "(.mailto:" OR references to `.qmd` files |
| 18 | + if (any(grepl("\\(\\.mailto:", file_contents)) || any(grepl("\\.qmd\\)", file_contents))) { |
19 | 19 | # Add the file name to the vector |
20 | 20 | matching_files <- c(matching_files, file) |
21 | 21 | } |
22 | 22 | } |
23 | 23 |
|
24 | | - # Iterate over the matching files |
| 24 | + # Iterate over the matching files to apply fixes |
25 | 25 | for (file in matching_files) { |
26 | 26 | # Read the contents of the file |
27 | 27 | file_contents <- readLines(file) |
28 | 28 |
|
29 | | - # Remove the "." from each line that contains the string |
| 29 | + # Fix email links |
30 | 30 | modified_contents <- gsub("\\(\\.mailto:", "(mailto:", file_contents) |
31 | 31 |
|
| 32 | + # Update links to `.qmd` files to point to `.md` files |
| 33 | + # Matches patterns like `[text](filename.qmd)` and replaces `.qmd` with `.md` |
| 34 | + modified_contents <- gsub("\\.qmd\\)", ".md)", modified_contents) |
| 35 | + |
32 | 36 | # Write the modified contents back to the file |
33 | 37 | writeLines(modified_contents, file) |
34 | 38 |
|
35 | | - # Print a message indicating the modification has been made |
| 39 | + # Print a message indicating what modifications have been made |
36 | 40 | message("Modified file:", file, "\n") |
37 | 41 | } |
38 | 42 |
|
39 | | - # Print the list of matching files |
| 43 | + # Print a list of matching files that were modified |
40 | 44 | message("Matching files:", matching_files, "\n") |
41 | 45 | } |
42 | 46 |
|
43 | | -# get all qmd files |
| 47 | +# Get all `.qmd` files |
44 | 48 | all_qmd <- list.files(full.names = FALSE, all.files = FALSE, pattern = ".qmd$", recursive = TRUE) |
45 | 49 |
|
46 | | -# modify if needed the link to email problem for link checker |
| 50 | +# Modify files to fix email links and update `.qmd` references |
47 | 51 | modify_files(all_qmd) |
48 | | -# get filenames ending with .md |
| 52 | + |
| 53 | +# Generate a list of `.md` filenames that will replace `.qmd` files |
49 | 54 | all_md <- gsub(".qmd$", ".md", all_qmd) |
50 | | -# rename all files |
| 55 | + |
| 56 | +# Rename all `.qmd` files to `.md` |
51 | 57 | file.rename(all_qmd, all_md) |
0 commit comments