generate_chapters.R (2237B)
1 # This script runs before the website renders, to generate chapter-index files. 2 toc <- readr::read_csv("toc.csv", show_col_types = FALSE) 3 header_template <- yaml::read_yaml("templates/_chapter_header.yml") 4 quarto_index_path <- "_quarto.yml" 5 quarto_index <- yaml::read_yaml(quarto_index_path) 6 7 standard_parts <- list( 8 "index.qmd", 9 list( 10 file = "slides/00.qmd", 11 target = "advr_club-slides" 12 ) 13 ) 14 15 slide_files <- unclass(fs::dir_ls("slides", regexp = "[.](R|q)md$")) 16 names(slide_files) <- fs::path_file(slide_files) |> 17 fs::path_ext_remove() 18 slide_files <- slide_files[names(slide_files) != "00"] 19 20 video_folders <- unclass(fs::dir_ls("videos", type = "directory")) 21 names(video_folders) <- basename(video_folders) 22 23 # Generate index files. 24 purrr::iwalk( 25 slide_files, 26 \(path, number_wide) { 27 this_row <- toc[toc$number_wide == number_wide, ] 28 header <- header_template 29 header$title <- glue::glue_data(this_row, header$title) 30 header$listing[[1]]$contents <- path 31 qmd_body <- "{{< include templates/_slides.qmd >}}" 32 if (number_wide %in% names(video_folders)) { 33 header$listing[[2]]$contents <- video_folders[[number_wide]] 34 qmd_body <- paste( 35 qmd_body, 36 "{{< include templates/_videos.qmd >}}", 37 sep = "\n\n" 38 ) 39 } else { 40 header$listing[[2]] <- NULL 41 } 42 qmd_contents <- paste( 43 "---", 44 yaml::as.yaml(header), 45 "---", 46 qmd_body, 47 sep = "\n" 48 ) 49 cat(qmd_contents, file = glue::glue("{number_wide}.qmd")) 50 } 51 ) 52 53 more_parts <- purrr::map( 54 unique(toc$part), 55 \(this_part) { 56 part_contents <- dplyr::filter(toc, .data$part == this_part) |> 57 dplyr::filter(.data$number_wide %in% names(slide_files)) |> 58 glue::glue_data("{number_wide}.qmd") 59 # Never inline contents. 60 if (length(part_contents) == 1) { 61 part_contents <- list(part_contents) 62 } 63 list( 64 section = this_part, 65 contents = part_contents 66 ) 67 } 68 ) 69 70 logical_handler <- function(x) { 71 to_return <- tolower(x) 72 class(to_return) <- "verbatim" 73 to_return 74 } 75 76 quarto_index$website$sidebar$contents <- c(standard_parts, more_parts) 77 yaml::write_yaml(quarto_index, quarto_index_path, handlers = list(logical = logical_handler))