From dc4419678abd345c7bc0af8e13c1ae6feff82f4b Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Sat, 25 Nov 2017 23:07:35 +0100 Subject: [PATCH] add support for overrding numeric index assignment --- .travis.yml | 5 ++- bibtexbrowser-documentation.wiki | 29 ++++++++++++++++- bibtexbrowser-test.php | 48 ++++++++++++++++++++++++++- bibtexbrowser.php | 56 +++++++++++++++++++++++--------- 4 files changed, 118 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2531277..e802053 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,11 +4,10 @@ php: - '5.4' - '5.5' - '5.6' - - '7.0' script: - - curl -o reflectivedoc.php http://www.monperrus.net/martin/reflectivedoc.php.txt - - curl -o gakowiki-syntax.php http://www.monperrus.net/martin/gakowiki-syntax.php.txt + - curl -L -o reflectivedoc.php https://www.monperrus.net/martin/reflectivedoc.php.txt + - curl -L -o gakowiki-syntax.php https://www.monperrus.net/martin/gakowiki-syntax.php.txt - phpunit bibtexbrowser-test.php sudo: false diff --git a/bibtexbrowser-documentation.wiki b/bibtexbrowser-documentation.wiki index 2bd45a0..cffce94 100755 --- a/bibtexbrowser-documentation.wiki +++ b/bibtexbrowser-documentation.wiki @@ -241,14 +241,41 @@ add into ''bibtexbrowser.local.php'': ''@define('BIBTEXBROWSER_BIBTEX_LINKS',false);'' ====How to change the reference indices?==== +The configuration of ABBRV_TYPE drives the indices
-
 // index => [1] The essence of metamodeling
 // year => [2005] The essence of metamodeling
 // x-abbrv => [SoSyM] The essence of metamodeling if the bibtex entry contains a field x-abbrv
 define('ABBRV_TYPE','year');// may be year/x-abbrv/key/none/index
 
+One can also extend class SimpleDisplay to tweak the indices. + +For instance, this configuration ... +
+// bibtexbrowser.local.php
+class SimpleDisplayExt extends SimpleDisplay {
+  // overriding the default
+  function setIndices() {
+    $this->setIndicesInIncreasingOrderChangingEveryYear();
+  }
+}
+bibtexbrowser_configure('BIBTEXBROWSER_DEFAULT_DISPLAY','SimpleDisplayExt');
+
+results in resetting the numeric indices every year as follows +
+2017
+[1] article1... (2017) [pdf] [doi]
+[2] article2... (2017) [pdf] [doi]
+[3] article3... (2017) [pdf] [doi]
+etc..
+
+2016
+[1] article1... (2017) [pdf] [doi]
+[2] article2... (2017) [pdf] [doi]
+[3] article3... (2017) [pdf] [doi]
+
+ ====How to use the "Academic style"?==== diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 7a5351f..6c2f558 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -44,6 +44,12 @@ if(is_file('reflectivedoc.php')) { require_once ('bibtexbrowser.php'); } +class SimpleDisplayExt extends SimpleDisplay { + function setIndices() { + $this->setIndicesInIncreasingOrderChangingEveryYear(); + } +} + class BTBTest extends PHPUnit_Framework_TestCase { @@ -55,7 +61,7 @@ class BTBTest extends PHPUnit_Framework_TestCase { function createDB() { return $this->_createDB("@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n" - ."@book{aKey/withSlash,title={Slash Dangerous for web servers},author={Ap Ache},publisher={Springer},year=2009}\n" + ."@book{aKey/withSlash,title={Slash Dangerous for web servers},author={Ap Ache},publisher={Springer},year=2010}\n" ."@article{aKeyA,title={An Article},author={Foo Bar and Jane Doe},volume=5,journal=\"New Results\",year=2009,pages={1-2}}\n"); } @@ -132,6 +138,21 @@ class BTBTest extends PHPUnit_Framework_TestCase { bibtexbrowser_configure('BIBTEXBROWSER_LINKS_TARGET','_top'); $this->assertEquals('An Article ( and ), In New Results, volume 5, . ',$first_entry->toHTML()); + // testing ABBRV_TYPE + bibtexbrowser_configure('ABBRV_TYPE','year'); + $this->assertEquals("[2009]",$first_entry->getAbbrv()); + bibtexbrowser_configure('ABBRV_TYPE','key'); + $this->assertEquals("[aKeyA]",$first_entry->getAbbrv()); + bibtexbrowser_configure('ABBRV_TYPE','index'); + $this->assertEquals("[]",$first_entry->getAbbrv()); + $first_entry->setIndex('foo'); + $this->assertEquals("[foo]",$first_entry->getAbbrv()); + bibtexbrowser_configure('ABBRV_TYPE','none'); + $this->assertEquals("",$first_entry->getAbbrv()); + + // resetting the default link style + bibtexbrowser_configure('BIBTEXBROWSER_LINK_STYLE','bib2links_default'); + } function testMultiSearch() { @@ -578,6 +599,31 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals("@string{foo={Foo}}",$btb->stringdb['foo']->toString()); } + function test_indices() { + $btb = $this->createDB(); + $this->assertEquals("[]", $btb->getEntryByKey('aKey')->getAbbrv()); + + $d = new SimpleDisplay(); + $d->setDB($btb); + ob_start(); + $d->display(); + ob_get_clean(); + + // the indices have been set by SimpleDisplay + $this->assertEquals("[1]", $btb->getEntryByKey('aKey')->getAbbrv()); + $this->assertEquals("[3]", $btb->getEntryByKey('aKey-withSlash')->getAbbrv()); + + // SimpleDisplayExt sets the indices differently, using setIndicesInIncreasingOrderChangingEveryYear + $d = new SimpleDisplayExt(); + $d->setDB($btb); + ob_start(); + $d->display(); + ob_get_clean(); + $this->assertEquals("[2]", $btb->getEntryByKey('aKey')->getAbbrv()); + $this->assertEquals("[1]", $btb->getEntryByKey('aKey-withSlash')->getAbbrv()); + + } + function test_identity() { $btb = new BibDataBase(); $btb->load('bibacid-utf8.bib'); diff --git a/bibtexbrowser.php b/bibtexbrowser.php index e4ef317..fc1dd03 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1676,28 +1676,28 @@ class BibEntry { /** Returns the raw, undecorated abbreviation depending on ABBRV_TYPE. */ function getRawAbbrv() { - if (ABBRV_TYPE == 'index') return $this->index; - if (ABBRV_TYPE == 'none') return ''; - if (ABBRV_TYPE == 'key') return $this->getKey(); - if (ABBRV_TYPE == 'year') return $this->getYear(); - if (ABBRV_TYPE == 'x-abbrv') { + if (c('ABBRV_TYPE') == 'index') return $this->index; + if (c('ABBRV_TYPE') == 'none') return ''; + if (c('ABBRV_TYPE') == 'key') return $this->getKey(); + if (c('ABBRV_TYPE') == 'year') return $this->getYear(); + if (c('ABBRV_TYPE') == 'x-abbrv') { if ($this->hasField('x-abbrv')) {return $this->getField('x-abbrv');} return $this->abbrv; } - if (ABBRV_TYPE == 'keys-index') { + if (c('ABBRV_TYPE') == 'keys-index') { if (isset($_GET[Q_INNER_KEYS_INDEX])) {return $_GET[Q_INNER_KEYS_INDEX][$this->getKey()]; } return ''; } // otherwise it is a user-defined function in bibtexbrowser.local.php - $f = ABBRV_TYPE; + $f = c('ABBRV_TYPE'); return $f($this); } /** Returns the abbreviation, etc [1] if ABBRV_TYPE='index'. */ function getAbbrv() { $abbrv = $this->getRawAbbrv(); - if ( ABBRV_TYPE != 'none' ) { + if ( c('ABBRV_TYPE') != 'none' ) { $abbrv = '['.$abbrv.']'; } return $abbrv; @@ -1768,7 +1768,7 @@ class BibEntry { break; case 'definition': $result .= '
'; - if (ABBRV_TYPE=='none') { die ('Cannot define an empty term!'); } + if (c('ABBRV_TYPE')=='none') { die ('Cannot define an empty term!'); } break; case 'none': break; @@ -3133,11 +3133,41 @@ class SimpleDisplay { return _DefaultBibliographyTitle($this->query); } + function setIndices() { + $this->setIndicesInDecreasingOrder(); + } + + function setIndicesInIncreasingOrderChangingEveryYear() { + $i=1; + $pred = NULL; + foreach ($this->entries as $bib) { + if ($this->changeSection($pred, $bib)) { + $i=1; + } + $bib->setIndex($i++); + $pred = $bib; + } // end foreach + } + + function setIndicesInDecreasingOrder() { + $count = count($this->entries); + $i=0; + foreach ($this->entries as $bib) { + // by default, index are in decreasing order + // so that when you add a publicaton recent , the indices of preceding publications don't change + $bib->setIndex($count-($i++)); + } // end foreach + } + /** Displays a set of bibtex entries in an HTML table */ function display() { uasort($this->entries, 'compare_bib_entries'); + // now that the entries are sorted, setting the index of entries + // this function can be overloaded + $this->setIndices(); + if ($this->options) { foreach($this->options as $fname=>$opt) { $this->$fname($opt,$entries); @@ -3147,7 +3177,7 @@ class SimpleDisplay { if (BIBTEXBROWSER_DEBUG) { echo 'Style: '.bibtexbrowser_configuration('BIBLIOGRAPHYSTYLE').'
'; echo 'Order: '.ORDER_FUNCTION.'
'; - echo 'Abbrv: '.ABBRV_TYPE.'
'; + echo 'Abbrv: '.c('ABBRV_TYPE').'
'; echo 'Options: '.@implode(',',$this->options).'
'; } @@ -3162,16 +3192,12 @@ class SimpleDisplay { } print_header_layout(); - $count = count($this->entries); - $i=0; $pred = NULL; foreach ($this->entries as $bib) { if ($this->changeSection($pred, $bib)) { echo $this->sectionHeader($bib, $pred); } - // by default, index are in decreasing order - // so that when you add a publicaton recent , the indices of preceding publications don't change - $bib->setIndex($count-($i++)); + echo $bib->toHTML(true); $pred = $bib;