Skip to content

Commit

Permalink
Merge pull request #27 from NIFU-NO/minor_fixes_for_cran_release
Browse files Browse the repository at this point in the history
Minor fixes for cran release
  • Loading branch information
sda030 authored Aug 23, 2024
2 parents 601162a + 18cf903 commit b63df30
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(combn_upto)
export(copy_folder_contents_to_dir)
export(create_directory_structure)
export(create_email_credentials)
export(create_mesos_qmd_files)
export(create_r_files)
export(download_zip_to_folder)
export(generate_yaml_from_directory)
Expand All @@ -14,6 +15,7 @@ export(initialize_saros_project)
export(omitted_recoder_df)
export(post_render_docx_img_replacer)
export(recode_checkbox_sets)
export(remove_reports_from_sidebars)
export(remove_special_chars_in_labels)
export(rename_by_labels)
export(replace_docx_imgs_with_mscharts)
Expand Down
51 changes: 51 additions & 0 deletions R/create_mesos_qmd_files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Create QMD-files for the mesos groups in a mesos variable
#'
#' @param dir_path String, path to folder where the reference qmd-files are stored
#' (and where in which the new mesos folders will be created)
#' @param mesos_var String, inserted into qmd-files as `params$mesos_var`
#' @param mesos_groups Character vector of mesos groups.
#'
#' @return Data frame with with names.
#' @export
#'
#' @examples
#' writeLines(c(""), con = fs::path(tempdir(), "index.qmd"))
#' create_mesos_qmd_files(dir_path=tempdir(),
#' mesos_var = "f_uni", mesos_groups = paste0("Uni of ", LETTERS[1:5]))
create_mesos_qmd_files <- function(
dir_path,
mesos_var,
mesos_groups) {

new_qmd_files <-
fs::dir_ls(path = dir_path, regexp = "\\.qmd", recurse = FALSE)
if(length(new_qmd_files)==0) {
cli::cli_abort("No files found.")
}
new_qmd_files <-
new_qmd_files |>
stringi::stri_replace_last_fixed(pattern = ".qmd", replacement = "") |>
basename() |>
tidyr::expand_grid(main_file = _, mesos_group = mesos_groups) |>
dplyr::mutate(main_file_no_ = stringi::stri_replace_first_regex(.data$main_file, pattern="^_", replacement = ""),
new_file_path = fs::path(.env$dir_path, .data$mesos_group, paste0(.data$main_file_no_, ".qmd")),
) |>
dplyr::rowwise() |>
dplyr::mutate(contents = {
yaml <- list(params = list(mesos_var = .env$mesos_var,
mesos_group = .data$mesos_group))
if(.data$main_file_no_ %in% c("index", "0_report")) yaml$title <- paste0(.data$mesos_group)
yaml <- yaml::as.yaml(x = yaml)
paste0("---\n", yaml, "---\n",
paste0("\n{{< include ../", .data$main_file, ".qmd >}}\n"),
sep="\n")
})

fs::dir_create(path = dirname(new_qmd_files$new_file_path))

for(i in seq_len(nrow(new_qmd_files))) {
cat(new_qmd_files[i, "contents", drop=TRUE],
file = new_qmd_files[i, "new_file_path", drop=TRUE])
}
new_qmd_files
}
30 changes: 30 additions & 0 deletions R/remove_reports_from_sidebars.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Remove Reports from Sidebars
#'
#' Finds HTML-files recursively in a path, and removes all entries in the sidebar
#' to certain filenames, such as "0_report.pdf"
#'
#'
#' @param path Folder path as string.
#' @param filename_as_regex Character vector of regex filenames to look for (i.e.,
#' the dot separating file extension must be escaped as \\.).
#'
#' @return List of files processed.
#' @export
#'
#' @examples remove_reports_from_sidebars(path = tempdir())
remove_reports_from_sidebars <- function(path = "_site",
filename_as_regex = c("0_report\\.pdf", "0_report\\.docx")) {
fs::dir_ls(path = path,
all = FALSE, recurse = TRUE, type = "file", regexp = "\\.html") |>
lapply(FUN = function(.x) {
readLines(.x) |>
paste0(collapse="\n") |>
stringi::stri_replace_all_regex(
pattern = paste0("<li class=\"sidebar-item\">\\s*<div class=\"sidebar-item-container\">\\s*<a href=\"[^\"]*", filename_as_regex, "\"[^>]*>\\s*<span class=\"menu-text\">[^<]*</span></a>\\s*</div>\\s*</li>"),
replacement = "") |>
stringi::stri_split_lines() |>
writeLines(con = .x)
.x
}) |>
unlist()
}
27 changes: 27 additions & 0 deletions man/create_mesos_qmd_files.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/remove_reports_from_sidebars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b63df30

Please sign in to comment.