Skip to content

Commit

Permalink
#16 start working on cff integration, more to do
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Jan 23, 2020
1 parent 9344804 commit 13446f7
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 5 deletions.
12 changes: 7 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Package: handlr
Title: Convert Among Citation Formats
Description: Converts among many citation formats, including 'BibTeX',
'Citeproc', 'Codemeta', 'RDF XML', 'RIS', and 'Schema.org'. A low
level 'R6' class is provided, as well as stand-alone functions
for each citation format for both read and write.
Version: 0.2.1.91
'Citeproc', 'Codemeta', 'RDF XML', 'RIS', 'Schema.org', and
'Citation File Format'. A low level 'R6' class is provided, as well
as stand-alone functions for each citation format for both read
and write.
Version: 0.2.2.92
License: MIT + file LICENSE
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"),
Expand All @@ -23,7 +24,8 @@ Imports:
xml2,
RefManageR,
urltools,
mime
mime,
yaml
Suggests:
testthat,
jsonld,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(print,handl)
export(HandlrClient)
export(bibtex_reader)
export(bibtex_writer)
export(cff_reader)
export(citeproc_reader)
export(citeproc_writer)
export(codemeta_reader)
Expand Down
84 changes: 84 additions & 0 deletions R/cff_reader.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#' Citation File Format (cff) reader
#'
#' @export
#' @param x (character) a file path or a yaml string
#' @return an object of class `handl`; see [handl] for more
#' @family readers
#' @family cff
#' @references CFF format:
#' https://github.com/citation-file-format/citation-file-format/blob/master/README.md
#' @examples
#' (z <- system.file('extdata/citation.cff', package = "handlr"))
#' cff_reader(x = z)
cff_reader <- function(x) {
assert(x, "character")
txt <- if (is_file(x)) yaml::yaml.load_file(x) else yaml::yaml.load(x)
structure(cff_read_one(txt),
class = "handl", from = "cff",
source_type = if (is_file(x)) "file" else "string",
file = if (is_file(x)) x else "", many = FALSE)
}

cff_read_one <- function(x) {
doi <- x$doi
author <- lapply(x$authors, function(z) {
list(
type = "Person",
name = pcsp(pcsp(z$`given-names`), pcsp(z$`family-names`)),
givenName = pcsp(z$`given-names`),
familyName = pcsp(z$`family-names`)
)
})
state <- if (!is.null(doi)) "findable" else "not_found"

list(
"cff_version" = x$`cff-version`,
"message" = x$message,
# "key" = attr(x, "key"),
"id" = normalize_doi(doi),
# "type" = type,
# "bibtex_type" = type,
# "citeproc_type" = CFF_TO_CP_TRANSLATIONS[[type]] %||% "misc",
# "ris_type" = CFF_TO_RIS_TRANSLATIONS[[type]] %||% "GEN",
# "resource_type_general" = SO_TO_DC_TRANSLATIONS[[type]],
# "additional_type" = CFF_TO_CR_TRANSLATIONS[[type]] %||% type,
"doi" = doi,
"b_url" = x$url %||% NULL,
"title" = x$title %||% NULL,
"author" = author,
"publisher" = x$publisher %||% NULL,
"is_part_of" = NULL,
"date_published" = x$`date-released` %||% NULL,
"b_version" = x$version %||% NULL,
"volume" = x$volume %||% NULL,
"first_page" = NULL,
"last_page" = NULL,
# "description" = list(text = x$abstract %||% NULL && sanitize(x$abstract)),
"description" = list(text = x$abstract %||% NULL),
"license" = list(id = x$copyright %||% NULL),
"state" = state
)
}

# CFF_TO_CP_TRANSLATIONS <- list(
# article = "article-journal",
# phdthesis = "thesis"
# )

# CFF_TO_RIS_TRANSLATIONS <- list(
# article = "JOUR",
# book = "BOOK",
# inbook = "CHAP",
# inproceedings = "CPAPER",
# manual = NULL,
# misc = "GEN",
# phdthesis = "THES",
# proceedings = "CONF",
# techreport = "RPRT",
# unpublished = "UNPD"
# )

# CFF_TO_SO_TRANSLATIONS <- list(
# article = "ScholarlyArticle",
# phdthesis = "Thesis"
# )
10 changes: 10 additions & 0 deletions inst/extdata/citation.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cff-version: 1.1.0
message: If you use this software, please cite it as below.
authors:
- family-names: Druskat
given-names: Stephan
orcid: https://orcid.org/0000-0003-4925-7248
title: "My Research Software"
version: 2.0.4
doi: 10.5281/zenodo.1234
date-released: 2017-12-18
1 change: 1 addition & 0 deletions man/bibtex_reader.Rd

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

34 changes: 34 additions & 0 deletions man/cff_reader.Rd

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

1 change: 1 addition & 0 deletions man/citeproc_reader.Rd

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

1 change: 1 addition & 0 deletions man/codemeta_reader.Rd

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

1 change: 1 addition & 0 deletions man/ris_reader.Rd

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

0 comments on commit 13446f7

Please sign in to comment.