From 91070361ab74aee8e33ec88fc61e646fa2786a7a Mon Sep 17 00:00:00 2001 From: Markus Jochim Date: Fri, 19 Sep 2014 12:57:31 +0200 Subject: [PATCH] i18n and graceful sorting for year values 'in press', 'submitted' and 'accepted' The sorting mechanism relied solely on strcmp before, which yielded a more or less random result. It is now possible to define an order as long as the strings in the bibtex file are given in their standard English form. In output though, the strings are internationalised. --- bibtexbrowser.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/bibtexbrowser.php b/bibtexbrowser.php index e1d3bb1..20f54b8 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -122,6 +122,9 @@ function bibtexbrowser_configure($key, $value) { @define('READLINE_LIMIT',1024); @define('Q_YEAR', 'year'); @define('Q_YEAR_PAGE', 'year_page'); +@define('Q_YEAR_INPRESS', 'in press'); +@define('Q_YEAR_ACCEPTED', 'accepted'); +@define('Q_YEAR_SUBMITTED', 'submitted'); @define('Q_FILE', 'bib'); @define('Q_AUTHOR', 'author'); @define('Q_AUTHOR_PAGE', 'author_page'); @@ -1376,6 +1379,9 @@ class BibEntry { /** Returns the year of this entry? */ function getYear() { + return __(strtolower($this->getField('year'))); + } + function getYearRaw() { return $this->getField('year'); } @@ -1728,7 +1734,47 @@ function compare_bib_entry_by_mtime($a, $b) */ function compare_bib_entry_by_year($a, $b) { - return -strcmp($a->getYear(),$b->getYear()); + $yearA = (int) $a->getYear(); + $yearB = (int) $b->getYear(); + + if ($yearA === 0) { + switch (strtolower($a->getYearRaw())) { + case Q_YEAR_INPRESS: + $yearA = PHP_INT_MAX; + break; + case Q_YEAR_ACCEPTED: + $yearA = PHP_INT_MAX - 1; + break; + case Q_YEAR_SUBMITTED: + $yearA = PHP_INT_MAX - 2; + break; + default: + $yearA = PHP_INT_MAX - 3; + } + } + + if ($yearB === 0) { + switch (strtolower($b->getYearRaw())) { + case 'in press': + $yearB = PHP_INT_MAX; + break; + case 'accepted': + $yearB = PHP_INT_MAX - 1; + break; + case 'submitted': + $yearB = PHP_INT_MAX - 2; + break; + default: + $yearB = PHP_INT_MAX - 3; + } + } + + if ($yearA === $yearB) + return 0; + else if ($yearA > $yearB) + return -1; + else + return 1; } /** compares two instances of BibEntry by title