-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify, parse with roxygen2::parse_file()
- Loading branch information
Showing
2 changed files
with
21 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,24 @@ | ||
.doc_parse <- function(all_r_files) { | ||
sources <- lapply(all_r_files, function(x) parse(x, keep.source = TRUE)) | ||
docs <- lapply(sources, function(x) try(comments(utils::getSrcref(x)))) | ||
docs <- lapply(docs, function(x) lapply(x, as.character)) | ||
indx <- lapply( | ||
lapply( | ||
docs, | ||
function(x) { | ||
lapply( | ||
x, | ||
function(y) { | ||
sum(grepl("#'", y)) | ||
} | ||
) | ||
} | ||
), | ||
function(x) { | ||
unlist(x, use.names = FALSE) > 0 | ||
} | ||
lst <- lapply(all_r_files, | ||
function(file){ | ||
file_lines <- readLines(file) | ||
roxy_blocks <- roxygen2::parse_file(file, env = NULL) | ||
roxy_block_list <- lapply(roxy_blocks, function(roxy_block){ | ||
# length(deparse(...) - 1 is to keep call intact if it's > 1 line | ||
# but 1 line only is the typical case (e.g. NULL, '_PACKAGE') | ||
end_line <- roxy_block$line + length(deparse(roxy_block$call)) - 1L | ||
roxy_lines <- vapply(roxy_block$tag, | ||
function(tag) as.integer(tag$line), 1L) | ||
roxy_line_seq <- seq(from = min(roxy_lines), to = end_line) | ||
out_lines <- file_lines[roxy_line_seq] | ||
# 2 line breaks between roxygen2 sections in final file write | ||
c(out_lines, rep("", 2)) | ||
}) | ||
names(roxy_block_list) <- vapply(roxy_blocks, | ||
roxygen2::block_get_tag_value, | ||
"", tag = 'name') | ||
roxy_block_list | ||
} | ||
) | ||
docs <- lapply(seq_along(docs), function(j) { | ||
docs[[j]][indx[[j]]] | ||
}) | ||
# Extract @name | ||
doc_names <- lapply(docs, function(x) { | ||
unlist(lapply(x, function(y) { | ||
gsub(".+@name (\\D+)", "\\1", { | ||
y[grepl("@name", y)] | ||
}) | ||
}), use.names = FALSE) | ||
}) | ||
docs <- unlist(docs, recursive = FALSE) | ||
docnames <- unlist(doc_names, recursive = FALSE) | ||
names(docs) <- gsub(" ", "", docnames) | ||
docs | ||
Reduce(c, lst) | ||
} |
This file was deleted.
Oops, something went wrong.