Browse Source

add support for command-line usage

pull/63/merge
Martin Monperrus 9 years ago
parent
commit
8474129a57
  1. 10
      bibtexbrowser-cli.php
  2. 8
      bibtexbrowser-documentation.wiki
  3. 42
      bibtexbrowser-test.php
  4. 34
      bibtexbrowser.php

10
bibtexbrowser-cli.php

@ -0,0 +1,10 @@
<?php
// a command line interface for bibtexbrowser
// example: php bibtexbrowser-cli.php test_cli.bib --id classical --set-title "a new title"
$_GET['library']=1;
function createBibEntry() { $x = new RawBibEntry(); return $x;}
require('bibtexbrowser.php');
bibtexbrowser_configure('BIBTEXBROWSER_BIBTEX_VIEW','reconstructed');
bibtexbrowser_configure('BIBTEXBROWSER_BIBTEX_VIEW_FILTEREDOUT','');
bibtexbrowser_cli($argv);
?>

8
bibtexbrowser-documentation.wiki

@ -505,6 +505,14 @@ You must add a ''@string'' block in the bibtex file, which contains the links. T
} }
</pre> </pre>
====How to modify bibtex entries on the command line?====
Use ''bibtexbrowser-cli.php''. For instance:
<pre>
php bibtexbrowser-cli.php mybib.bib --id classical --set-title \"a new title\" --id with_abstract --set-title \"a new title\" --set-year 1990
</pre>
=====Related tools===== =====Related tools=====
Old-fashioned: Old-fashioned:

42
bibtexbrowser-test.php

@ -27,11 +27,14 @@ class BTBTest extends PHPUnit_Framework_TestCase {
} }
function createDB() { 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" ."@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); fseek($test_data,0);
$btb = new BibDataBase(); $btb = new BibDataBase();
$btb->update_internal("inline", $test_data); $btb->update_internal("inline", $test_data);
@ -487,6 +490,37 @@ class BTBTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('<a href="http://example.net/">Foo Acé</a>', $authors[1]); $this->assertEquals('<a href="http://example.net/">Foo Acé</a>', $authors[1]);
$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_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 } // end class

34
bibtexbrowser.php

@ -1905,6 +1905,12 @@ class BibEntry {
} // enc 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. <TABLE> */ /** returns an HTML tag depending on BIBTEXBROWSER_LAYOUT e.g. <TABLE> */
function get_HTML_tag_for_layout() { function get_HTML_tag_for_layout() {
switch(BIBTEXBROWSER_LAYOUT) { /* switch for different layouts */ switch(BIBTEXBROWSER_LAYOUT) { /* switch for different layouts */
@ -3778,7 +3784,7 @@ class BibDataBase {
/** Adds a new bib entry to the database. */ /** Adds a new bib entry to the database. */
function addEntry($entry) { function addEntry($entry) {
if (!$entry->hasField('key')) { 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 // we keep its insertion order
$entry->order = count($this->bibdb); $entry->order = count($this->bibdb);
@ -3875,6 +3881,13 @@ class BibDataBase {
} }
return $result; return $result;
} }
function toBibtex() {
$s = "";
foreach($this->bibdb as $bibentry) { $s.=$bibentry->getText()."\n"; }
return $s;
}
} // end class } // end class
/** returns the default CSS of bibtexbrowser */ /** returns the default CSS of bibtexbrowser */
@ -4679,6 +4692,25 @@ class Dispatcher {
} // end 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;$i<count($arguments); $i++) {
$arg=$arguments[$i];
if ($arg=='--id') {
$current_entry = $db->getEntryByKey($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')) } // end if (!defined('BIBTEXBROWSER'))
@include(preg_replace('/\.php$/','.after.php',__FILE__)); @include(preg_replace('/\.php$/','.after.php',__FILE__));

Loading…
Cancel
Save