From 6dc070ef3368b3a5b64e0b2b718c222cdb4e100f Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Wed, 17 Feb 2021 23:29:27 +0100 Subject: [PATCH 1/5] vectorise textVersion of bib entries tweaking a949228 and #1267 to account for CITATION files with multiple bib entries, where pkgdown currently always uses the textVersion even if that is missing --- R/build-home-citation.R | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/R/build-home-citation.R b/R/build-home-citation.R index 3a906e78c..fe1987251 100644 --- a/R/build-home-citation.R +++ b/R/build-home-citation.R @@ -45,19 +45,14 @@ data_citations <- function(pkg = ".") { cit <- read_citation(pkg$src_path) text_version <- format(cit, style = "textVersion") - if (identical(text_version, "")) { - cit <- list( - html = format(cit, style = "html"), - bibtex = format(cit, style = "bibtex") - ) - } else { - cit <- list( - html = paste0("

",text_version, "

"), - bibtex = format(cit, style = "bibtex") - ) - } + cit <- list( + html = ifelse(text_version == "", + format(cit, style = "html"), + paste0("

", text_version, "

")), + bibtex = format(cit, style = "bibtex") + ) - purrr::transpose(cit) + purrr::transpose(cit) } build_citation_authors <- function(pkg = ".") { From 9ff9b57c7ecd7e15f3d8830b5bb76442736ae303 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Fri, 19 Feb 2021 16:22:48 +0100 Subject: [PATCH 2/5] add test for 6dc070e --- tests/testthat/assets/site-citation/multi/DESCRIPTION | 6 ++++++ tests/testthat/assets/site-citation/multi/inst/CITATION | 2 ++ tests/testthat/test-build-citation-authors.R | 9 +++++++++ 3 files changed, 17 insertions(+) create mode 100644 tests/testthat/assets/site-citation/multi/DESCRIPTION create mode 100644 tests/testthat/assets/site-citation/multi/inst/CITATION diff --git a/tests/testthat/assets/site-citation/multi/DESCRIPTION b/tests/testthat/assets/site-citation/multi/DESCRIPTION new file mode 100644 index 000000000..58c608624 --- /dev/null +++ b/tests/testthat/assets/site-citation/multi/DESCRIPTION @@ -0,0 +1,6 @@ +Package: testpackage +Title: A test package +Version: 1.0.0 +Author: Sebastian Meyer +Maintainer: Sebastian Meyer +Description: Test multiple citations. diff --git a/tests/testthat/assets/site-citation/multi/inst/CITATION b/tests/testthat/assets/site-citation/multi/inst/CITATION new file mode 100644 index 000000000..415e3a61f --- /dev/null +++ b/tests/testthat/assets/site-citation/multi/inst/CITATION @@ -0,0 +1,2 @@ +bibentry("misc", title="Title One", author="Author One", year="2021") +bibentry("misc", title="Title Two", author="Author Two", year="2022") diff --git a/tests/testthat/test-build-citation-authors.R b/tests/testthat/test-build-citation-authors.R index d6a108cc8..d2e3d3184 100644 --- a/tests/testthat/test-build-citation-authors.R +++ b/tests/testthat/test-build-citation-authors.R @@ -34,3 +34,12 @@ test_that("source link is added to citation page", { lines <- read_lines(path(path, "docs", "authors.html")) expect_true(any(grepl("inst/CITATION", lines))) }) + +test_that("multiple citations all have HTML and BibTeX formats", { + path <- test_path("assets/site-citation/multi") + citations <- data_citations(path) + expect_length(citations, 2) + expect_true(all(lengths(citations) == 2)) + expect_true(all(rapply(citations, nchar) > 9)) + ## html was "

" in pkgdown 1.6.1 +}) From 6a09f9c6509978b3502868f444d291b3d0ab7f96 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Fri, 19 Feb 2021 18:26:23 +0100 Subject: [PATCH 3/5] escape textVersion for HTML output + use snapshot test --- R/build-home-citation.R | 2 +- .../testthat/_snaps/build-citation-authors.md | 19 +++++++++++++++++++ .../assets/site-citation/multi/inst/CITATION | 3 ++- tests/testthat/test-build-citation-authors.R | 6 ++---- 4 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/_snaps/build-citation-authors.md diff --git a/R/build-home-citation.R b/R/build-home-citation.R index fe1987251..a69b75743 100644 --- a/R/build-home-citation.R +++ b/R/build-home-citation.R @@ -48,7 +48,7 @@ data_citations <- function(pkg = ".") { cit <- list( html = ifelse(text_version == "", format(cit, style = "html"), - paste0("

", text_version, "

")), + paste0("

", escape_html(text_version), "

")), bibtex = format(cit, style = "bibtex") ) diff --git a/tests/testthat/_snaps/build-citation-authors.md b/tests/testthat/_snaps/build-citation-authors.md new file mode 100644 index 000000000..220471332 --- /dev/null +++ b/tests/testthat/_snaps/build-citation-authors.md @@ -0,0 +1,19 @@ +# multiple citations all have HTML and BibTeX formats + + [[1]] + [[1]]$html + [1] "

A & B (2021): Proof of b < a > c.

" + + [[1]]$bibtex + [1] "@Misc{,\n title = {Proof of b < a > c},\n author = {{A} and {B}},\n year = {2021},\n}" + + + [[2]] + [[2]]$html + [1] "

Two A (2022).\n“Title Two.” \n

" + + [[2]]$bibtex + [1] "@Misc{,\n title = {Title Two},\n author = {Author Two},\n year = {2022},\n}" + + + diff --git a/tests/testthat/assets/site-citation/multi/inst/CITATION b/tests/testthat/assets/site-citation/multi/inst/CITATION index 415e3a61f..5bc0c82e6 100644 --- a/tests/testthat/assets/site-citation/multi/inst/CITATION +++ b/tests/testthat/assets/site-citation/multi/inst/CITATION @@ -1,2 +1,3 @@ -bibentry("misc", title="Title One", author="Author One", year="2021") +bibentry("misc", title="Proof of b < a > c", author=c("A", "B"), year="2021", + textVersion="A & B (2021): Proof of b < a > c.") bibentry("misc", title="Title Two", author="Author Two", year="2022") diff --git a/tests/testthat/test-build-citation-authors.R b/tests/testthat/test-build-citation-authors.R index d2e3d3184..3679f2ed6 100644 --- a/tests/testthat/test-build-citation-authors.R +++ b/tests/testthat/test-build-citation-authors.R @@ -38,8 +38,6 @@ test_that("source link is added to citation page", { test_that("multiple citations all have HTML and BibTeX formats", { path <- test_path("assets/site-citation/multi") citations <- data_citations(path) - expect_length(citations, 2) - expect_true(all(lengths(citations) == 2)) - expect_true(all(rapply(citations, nchar) > 9)) - ## html was "

" in pkgdown 1.6.1 + expect_equal(lengths(citations), c(2, 2)) + expect_snapshot_output(citations) }) From f8c259f7ed87bcfdacd4016d7815db0802aff925 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 23 Feb 2021 14:17:54 -0600 Subject: [PATCH 4/5] Style --- R/build-home-citation.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/R/build-home-citation.R b/R/build-home-citation.R index a69b75743..bf0ea1f44 100644 --- a/R/build-home-citation.R +++ b/R/build-home-citation.R @@ -46,9 +46,11 @@ data_citations <- function(pkg = ".") { text_version <- format(cit, style = "textVersion") cit <- list( - html = ifelse(text_version == "", - format(cit, style = "html"), - paste0("

", escape_html(text_version), "

")), + html = ifelse( + text_version == "", + format(cit, style = "html"), + paste0("

", escape_html(text_version), "

") + ), bibtex = format(cit, style = "bibtex") ) From c89545cbb41e0cc8bf34d297e92f75c792ff7324 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 23 Feb 2021 14:20:35 -0600 Subject: [PATCH 5/5] Add news bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 002e95fd7..92f337777 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # pkgdown (development version) +* Better handling for mix of citations with and without text version. Also + escapes HTML in the text version (@bastistician, #1507). + * pkgdown now recognizes GitLab URLs to the source repository and adds the corresponding icon to the navbar (#1493).