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

Fix toBiblatex when BibEntry has Japanese author names #107

Open
wants to merge 4 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RefManageR
Version: 1.4.0
Version: 1.4.1
Title: Straightforward 'BibTeX' and 'BibLaTeX' Bibliography Management
Authors@R: person(c("Mathew", "W."), "McLean", role = c("aut", "cre"),
email = "[email protected]",
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
18 changes: 16 additions & 2 deletions R/toBiblatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 @@ -67,7 +67,7 @@ toBiblatex <- function(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")
object[i] <- EncodedNameListToLaTeX(object[[i]])
rval <- c(rval, vapply(names(object), function(n) paste0(" ",
n, " = {", object[[n]], "},"), ""), "}", "")
return(rval)
Expand All @@ -81,3 +81,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)
}
6 changes: 2 additions & 4 deletions R/toBibtex.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ ConvertToBibtex <- function(object, note.replace.field, extra.fields){
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")
object$author <- EncodedNameListToLaTeX(object$author)
if ("editor" %in% obj.names)
object$editor <- encoded_text_to_latex(format_author(object$editor),
"UTF-8")
object$editor <- EncodedNameListToLaTeX(object$editor)
# see 2.3 Usage Notes p. 28

if (bibtype == "article" && 'journaltitle' %in% obj.names &&
Expand Down
7 changes: 7 additions & 0 deletions inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
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).


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

Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-tobiblatex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
bib <- c(BibEntry(bibtype = "article",
key = "shiotsuki2011kasai",
title = "葛西賢太著,『現代瞑想論-変性意識がひらく世界-』",
author = "塩,亮子 and 葛西,賢太",
journal = "宗教と社会",
volume = 17,
pages = "67--69",
year = 2011,
publisher = "「宗教と社会」学会"),
BibEntry(bibtype = "article",
key = "hiromitsu2022altered",
title = "意識状態の変容と脳内ネットワーク",
author = "弘光健太郎 and ヒロミツケンタロウ",
journal = "鶴見大学仏教文化研究所紀要",
volume = 27,
pages = "53--66",
year = 2022,
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)))
})
Loading