Skip to content

Commit

Permalink
Add a vectorized wrapper for xfun::relative_path (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored Mar 4, 2021
1 parent ca0145f commit 6ae8ead
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

- Added the same CSS as in default Pandoc's template for when a CSL is used (#1045).

- No more warnings are thrown when passing several input files to `render_book(preview = TRUE)` (#1091).

## MINOR CHANGES

- `anchor_sections = TRUE` becomes the default for `bookdown::gitbook()`.
Expand Down
2 changes: 1 addition & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ render_book = function(
# store output directory and the initial input Rmd name
opts$set(
output_dir = output_dir,
input_rmd = xfun::relative_path(input, dir = "."),
input_rmd = relative_path(input),
preview = preview
)

Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ dir_create = function(path) {
dir_exists(path) || dir.create(path, recursive = TRUE)
}

# vectorized version to get relative path of multiple input
relative_path = function(inputs, dir = ".") {
f = Vectorize(xfun::relative_path, "x", USE.NAMES = FALSE)
f(x = inputs, dir = dir, use.. = TRUE, error = TRUE)
}

# a wrapper of file.path to ignore `output_dir` if it is NULL
output_path = function(...) {
dir = opts$get('output_dir')
Expand Down
5 changes: 5 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,8 @@ assert('fence_theorems() converts the knitr engine syntax to fenced Divs', {
res = fence_theorems(text = old)
(unclass(res) %==% old)
})

assert("relative_path is vectorized", {
relative_path(c('foo/bar.txt', 'foo/baz.txt'), 'foo/') %==% c("bar.txt", "baz.txt")
relative_path('foo/bar.txt', 'foo') %==% "bar.txt"
})

0 comments on commit 6ae8ead

Please sign in to comment.