|
| 1 | +# https://github.com/tidymodels/parsnip/blob/main/R/engine_docs.R |
| 2 | + |
| 3 | +#' Knit engine-specific documentation |
| 4 | +#' @param pattern A regular expression to specify which files to knit. The |
| 5 | +#' default knits all engine documentation files. |
| 6 | +#' @param ... Options passed to [knitr::knit()]. |
| 7 | +#' @return A tibble with column `file` for the file name and `result` (a |
| 8 | +#' character vector that echos the output file name or, when there is |
| 9 | +#' a failure, the error message). |
| 10 | +#' @keywords internal |
| 11 | +#' @export |
| 12 | +knit_engine_docs <- function(pattern = NULL) { |
| 13 | + rmd_files <- list.files("man/rmd", pattern = "\\.Rmd", full.names = TRUE) |
| 14 | + |
| 15 | + if (!is.null(pattern)) { |
| 16 | + target_exists <- grepl(pattern, rmd_files) |
| 17 | + files <- rmd_files[target_exists] |
| 18 | + } else { |
| 19 | + files <- rmd_files[!grepl("(template-)|(setup\\.)|(aaa\\.)", rmd_files)] |
| 20 | + } |
| 21 | + outputs <- gsub("Rmd$", "md", files) |
| 22 | + |
| 23 | + res <- map2(files, outputs, ~ try(knitr::knit(.x, .y), silent = TRUE)) |
| 24 | + is_error <- map_lgl(res, ~ inherits(.x, "try-error")) |
| 25 | + |
| 26 | + if (any(is_error)) { |
| 27 | + # In some cases where there are issues, the md file is empty. |
| 28 | + errors <- res[which(is_error)] |
| 29 | + error_nms <- basename(files)[which(is_error)] |
| 30 | + errors <- |
| 31 | + map_chr(errors, ~ cli::ansi_strip(as.character(.x))) %>% |
| 32 | + map2_chr(error_nms, ~ paste0(.y, ": ", .x)) %>% |
| 33 | + map_chr(~ gsub("Error in .f(.x[[i]], ...) :", "", .x, fixed = TRUE)) |
| 34 | + cat("There were failures duing knitting:\n\n") |
| 35 | + cat(errors) |
| 36 | + cat("\n\n") |
| 37 | + } |
| 38 | + |
| 39 | + res <- map_chr(res, as.character) |
| 40 | + |
| 41 | + issues <- list_md_problems() |
| 42 | + if (nrow(issues) > 0) { |
| 43 | + cat("There are some issues with the help files:\n") |
| 44 | + print(issues) |
| 45 | + } |
| 46 | + |
| 47 | + invisible(tibble::tibble(file = basename(files), result = res)) |
| 48 | +} |
| 49 | + |
| 50 | +#' Locate and show errors/warnings in engine-specific documentation |
| 51 | +#' @return A tibble with column `file` for the file name, `line` indicating |
| 52 | +#' the line where the error/warning occurred, and `problem` showing the |
| 53 | +#' error/warning message. |
| 54 | +#' @keywords internal |
| 55 | +#' @export |
| 56 | +list_md_problems <- function() { |
| 57 | + md_files <- list.files("man/rmd", pattern = "\\.md", full.names = TRUE) |
| 58 | + |
| 59 | + get_errors <- function(file) { |
| 60 | + lines <- readLines(file) |
| 61 | + line <- grep("## (Error|Warning)", lines) |
| 62 | + problem <- lines[line] |
| 63 | + tibble::tibble(basename(file), line, problem) |
| 64 | + } |
| 65 | + |
| 66 | + map(md_files, get_errors) %>% vctrs::vec_rbind() |
| 67 | +} |
0 commit comments