diff --git a/R/create_template.R b/R/create_template.R index f8c61c37..bbd896c6 100644 --- a/R/create_template.R +++ b/R/create_template.R @@ -586,40 +586,7 @@ create_template <- function( } } # close check for previous files & respective copying - # Handle legacy document order and migration - # TODO: Not sure how setting this to rerender = FALSE always impacts this feature - # Maybe this should go through deprecation in x amnt of time? - fig_info <- migrate_legacy_docs(subdir, doc_type = "figures", rerender_skeleton = FALSE) - tbl_info <- migrate_legacy_docs(subdir, doc_type = "tables", rerender_skeleton = FALSE) - - renamed_tables_doc <- FALSE - if (can_rename_legacy_doc(tbl_info)) { - from <- fs::path(subdir, tbl_info$legacy_name) - to <- fs::path(subdir, tbl_info$current_name) - if (!identical(from, to) && file.exists(from)) { - renamed_tables_doc <- file.rename(from = from, to = to) - } - } - - renamed_figures_doc <- FALSE - if (can_rename_legacy_doc(fig_info)) { - from <- fs::path(subdir, fig_info$legacy_name) - to <- fs::path(subdir, fig_info$current_name) - if (!identical(from, to) && file.exists(from)) { - renamed_figures_doc <- file.rename(from = from, to = to) - } - } - - if (renamed_figures_doc || renamed_tables_doc) { - renamed_docs <- c( - if (renamed_figures_doc) paste0("{.file ", fig_info$current_name, "}"), - if (renamed_tables_doc) paste0("{.file ", tbl_info$current_name, "}") - ) - cli::cli_alert_info("Detected legacy figure/table document order in the skeleton.") - cli::cli_alert_info("asar switched to {toString(renamed_docs)}.") - } - - # Created tables doc + # Create tables doc tables_doc_name <- switch( type, "nemt" = "06_tables.qmd", @@ -651,6 +618,14 @@ create_template <- function( to = fs::path(subdir, figures_doc_name) ) } + + # rename tables doc + if (tables_doc_name != "09_tables.qmd") { + file.rename( + from = fs::path(subdir, "09_tables.qmd"), + to = fs::path(subdir, tables_doc_name) + ) + } # Part I # Create a report template file to render for the region and species diff --git a/R/rerender_skeleton.R b/R/rerender_skeleton.R index 371188b4..99f278db 100644 --- a/R/rerender_skeleton.R +++ b/R/rerender_skeleton.R @@ -531,4 +531,35 @@ rerender_skeleton <- function( } # Print message cli::cli_alert_success("Updated report skeleton in directory {file_dir}.") + + + # Handle legacy document order and migration + fig_doc <- list.files(file_dir, pattern = "figures.qmd") + tbl_doc <- list.files(file_dir, pattern = "tables.qmd") + + # if the number preceding the figures.qmd is higher than the number preceding the tables.qmd, then rename the files to match the new order + fig_num <- as.numeric(stringr::str_extract(fig_doc, "(?<=^)[0-9]+")) + tbl_num <- as.numeric(stringr::str_extract(tbl_doc, "(?<=^)[0-9]+")) + + if (!is.na(fig_num) && !is.na(tbl_num) && fig_num > tbl_num) { + cli::cli_alert_info("Detected legacy figure/table document order in the skeleton.") + + # Rename the files to match the new order + file.rename( + from = fs::path(file_dir, fig_doc), + to = fs::path(file_dir, + paste0(stringi::stri_pad_left(tbl_num, 2, "0"), + "_figures.qmd") + ) + ) + file.rename( + from = fs::path(file_dir, tbl_doc), + to = fs::path(file_dir, + paste0(stringi::stri_pad_left(fig_num, 2, "0"), + "_tables.qmd") + ) + ) + cli::cli_alert_success("Changed order to match the new skeleton.") + + } }