Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API #65

Merged
merged 32 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b021277
Add new as.data.frame method
dieghernan Feb 15, 2024
7567cd6
Review docs
dieghernan Feb 15, 2024
784c8dc
Add as.person method
dieghernan Feb 16, 2024
6d8ece2
Update docs with pkgdev
github-actions[bot] Feb 16, 2024
d4642b6
revdepcheck
github-actions[bot] Feb 16, 2024
c0d7d63
Add head, tail and start working in reading files
dieghernan Feb 19, 2024
c1beb0e
Finish cff_read()
dieghernan Feb 19, 2024
b4d1f75
TODO examples
dieghernan Feb 19, 2024
f8673e9
revdepcheck
github-actions[bot] Feb 19, 2024
d9021af
Move readers
dieghernan Feb 20, 2024
40002b6
Handle DOI and corner case for VGAM
dieghernan Feb 20, 2024
2d6b569
New toBibtex.cff method
dieghernan Feb 26, 2024
aa16983
Improve linting
dieghernan Feb 27, 2024
df92a9d
Update tests
dieghernan Feb 27, 2024
186bdb1
Add new cff_write funs
dieghernan Feb 27, 2024
364a9f5
Deprecate warnings and other improvements
dieghernan Feb 27, 2024
44dc4d4
Start the review of the person conversion
dieghernan Feb 28, 2024
fe36c20
Try fixing issues
dieghernan Feb 28, 2024
e5e7c7a
Fix tests for devel version
dieghernan Feb 28, 2024
b5f6d85
Prepare new person and refactor tests and API
dieghernan Feb 29, 2024
3192a40
Add cff_create_cff_person
dieghernan Feb 29, 2024
b426462
Fix regex for older versions of R
dieghernan Feb 29, 2024
3d54999
revdepcheck
github-actions[bot] Mar 1, 2024
b5b468e
Update docs with pkgdev
github-actions[bot] Mar 1, 2024
b6a79d8
New API review and NEWS
dieghernan Mar 1, 2024
3245b70
Update pkgdown
dieghernan Mar 1, 2024
7b8430a
add_cff method
dieghernan Mar 1, 2024
e8cff76
Add as_cff, still left core functions
dieghernan Mar 2, 2024
de412dd
Update docs
dieghernan Mar 2, 2024
0247fbc
Deprecate arguments in cff()
dieghernan Mar 2, 2024
e924743
Add cff_modify
dieghernan Mar 3, 2024
8c751c9
Finish API
dieghernan Mar 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
linters: linters_with_defaults() # see vignette("lintr")
encoding: "UTF-8"
exclusions: list("data-raw")
exclusions: list(
"data-raw",
"tests/testthat/test_ci/test-full_cff.R",
"vignettes/cffr.Rmd",
"vignettes/bibtex_cff.Rmd"
)
10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(as.data.frame,cff)
S3method(as.person,cff)
S3method(c,cff)
S3method(head,cff)
S3method(print,cff)
S3method(tail,cff)
export(as.cff)
export(cff)
export(cff_create)
Expand All @@ -14,6 +18,10 @@ export(cff_parse_citation)
export(cff_parse_person)
export(cff_parse_person_bibtex)
export(cff_read)
export(cff_read_bib)
export(cff_read_cff_citation)
export(cff_read_citation)
export(cff_read_description)
export(cff_schema_definitions_entity)
export(cff_schema_definitions_person)
export(cff_schema_definitions_refs)
Expand All @@ -33,9 +41,11 @@ importFrom(utils,bibentry)
importFrom(utils,capture.output)
importFrom(utils,citation)
importFrom(utils,download.file)
importFrom(utils,head)
importFrom(utils,installed.packages)
importFrom(utils,modifyList)
importFrom(utils,packageDescription)
importFrom(utils,person)
importFrom(utils,read.csv)
importFrom(utils,tail)
importFrom(utils,toBibtex)
24 changes: 23 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
# cffr (development version)

## Major changes in the API
## Major changes

- Now `class()` of `cff` objects are `c("cff", "list")` instead of single
value (`"cff"`).

### API

- The conversion from `cff` to `bibentry` is performed now by a new function
`cff_to_bibentry()`. Previous names of this function were `cff_to_bibtex()`
and `cff_extract_to_bibtex()` that are now superseded.

- Now reading from external files is performed exclusively by `cff_read()` and
additionally by the more-specific new functions `cff_read_cff_citation()`,
`cff_read_description()`, `cff_read_citation()` and `cff_read_bib()`.

### Methods

- New methods added:

- `as.data.frame.cff().`
- `as.person.cff()`, that provides results **only** for CFF keys defined
as
[person](https://github.com/citation-file-format/citation-file-format/blob/main/schema-guide.md#definitionsperson)
or
[entity](https://github.com/citation-file-format/citation-file-format/blob/main/schema-guide.md#definitionsentity)
(e.g. authors, contacts, editors, publisher).
- `head.cff()`, `tail.cff()`.

## Changes on bibtex crosswalk

- **\@inbook** and **\@book** gains a new value on [CFF]{.underline} when
Expand Down
11 changes: 6 additions & 5 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ is_email <- function(email) {
return(FALSE)
}

# See https://www.nicebread.de/validating-email-adresses-in-r/
x <- grepl("\\<[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\>",
as.character(email),
ignore.case = TRUE
email <- trimws(as.character(email))

# See CFF validation schema
x <- grepl("^[\\S]+@[\\S]+\\.[\\S]{2,}$", email,
ignore.case = TRUE, perl = TRUE
)
x
}
Expand All @@ -22,7 +23,7 @@ is_url <- function(url) {
return(FALSE)
}

x <- grepl("^http://|^https://|^ftp://|sftp://", url)
x <- grepl("^(https|http|ftp|sftp)://.+", url)
x
}

Expand Down
127 changes: 127 additions & 0 deletions R/cff-methods.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#' Print Values
#'
#' @noRd
#' @export
print.cff <- function(x, ...) {
cat(yaml::as.yaml(x))
}

#' Combine Values into a Vector or List
#'
#' @source
#' Based on `?c.person` \CRANpkg{utils}.
#'
#' <https://github.com/wch/r-source/blob/trunk/src/library/utils/R/citation.R>
#'
#' @noRd
#' @export
c.cff <- function(..., recursive = FALSE) {
args <- list(...)
args <- lapply(args, unclass)
rval <- do.call("c", args)
class(rval) <- "cff"
rval
}


#' Coerce to a Data Frame
#'
#' @noRd
#' @export
as.data.frame.cff <- function(x, row.names = NULL, optional = FALSE, ...) {

Check warning on line 31 in R/cff-methods.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/cff-methods.R,line=31,col=34,[object_name_linter] Variable and function name style should match snake_case or symbols.
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
# If the cff is unnamed is a list of persons/references
if (is.null(names(x))) {
the_df <- cff_list_to_df(x)
} else {
the_df <- cff_to_df(x)
}

the_df <- as.data.frame(the_df,
row.names = row.names, optional = optional,
...
)

return(the_df)
}

#' Persons
#'
#' @noRd
#' @export
as.person.cff <- function(x) {
# If single enclose on a list
is_single <- any(grepl("^name$|^given-names|^family-names", names(x)))

if (is_single) x <- list(x)


pers <- lapply(x, make_r_person)

# If not all extracted, malformed, return null
if (!all(lengths(pers) > 0)) {
return(person())
}
do.call(c, pers)
}

#' Head
#'
#' @noRd
#' @export
head.cff <- function(x, n = 6L, ...) {
as.cff(NextMethod())
}

#' Tail
#'
#' @noRd
#' @export
tail.cff <- function(x, n = 6L, ...) {
as.cff(NextMethod())
}

make_r_person <- function(x) {
if (is.null(names(x))) {
return(person())
}
checknames <- grepl("^name$|given-names|family-names", names(x))
if (!isTRUE(any(checknames))) {
return(person())
}
# Prepare list
# Family is special key
fam1 <- clean_str(x$name)
fam2 <- clean_str(
paste(
clean_str(x$`name-particle`), clean_str(x$`family-names`),
clean_str(x$`name-suffix`)
)
)

given <- clean_str(x$`given-names`)
family <- clean_str(c(fam1, fam2))

# Make comments
x_comments <- x[!names(x) %in% c(
"family-names", "given-names",
"name-particle", "name-suffix", "email"
)]

x_comments <- lapply(x_comments, clean_str)
x_comments <- unlist(x_comments, use.names = TRUE)

# Prepare ORCID
x_comments <- gsub("^https://orcid.org/", "", x_comments)
nm <- gsub("orcid", "ORCID", names(x_comments), fixed = TRUE)
names(x_comments) <- nm

pers_list <- list(
given = given,
family = family,
email = clean_str(x$email),
comment = x_comments
)


do.call(person, pers_list)
}
Loading
Loading