-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_sport_pages.R
More file actions
58 lines (47 loc) · 1.65 KB
/
build_sport_pages.R
File metadata and controls
58 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# build_sport_pages.R
library(fs)
library(stringr)
library(glue)
# make pretty titles
title_case_smart <- function(x) {
words <- str_split(str_replace_all(x, "[-_]", " "), " ")[[1]]
small_words <- c("and", "or", "of", "the", "in", "with", "for", "a", "an")
words <- ifelse(tolower(words) %in% small_words, tolower(words), str_to_title(words))
words[1] <- str_to_title(words[1]) # Always capitalize the first word
paste(words, collapse = " ")
}
# Step 1: Find all index.qmd files in subfolders like Sport/Module/index.qmd
module_files <- dir_ls(path = ".", recurse = TRUE, glob = "*/*/index.qmd")
module_files <- module_files[!str_detect(
module_files,
regex("(^|/)early[_\\-\\s]?drafts(/|$)", ignore_case = TRUE)
)]
# Step 2: Extract the sport name from the top-level folder
file_df <- data.frame(
file = module_files,
# sport = str_match(module_files, "^.*/(.*?)/.*/index\\.qmd$")[,2],
sport = str_match(module_files, "^([^/]+)/[^/]+/index\\.qmd$")[,2],
stringsAsFactors = FALSE
)
# Step 3: Keep only valid entries
file_df <- file_df[!is.na(file_df$sport), ]
# Step 4: Generate one index.qmd file inside each sport folder
for (sport in unique(file_df$sport)) {
file_path <- glue("{sport}/index.qmd")
pretty_title <- title_case_smart(sport)
page_content <- glue(
'---
title: "{pretty_title}"
listing:
contents:
- "./*/index.qmd"
sort: date desc
image-placeholder: "../_img/default_thumbnail.png"
fields: [image, title, author, date, categories, description]
---
These modules use **{sport}** data to teach topics in statistics and data science.
'
)
writeLines(page_content, file_path)
cat(glue("✓ Created {file_path}\n"))
}