From 8474129a57d945fa5e858a8ed107d6b4bf68bba3 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 17 Aug 2016 12:22:17 +0200 Subject: [PATCH] add support for command-line usage --- bibtexbrowser-cli.php | 10 ++++++++ bibtexbrowser-documentation.wiki | 8 ++++++ bibtexbrowser-test.php | 42 +++++++++++++++++++++++++++++--- bibtexbrowser.php | 34 +++++++++++++++++++++++++- 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 bibtexbrowser-cli.php diff --git a/bibtexbrowser-cli.php b/bibtexbrowser-cli.php new file mode 100644 index 0000000..b53b93d --- /dev/null +++ b/bibtexbrowser-cli.php @@ -0,0 +1,10 @@ + diff --git a/bibtexbrowser-documentation.wiki b/bibtexbrowser-documentation.wiki index 0ec3ff6..5e2116c 100755 --- a/bibtexbrowser-documentation.wiki +++ b/bibtexbrowser-documentation.wiki @@ -505,6 +505,14 @@ You must add a ''@string'' block in the bibtex file, which contains the links. T } +====How to modify bibtex entries on the command line?==== + +Use ''bibtexbrowser-cli.php''. For instance: + +
+php bibtexbrowser-cli.php mybib.bib --id classical --set-title \"a new title\" --id with_abstract --set-title \"a new title\" --set-year 1990
+
+ =====Related tools===== Old-fashioned: diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 83e56fa..e5b2927 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -27,11 +27,14 @@ class BTBTest extends PHPUnit_Framework_TestCase { } function createDB() { - $test_data = fopen('php://memory','x+'); - fwrite($test_data, "@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n" + 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" - ."@article{aKeyA,title={An Article},author={Foo Bar and Jane Doe},volume=5,journal=\"New Results\",year=2009,pages={1-2}}\n" - ); + ."@article{aKeyA,title={An Article},author={Foo Bar and Jane Doe},volume=5,journal=\"New Results\",year=2009,pages={1-2}}\n"); + } + + function _createDB($content) { + $test_data = fopen('php://memory','x+'); + fwrite($test_data, $content); fseek($test_data,0); $btb = new BibDataBase(); $btb->update_internal("inline", $test_data); @@ -487,6 +490,37 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals('Foo Acé', $authors[1]); $this->assertEquals('Monperrus, Martin', $authors[2]); } + + function test_identity() { + $btb = new BibDataBase(); + $btb->load('bibacid-utf8.bib'); + $nref = count($btb->bibdb); + $bibtex = $btb->toBibtex(); + // reparsing the new content + $btb2 = $this->_createDB($bibtex); + // there is the same number of entries + $this->assertEquals($nref, count($btb2->bibdb)); + $this->assertEquals($bibtex, $btb2->toBibtex()); + } + + function test_cli() { + $test_file="test_cli.bib"; + copy('bibacid-utf8.bib', $test_file); + system('php bibtexbrowser-cli.php '.$test_file." --id classical --set-title \"a new title\""); + $db = new BibDataBase(); + $db->load($test_file); + $this->assertEquals("a new title", $db->getEntryByKey('classical')->getField('title')); + + // multiple changes + system('php bibtexbrowser-cli.php '.$test_file." --id classical --set-title \"a new title\" --id with_abstract --set-title \"a new title\" --set-year 1990"); + $db = new BibDataBase(); + $db->load($test_file); + $this->assertEquals("a new title", $db->getEntryByKey('classical')->getField('title')); + $this->assertEquals("a new title", $db->getEntryByKey('with_abstract')->getField('title')); + $this->assertEquals("1990", $db->getEntryByKey('with_abstract')->getField('year')); + + unlink($test_file); + } } // end class diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 33b3626..5b5e24b 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1905,6 +1905,12 @@ class BibEntry { } // enc class BibEntry +class RawBibEntry extends BibEntry { + function setField($name, $value) { + $this->fields[$name]=$value; + } +} + /** returns an HTML tag depending on BIBTEXBROWSER_LAYOUT e.g. */ function get_HTML_tag_for_layout() { switch(BIBTEXBROWSER_LAYOUT) { /* switch for different layouts */ @@ -3778,7 +3784,7 @@ class BibDataBase { /** Adds a new bib entry to the database. */ function addEntry($entry) { if (!$entry->hasField('key')) { - die('error: a bibliographic entry must have a key'); + die('error: a bibliographic entry must have a key '.$entry->getText()); } // we keep its insertion order $entry->order = count($this->bibdb); @@ -3875,6 +3881,13 @@ class BibDataBase { } return $result; } + + function toBibtex() { + $s = ""; + foreach($this->bibdb as $bibentry) { $s.=$bibentry->getText()."\n"; } + return $s; + } + } // end class /** returns the default CSS of bibtexbrowser */ @@ -4679,6 +4692,25 @@ class Dispatcher { } // end class Dispatcher +function bibtexbrowser_cli($arguments) { + $db = new BibDataBase(); + $db->load($arguments[1]); + $current_entry=NULL; + $current_field=NULL; + for ($i=2;$igetEntryByKey($arguments[$i+1]); + $i=$i+1; + } + if (preg_match('/^--set-(.*)/',$arg,$matches)) { + $current_entry->setField($matches[1],$arguments[$i+1]); + $i=$i+1; + } + } + file_put_contents($arguments[1],$db->toBibtex()); +} + } // end if (!defined('BIBTEXBROWSER')) @include(preg_replace('/\.php$/','.after.php',__FILE__));