Skip to content

Commit

Permalink
Improve preferred-citation handling (#38)
Browse files Browse the repository at this point in the history
* Improve preferred-citation

* Create preferred-citation only if a CITATION file is detected

#37

* Update docs with pkgdev

* Fix Roxygen warning

Co-authored-by: dieghernan <dieghernan@users.noreply.github.com>
  • Loading branch information
dieghernan and dieghernan committed Aug 29, 2022
1 parent b2d9cfa commit 732c2d0
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 902 deletions.
17 changes: 5 additions & 12 deletions CITATION.cff
@@ -1,5 +1,5 @@
# -----------------------------------------------------------
# CITATION file created with {cffr} R package, v0.2.2
# CITATION file created with {cffr} R package, v0.2.3.9000
# See also: https://docs.ropensci.org/cffr/
# -----------------------------------------------------------

Expand All @@ -8,7 +8,7 @@ message: 'To cite package "cffr" in publications use:'
type: software
license: GPL-3.0-or-later
title: 'cffr: Generate Citation File Format (''cff'') Metadata for R Packages'
version: 0.2.3
version: 0.2.3.9000
doi: 10.21105/joss.03900
abstract: The Citation File Format version 1.2.0 <doi:10.5281/zenodo.5171937> is a
human and machine readable file format which provides citation metadata for software.
Expand Down Expand Up @@ -78,7 +78,7 @@ references:
given-names: Gábor
email: csardi.gabor@gmail.com
year: '2022'
url: https://CRAN.R-project.org/package=crayon
url: https://github.com/r-lib/crayon#readme
version: '>= 1.4.1'
- type: software
title: desc
Expand All @@ -94,7 +94,6 @@ references:
given-names: Jim
email: james.f.hester@gmail.com
year: '2022'
url: https://CRAN.R-project.org/package=desc
version: '>= 1.3.0'
- type: software
title: jsonlite
Expand All @@ -106,7 +105,6 @@ references:
email: jeroen@berkeley.edu
orcid: https://orcid.org/0000-0002-4035-0289
year: '2022'
url: https://CRAN.R-project.org/package=jsonlite
version: '>= 1.7.2'
- type: software
title: jsonvalidate
Expand All @@ -123,7 +121,6 @@ references:
- family-names: Poberezkin
given-names: Evgeny
year: '2022'
url: https://CRAN.R-project.org/package=jsonvalidate
version: '>= 1.1.0'
- type: software
title: yaml
Expand Down Expand Up @@ -156,7 +153,7 @@ references:
- family-names: Kamvar
given-names: Zhian N.
year: '2022'
url: https://CRAN.R-project.org/package=yaml
url: https://github.com/vubiostat/r-yaml/
version: '>= 2.2.1'
- type: software
title: knitr
Expand All @@ -168,7 +165,7 @@ references:
email: xie@yihui.name
orcid: https://orcid.org/0000-0003-0645-5666
year: '2022'
url: https://CRAN.R-project.org/package=knitr
url: https://yihui.org/knitr/
- type: software
title: lifecycle
abstract: 'lifecycle: Manage the Life Cycle of your Package Functions'
Expand All @@ -182,7 +179,6 @@ references:
email: hadley@rstudio.com
orcid: https://orcid.org/0000-0003-4757-117X
year: '2022'
url: https://CRAN.R-project.org/package=lifecycle
- type: software
title: rmarkdown
abstract: 'rmarkdown: Dynamic Documents for R'
Expand Down Expand Up @@ -221,7 +217,6 @@ references:
email: rich@rstudio.com
orcid: https://orcid.org/0000-0003-3925-190X
year: '2022'
url: https://CRAN.R-project.org/package=rmarkdown
- type: software
title: testthat
abstract: 'testthat: Unit Testing for R'
Expand All @@ -231,7 +226,6 @@ references:
given-names: Hadley
email: hadley@rstudio.com
year: '2022'
url: https://CRAN.R-project.org/package=testthat
version: '>= 3.0.0'
- type: software
title: usethis
Expand All @@ -251,4 +245,3 @@ references:
email: malcolmbarrett@gmail.com
orcid: https://orcid.org/0000-0003-0299-5825
year: '2022'
url: https://CRAN.R-project.org/package=usethis
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: cffr
Title: Generate Citation File Format ('cff') Metadata for R Packages
Version: 0.2.3
Version: 0.2.3.9000
Authors@R: c(
person("Diego", "Hernangómez", , "diego.hernangomezherrero@gmail.com", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-8457-4658")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
@@ -1,3 +1,8 @@
# cffr (development version)

- `preferred-citation` is only produced when a CITATION (**R**) file has been
provided with the package (#37).

# cffr 0.2.3

- Fix HTML5 issue as per CRAN request
Expand Down
57 changes: 28 additions & 29 deletions R/cff_create.R
Expand Up @@ -104,9 +104,9 @@ cff_create <- function(x,
cff_version = "1.2.0",
gh_keywords = TRUE,
dependencies = TRUE) {
# On missing use package root
if (missing(x)) x <- getwd()


if (!is.cff(x) && !is.character(x)) {
stop("x should be a cff or a character",
call. = FALSE
Expand All @@ -126,32 +126,31 @@ cff_create <- function(x,
cffobj <- x
cffobj["cff-version"] <- cff_version
} else {
# Need to parse with desc_path

if (x %in% instpack) {

# Installed package, have to parse
desc_path <- file.path(find.package(x), "DESCRIPTION")

# Parse citation from installation
citobj <- lapply(citation(x), cff_parse_citation)
if (length(citobj) == 0) citobj <- NULL
citobj <- drop_null(citobj)
} else if (x == getwd()) {
# Detect a package
if (x %in% instpack) x <- file.path(find.package(x), "DESCRIPTION")
# If is on the root create DESCRIPTION path
if (x == getwd()) x <- file.path(x, "DESCRIPTION")

# In development package
# nocov start
desc_path <- file.path(x, "DESCRIPTION")
cit_path <- file.path(x, "inst/CITATION")
if (isTRUE(grep("DESCRIPTION", x) == 1)) {
# Call for a DESCRIPTION file
desc_path <- x
# Look if a CITATION file on inst/ folder
# for in-development packages
cit_path <- gsub("DESCRIPTION$", "inst/CITATION", x)
# If it doesn't exists look on the root
# this is for call to installed packages with system.file()
if (!file.exists(cit_path)) {
cit_path <- gsub(
"DESCRIPTION$",
"CITATION", x
)
}
if (file.exists(cit_path)) {
citobj <- parse_r_citation(desc_path, cit_path)
citobj <- lapply(citobj, cff_parse_citation)
if (length(citobj) == 0) citobj <- NULL
citobj <- drop_null(citobj)
}
# nocov end
} else if (isTRUE(grep("DESCRIPTION", x) == 1)) {
desc_path <- x
} else {
stop("object x: '", x, "' not valid. If it is a package ",
"you may need to install it.",
Expand Down Expand Up @@ -197,8 +196,10 @@ cff_create <- function(x,
cffobjend <- cffobjend[cff_schema_keys()]

# Enhance authors info
cffobjend$`preferred-citation`$authors <- enhance_pref_authors(cffobjend)

if (!is.null(cffobjend$`preferred-citation`)) {
cffobjend$`preferred-citation`$authors <- enhance_pref_authors(cffobjend)
}
cffobjend <- as.cff(cffobjend)
cffobjend
}
Expand All @@ -208,6 +209,12 @@ cff_create <- function(x,
#' @noRd
merge_desc_cit <- function(cffobj, citobj) {

# If no citobj then return null

if (is.null(citobj)) {
return(cffobj)
}

# Add doi from citation if missing
if (is.null(cffobj$doi)) {
cffobj$doi <- clean_str(citobj[[1]]$doi)
Expand All @@ -226,14 +233,6 @@ merge_desc_cit <- function(cffobj, citobj) {
oldids
)

# Add auto preferred if not present
if (is.null(cffobjend[["preferred-citation"]])) {
pref_auto <- parse_preferred_auto(cffobjend)
cffobjend <- c(cffobjend,
"preferred-citation" = list(pref_auto)
)
}

# Reorder
cffobjfinal <- c(
cffobjend[!names(cffobjend) %in% c("identifiers", "references")],
Expand Down
5 changes: 4 additions & 1 deletion R/cff_to_bibtex.R
@@ -1,4 +1,4 @@
#' Create a BibTeX entry from a CITATION file or a `cff` object
#' Create a BibTeX entry from a CITATION.cff file or a `cff` object
#'
#' Creates a `bibentry` object ([bibentry()]) from a `cff` object
#'
Expand Down Expand Up @@ -33,6 +33,9 @@
#' obj
#'
#' toBibtex(obj)
#'
#' # Same info as
#' toBibtex(citation("rmarkdown")[1])
#' }
cff_to_bibtex <- function(x) {
if (is.null(x)) {
Expand Down
72 changes: 40 additions & 32 deletions R/parse_citation.R
Expand Up @@ -49,38 +49,46 @@ parse_package_meta <- function(desc_path) {
meta
}


#' Used for creating an auto preferred-citation from an
#' cff object if not present
#' It mocks citation(auto = meta) of base R
#' @noRd
parse_preferred_auto <- function(cffobjend) {
valid <- cff_schema_definitions_refs()

pref <- cffobjend[names(cffobjend) %in%
valid]

pref$type <- "manual"

# Handle year

date_rel <- pref[["date-released"]]

if (is.null(date_rel)) {
year <- format(Sys.Date(), "%Y")
} else {
year <- format(as.Date(pref[["date-released"]]), "%Y")
}

pref$year <- year

# Order and output
ordernames <- unique(c("type", "title", "authors", names(pref)))
pref <- pref[unique(c(ordernames, "identifiers"))]
pref <- as.cff(pref)

pref
}
# DEPRECATED
# Used for creating an auto preferred-citation from an
# cff object if not present
# It mocks citation(auto = meta) of base R
# parse_preferred_auto <- function(cffobjend) {
# valid <- cff_schema_definitions_refs()
#
# pref <- cffobjend[names(cffobjend) %in%
# valid]
#
# pref$type <- "manual"
#
# # Handle year
#
# date_rel <- pref[["date-released"]]
#
# if (is.null(date_rel)) {
# year <- format(Sys.Date(), "%Y")
# } else {
# year <- format(as.Date(pref[["date-released"]]), "%Y")
# }
#
# pref$year <- year
# pref$notes <- NULL
# if (!is.null(pref$version)) {
# pref$notes <- paste("R package version", pref$version)
# }
# # If it has a repository (CRAN) use that as url
# if (!is.null(pref$repository)) pref$url <- pref$repository
#
# # Order and output
# ordernames <- c(
# "type", "title", "authors", "year",
# "notes", "url"
# )
# pref <- pref[ordernames]
# pref <- as.cff(pref)
#
# pref
# }
## Building blocks ----

#' BB for doi
Expand Down

0 comments on commit 732c2d0

Please sign in to comment.