Created
August 29, 2024 22:50
-
-
Save elipousson/0978ed9413a15810462e5ea296766a38 to your computer and use it in GitHub Desktop.
R function for reading a subset of front-matter keys for one or more Qmd documents into a table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' Use lightparser::split_to_tbl to read YAML keys from one or more Quarto | |
| #' document | |
| #' | |
| #' Created 2024-08-29 to reconcile the file names and schedule of the slides, | |
| #' week overview pages, and exercises. | |
| read_qmd_params <- function(path, | |
| ..., | |
| recurse = FALSE, | |
| keys = c("order", | |
| "title", | |
| "subtitle", | |
| "date", | |
| "date-due", | |
| "date-modified", | |
| "image"), | |
| regex = "qmd$", | |
| perl = TRUE) { | |
| files <- path | |
| if (fs::is_dir(path)) { | |
| files <- fs::dir_ls( | |
| path, | |
| regexp = paste0("^", path ,"/_"), | |
| invert = TRUE, | |
| type = "file" | |
| ) | |
| files <- stringr::str_subset( | |
| files, | |
| pattern = "^_", | |
| negate = TRUE | |
| ) | |
| } | |
| files <- rlang::set_names(files, files) | |
| file_tbl <- purrr::map( | |
| files, | |
| \(x) { | |
| tbl <- withCallingHandlers( | |
| lightparser::split_to_tbl(x), | |
| error = NULL | |
| ) | |
| if (is.null(tbl)) { | |
| return(NULL) | |
| } | |
| params <- tbl |> | |
| dplyr::filter(type == "yaml") |> | |
| dplyr::pull(params) | |
| # Select the frontmatter | |
| params <- params[[1]][keys] | |
| as.data.frame(vctrs::list_drop_empty(params)) | |
| } | |
| ) | |
| file_tbl |> | |
| purrr::list_rbind( | |
| names_to = "path" | |
| ) |> | |
| dplyr::mutate( | |
| filename = fs::path_ext(path), | |
| .after = dplyr::all_of("path") | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment