-
Notifications
You must be signed in to change notification settings - Fork 34
/
_utils.R
75 lines (63 loc) · 2.08 KB
/
_utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Utilities that can be ran manually
# Convert RTF to PDF
r2rtf:::rtf_convert_format(
list.files("tlf", pattern = ".rtf", full.names = TRUE),
output_dir = "tlf"
)
#' Flatten copy
#'
#' @param from Source directory path.
#' @param to Destination directory path.
#'
#' @return Destination directory path.
#'
#' @details
#' Copy all `.Rmd`, `.qmd`, and `.md` files from source to destination,
#' rename the `.qmd` and `.md` files with an additional `.Rmd` extension,
#' and get a flat destination structure with path-preserving file names.
flatten_copy <- function(from, to) {
rmd <- list.files(from, pattern = "\\.Rmd$", recursive = TRUE, full.names = TRUE)
xmd <- list.files(from, pattern = "\\.qmd$|\\.md$", recursive = TRUE, full.names = TRUE)
src <- c(rmd, xmd)
dst <- c(rmd, paste0(xmd, ".Rmd"))
# Remove starting `./` (if any)
dst <- gsub("^\\./", replacement = "", x = dst)
# Replace the forward slash in path with Unicode big solidus
dst <- gsub("/", replacement = "\u29F8", x = dst)
file.copy(src, to = file.path(to, dst))
invisible(to)
}
#' Check URLs in an R Markdown or Quarto project
#'
#' @param input Path to the project directory.
#'
#' @return URL checking results from `urlchecker::url_check()`
#' for all `.Rmd`, `.qmd`, and `.md` files in the project.
#'
#' @details
#' The `tools::pkgVignettes()$docs` call in urlchecker requires
#' two core criteria (`VignetteBuilder` and `VignetteEngine`)
#' to recognize `.Rmd` files as package vignettes.
check_url <- function(input = ".") {
# Create a source package directory
pkg <- tempfile()
dir.create(pkg)
# Flatten copy relevant files
vig <- file.path(pkg, "vignettes")
dir.create(vig)
flatten_copy(input, vig)
# Create a minimal DESCRIPTION file
write("VignetteBuilder: knitr", file = file.path(pkg, "DESCRIPTION"))
# Make the copied files look like vignettes
lapply(
list.files(vig, full.names = TRUE),
function(x) {
write(
"---\nvignette: >\n %\\VignetteEngine{knitr::rmarkdown}\n---",
file = x, append = TRUE
)
}
)
urlchecker::url_check(pkg)
}
check_url()