Skip to content

Commit

Permalink
link::auto()
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Feb 24, 2024
1 parent a5639a6 commit f975137
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Imports:
cli,
downlit,
glue,
rlang
knitr,
rlang,
stringr
URL: https://github.com/tadascience/link, http://tada.science/link/
BugReports: https://github.com/tadascience/link/issues
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(auto)
export(to)
importFrom(cli,cli_abort)
importFrom(rlang,enexpr)
Expand Down
41 changes: 41 additions & 0 deletions R/auto.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
rx_valid_pkg <- "[a-zA-Z][a-zA-Z0-9.]*[a-zA-Z0-9]"
rx_valid_fun <- "([a-zA-Z]\\w*|\\.\\w+)"

autolink_pkg <- function(x) {
rx_pkg <- glue::glue("[{{]({rx_valid_pkg})[}}]")
stringr::str_replace_all(
x, rx_pkg,
function(pkg) {
to(package = stringr::str_extract(pkg, rx_pkg, group = 1))
}
)
}

autolink_call <- function(x) {
rx_call <- glue::glue("[{{]({rx_valid_pkg})::({rx_valid_fun})[(][)][}}]")
stringr::str_replace_all(
x, rx_call,
function(call){
to(
package = stringr::str_extract(call, rx_call, group = 1),
topic = stringr::str_extract(call, rx_call, group = 2)
)
}
)

}

#' Setup automatic linking
#'
#' @export
auto <- function() {
default_text_hook <- knitr::knit_hooks$get("text")
knitr::knit_hooks$set(text = function(x) {

x <- autolink_pkg(x)
x <- autolink_call(x)

default_text_hook(x)
})

}
11 changes: 11 additions & 0 deletions man/auto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f975137

Please sign in to comment.