Skip to content

Commit

Permalink
factor out info_
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Feb 26, 2024
1 parent 877297c commit 10001e8
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions R/auto.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@ get_title <- function(url) {
xml2::xml_text()
}

#' @rdname auto
#' @export
link_pkg <- function(pkg, keep_braces = TRUE) {
info_pkg <- function(pkg, keep_braces = TRUE){
url <- downlit::href_package(pkg)
link_text <- if (keep_braces) "{{{pkg}}}" else "{pkg}"
tags$a(glue::glue(link_text), href = url, class = "r-link-pkg", target = "_blank")
link_text <- glue::glue(if (keep_braces) "{{{pkg}}}" else "{pkg}")
link <- tags$a(link_text, href = url, class = "r-link-pkg", target = "_blank")

list(url = url, link_text = link_text, link = link)
}

#' @rdname auto
#' @export
tip_pkg <- function(pkg, keep_braces = TRUE, text = get_title(url), ...) {
bslib::tooltip(
link_pkg(pkg, keep_braces = keep_braces),
text,
...
)
link_pkg <- function(pkg, keep_braces = TRUE) {
info_pkg(pkg, keep_braces = keep_braces)$link
}

#' @rdname auto
#' @export
link_call <- function(call, keep_pkg_prefix = TRUE) {
tip_pkg <- function(pkg, keep_braces = TRUE, text, ...) {
info <- info_pkg(pkg, keep_braces = keep_braces)
if (missing(text)) {
text <- get_title(info$url)
}

bslib::tooltip(info$link, text, ...)
}

info_call <- function(call, keep_pkg_prefix = TRUE) {
url <- downlit::autolink_url(call)

link_text <- if (keep_pkg_prefix) {
Expand All @@ -41,17 +46,27 @@ link_call <- function(call, keep_pkg_prefix = TRUE) {
glue::glue("{fun}()", fun = stringr::str_extract(call, rx_call, group = 2))
}

tags$a(link_text, href = url, class = "r-link-call", target = "_blank")
link <- tags$a(link_text, href = url, class = "r-link-call", target = "_blank")

list(url = url, link_text = link_text, link = link)
}


#' @rdname auto
#' @export
tip_call <- function(call, keep_pkg_prefix = TRUE, text = get_title(url), ...) {
bslib::tooltip(
link_call(call, keep_pkg_prefix = keep_pkg_prefix),
text,
...
)
link_call <- function(call, keep_pkg_prefix = TRUE) {
info_call(call, keep_pkg_prefix = keep_pkg_prefix)$link
}

#' @rdname auto
#' @export
tip_call <- function(call, keep_pkg_prefix = TRUE, text, ...) {
info <- info_call(call, keep_pkg_prefix = keep_pkg_prefix)
if (missing(text)) {
text <- get_title(info$url)
}

bslib::tooltip(info$link, text, ...)
}

autolink_pkg <- function(x, keep_braces = TRUE) {
Expand Down

0 comments on commit 10001e8

Please sign in to comment.