Skip to content

Commit

Permalink
Merge pull request #140 from NIFU-NO/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
sda030 authored Jan 9, 2025
2 parents cd9f781 + 5e2752b commit 5e3dc16
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 173 deletions.
8 changes: 4 additions & 4 deletions R/download_zip_to_folder.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ download_zip_to_folder <-
zip_temp_path <- zip::unzip(zipfile = zip_path, files = files, exdir = tmpfolder, junkpaths = FALSE, overwrite = TRUE)
folder_in_temp_path <- fs::dir_ls(path = tmpfolder, recurse = FALSE, type = "directory")
new_files <- fs::dir_ls(folder_in_temp_path, recurse = TRUE, type = "file", all = TRUE)
new_files <- gsub(x = new_files, pattern = folder_in_temp_path, replacement = "")
new_files <- gsub(x = new_files, pattern = "^/", replacement = "")
new_files <- stringi::stri_replace_first_fixed(new_files, pattern = folder_in_temp_path, replacement = "")
new_files <- stringi::stri_replace_first_regex(new_files, pattern = "^/", replacement = "")
dir.create(out_path, showWarnings = FALSE, recursive = TRUE)
old_files <- fs::dir_ls(out_path, recurse = TRUE, type = "file", all = TRUE)
old_files <- gsub(x = old_files, pattern = out_path, replacement = "")
old_files <- gsub(x = old_files, pattern = "^/", replacement = "")
old_files <- stringi::stri_replace_first_fixed(old_files, pattern = out_path, replacement = "")
old_files <- stringi::stri_replace_first_regex(old_files, pattern = "^/", replacement = "")
intersects <- new_files[new_files %in% old_files]
if (isTRUE(prompt) && length(intersects) > 0 && isTRUE(overwrite)) {
cli::cli_inform(intersects)
Expand Down
10 changes: 9 additions & 1 deletion R/draft_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
#' To turn off, set `pdf=FALSE`.
#'
#'
#' @param report_includes_files *Whether report.qmd includes \{\{< include chapter.qmd >\}\}*
#'
#' `scalar<logical>` // *default:* `FALSE`
#'
#' Useful to have in mesos reports. However, bear in mind that including other qmd files with conflicting YAML-headers might be risky.
#'
#' @param chapter_qmd_start_section_filepath,chapter_qmd_end_section_filepath,index_qmd_start_section_filepath,index_qmd_end_section_filepath,report_qmd_start_section_filepath,report_qmd_end_section_filepath, *Path to qmd-bit for start/end of each qmd*
#'
#' `scalar<character>` // *default:* `NULL` (`optional`)
Expand Down Expand Up @@ -212,6 +218,7 @@ draft_report <-
report_yaml_file = NULL,
report_qmd_start_section_filepath = NULL,
report_qmd_end_section_filepath = NULL,
report_includes_files = FALSE,
ignore_heading_for_group = c(
".template_name",
".variable_type_dep",
Expand Down Expand Up @@ -281,6 +288,7 @@ draft_report <-
qmd_start_section_filepath = args$report_qmd_start_section_filepath,
qmd_end_section_filepath = args$report_qmd_end_section_filepath,
chapter_structure = args$chapter_structure,
include_files = if (isTRUE(args$report_includes_files)) sort(basename(chapter_filepaths)),
title = args$title,
authors = all_authors,
output_formats = NULL,
Expand All @@ -301,7 +309,7 @@ draft_report <-
title = args$title,
authors = all_authors,
output_formats = if (!is.null(args$report_yaml_file)) find_yaml_formats(args$report_yaml_file),
output_filename = args$report_filename,
output_filename = stringi::stri_replace_first_regex(str = args$report_filename, pattern = "^_", replacement = ""),
call = rlang::caller_env()
)
processed_files <- c(processed_files, index_filepath)
Expand Down
31 changes: 20 additions & 11 deletions R/filename_sanitizer.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,36 @@ filename_sanitizer <- function(x,
valid_obj = FALSE,
to_lower = FALSE,
make_unique = TRUE) {

pattern <- if(isTRUE(accept_hyphen)) "[^[:alnum:]-+]+" else "[^[:alnum:]+]+"
pattern <- if (isTRUE(accept_hyphen)) "[^[:alnum:]-+]+" else "[^[:alnum:]+]+"
out <-
stringi::stri_replace_all_regex(x,
pattern = pattern,
replacement = sep)
if(isTRUE(valid_obj)) {
pattern = pattern,
replacement = sep
)
if (isTRUE(valid_obj)) {
out <-
stringi::stri_replace_all_regex(out, pattern = "^[^[:alpha:]]+", replacement = "")
}

out <- iconv(out, from ="UTF-8", to="ASCII//TRANSLIT", sub='')
out <- iconv(out, from = "UTF-8", to = "ASCII//TRANSLIT", sub = "")

if(isTRUE(to_lower)) out <- tolower(out)
if (isTRUE(to_lower)) out <- tolower(out)

out <- truncator(x=out, max_chars = max_chars)
if(isTRUE(make_unique)) out <- make.unique(out, sep=sep)
out <- truncator(x = out, max_chars = max_chars)
out <- avoid_ending_with_specials(out)
if (isTRUE(make_unique)) out <- make.unique(out, sep = sep)
out
}

truncator <- function(x, max_chars = NA_integer_) {
if(!(is.na(max_chars) || is.null(max_chars)) &&
length(x)>0) stringi::stri_sub(x, from = 1, to = max_chars) else x
if (!(is.na(max_chars) || is.null(max_chars)) &&
length(x) > 0) {
stringi::stri_sub(x, from = 1, to = max_chars)
} else {
x
}
}

avoid_ending_with_specials <- function(x) {
stringi::stri_replace_last_regex(x, pattern = "[^[:alnum:]]+$", replacement = "")
}
122 changes: 68 additions & 54 deletions R/gen_qmd_file.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#' Generate Quarto Index File That Merges All Chapters
#'
#' This function creates an index Quarto file (QMD) that merges all chapters in the specified order. It can also include optional title and author(s) information.
Expand All @@ -18,97 +16,113 @@
#' @return A string containing the filepath to the generated Quarto report file.
#' @keywords internal
gen_qmd_file <-
function(
path = NULL,
filename = "report",
yaml_file = NULL,
qmd_start_section_filepath = NULL,
qmd_end_section_filepath = NULL,
chapter_structure = NULL,
title = NULL,
authors = NULL,
output_formats = NULL,
output_filename = NULL,
call = rlang::caller_env()) {

check_string(path, n = 1, null.ok = TRUE, call = call)
check_string(filename, n = 1, null.ok = TRUE, call = call)
function(path = NULL,
filename = "report",
yaml_file = NULL,
qmd_start_section_filepath = NULL,
qmd_end_section_filepath = NULL,
chapter_structure = NULL,
include_files = NULL,
title = NULL,
authors = NULL,
output_formats = NULL,
output_filename = NULL,
call = rlang::caller_env()) {
check_string(path, n = 1, null.ok = TRUE, call = call)
check_string(filename, n = 1, null.ok = TRUE, call = call)
check_string(yaml_file, n = 1, null.ok = TRUE, call = call)
check_string(qmd_start_section_filepath, n = 1, null.ok = TRUE, call = call)
check_string(qmd_end_section_filepath, n = 1, null.ok = TRUE, call = call)
check_string(title, n = 1, null.ok = TRUE, call = call)
check_string(authors, n = NULL, null.ok = TRUE, call = call)
check_string(output_formats, n = NULL, null.ok = TRUE, call = call)
check_string(output_filename, n = 1, null.ok = TRUE, call = call)
if(is.null(title) && is.null(filename)) {
if (is.null(title) && is.null(filename)) {
cli::cli_abort("{.arg filename} and {.arg title} cannot be both `NULL`.")
}

yaml_section <-
process_yaml(yaml_file = yaml_file,
title = title,
authors = authors[!is.na(authors)])
process_yaml(
yaml_file = yaml_file,
title = title,
authors = authors[!is.na(authors)]
)


report_links <-
if(is.character(output_filename) &&
is.character(output_formats)) {

if (is.character(output_filename) &&
is.character(output_formats)) {
stringi::stri_c(
lapply(output_formats, function(frmt) {
if(frmt == "typst") frmt <- "pdf"
lapply(output_formats, function(frmt) {
if (frmt == "typst") frmt <- "pdf"

stringi::stri_c("-\t[(", toupper(frmt), ")](", output_filename, ".", frmt, ")")
}), collapse="\n")
stringi::stri_c("-\t[(", toupper(frmt), ")](", output_filename, ".", frmt, ")")
}),
collapse = "\n"
)
}

if(inherits(chapter_structure, "data.frame")) {
if (inherits(chapter_structure, "data.frame")) {
chapter_structure_simplified <-
collapse_chapter_structure_to_chr(chapter_structure)
}

qmd_start_section <-
if(!is.null(qmd_start_section_filepath)) {
out <- stringi::stri_c(collapse="\n",
ignore_null=TRUE,
readLines(con = qmd_start_section_filepath)
)
if(inherits(chapter_structure, "data.frame")) {
if (!is.null(qmd_start_section_filepath)) {
out <- stringi::stri_c(
collapse = "\n",
ignore_null = TRUE,
readLines(con = qmd_start_section_filepath)
)
if (inherits(chapter_structure, "data.frame")) {
tryCatch(glue::glue_data(chapter_structure_simplified, out, .na = ""),
error = function(cnd) glue_err(cnd=cnd, arg_name=paste0(filename, "_qmd_start_section")))
} else out
error = function(cnd) glue_err(cnd = cnd, arg_name = paste0(filename, "_qmd_start_section"))
)
} else {
out
}
}
qmd_end_section <-
if(!is.null(qmd_end_section_filepath)) {
if (!is.null(qmd_end_section_filepath)) {
out <-
stringi::stri_c(collapse="\n",
ignore_null=TRUE,
readLines(con = qmd_end_section_filepath)
)
if(inherits(chapter_structure, "data.frame")) {
stringi::stri_c(
collapse = "\n",
ignore_null = TRUE,
readLines(con = qmd_end_section_filepath)
)
if (inherits(chapter_structure, "data.frame")) {
tryCatch(glue::glue_data(chapter_structure_simplified, out, .na = ""),
error = function(cnd) glue_err(cnd=cnd, arg_name=paste0(filename, "_qmd_end_section")))
} else out
error = function(cnd) glue_err(cnd = cnd, arg_name = paste0(filename, "_qmd_end_section"))
)
} else {
out
}
}

if (!is.null(include_files)) {
include_files <-
paste0('{{< include "', include_files, '">}}', collapse = "\n\n")
}

out <-
c(yaml_section, "\n",
c(
yaml_section, "\n",
qmd_start_section,
report_links,
qmd_end_section)
include_files,
qmd_end_section
)
out <- stringi::stri_remove_na(out)
out <- stringi::stri_c(out, collapse="\n", ignore_null=TRUE)
out <- stringi::stri_c(out, collapse = "\n", ignore_null = TRUE)
out <- stringi::stri_replace_all_regex(out,
pattern = "\n{3,}",
replacement = "\n\n\n")
pattern = "\n{3,}",
replacement = "\n\n\n"
)
filepath <-
if(is.character(filename)) {
if (is.character(filename)) {
file.path(path, stringi::stri_c(filename, ".qmd"))
} else if(is.null(filename)) { # Produce unique report file name based on title
} else if (is.null(filename)) { # Produce unique report file name based on title
file.path(path, stringi::stri_c("0_", filename_sanitizer(title), ".qmd", ignore_null = TRUE))
}
cat(out, file = filepath, append = FALSE)
filepath
}

16 changes: 10 additions & 6 deletions R/process_yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ process_yaml <- function(yaml_file = NULL,
)

if (add_fences) {
yaml_section <- stringi::stri_c("---",
yaml_section,
"---",
sep = "\n",
ignore_null = TRUE
)
yaml_section <- add_yaml_fences(yaml_section)
}
yaml_section
}

add_yaml_fences <- function(x) {
stringi::stri_c("---",
x,
"---",
sep = "\n",
ignore_null = TRUE
)
}


find_yaml_formats <- function(yaml_file) {
x <- yaml::read_yaml(file = yaml_file)
Expand Down
Loading

0 comments on commit 5e3dc16

Please sign in to comment.