Skip to content

Commit

Permalink
Improving package description auto-linking (#1315)
Browse files Browse the repository at this point in the history
Fixes #1265. Fixes #1164.
  • Loading branch information
dieghernan authored Apr 6, 2022
1 parent 950bd02 commit 0ecdcde
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# roxygen2 (development version)

* DOIs, arXiv links, and urls in the `Description` field of the `DESCRIPTION`
are now converted to the appropriate Rd markup (@dieghernan, #1265, #1164).

* Arguments containing syntactically significant whitespace (e.g anonymous
functions) are now wrapped correctly (#1281).

Expand Down
1 change: 1 addition & 0 deletions R/object-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ object_defaults.package <- function(x) {
desc <- x$value$desc

description <- as.character(desc$Description)
description <- package_url_parse(description)
logo_path <- file.path(x$value$path, "man", "figures", "logo.png")
if (file.exists(logo_path)) {
fig <- "\\if{html}{\\figure{logo.png}{options: align='right' alt='logo' width='120'}}"
Expand Down
29 changes: 29 additions & 0 deletions R/object-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,32 @@ itemize <- function(header, x) {
"}\n"
)
}

package_url_parse <- function(x) {
# <doi:XX.XXX> -> \doi{XX.XXX} to avoid CRAN Notes, etc.
x <- str_replace_all(x, "<(doi|DOI):(.*?)>", function(match) {
match <- str_remove_all(match, "^<(doi|DOI):|>$")
paste0("\\doi{", escape(match), "}")
})

# <http:XX.XXX> -> \url{http:XX.XXX}
x <- str_replace_all(x, "<(http|https):\\/\\/(.*?)>", function(match) {
match <- str_remove_all(match, "^<|>$")
paste0("\\url{", escape(match), "}")
})

# <arxiv:XXX> -> \href{https://arxiv.org/abs/XXX}{arXiv:XXX}
# https://github.com/wch/r-source/blob/trunk/src/library/tools/R/Rd2pdf.R#L149-L151
patt_arxiv <- "<(arXiv:|arxiv:)([[:alnum:]/.-]+)([[:space:]]*\\[[^]]+\\])?>"
x <- str_replace_all(x, patt_arxiv, function(match) {
match <- str_remove_all(match, "^<(arXiv:|arxiv:)|>$")
# Special cases has <arxiv:id [code]>.
# See https://CRAN.R-project.org/package=ciccr
# Extract arxiv id, split by space
arxiv_id <- str_split_fixed(match, " ", n = 2)[, 1]

paste0("\\href{https://arxiv.org/abs/", escape(arxiv_id), "}{arXiv:", match, "}")
})

x
}
44 changes: 44 additions & 0 deletions tests/testthat/test-object-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,47 @@ test_that("can convert DOIs in url", {
"\\doi{10.5281/zenodo.1485309}"
)
})

test_that("can autolink urls on package Description", {
expect_equal(
package_url_parse("x <https://x.com> y"),
"x \\url{https://x.com} y"
)
expect_equal(
package_url_parse("x <https://x.com/%3C-%3E> y"),
"x \\url{https://x.com/\\%3C-\\%3E} y"
)
})

test_that("can autolink DOIs", {
expect_equal(package_url_parse("x <doi:abcdef> y"), "x \\doi{abcdef} y")
expect_equal(package_url_parse("x <DOI:abcdef> y"), "x \\doi{abcdef} y")
expect_equal(package_url_parse("x <DOI:%3C-%3E> y"), "x \\doi{\\%3C-\\%3E} y")
})

test_that("can autolink arxiv", {
expect_equal(
package_url_parse("x <arXiv:abc> y"),
"x \\href{https://arxiv.org/abs/abc}{arXiv:abc} y"
)
expect_equal(
package_url_parse("x <arxiv:abc> y"),
"x \\href{https://arxiv.org/abs/abc}{arXiv:abc} y"
)
expect_equal(
package_url_parse("x <arxiv:abc [def]> y"),
"x \\href{https://arxiv.org/abs/abc}{arXiv:abc [def]} y"
)
})

test_that("autolink several matching patterns", {
text <- "url <http://a.com> doi <doi:xx> arxiv <arXiv:xx>"
expect_equal(
package_url_parse(text),
paste(
"url \\url{http://a.com}",
"doi \\doi{xx}",
"arxiv \\href{https://arxiv.org/abs/xx}{arXiv:xx}"
)
)
})

0 comments on commit 0ecdcde

Please sign in to comment.