11# !/usr/bin/env Rscript
22
3- # Set vars and constants
3+ # -------------------------------------------------------------
4+ # Define constants for start and end tags for the dropdown menu
5+ # -------------------------------------------------------------
6+ # Mark the beginning of the dropdown menu for versions in the HTML file
47start_tag <- " <!-- start dropdown for versions -->"
8+ # Mark the end of the dropdown menu for versions in the HTML file
59end_tag <- " <!-- end dropdown for versions -->"
610
11+ # -----------------------------------------------------
12+ # Extract the repository name from environment variables
13+ # -----------------------------------------------------
14+ # Extracts the repository name by removing the owner's name from the full repository path
715repo_name <-
816 gsub(
917 paste0(Sys.getenv(" GITHUB_REPOSITORY_OWNER" ), " /" ),
1018 " " ,
1119 Sys.getenv(" GITHUB_REPOSITORY" )
1220 )
21+
22+ # Construct the base URL for the repository by combining the owner's name, the repository name, and the GitHub Pages domain
1323base_url <- paste0(
1424 " https://" ,
1525 Sys.getenv(" GITHUB_REPOSITORY_OWNER" ),
1626 " .github.io/" ,
1727 repo_name
1828)
29+
30+ # Define the name of the search index file
1931search_index_file <- " search.json"
2032
33+ # ---------------------------------------------------
34+ # Define a function to handle missing references list
35+ # ---------------------------------------------------
36+ # This function takes a list of references as input and returns a regular expression pattern to match them
37+ # If the input is empty, it returns a default pattern that matches any string except those ending with ".git"
2138handle_missing_refs_list <- function (refs_to_list ) {
2239 if (refs_to_list == " " ) {
2340 return (" ^(?!\\ .git$).+$" )
2441 }
2542 return (refs_to_list )
2643}
2744
28-
45+ # -------------------------------------------------------------
46+ # Define a function to filter versions based on a given pattern
47+ # -------------------------------------------------------------
2948filter_versions <- function (refs_to_list = paste(
3049 " ^main$" ,
3150 " ^devel$" ,
@@ -34,7 +53,7 @@ filter_versions <- function(refs_to_list = paste(
3453 " ^v([0-9]+\\ .)?([0-9]+\\ .)?([0-9]+)$" ,
3554 sep = " |"
3655 )) {
37- # List and sort versions
56+ # List all directories in the current path, sorts them in descending order, and stores them in the ' versions' vector
3857 versions <- sort(
3958 list.dirs(
4059 path = " ." ,
@@ -44,7 +63,7 @@ filter_versions <- function(refs_to_list = paste(
4463 decreasing = TRUE
4564 )
4665
47- # Filter versions according to refs_to_list
66+ # Filter the ' versions' vector based on the pattern provided in ' refs_to_list' and returns the filtered vector
4867 return (versions [
4968 grep(
5069 handle_missing_refs_list(refs_to_list ),
@@ -54,6 +73,9 @@ filter_versions <- function(refs_to_list = paste(
5473 ])
5574}
5675
76+ # -----------------------------------------------------------
77+ # Define a function to update search indexes for each version
78+ # -----------------------------------------------------------
5779update_search_indexes <- function (refs_to_list = paste(
5880 " ^main$" ,
5981 " ^devel$" ,
@@ -62,29 +84,40 @@ update_search_indexes <- function(refs_to_list = paste(
6284 " ^v([0-9]+\\ .)?([0-9]+\\ .)?([0-9]+)$" ,
6385 sep = " |"
6486 )) {
65- # Filter versions according to refs_to_list
87+ # Filter the versions based on the provided pattern
6688 versions <- filter_versions(refs_to_list )
6789
90+ # Save the current working directory
6891 oldwd <- getwd()
92+
6993 for (version in versions ) {
70- # Navigate to the ref
94+ # Change the working directory to the current version directory
7195 setwd(version )
96+
7297 # Update the indexes
7398 if (file.exists(search_index_file )) {
99+ # Read the contents of the search index file into a vector
74100 search_index <- readLines(search_index_file )
101+
102+ # Update the search index by replacing the base URL with the version-specific URL
75103 updated_index <- gsub(
76104 paste0(base_url , " (?!/" , version , " )" ),
77105 paste0(base_url , " /" , version ),
78106 search_index ,
79107 perl = TRUE
80108 )
109+
110+ # Write the updated search index back to the file
81111 writeLines(updated_index , search_index_file )
82112 }
83- # Reset working directory
113+ # Reset the working directory to the original directory
84114 setwd(oldwd )
85115 }
86116}
87117
118+ # -------------------------------------------------------------
119+ # Define a function to prepare the dropdown button for versions
120+ # -------------------------------------------------------------
88121prepare_dropdown_button <- function (
89122 refs_to_list = paste(
90123 " ^main$" ,
@@ -101,29 +134,32 @@ prepare_dropdown_button <- function(
101134 " latest-tag"
102135 ),
103136 version_tab = " " ) {
137+ # Evaluate the version tab configuration string into a list
104138 conf <- eval(parse(text = version_tab ))
105139
106140 # Filter versions according to refs_to_list
107141 versions <- filter_versions(refs_to_list )
142+
143+ # Initialize an empty vector to store the ordered versions
108144 output <- c()
109145
110- # Append versions to output vector according to
111- # the order in refs_order
146+ # Iterate over the ordered references and appends the matching versions to the 'output' vector
112147 for (ref in refs_order ) {
113148 result <- versions [grep(ref , versions )]
114149 if (! identical(result , character (0 ))) {
115150 output <- c(output , result )
116151 }
117152 }
118153
154+ # Filter out versions that do not match any of the ordered references
119155 other_versions <-
120156 versions [! grepl(
121157 paste0(refs_order , collapse = " |" ),
122158 versions
123159 )]
124160
125161 # Append versions other than ones in refs_order
126- # at the bottom of drop-down list.
162+ # at the bottom of drop-down list
127163 # Sorting is done according to the number of characters:
128164 # E.g. v0.1.1 should not be before v0.1.10
129165 versions <- c(
@@ -133,8 +169,10 @@ prepare_dropdown_button <- function(
133169 other_versions
134170 )])
135171 )
172+
136173 print(paste0(" Version order in drop-down: " , versions ))
137174
175+ # Construct the text for each version based on the configuration
138176 text <- sapply(
139177 versions ,
140178 FUN = function (x ) {
@@ -148,6 +186,7 @@ prepare_dropdown_button <- function(
148186 simplify = TRUE
149187 )
150188
189+ # Construct the tooltip for each version based on the configuration
151190 tooltip <- sapply(
152191 versions ,
153192 FUN = function (x ) {
@@ -161,6 +200,7 @@ prepare_dropdown_button <- function(
161200 simplify = TRUE
162201 )
163202
203+ # Construct the HTML for each version in the dropdown menu
164204 menu_items <- paste0(
165205 ' <a class="dropdown-item" data-toggle="tooltip" title="' ,
166206 tooltip ,
@@ -173,7 +213,8 @@ prepare_dropdown_button <- function(
173213 " </a>" ,
174214 collapse = " \n "
175215 )
176- # Generate element
216+
217+ # Construct the complete HTML for the dropdown menu
177218 button <- paste(
178219 paste(
179220 start_tag ,
@@ -188,9 +229,13 @@ prepare_dropdown_button <- function(
188229 menu_items ,
189230 paste(" </div></li>" , end_tag , sep = " \n " )
190231 )
232+
191233 return (button )
192234}
193235
236+ # ------------------------------------------------------------
237+ # Define a function to update content with the dropdown button
238+ # ------------------------------------------------------------
194239update_content <- function (
195240 refs_to_list = paste(
196241 " ^main$" ,
@@ -208,9 +253,11 @@ update_content <- function(
208253 " latest-tag"
209254 ),
210255 version_tab = " " ) {
256+ # Prepare the dropdown button based on the provided parameters
211257 dropdown_button <-
212258 prepare_dropdown_button(handle_missing_refs_list(refs_to_list ), refs_order , version_tab )
213259
260+ # List all HTML files in the current directory and its subdirectories
214261 html_files <- list.files(
215262 path = " ." ,
216263 pattern = " .html$" ,
@@ -219,30 +266,54 @@ update_content <- function(
219266 full.names = TRUE
220267 )
221268
269+ # Construct the string to search for in the HTML files to insert the dropdown button after
222270 insert_after <-
223271 paste0(' index.html">' , insert_after_section , " </a>" )
224272
273+ # Adjust the insert line location based on the version of pkgdown
274+ pkgdown_version <- as.character(packageVersion(" pkgdown" ))
275+ if (compareVersion(pkgdown_version , " 2.1.0" ) > = 0 ) {
276+ insert_after <- paste0(insert_after , " </li>" )
277+ }
278+
279+ # Iterate over each HTML file, updates the content by inserting the
280+ # dropdown button, and writes the updated content back to the file
225281 for (f in html_files ) {
282+ # Read the contents of each HTML file into a vector
226283 html_content <- readLines(f )
284+
227285 # Replace previous instances
228286 drowdown_start_line <-
229287 grep(pattern = start_tag , x = html_content )
288+ # Find the start and end lines of the dropdown button in the HTML content
230289 dropdown_end_line <- grep(pattern = end_tag , x = html_content )
290+
291+ # Remove the previous dropdown button from the HTML content if it exists
231292 if (length(drowdown_start_line ) > 0 &&
232293 length(dropdown_end_line ) > 0 ) {
233294 html_content <-
234295 html_content [- (drowdown_start_line : dropdown_end_line )]
235296 }
297+
298+ # Find the line number where the dropdown button should be inserted
236299 insert_line <-
237300 grep(pattern = insert_after , x = html_content ) + 1
238- if (length(insert_line > 0 )) {
301+ if (compareVersion(pkgdown_version , " 2.1.0" ) > = 0 ) {
302+ insert_line <- insert_line - 1
303+ }
304+
305+ # Check if the insert line is found
306+ if (length(insert_line ) > 0 ) {
307+ # Insert the dropdown button into the HTML content
239308 html_content <- c(
240309 html_content [1 : insert_line ],
241310 dropdown_button ,
242311 html_content [(insert_line + 1 ): length(html_content )]
243312 )
313+ # Write the updated HTML content back to the file
244314 writeLines(html_content , f )
245315 } else {
316+ # If the insert line is not found, print a message
246317 message(paste(
247318 f ,
248319 " : Could not find the" ,
@@ -251,5 +322,7 @@ update_content <- function(
251322 ))
252323 }
253324 }
325+
326+ # Update the search indexes for each version
254327 update_search_indexes(refs_to_list )
255328}
0 commit comments