-
-
Save hadley/d1bbd44894a99a2e1d1b to your computer and use it in GitHub Desktop.
Convert Rd to roxygen
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
# Parse input Rd file ------------------------------------------------------- | |
parse_file <- fuction(path) { | |
rd <- tools::parse_Rd(path) | |
tags <- sapply(rd, tag) | |
tags <- gsub("\\\\", "", tags) | |
names(rd) <- tags | |
# Remove top-level text strings - just line breaks between sections | |
rd <- rd[tags != "TEXT"] | |
out <- list() | |
# Title, description, value and examples, need to be stitched into a | |
# single string. | |
out$title <- reconstruct(untag(rd$title)) | |
out$desc <- gsub("$\n+|\n+^", "", reconstruct(untag(rd$description))) | |
out$details <- reconstruct(untag(rd$details)) | |
out$value <- reconstruct(untag(rd$value)) | |
out$examples <- reconstruct(untag(rd$examples)) | |
# Join together aliases and keywords | |
out$name <- reconstruct(rd$name) | |
out$aliases <- unname(sapply(rd[names(rd) == "alias"], "[[", 1)) | |
# If the only alias is the name, then skip it | |
if (identical(aliases, name)) { | |
aliases <- NULL | |
} | |
out$keywords <- unname(sapply(rd[names(rd) == "keyword"], "[[", 1)) | |
# Pull apart arguments | |
arguments <- rd$arguments | |
arguments <- arguments[sapply(arguments, tag) != "TEXT"] | |
out$params <- sapply(arguments, function(argument) { | |
paste(argument[[1]], reconstruct(argument[[2]])) | |
}) | |
out | |
} | |
# Create output -------------------------------------------------------------- | |
create_roxygen <- function(info) { | |
c( | |
comment_line(info$title), | |
comment_line(info$desc), | |
comment_line(), | |
comment_line(info$details), | |
comment_line(), | |
comment_tag("@param", info$params), | |
comment_tag("@value", info$value), | |
comment_tag("@keywords", paste(info$keywords, collapse = " ")), | |
comment_tag("@aliases", paste(info$aliases, collapse = " ")), | |
if (!is.null(info$examples)) { | |
c( | |
comment_line("@examples\n"), | |
paste("#' ", gsub("\n", "\n#' ", info$examples), sep = "") | |
) | |
}, | |
"\n" | |
) | |
} | |
parse_and_save <- function(path) { | |
parsed <- parse_file(path) | |
roxygen <- create_roxygen(parsed) | |
cat(paste(output, collapse = "\n")) | |
} |
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
tag <- function(x) attr(x, "Rd_tag") | |
untag <- function(x) { | |
attr(x, "Rd_tag") <- "TEXT" | |
x | |
} | |
reconstruct <- function(rd) { | |
if (is.null(rd)) return() | |
if (is.list(rd)) { | |
special <- tag(rd) == toupper(tag(rd)) | |
prefix <- ifelse(special, "", paste(tag(rd), "{", sep = "")) | |
suffix <- ifelse(special, "", "}") | |
paste(prefix, paste(sapply(rd, reconstruct), collapse = ""), suffix, | |
sep = "") | |
} else { | |
rd | |
} | |
} | |
comment_line <- function(x, exdent = 0) { | |
if (missing(x)) return("#' ") | |
strwrap(x, width = 80, exdent = exdent, prefix = "#' ") | |
} | |
comment_tag <- function(tag, value) { | |
if (is.null(value) || value == "" || length(value) == 0) return() | |
comment_line(paste(tag, value), exdent = 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment