Skip to content

Commit

Permalink
Citation meta problem ... potential solution (#241)
Browse files Browse the repository at this point in the history
* Add tempdir() to avoid to writing guess_other_metadata() into the parent directory.

* + swap the default
+ updating the roxygen docs

* + Add myself as a ctb in DESCRIPTION

* + mention of changes in NEWS.md
+ linking this PR

* Address #240 and avoid pre-commit hook without user consent

* Correct link in NEWS.md #240

* Modify the way the file size is calculated.

+ Amend guess_fileSize() in guess_other_metadata.R as proposed #239
+ Update tests
+ Drop dependency to 'pkgbuild'
+ Add NEWS in NEWS.md

* Modify the way the file size is calculated.

+ Amend guess_fileSize() in guess_other_metadata.R as proposed #239
+ Update tests
+ Drop dependency to 'pkgbuild'
+ Update documentation in 'write_codemeta()'
+ Upddate corresponding Rd-files
+ Add NEWS in NEWS.md

* Address #238

+ Add NEWS

* Update tests and add CITATION_ex2 #241
  • Loading branch information
RLumSK authored and cboettig committed Aug 5, 2019
1 parent 63bb55f commit 929a41a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* `write_codemeta()`: the default of argument `use_git_hook` is now `FALSE` to avoid an
unwanted alteration of the user's git environment [issue #240](https://github.com/ropensci/codemetar/issues/240).
* Package dependency to 'pkgbuild' has been dropped.
* `write_codemeta()` does not crash anymore if the `CITATION` file contains a line `citation(auto = meta)` [Issue #238](https://github.com/ropensci/codemetar/issues/238).


# codemetar 0.1.7 2018-12

Expand Down
36 changes: 32 additions & 4 deletions R/parse_citation.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ parse_citation <- function(bib) {
## so would need to include schema.org context

bibentry_to_schema_field <- function(bibtype) {

switch(
bibtype,
"Article" = "ScholarlyArticle",
Expand Down Expand Up @@ -159,11 +158,40 @@ guess_citation <- function(pkg) {
# read_citation_with_encoding --------------------------------------------------
read_citation_with_encoding <- function(citation_file, encoding = NA)
{
meta <- if (! is.na(encoding)) {

meta <- if (!is.na(encoding)) {
list(Encoding = encoding)

} # else NULL implicitly

utils::readCitationFile(citation_file, meta = meta)
## try to read citation file
citation <- try(utils::readCitationFile(citation_file, meta = meta), silent = TRUE)

## if this fails for a very specific reason, namely a line similar to
## citation(auto = meta), this line gets removed and we continue working
## with a temporary CITATION file
if(inherits(citation, "try-error")){
if(grepl(pattern = "Error in.+?auto", citation[1])){
## >> (1) read original CITATION file
temp_citation <-
readLines(
con = citation_file,
encoding = if (!is.na(encoding)) encoding else "unknown")

## >> (2) remove citation(auto = meta)
repl_id <- which(grepl(
pattern = "citation\\s*\\(auto\\s*=\\s*meta\\s*\\)",
x = temp_citation
))
temp_citation <- temp_citation[-repl_id]

## >> (3) write new temporary citation file
temp_file <- tempfile()
writeLines(temp_citation, temp_file)

## >> (4) apply extraction
citation <- utils::readCitationFile(temp_file, meta = meta)
}
}

return(citation)
}
28 changes: 28 additions & 0 deletions inst/examples/CITATION_ex2
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
citation(auto = meta)
bibentry(bibtype = "Article",
header = "To cite RNeXML in publications, please use:",
title = "{RNeXML}: {A} Package for Reading and Writing Richly Annotated Phylogenetic, Character, and Trait Data in {R}",
journal = "Methods in Ecology and Evolution",
author = c(
person("Carl", "Boettiger"),
person("Scott", "Chamberlain"),
person("Rutger", "Vos"),
person("Hilmar", "Lapp")),
year = 2016,
volume = 7,
pages = "352--357",
doi = "10.1111/2041-210X.12469")

bibentry(bibtype = "Article",
header = "To cite RNeXML in publications, please use:",
title = "{RNeXML}: {A} Package for Reading and Writing Richly Annotated Phylogenetic, Character, and Trait Data in {R}",
journal = "Methods in Ecology and Evolution",
author = c(
person("Carl", "Boettiger"),
person("Scott", "Chamberlain"),
person("Rutger", "Vos"),
person("Hilmar", "Lapp")),
year = 2016,
volume = 7,
pages = "352--357",
doi = "https://doi.org/10.1111/2041-210X.12469")
6 changes: 4 additions & 2 deletions tests/testthat/test-parse_citation.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
testthat::context("parse citation")


testthat::test_that("We can parse bibentry citations into schema.org",{

bib <- citation("knitr") # Manual, Book, Incollection
Expand All @@ -14,7 +13,6 @@ testthat::test_that("We can parse bibentry citations into schema.org",{
})

testthat::test_that("We can parse citations", {

## installed package
a <- guess_citation("knitr")

Expand All @@ -32,3 +30,7 @@ testthat::test_that("We can use encoding", {
testthat::expect_silent(parse_citation(bib))
})

testthat::test_that("Test citation with encoding and citation line", {
f <- system.file("examples/CITATION_ex2", package = "codemetar")
testthat::expect_s3_class(read_citation_with_encoding(f), "citation")
})

0 comments on commit 929a41a

Please sign in to comment.