fix bugs in author menu

This commit is contained in:
Martin Monperrus
2016-08-20 07:52:32 +02:00
parent d026780fed
commit 85a42ceb7c
3 changed files with 48 additions and 26 deletions
Regular → Executable
View File
+15
View File
@@ -470,6 +470,7 @@ class BTBTest extends PHPUnit_Framework_TestCase {
$this->assertEquals("Advanced Air and Ground Research Team", $authors[1]); $this->assertEquals("Advanced Air and Ground Research Team", $authors[1]);
$this->assertEquals("Foo Bar", $authors[2]); $this->assertEquals("Foo Bar", $authors[2]);
$this->assertEquals("J{\'e} Ko", $authors[3]); $this->assertEquals("J{\'e} Ko", $authors[3]);
$this->assertEquals("Jé Ko", $entry->getFormattedAuthorsArray()[3]);
} }
function test_latex2html() { function test_latex2html() {
@@ -493,6 +494,20 @@ class BTBTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('<a href="http://www.monperrus.net/martin">Monperrus, Martin</a>', $authors[2]); $this->assertEquals('<a href="http://www.monperrus.net/martin">Monperrus, Martin</a>', $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() { function test_identity() {
$btb = new BibDataBase(); $btb = new BibDataBase();
$btb->load('bibacid-utf8.bib'); $btb->load('bibacid-utf8.bib');
+33 -26
View File
@@ -1187,6 +1187,19 @@ class BibEntry {
$this->setField(Q_KEY,str_replace('/','-',$value)); $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. */ /** Sets a field of this bib entry. */
function setField($name, $value) { function setField($name, $value) {
$name = strtolower($name); $name = strtolower($name);
@@ -1197,16 +1210,9 @@ class BibEntry {
// but instead could contain HTML code (with links using the character "~" for example) // but instead could contain HTML code (with links using the character "~" for example)
// so "comment" is not transformed too // so "comment" is not transformed too
if ($name!='url' && $name!='comment') { if ($name!='url' && $name!='comment') {
// 1. trim space
$value = xtrim($value);
if (c('BIBTEXBROWSER_USE_LATEX2HTML')) { if (c('BIBTEXBROWSER_USE_LATEX2HTML')) {
// 2. transform Latex markup to HTML entities (easier than a one to one mapping to each character) $value = $this->transformValue($value);
// 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);
} }
// 4. transform existing encoded character in the new format // 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 * encoded in USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT and USE_INITIALS_FOR_NAMES
*/ */
function formatAuthor($author){ function formatAuthor($author){
$author = $this->transformValue($author);
if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) { if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) {
return $this->formatAuthorCommaSeparated($author); return $this->formatAuthorCommaSeparated($author);
} else if (bibtexbrowser_configuration('USE_INITIALS_FOR_NAMES')) { }
if (bibtexbrowser_configuration('USE_INITIALS_FOR_NAMES')) {
return $this->formatAuthorInitials($author); return $this->formatAuthorInitials($author);
} else if (bibtexbrowser_configuration('USE_FIRST_THEN_LAST')) { }
if (bibtexbrowser_configuration('USE_FIRST_THEN_LAST')) {
return $this->formatAuthorCanonical($author); return $this->formatAuthorCanonical($author);
} }
// otherwise to formatting return $author;
else return $author;
} }
/** /**
@@ -3718,24 +3728,21 @@ class BibDataBase {
* and values <LastName, FirstName>. * and values <LastName, FirstName>.
*/ */
function authorIndex(){ function authorIndex(){
$result = array(); $tmp = array();
foreach ($this->bibdb as $bib) { 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 //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); ksort($tmp);
$result=array();
// now authors are sorted by last name foreach ($tmp as $k=>$v) {
// we rebuild a new array for having good keys in author page $result[$v]=$v;
$realresult = array();
foreach($result as $x) {
ksort($x);
foreach($x as $v => $tmp) $realresult[$v] = $v;
} }
return $realresult; return $result;
} }
/** Generates and returns an array consisting of all tags. /** Generates and returns an array consisting of all tags.