From 85a42ceb7c6d0607d624b5fdd7140bdd6d86fa22 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Fri, 19 Aug 2016 19:02:01 +0200 Subject: [PATCH] fix bugs in author menu --- bibtexbrowser-cli.php | 0 bibtexbrowser-test.php | 15 +++++++++++ bibtexbrowser.php | 59 +++++++++++++++++++++++------------------- 3 files changed, 48 insertions(+), 26 deletions(-) mode change 100644 => 100755 bibtexbrowser-cli.php diff --git a/bibtexbrowser-cli.php b/bibtexbrowser-cli.php old mode 100644 new mode 100755 diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 1a248f9..a0cab00 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -470,6 +470,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals("Advanced Air and Ground Research Team", $authors[1]); $this->assertEquals("Foo Bar", $authors[2]); $this->assertEquals("J{\'e} Ko", $authors[3]); + $this->assertEquals("Jé Ko", $entry->getFormattedAuthorsArray()[3]); } function test_latex2html() { @@ -493,6 +494,20 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals('Monperrus, Martin', $authors[2]); } + function test_author_index() { + bibtexbrowser_configure('USE_FIRST_THEN_LAST', true); + + $bibtex = "@string{hp_MartinMonperrus={http://www.monperrus.net/martin},hp_FooAcé={http://example.net/}},@article{aKey61,title={An article Book},author = {Martin Monperrus and Foo Ac\'e and Monperrus, Martin}}\n"; + $test_data = fopen('php://memory','x+'); + fwrite($test_data, $bibtex); + fseek($test_data,0); + $db = new BibDataBase(); + $db->update_internal("inline", $test_data); + + $index = var_export($db->authorIndex(), true); + $this->assertEquals("array (\n 'Foo Acé' => 'Foo Acé',\n 'Martin Monperrus' => 'Martin Monperrus',\n)", $index); + } + function test_identity() { $btb = new BibDataBase(); $btb->load('bibacid-utf8.bib'); diff --git a/bibtexbrowser.php b/bibtexbrowser.php index e4837c0..532e26d 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1187,6 +1187,19 @@ class BibEntry { $this->setField(Q_KEY,str_replace('/','-',$value)); } + function transformValue($value) { + // trim space + $value = xtrim($value); + + // transform Latex markup to HTML entities (easier than a one to one mapping to each character) + // HTML entity is an intermediate format + $value = latex2html($value); + + // transform to the target output encoding + $value = html_entity_decode($value, ENT_QUOTES|ENT_XHTML, OUTPUT_ENCODING); + return $value; + } + /** Sets a field of this bib entry. */ function setField($name, $value) { $name = strtolower($name); @@ -1197,16 +1210,9 @@ class BibEntry { // but instead could contain HTML code (with links using the character "~" for example) // so "comment" is not transformed too if ($name!='url' && $name!='comment') { - // 1. trim space - $value = xtrim($value); if (c('BIBTEXBROWSER_USE_LATEX2HTML')) { - // 2. transform Latex markup to HTML entities (easier than a one to one mapping to each character) - // HTML entity is an intermediate format - $value = latex2html($value); - - // 3. transform to the target output encoding - $value = html_entity_decode($value, ENT_QUOTES|ENT_XHTML, OUTPUT_ENCODING); + $value = $this->transformValue($value); } // 4. transform existing encoded character in the new format @@ -1442,16 +1448,20 @@ class BibEntry { * encoded in USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT and USE_INITIALS_FOR_NAMES */ function formatAuthor($author){ + $author = $this->transformValue($author); if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) { return $this->formatAuthorCommaSeparated($author); - } else if (bibtexbrowser_configuration('USE_INITIALS_FOR_NAMES')) { + } + + if (bibtexbrowser_configuration('USE_INITIALS_FOR_NAMES')) { return $this->formatAuthorInitials($author); - } else if (bibtexbrowser_configuration('USE_FIRST_THEN_LAST')) { + } + + if (bibtexbrowser_configuration('USE_FIRST_THEN_LAST')) { return $this->formatAuthorCanonical($author); - } - - // otherwise to formatting - else return $author; + } + + return $author; } /** @@ -3718,24 +3728,21 @@ class BibDataBase { * and values . */ function authorIndex(){ - $result = array(); + $tmp = array(); foreach ($this->bibdb as $bib) { - foreach($bib->getRawAuthors() as $a){ + foreach($bib->getFormattedAuthorsArray() as $a){ + $a = strip_tags($a); //we use an array because several authors can have the same lastname - @$result[$bib->getLastName($a)][$bib->formatAuthor($a)]++; + @$tmp[$bib->getLastName($a)]=$a; } } - ksort($result); - - // now authors are sorted by last name - // we rebuild a new array for having good keys in author page - $realresult = array(); - foreach($result as $x) { - ksort($x); - foreach($x as $v => $tmp) $realresult[$v] = $v; + ksort($tmp); + $result=array(); + foreach ($tmp as $k=>$v) { + $result[$v]=$v; } - return $realresult; + return $result; } /** Generates and returns an array consisting of all tags.