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