1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
# Reference page ---------------------------------------------------------------
# For testing
usage2text <- function(x) {
rd <- rd_text(paste0("\\usage{", x, "}"), FALSE)[[1]]
strip_html_tags(as_data(rd))
}
#' @export
as_data.tag_usage <- function(x, ...) {
text <- paste(flatten_text(x, ..., escape = FALSE), collapse = "\n")
text <- str_trim(text)
# Look for single line calls to non-syntactic functions (except for `=`
# since that's probably a single argument on its own line) and then use
# deparse1 to convert to standard style. We want to avoid reparsing
# any other lines to avoid losing whitespace, comments etc. (These
# are not generated by roxygen but can be added by the user.)
lines <- strsplit(text, "\n", fixed = TRUE)[[1]]
parsed <- lapply(
lines,
function(x) tryCatch(parse(text = x)[[1]], error = function(e) NULL)
)
needs_tweak <- function(x) {
is_call(x) &&
!is_call(x, "=") &&
(is_symbol(x[[1]]) && !is_syntactic(x[[1]]))
}
to_tweak <- vapply(parsed, needs_tweak, logical(1))
lines[to_tweak] <- vapply(parsed[to_tweak], deparse1, character(1))
text <- paste(lines, collapse = "\n")
highlight_text(text)
}
#' @export
as_html.tag_method <- function(x, ...) method_usage(x, "S3")
#' @export
as_html.tag_S3method <- function(x, ...) method_usage(x, "S3")
#' @export
as_html.tag_S4method <- function(x, ...) method_usage(x, "S4")
method_usage <- function(x, type) {
# Despite these being called from the as_html() generic, the target isn't
# actually HTML, but R code, which is turned into HTML by the syntax
# highlighting in as as_data.tag_usage()
fun <- as_html(x[[1]], escape = FALSE)
class <- as_html(x[[2]], escape = FALSE)
if (x[[2]] == "default") {
method <- sprintf(tr_("# Default %s method"), type)
} else {
method <- sprintf(tr_("# %s method for class '%s'"), type, class)
}
if (!is_syntactic(fun)) {
fun <- paste0("`", fun, "`")
}
paste0(method, "\n", fun)
}
# Reference index --------------------------------------------------------------
topic_funs <- function(rd) {
funs <- parse_usage(rd)
# Remove all methods for generics documented in this file
name <- purrr::map_chr(funs, "name")
type <- purrr::map_chr(funs, "type")
gens <- name[type == "fun"]
self_meth <- (name %in% gens) & (type %in% c("s3", "s4"))
funs <- purrr::map_chr(
funs[!self_meth],
~ short_name(.$name, .$type, .$signature)
)
unique(funs)
}
parse_usage <- function(x) {
if (!inherits(x, "tag")) {
usage <- paste0("\\usage{", x, "}")
x <- rd_text(usage, fragment = FALSE)
}
r <- usage_code(x)
if (length(r) == 0) {
return(list())
}
exprs <- tryCatch(
parse_exprs(r),
error = function(e) {
cli::cli_warn("Failed to parse usage: {.code {r}}")
list()
}
)
purrr::map(exprs, usage_type)
}
short_name <- function(name, type, signature) {
name <- escape_html(name)
qname <- auto_quote(name)
if (type == "data") {
qname
} else if (type == "fun") {
if (is_infix(name)) {
qname
} else {
paste0(qname, "()")
}
} else {
sig <- paste0("<i><", escape_html(signature), "></i>", collapse = ",")
paste0(qname, "(", sig, ")")
}
}
# Given single expression generated from usage_code, extract
usage_type <- function(x) {
if (is_symbol(x)) {
list(type = "data", name = as.character(x))
} else if (is_call(x, "data")) {
list(type = "data", name = as.character(x[[2]]))
} else if (is.call(x)) {
if (identical(x[[1]], quote(`<-`))) {
replacement <- TRUE
x <- x[[2]]
} else {
replacement <- FALSE
}
out <- fun_info(x)
out$replacement <- replacement
out$infix <- is_infix(out$name)
if (replacement) {
out$name <- paste0(out$name, "<-")
}
out
} else {
untype <- paste0(typeof(x), " (in ", as.character(x), ")")
cli::cli_abort(
"Unknown type: {.val {untype}}",
call = caller_env()
)
}
}
is_infix <- function(x) {
if (is.null(x)) {
return(FALSE)
}
x <- as.character(x)
ops <- c(
"+",
"-",
"*",
"^",
"/",
"==",
">",
"<",
"!=",
"<=",
">=",
"&",
"|",
"[[",
"[",
"$"
)
grepl("^%.*%$", x) || x %in% ops
}
fun_info <- function(fun) {
stopifnot(is.call(fun))
if (is.call(fun[[1]])) {
x <- fun[[1]]
if (identical(x[[1]], quote(S3method))) {
list(
type = "s3",
name = as.character(x[[2]]),
signature = as.character(x[[3]])
)
} else if (identical(x[[1]], quote(S4method))) {
list(
type = "s4",
name = as.character(x[[2]]),
signature = sub("^`(.*)`$", "\\1", as.character(as.list(x[[3]])[-1]))
)
} else if (is_call(x, c("::", ":::"))) {
# TRUE if fun has a namespace, pkg::fun()
list(
type = "fun",
name = call_name(fun)
)
} else {
cli::cli_abort(
"Unknown call: {.val {as.character(x[[1]])}}",
call = caller_env()
)
}
} else {
list(
type = "fun",
name = as.character(fun[[1]]),
signature = NULL
)
}
}
# usage_code --------------------------------------------------------------
# Transform Rd embedded inside usage into parseable R code
usage_code <- function(x) {
UseMethod("usage_code")
}
#' @export
usage_code.Rd <- function(x) {
usage <- purrr::detect(x, inherits, "tag_usage")
usage_code(usage)
}
#' @export
usage_code.NULL <- function(x) character()
# Tag without additional class use
#' @export
usage_code.tag <- function(x) {
if (!identical(class(x), "tag")) {
cli::cli_abort(
"Undefined tag in usage: {.val class(x)[[1]]}}",
call = caller_env()
)
}
paste0(purrr::flatten_chr(purrr::map(x, usage_code)), collapse = "")
}
#' @export
usage_code.tag_special <- function(x) {
paste0(purrr::flatten_chr(purrr::map(x, usage_code)), collapse = "")
}
#' @export
usage_code.tag_dots <- function(x) "..."
#' @export
usage_code.tag_ldots <- function(x) "..."
#' @export
usage_code.TEXT <- function(x) as.character(x)
#' @export
usage_code.RCODE <- function(x) as.character(x)
#' @export
usage_code.VERB <- function(x) as.character(x)
#' @export
usage_code.COMMENT <- function(x) character()
#' @export
usage_code.tag_S3method <- function(x) {
generic <- paste0(usage_code(x[[1]]), collapse = "")
class <- paste0(usage_code(x[[2]]), collapse = "")
paste0("S3method(`", generic, "`, ", class, ")")
}
#' @export
usage_code.tag_method <- usage_code.tag_S3method
#' @export
usage_code.tag_S4method <- function(x) {
generic <- paste0(usage_code(x[[1]]), collapse = "")
class <- strsplit(usage_code(x[[2]]), ",")[[1]]
class <- paste0("`", class, "`")
class <- paste0(class, collapse = ",")
paste0("S4method(`", generic, "`, list(", class, "))")
}
#' @export
usage_code.tag_usage <- function(x) {
paste0(purrr::flatten_chr(purrr::map(x, usage_code)), collapse = "")
}
|