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

Add argument to toBiblatex and toBibtex to toggle LaTeX conversion behaviour #108

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RefManageR
Version: 1.4.0
Version: 1.4.2
Title: Straightforward 'BibTeX' and 'BibLaTeX' Bibliography Management
Authors@R: person(c("Mathew", "W."), "McLean", role = c("aut", "cre"),
email = "[email protected]",
Expand Down Expand Up @@ -39,4 +39,4 @@ Depends:
VignetteBuilder: knitr
BugReports: https://github.com/ropensci/RefManageR/issues
URL: https://github.com/ropensci/RefManageR/
RoxygenNote: 7.2.1
RoxygenNote: 7.3.2
20 changes: 4 additions & 16 deletions R/RefManageR-package.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
#' Import and Manage BibTeX and BibLaTeX references with RefManageR
#'
#' RefManageR provides tools for importing and working with
#' bibliographic references. It greatly enhances the bibentry class by
#' providing a class BibEntry which stores BibTeX and BibLaTeX references,
#' supports UTF-8 encoding, and can be easily searched by any field, by date
#' ranges, and by various formats for name lists (author by last names,
#' translator by full names, etc.). Entries can be updated, combined, sorted, printed
#' in a number of styles, and exported. BibTeX and BibLaTeX .bib files can be
#' read into R and converted to BibEntry objects. Interfaces to NCBI's
#' Entrez, CrossRef, and Zotero are provided for importing references and
#' references can be created from locally stored PDFs using Poppler. Includes
#' functions for citing and generating a bibliography with hyperlinks for
#' documents prepared with RMarkdown or RHTML.
#' @keywords internal
"_PACKAGE"

#' @name RefManageR-package
#' @aliases RefManageR refmanager
#' @docType package
#' @author McLean, M. W. \email{mathew.w.mclean@@gmail.com}
#' @details
#' \bold{Importing and Creating References}
Expand Down Expand Up @@ -56,7 +44,7 @@
#' \code{\link{Cite}}. Its interface is similar to \code{\link{options}}.
#' @keywords package
#' @references McLean, M. W. (2014). Straightforward Bibliography Management in R Using the RefManageR Package.
#' \href{https://arxiv.org/abs/1403.2036}{arXiv: 1403.2036 [cs.DL]}. Submitted.
#' \href{https://arxiv.org/abs/1403.2036}{arXiv: 1403.2036 [cs.DL]}.
#' @references Kime, P., M. Wemheuer, and P. Lehman (2022). The biblatex Package.
#' \url{http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/biblatex/doc/biblatex.pdf}.
#' @references Hornik, K., D. Murdoch, and A. Zeileis (2012).
Expand Down
1 change: 0 additions & 1 deletion R/WriteBib.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#' @note To write the contents of \code{bib} \dQuote{as is}, the argument
#' \code{biblatex} should be \code{TRUE}, otherwise
#' conversion is done as in \code{\link{toBibtex.BibEntry}}.
#' @importFrom tools encoded_text_to_latex
#' @author McLean, M. W. based on \code{write.bib} by Gaujoux, R.
#' in package \code{bibtex}.
#' @export
Expand Down
29 changes: 24 additions & 5 deletions R/toBiblatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' @param extra.fields character vector; fields that are not supported in standard BibTeX styles are by default dropped
#' in the result return by the toBibtex function.
#' Any fields specified in extra.fields will \emph{not} be dropped if present in an entry.
#' @param encoded.names.to.latex if \code{TRUE} (the default) then name list fields
#' such as \sQuote{author} and \sQuote{editor} will have non-ASCII characters
#' translated to LaTeX escape sequences by \code{\link{encoded_text_to_latex}}.
#' @param ... ignored
#' @export
#' @return an object of class \dQuote{Bibtex} - character vectors where each element holds one line of a BibTeX or BibLaTeX file
Expand Down Expand Up @@ -50,7 +53,7 @@
#' }
#' @seealso \code{\link{toBibtex}}, \code{\link{BibEntry}}, \code{\link{print.BibEntry}}
#' @author McLean, M. W. \email{mathew.w.mclean@@gmail.com}
#' @importFrom tools encoded_text_to_latex parseLatex deparseLatex latexToUtf8
#' @importFrom tools parseLatex deparseLatex latexToUtf8
#' @keywords database IO utilities
#' @aliases toBibtex.BibEntry toBibtex
#' @examples
Expand All @@ -60,14 +63,16 @@
#' toBiblatex(bib[70:72])
#' toBibtex(bib[70:72])
#' }
toBiblatex <- function(object, ...){
toBiblatex <- function(object, encoded.names.to.latex = TRUE, ...){
format_bibentry1 <- function(object) {
object <- unclass(object)[[1L]]
rval <- paste0("@", attr(object, "bibtype"), "{", attr(object,
"key"), ",")
nl.ind <- which(names(object) %in% .BibEntryNameList)
for (i in nl.ind)
object[i] <- encoded_text_to_latex(format_author(object[[i]]), "UTF-8")
if (encoded.names.to.latex) {
nl.ind <- which(names(object) %in% .BibEntryNameList)
for (i in nl.ind)
object[i] <- EncodedNameListToLaTeX(object[[i]])
}
rval <- c(rval, vapply(names(object), function(n) paste0(" ",
n, " = {", object[[n]], "},"), ""), "}", "")
return(rval)
Expand All @@ -81,3 +86,17 @@ toBiblatex <- function(object, ...){
class(rval) <- "Bibtex"
rval
}

#' Wrapper for tools:: encoded_text_to_latex that returns original
#' text if translation to LaTeX fails
#' @importFrom tools encoded_text_to_latex
#' @noRd
#' @seealso \url{https://github.com/ropensci/RefManageR/issues/106}
EncodedNameListToLaTeX <- function(name.list, encoding = "UTF-8")
{
formatted.text <- format_author(name.list)
out <- encoded_text_to_latex(formatted.text, encoding)
if (grepl("^[?]", out))
return(formatted.text)
return(out)
}
25 changes: 15 additions & 10 deletions R/toBibtex.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
toBibtex.BibEntry <- function(object,
note.replace.field = c('urldate', "pubsate",
"addendum"),
extra.fields = NULL, ...){
extra.fields = NULL,
encoded.names.to.latex = TRUE,
...){


object <- .BibEntry_expand_crossrefs(unclass(object), to.bibtex = TRUE)
if (length(object)) {
object$.index <- NULL
rval <- head(unlist(lapply(object, ConvertToBibtex,
note.replace.field, extra.fields)),
note.replace.field, extra.fields,
encoded.names.to.latex)),
-1L)
}
else rval <- character()
Expand All @@ -21,18 +24,20 @@ toBibtex.BibEntry <- function(object,
}

#' @noRd
ConvertToBibtex <- function(object, note.replace.field, extra.fields){
ConvertToBibtex <- function(object,
note.replace.field,
extra.fields,
encoded.names.to.latex) {
object <- unclass(object)[[1L]]
bibtype <- tolower(attr(object, "bibtype"))
obj.names <- names(object)
if ("author" %in% obj.names)
object$author <- encoded_text_to_latex(format_author(object$author),
"UTF-8")
if ("editor" %in% obj.names)
object$editor <- encoded_text_to_latex(format_author(object$editor),
"UTF-8")
if (encoded.names.to.latex) {
if ("author" %in% obj.names)
object$author <- EncodedNameListToLaTeX(object$author)
if ("editor" %in% obj.names)
object$editor <- EncodedNameListToLaTeX(object$editor)
}
# see 2.3 Usage Notes p. 28

if (bibtype == "article" && 'journaltitle' %in% obj.names &&
is.null(object$journal))
object$journal <- object$journaltitle
Expand Down
9 changes: 9 additions & 0 deletions inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Changes in Version 1.4.1 (2024-09-23)
========================================================
* Fix for `toBibLaTeX` and `toBibTeX` non-ASCII name list fields. Add escape
hatch if `tools::encoded_text_to_latex` fails to convert name lists to valid
LaTeX (observed for Japanese names in #106 h/t @kijinosu).
* Add argument `encoded.names.to.latex` to `toBibTeX` and `toBibLaTeX` to
toggle whether `tools::encoded_text_to_latex` is called on name list fields
(requested in #105)

Changes in Version 1.4.0 (2022-09-30)
========================================================

Expand Down
29 changes: 15 additions & 14 deletions man/RefManageR-package.Rd

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

7 changes: 6 additions & 1 deletion man/toBiblatex.Rd

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

45 changes: 45 additions & 0 deletions tests/testthat/test-tobiblatex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
bib = BibEntry(bibtype = "article",
key = "shiotsuki2011kasai",
title = "葛西賢太著,『現代瞑想論-変性意識がひらく世界-』",
author = "塩,亮子 and 葛西,賢太",
journal = "宗教と社会",
volume = 17,
pages = "67--69",
year = 2011,
publisher = "「宗教と社会」学会")

test_that("toBiblatex doesn't replace CJK character name lists with ???? (#106)",
{
out <- toBiblatex(bib)
expect_false(any(grepl("[?]", out)))
})

test_that("toBibtex doesn't replace CJK character name lists with ???? (#106)",
{
out <- toBibtex(bib)
expect_false(any(grepl("[?]", out)))
})


bib <- BibEntry(bibtype = "Article", key = "smith2014", title = "An Article Title",
author = "Smith, Joé", journaltitle = "The Journal Title",
date = "2014-02-06", pubstate = "forthcoming")
test_that("toBiblatex can toggle encoding names to LaTeX (#105)",
{
out <- toBiblatex(bib, encoded.names.to.latex = TRUE)
out.author = out[3]
expect_true(grepl("{\\a'e}", out.author, fixed = TRUE))
out <- toBiblatex(bib, encoded.names.to.latex = FALSE)
out.author = out[3]
expect_false(grepl("{\\a'e}", out.author, fixed = TRUE))
})

test_that("toBibtex can toggle encoding names to LaTeX (#105)",
{
out <- toBibtex(bib, encoded.names.to.latex = TRUE)
out.author = out[3]
expect_true(grepl("{\\a'e}", out.author, fixed = TRUE))
out <- toBibtex(bib, encoded.names.to.latex = FALSE)
out.author = out[3]
expect_false(grepl("{\\a'e}", out.author, fixed = TRUE))
})
Loading