Skip to content

Commit

Permalink
Correctly escape paren. in DOIs in Markdown URLs
Browse files Browse the repository at this point in the history
* Prior commit/attemped fix failed b/c need to escape %s from
encoded URL for downstream processing in tools::Rd2txt
* Closes #54
  • Loading branch information
mwmclean committed Jun 5, 2018
1 parent 9277789 commit 3834c33
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
10 changes: 6 additions & 4 deletions R/CommonFormatFuns.R
Expand Up @@ -363,14 +363,16 @@ GetFormatFunctions <- function(docstyle = "text", DateFormatter){
fmtISRN <- label(prefix = 'ISRN: ', suffix = '.')
#' @importFrom util URLencode
fmtDOI <- switch(docstyle, html = function(doi){
if (length(doi)){
if (length(doi))
paste0("DOI: \\href{https://doi.org/",
URLencode(doi, reserved = TRUE), "}{", doi, "}.")
}
doi, "}{", doi, "}.")
}, markdown = function(doi){
if (length(doi)){
## #54 possible parentheses in DOI need to be escaped
## need to gsub/escape % for later call to tools::Rd2txt
enc.doi <- gsub("%", "\\\\%", URLencode(doi, reserved = TRUE))
paste0("DOI: [", doi, "](https://doi.org/",
URLencode(doi, reserved = TRUE), ").")
enc.doi, ").")
}
}, label(prefix = 'DOI: ', suffix = '.'))

Expand Down
25 changes: 23 additions & 2 deletions tests/testthat/test-cites.R
Expand Up @@ -161,13 +161,34 @@ test_that("PrintBibliography start and stop parameters", {

if (!length(bib2))
skip("Couldn't load RCJ.bib'")
Citet(bib2, author = "Xun")
Citet(bib2, author = "Xun")
AutoCite(bib2, title = "binary longitudinal data")
bibtext <- capture.output(PrintBibliography(bib2, end = 1,
.opts = list(cite.style = "numeric")))
## only one entry is printed (so that no blank lines separating entries)
expect_false(any(grepl("^$", bibtext)))
bibtext <- capture.output(PrintBibliography(bib2, start = 2,
.opts = list(cite.style = "numeric")))
expect_false(any(grepl("^$", bibtext)))
expect_false(any(grepl("^$", bibtext)))
})

test_that("#54 DOI with parentheses works with markdown", {
bibtext <- "@article{Mendenhall:1984,
Author = {Mendenhall, William M. and Million, Rodney R. and Sharkey, Daniel E. and Cassisi, Nicholas J.},
Date-Added = {2018-06-04 03:03:17 +0000},
Date-Modified = {2018-06-04 03:03:17 +0000},
Doi = {10.1016/0360-3016(84)90054-3},
Isbn = {0360-3016},
Journal = {International Journal of Radiation Oncology *Biology *Physics},
Number = {3},
Pages = {357--363},
Title = {Stage T3 squamous cell carcinoma of the glottic larynx treated with surgery and/or radiation therapy},
Volume = {10},
Year = {1984},
Bdsk-Url-1 = {https://doi.org/10.1016/0360-3016%5C%252884%5C%252990054-3}}"
tfile <- tempfile()
writeLines(bibtext, tfile)
bib <- ReadBib(tfile)
expect_length(grep("[(]https://doi.org/10.1016%2F0360-3016%2884%2990054-3[)]",
capture.output(print(bib, .opts = list(style = "markdown")))), 1L)
})

0 comments on commit 3834c33

Please sign in to comment.