Skip to content

Commit

Permalink
fix #16 more cff work, probably still some stuff to do, but basic sup…
Browse files Browse the repository at this point in the history
…port is there
  • Loading branch information
sckott committed Oct 14, 2020
1 parent d4655a7 commit 72b8a28
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 10 deletions.
55 changes: 52 additions & 3 deletions R/cff_writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
#' https://github.com/citation-file-format/citation-file-format
#' @details uses `yaml::write_yaml` to write to yaml format that
#' CFF uses
#' @section Converting to CFF from other formats:
#' CFF has required fields that can't be missing. This means that
#' converting from other citation types to CFF will likely require
#' adding the required CFF fields manually. Adding fields to a
#' `handl` object is easy: it's really just an R list so add
#' named elements to it. The required CFF fields are:
#'
#' - cff-version: add `cff_version`
#' - message: add `message`
#' - version: add `software_version`
#' - title: add `title`
#' - authors: add `author`
#' - date-released: add `date_published`
#'
#' @examples
#' (z <- system.file('extdata/citation.cff', package = "handlr"))
#' res <- cff_reader(x = z)
Expand All @@ -21,6 +35,19 @@
#' cff_writer(res, f)
#' readLines(f)
#' unlink(f)
#'
#' # convert from a different citation format
#' ## see "Converting to CFF from other formats" above
#' z <- system.file('extdata/citeproc.json', package = "handlr")
#' w <- citeproc_reader(x = z)
#' # cff_writer(w) # fails unless we add required fields
#' w$cff_version <- "1.1.0"
#' w$message <- "Please cite the following works when using this software."
#' w$software_version <- "2.5"
#' w$title <- "A cool library"
#' w$date_published <- "2017-12-18"
#' cff_writer(w)
#' cat(cff_writer(w))
cff_writer <- function(z, path = NULL) {
assert(z, "handl")
stopifnot(length(z) > 0)
Expand All @@ -29,7 +56,7 @@ cff_writer <- function(z, path = NULL) {

cff_write_one <- function(z, path) {
zz <- ccp(list(
'cff-version' = req(z$cff_version, "cff-version"),
'cff-version' = req(z$cff_version %||% cff_version, "cff-version"),
message = req(z$message, "message"),
version = req(z$software_version, "version"),
title = req(
Expand All @@ -50,10 +77,12 @@ cff_write_one <- function(z, path) {

cff_auths <- function(e) {
lapply(e, function(w) {
# rename some fields
names(w)[names(w) == "familyName"] <- "family-names"
names(w)[names(w) == "givenName"] <- "given-names"
w$type <- NULL
w$name <- NULL
# drop unsupported fields
w <- w[names(w) %in% cff_person_fields]
# sort fields
w[order(names(w))]
})
}
Expand All @@ -65,3 +94,23 @@ req <- function(x, var) {
}
return(x)
}

cff_version <- "1.1.0"

cff_person_fields <- c(
"family-names",
"given-names",
"name-particle",
"name-suffix",
"affiliation",
"address",
"city ",
"region ",
"post-code",
"country",
"orcid",
"email",
"tel",
"fax",
"website"
)
6 changes: 3 additions & 3 deletions R/client.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
handlr_readers <- c('citeproc', 'ris', 'bibtex', 'codemeta', 'cff')
handlr_writers <- c('citeproc', 'ris', 'bibtex', 'schema_org',
'rdfxml', 'codemeta')
'rdfxml', 'codemeta', 'cff')

#' @title HandlrClient
#' @description handlr client, read and write to and from all citation formats
Expand Down Expand Up @@ -105,7 +105,7 @@ handlr_writers <- c('citeproc', 'ris', 'bibtex', 'schema_org',
#' x$parsed
#' x$write("codemeta")
#'
#' # cff
#' # cff: Citation File Format
#' (z <- system.file('extdata/citation.cff', package = "handlr"))
#' (x <- HandlrClient$new(x = z))
#' x$path
Expand All @@ -114,7 +114,7 @@ handlr_writers <- c('citeproc', 'ris', 'bibtex', 'schema_org',
#' x$parsed
#' x$write("codemeta")
#'
#' # > 1
#' # > 1 citation
#' z <- system.file('extdata/citeproc-many.json', package = "handlr")
#' (x <- HandlrClient$new(x = z))
#' x$parsed
Expand Down
2 changes: 2 additions & 0 deletions R/handl.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
#' - license: (string) license of the work, a named list
#' - state: (string) the state of the list
#' - software_version: (string) software version
#'
#' Citeproc formats may have extra fields that begin with `csl_`
NULL

#' @export
Expand Down
4 changes: 2 additions & 2 deletions man/HandlrClient.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/cff_writer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/citeproc_reader.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/handl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72b8a28

Please sign in to comment.