Skip to content

Commit

Permalink
Merge pull request #142 from ropensci/roxy_block
Browse files Browse the repository at this point in the history
simplify, parse documentation with roxygen2::parse_file()
  • Loading branch information
slager committed May 1, 2024
2 parents 1364e72 + bfafc4f commit b4c05db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 51 deletions.
54 changes: 21 additions & 33 deletions R/parseDocumentation.R
@@ -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)
}
18 changes: 0 additions & 18 deletions R/roxygen2_functions.R

This file was deleted.

0 comments on commit b4c05db

Please sign in to comment.