Skip to content

Commit

Permalink
read_all()'s before and after functions can take the file content…
Browse files Browse the repository at this point in the history
… as the second argument
  • Loading branch information
yihui committed Oct 12, 2024
1 parent 4d3e7e8 commit 2ad01bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.48.5
Version: 0.48.6
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- `html_escape()` will not escape double quotes (i.e., convert `"`" to `"`) by default, and the conversion will be done only for `html_escape(attr = TRUE)`.

- The arguments `before` and `after` of `read_all()` can take functions of two arguments now, with the second argument being the content of each file.

- Added an argument `start` to `make_fence()`.

# CHANGES IN xfun VERSION 0.48
Expand Down
17 changes: 11 additions & 6 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ read_bin = function(file, what = 'raw', n = file.info(file)$size, ...) {
#' Read files one by one, and optionally add text before/after the content. Then
#' combine all content into one character vector.
#' @param files A vector of file paths.
#' @param before,after A function that takes one file path as the input and
#' returns values to be added before or after the content of the file.
#' Alternatively, they can be constant values to be added.
#' @param before,after A function that takes one file path and its content as
#' the input and returns values to be added before or after the content of the
#' file. Alternatively, they can be constant values to be added.
#' @return A character vector.
#' @export
#' @examples
Expand All @@ -132,10 +132,15 @@ read_bin = function(file, what = 'raw', n = file.info(file)$size, ...) {
#'
#' # add constants
#' xfun::read_all(fs, before = '/*', after = c('*/', ''))
read_all = function(files, before = function(f) NULL, after = function(f) NULL) {
b = before; a = after
read_all = function(files, before = function(f, x) NULL, after = function(f, x) NULL) {
call_fun = function(fun, x, ...) {
if (is.function(fun)) {
if (length(formals(fun)) > 1) fun(x, ...) else fun(x)
} else fun
}
x = unlist(lapply(files, function(f) {
c(if (is.function(b)) b(f) else b, read_utf8(f), if (is.function(a)) a(f) else a)
t = read_utf8(f)
c(call_fun(before, f, t), t, call_fun(after, f, t))
}))
raw_string(x)
}
Expand Down
8 changes: 4 additions & 4 deletions man/read_all.Rd

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

0 comments on commit 2ad01bd

Please sign in to comment.