From 6c9daef93eb8686515ff97331e1f33c164d1190f Mon Sep 17 00:00:00 2001 From: Jens Kober Date: Thu, 21 Mar 2019 08:53:49 +0100 Subject: [PATCH] feat: add support for Oxford commas in name lists (#95) --- bibtexbrowser-test.php | 13 +++++++++++-- bibtexbrowser.php | 9 ++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 85a157d..e37aff6 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -486,7 +486,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { function test_formatting() { - $bibtex = "@article{aKey61,title={An article Book},author = {Meyer, Heribert and {Advanced Air and Ground Research Team} and Foo Bar}}\n"; + $bibtex = "@article{aKey61,title={An article Book},author = {Meyer, Heribert and {Advanced Air and Ground Research Team} and Foo Bar}}\n@article{bKey61,title={An article Book},author = {Meyer, Heribert and Foo Bar}}\n"; $test_data = fopen('php://memory','x+'); fwrite($test_data, $bibtex); fseek($test_data,0); @@ -531,7 +531,16 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals("Advanced Air and Ground Research Team", $authors[1]); $this->assertEquals("Foo Bar", $authors[2]); $this->assertEquals("Heribert Meyer, Advanced Air and Ground Research Team and Foo Bar", $entry->getFormattedAuthorsString()); - + + // test Oxford comma with default options + bibtexbrowser_configure('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT', false); + bibtexbrowser_configure('USE_INITIALS_FOR_NAMES', false); + bibtexbrowser_configure('USE_FIRST_THEN_LAST', false); + bibtexbrowser_configure('USE_OXFORD_COMMA', true); + $this->assertEquals("Meyer, Heribert, Advanced Air and Ground Research Team, and Foo Bar", $entry->getFormattedAuthorsString()); + $entry = $db->getEntryByKey('bKey61'); + $this->assertEquals("Meyer, Heribert and Foo Bar", $entry->getFormattedAuthorsString()); + bibtexbrowser_configure('USE_OXFORD_COMMA', false); } function test_parsing_author_list() { diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 0a83c91..aacbf76 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -171,6 +171,7 @@ if (defined('ENCODING')) { @define('USE_FIRST_THEN_LAST',false); // put first names before last names? @define('FORCE_NAMELIST_SEPARATOR', ''); // if non-empty, use this to separate multiple names regardless of USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT @define('LAST_AUTHOR_SEPARATOR',' and '); +@define('USE_OXFORD_COMMA',false); // adds an additional separator in addition to LAST_AUTHOR_SEPARATOR if there are more than two authors @define('TYPES_SIZE',10); // number of entry types per table @define('YEAR_SIZE',20); // number of years per table @@ -1570,7 +1571,13 @@ class BibEntry { for ($i=0;$i2) { + $lastAuthorSeperator = $sep.$lastAuthorSeperator; + $lastAuthorSeperator = preg_replace("/ {2,}/", " ", $lastAuthorSeperator); // get rid of double spaces + } + $result .= $authors[count($authors)-2].$lastAuthorSeperator.$authors[count($authors)-1]; return $result; }