3 /*Generátor citací, podle specifikace citeproc
5 define("field_not_exist","field doesn`t exist");
6 define("error_message","Error occured: ");
7 define("bad_data","multiple fields, should be just one");
11 private $data=array();
12 private $fieldslist=array();
14 public function loadData($data){
15 for($i=0;$i<count($data);$i++
){
16 $this->data
[$i]=$data[$i];
17 $this->fieldslist
[$data[$i]["type"]][]=$i;
20 private function getData($field){
21 if(array_key_exists($field,$this->fieldslist
)){
23 foreach($this->fieldslist
[$field] as $record){
24 $list[]=$this->data
[$record];
28 throw new Exception(field_not_exist
);
31 public function getField($field){
33 $list=$this->getData($field);
35 return($list[0]["value"]);
37 throw new Exception(bad_data
);
40 $this->printError($e);
43 public function getList($field){
44 return $this->getData($field);
51 public $macros=array();
53 function __construct(citeprocData
$data=null){
56 public function addMacro($name,&$code){
57 $this->macros
[$name]=$code;
60 public function get($value){
61 return $this->prefix
.$this->data
->getField($value).$this->suffix
;
64 public function printError(Exception
$e){
65 echo "<div style=\"border:1px solid black;color:red;\">".error_message
.$e->getMessage()."</div>\n";
69 class citeprocInfo
extends citeprocBase
{
73 public $author=array(array("name"=>"","email"=>"","uri"=>""));
74 public $citation_format="";
81 class citeprocBibliography
extends citeprocBase
{
82 function printBibliography(array $fields){
83 foreach($fields as $field=>$macro){
84 $exec=$this->macros
[$macro];
85 echo $exec->get($field);
89 class citeprocCitation
extends citeprocBase
{}
91 class citeprocField
extends citeprocBase
{
95 public function __construct(citeprocData
$data,$locale=null){
97 $this->locale
=$locale;
100 class citeprocText
extends citeprocField
{}
102 class citeprocDate
extends citeprocField
{
103 public $form=array("year-month-day","year-month","year");
104 /* "year-month-day" - default, displays year, month and day
105 * "year-month" - displays year and month
106 * "year" - displays year only*/
109 class citeprocList
extends citeprocField
{}
111 class citeprocAuthor
extends citeprocList
{
113 public $et_al_uses_first=3;
115 public $delimiter=" - ";
116 public $delimiter_precedes_last=array("");
118 private function formatAuthor($author){
119 return $author["last"].", ".$author["first"];
121 public function get($field){
122 $authors=$this->data
->getList($field);
124 if(count($authors)>0){
126 count($authors)<$this->et_al_min ?
$authorlist=count($authors) : $authorlist=$this->et_al_uses_first
;
127 $authorlist==count($authors) ?
$etal="" : $etal=$this->locale
->get("et-al");
129 for($i=0;$i<$authorlist;$i++
){
130 $m[]=$this->formatAuthor($authors[$i]["value"]);
132 $v[]=implode($this->delimiter
,$m);
138 return $this->prefix
.implode("",$v);
143 class citeprocLocale
extends citeprocBase
{
145 public $locale=array("cs"=>array(
149 return $this->locale
[$this->lang
][$c];
156 protected $n=array("kk","nn","pp");
162 if(count($this->np
)<1)return null;
164 return array_shift($this->np
);
172 $this->np
[]=in_array($c,$this->n
) ?
true : false;
177 $m=$this->getValue();
180 $m=$this->getValue();
187 $m=$this->getValue();
190 $m=$this->getValue();
197 echo "<p>$this->op</p>";
203 $citeData=new citeprocData();
205 array("type"=>"author","value"=>array("first"=>"Jan","last"=>"Krka")),
206 array("type"=>"author","value"=>array("first"=>"Martin","last"=>"Bla¾ek")),
207 array("type"=>"author","value"=>array("first"=>"Pepa","last"=>"Nosek")),
208 array("type"=>"author","value"=>array("first"=>"Jirka","last"=>"Cicek")),
209 array("type"=>"author","value"=>array("first"=>"Martin","last"=>"Bla¾ek")),
210 array("type"=>"title","value"=>"Titulek knihy"),
211 array("type"=>"date","value"=>"2009"),
212 array("type"=>"publisher","value"=>"Academia")
214 $citeData->loadData($data);
215 $locale=new citeprocLocale();
217 //echo $citeData->getField("autnhor");
218 $base=new citeprocField($citeData,$locale);
219 $author=new citeprocAuthor($citeData,$locale);
220 $bibliography=new citeprocBibliography($citeData);
221 $bibliography->addMacro("base",$base);
222 $bibliography->addMacro("author",$author);
223 $bibliography->printBibliography(array(
230 $c->is("nn")->is("kk")->is("kk")->a()->is("nn")->a()->is("nn")->a()->is("nnk")->o()->dump();
231 echo (microtime()-$cas);