PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/php/bibtex.php

#
PHP | 796 lines | 721 code | 9 blank | 66 comment | 10 complexity | 4b1ddcb6d213c3f5fd5031969514eec9 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * This file is part of BibORB
  4. *
  5. * Copyright (C) 2003-2008 Guillaume Gardey <glinmac+biborb@gmail.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /**
  22. * File: bibtex.php
  23. *
  24. * Description:
  25. *
  26. * This file defines the BibTeX_Tools class. It provides functions to
  27. * deal with bibtex data:
  28. * * parse a string/file (using PARSEENTRIES from bibliophile.sf.net)
  29. * * convert to xml
  30. * * convert to RIS
  31. * * convert to DocBook
  32. */
  33. class_exists('PARSEENTRIES') || include('./php/third_party/bibtexParse/PARSEENTRIES.php');
  34. class_exists('PARSECREATORS') || include('./php/third_party/bibtexParse/PARSECREATORS.php');
  35. /**
  36. * A class to transform, parse BibTeX references.
  37. *
  38. * @author G. Gardey
  39. */
  40. class BibTeX_Tools
  41. {
  42. // The following variables are specific to DocBook importation
  43. // XML parser
  44. var $xp;
  45. var $entries;
  46. var $currentEntry;
  47. var $currentAuthor;
  48. var $currentTag;
  49. var $currentTitle;
  50. /**
  51. * Return an array of entries
  52. * @param $filename is a BibTeX file
  53. * @return an array of entries
  54. */
  55. function get_array_from_file($filename){
  56. $bibtex_parser = new PARSEENTRIES();
  57. $bibtex_parser->openBib($filename);
  58. $bibtex_parser->extractEntries();
  59. $bibtex_parser->expandMacro = TRUE;
  60. $bibtex_parser->closeBib();
  61. $res = $bibtex_parser->returnArrays();
  62. $this->bibtex_import_post_traitment($entries);
  63. return $res[2];
  64. }
  65. /**
  66. * Convert an array representation of an entry in XML.
  67. * @param $tab An array (field => value).
  68. * @return An XML string.
  69. */
  70. function entry_array_to_xml($tab){
  71. $xml = "<bibtex:entry id='".$tab['id']."'>";
  72. $xml .= "<bibtex:".$tab['___type'].">";
  73. foreach($tab as $key => $value){
  74. if($key != 'groups' && $key!= '___type' && $key != 'id'){
  75. $xml .= "<bibtex:".$key.">";
  76. $xml .= trim(specialFiveToHtml($value));
  77. $xml .= "</bibtex:".$key.">";
  78. }
  79. else if($key == 'groups') {
  80. $xml .= "<bibtex:groups>";
  81. $groupvalues = split(',',$value);
  82. foreach($groupvalues as $gr){
  83. $xml .= "<bibtex:group>";
  84. $xml .= trim(specialFiveToHtml($gr));
  85. $xml .= "</bibtex:group>";
  86. }
  87. $xml .= "</bibtex:groups>";
  88. }
  89. }
  90. $xml .= "</bibtex:".$tab['___type'].">";
  91. $xml .= "</bibtex:entry>";
  92. return $xml;
  93. }
  94. /**
  95. * Convert an array of entries to XML.
  96. * Return: array(number of entries, array of ids, xml string)
  97. */
  98. function entries_array_to_xml($tab){
  99. $ids = array();
  100. $xml_content = "<?xml version='1.0' encoding='UTF-8'?>";
  101. $xml_content .= "<bibtex:file xmlns:bibtex='http://bibtexml.sf.net/' version='".BIBORB_XML_VERSION."' >";
  102. foreach($tab as $entry){
  103. $xml_content .= $this->entry_array_to_xml($entry);
  104. array_push($ids,$entry['id']);
  105. }
  106. $xml_content .= "</bibtex:file>";
  107. return array(count($tab),$ids,$xml_content);
  108. }
  109. /**
  110. * Convert a bibtex string to xml.
  111. * Return: array(number of entries, array of ids, xml string)
  112. */
  113. function bibtex_string_to_xml($string){
  114. $entries = $this->get_array_from_string($string);
  115. return $this->entries_array_to_xml($entries);
  116. }
  117. /**
  118. * Convert a bibtex file to xml.
  119. * Return: array(number of entries, array of ids, xml string)
  120. */
  121. function bibtex_file_to_xml($filename){
  122. $entries = $this->get_array_from_file($filename);
  123. return $this->entries_array_to_xml($entries);
  124. }
  125. /**
  126. * Convert a XML string to an array
  127. */
  128. function xml_to_bibtex_array($iXmlString)
  129. {
  130. // result
  131. $aRes = array();
  132. // convert the string in a one line string
  133. $aXml = str_replace('\n','',$iXmlString);
  134. // match all bibtex entries
  135. preg_match_all("/<bibtex:entry id=['|\"](.*)['|\"]>(.*)<\/bibtex:entry>/U", $aXml, $aEntries, PREG_PATTERN_ORDER);
  136. for ($i=0; $i<count($aEntries[1]); $i++)
  137. {
  138. // xml data of the current entry
  139. $aEntry = $aEntries[2][$i];
  140. // the array of bibtex data that will be filled
  141. $aBibtexData = array( 'id'=> $aEntries[1][$i] );
  142. // get the bibtex type
  143. preg_match("/<bibtex:(.[^>]*)>(.*)<\/bibtex:(.[^>]*)>/", $aEntry, $aMatches);
  144. $aBibtexData['___type'] = $aMatches[1];
  145. // get groups value
  146. $aBibtexFields = $aMatches[2];
  147. preg_match("/<bibtex:groups>(.*)<\/bibtex:groups>/U", $aBibtexFields, $aGroups);
  148. if (isset($aGroups[1]))
  149. {
  150. preg_match_all("/<bibtex:group>(.*)<\/bibtex:group>/U", $aGroups[1], $aGroup);
  151. $aBibtexData['groups'] = implode(',',$aGroup[1]);
  152. $aBibtexFields = str_replace($aGroups[0],'',$aBibtexFields);
  153. }
  154. // analyse all remaining fields
  155. preg_match_all("/<bibtex:(.[^>]*)>(.*)<\/bibtex:(.[^>]*)>/U", $aBibtexFields, $aFields);
  156. // analyse each fields
  157. for ($j=0; $j<count($aFields[1]); $j++)
  158. {
  159. $aBibtexData[$aFields[1][$j]] = specialFiveToText(trim($aFields[2][$j]));
  160. }
  161. $aRes[] = $aBibtexData;
  162. }
  163. return $aRes;
  164. }
  165. /**
  166. * Convert an array to bibtex
  167. * @param $tab An array of references
  168. * @param $fields_to_export Array of fields to export
  169. * @return A bibtex formated string.
  170. */
  171. function array_to_bibtex_string($tab,$fields_to_export){
  172. $export = "";
  173. foreach($tab as $entry){
  174. $entry_exported = "";
  175. $export .= "@".$entry['___type']."{".$entry['id'];
  176. foreach($fields_to_export as $field){
  177. if(array_key_exists($field,$entry)){
  178. $export .= ",\n";
  179. if($entry[$field][0] != "#"){
  180. $export .= "\t".$field." = {".$entry[$field]."}";
  181. }
  182. else{
  183. $export .= "\t".$field." = ".substr($entry[$field],1);
  184. }
  185. }
  186. }
  187. $export .= "\n}\n";
  188. }
  189. return $export;
  190. }
  191. /**
  192. * Export an array of references to a RIS formated string.
  193. * @param $tab An array of references.
  194. * @return A RIS formated string.
  195. */
  196. function array_to_RIS($tab){
  197. $ris_type_translate = array('article' => 'JOUR',
  198. 'book' => 'BOOK',
  199. 'booklet' => 'BOOK',
  200. 'inbook' => 'CHAP',
  201. 'incollection' => 'JOUR',
  202. 'inproceedings' => 'JOUR',
  203. 'manual' => 'BOOK',
  204. 'masterthesis' => 'THES',
  205. 'misc' => 'GEN',
  206. 'phdthesis' => 'THES',
  207. 'proceedings' => 'CONF',
  208. 'techreport' => 'RPRT',
  209. 'unpublished' => 'UNPB');
  210. $pc = new PARSECREATORS();
  211. $export = "";
  212. foreach($tab as $entry){
  213. $export .= sprintf("TY - %s\n",$ris_type_translate[$entry['___type']]);
  214. // authors
  215. if(array_key_exists('author',$entry)){
  216. $authors = $pc->parse($entry['author']);
  217. foreach($authors as $author){
  218. $export .= sprintf("A1 - %s, %s\n",$author[2],$author[0]);
  219. }
  220. }
  221. // title
  222. if(array_key_exists('title',$entry)){
  223. $export .= sprintf("T1 - %s\n",$entry['title']);
  224. }
  225. // journal
  226. if(array_key_exists('journal',$entry)){
  227. $export .= sprintf("JO - %s\n",$entry['journal']);
  228. }
  229. // volume
  230. if(array_key_exists('volume',$entry)){
  231. $export .= sprintf("VL - %s\n",$entry['volume']);
  232. }
  233. // number
  234. if(array_key_exists('number',$entry)){
  235. $export .= sprintf("IS - %s\n",$entry['number']);
  236. }
  237. // start/end page
  238. if(array_key_exists('pages',$entry)){
  239. $pages = split('-',$entry['pages']);
  240. $pages = remove_null_values($pages);
  241. if(isset($pages[0])){
  242. $export .= sprintf("SP - %s\n",$pages[0]);
  243. }
  244. if(isset($pages[1])){
  245. $export .= sprintf("EP - %s\n",$pages[1]);
  246. }
  247. }
  248. // series
  249. if(array_key_exists('series',$entry)){
  250. $export .= sprintf("T3 - %s\n",$entry['series']);
  251. }
  252. // editor
  253. if(array_key_exists('editor',$entry)){
  254. $editors = $pc->parse($entry['editor']);
  255. foreach($editors as $editor){
  256. $export .= sprintf("A3 - %s, %s\n",$editor[2],$editor[0]);
  257. }
  258. }
  259. // year
  260. if(array_key_exists('year',$entry)){
  261. $export .= sprintf("Y1 - %s\n",$entry['year']);
  262. }
  263. // pusblisher
  264. if(array_key_exists('publisher',$entry)){
  265. $export .= sprintf("PB - %s\n",$entry['publisher']);
  266. }
  267. // address
  268. if(array_key_exists('address',$entry)){
  269. $export .= sprintf("AD - %s\n",$entry['address']);
  270. }
  271. // note
  272. if(array_key_exists('note',$entry)){
  273. $export .= sprintf("N1 - %s\n",$entry['note']);
  274. }
  275. // abstract
  276. if(array_key_exists('abstract',$entry)){
  277. $export .= sprintf("N2 - %s\n",$entry['abstract']);
  278. }
  279. // keywords
  280. if(array_key_exists('keywords',$entry)){
  281. $keywords = split(',',$entry['keywords']);
  282. foreach($keywords as $keyword){
  283. $export .= sprintf("KW - %s\n",$keyword);
  284. }
  285. }
  286. // url
  287. if(array_key_exists('url',$entry)){
  288. $export .= sprintf("UR - %s\n",$entry['url']);
  289. }
  290. // pdf
  291. if(array_key_exists('pdf',$entry)){
  292. $export .= sprintf("L1 - %s\n",$entry['pdf']);
  293. }
  294. $export .= "ER - \n";
  295. $export .= "\n";
  296. }
  297. return $export;
  298. }
  299. /**
  300. * Import RIS references.
  301. * @param A RIS string.
  302. * @return An array of references.
  303. */
  304. function RIS_to_array($ris)
  305. {
  306. $entries = array();
  307. $ris_type_translate = array('JOUR' => 'article',
  308. 'BOOK' => 'book',
  309. 'CHAP' => 'inbook',
  310. 'GEN' => 'misc',
  311. 'THES' => 'phdthesis',
  312. 'CONF' => 'proceedings',
  313. 'RPRT' => 'techreport',
  314. 'UNPB' => 'unpublished');
  315. foreach($ris as $line){
  316. echo $line;
  317. // type
  318. if(preg_match('/TY\s*-\s*(.*)\s*/',$line,$matches)){
  319. $entry = array();
  320. $entry['___type'] = $ris_type_translate[trim($matches[1])];
  321. }
  322. //author
  323. if(preg_match('/A1\s*-\s*(.*)\s*/',$line,$matches)){
  324. $authors = split(',',$matches[1]);
  325. if(isset($entry['author'])){
  326. $entry['author'] .= " and ";
  327. }
  328. else{
  329. $entry['author'] = "";
  330. }
  331. $entry['author'] .= " ".$authors[1]." ".$authors[0];
  332. }
  333. //title
  334. if(preg_match('/T1\s*-\s*(.*)\s*/',$line,$matches)){
  335. $entry['title'] = $matches[1];
  336. }
  337. // journal
  338. if(preg_match('/JO\s*-\s*(.*)\s*/',$line,$matches)){
  339. $entry['journal'] = $matches[1];
  340. }
  341. // volume
  342. if(preg_match('/VL\s*-\s*(.*)\s*/',$line,$matches)){
  343. $entry['volume'] = $matches[1];
  344. }
  345. // number
  346. if(preg_match('/IS\s*-\s*(.*)\s*/',$line,$matches)){
  347. $entry['number'] = $matches[1];
  348. }
  349. // pages
  350. if(preg_match('/(SP|EP)\s*-\s*(.*)\s*/',$line,$matches)){
  351. $entry[$matches[1]] = $matches[2];
  352. }
  353. // series
  354. if(preg_match('/T3\s*-\s*(.*)\s*/',$line,$matches)){
  355. $entry['series'] = $matches[1];
  356. }
  357. // editor
  358. if(preg_match('/A3\s*-\s*(.*)\s*/',$line,$matches)){
  359. $authors = split(',',$matches[1]);
  360. if(isset($entry['editor'])){
  361. $entry['editor'] .= " and ";
  362. }
  363. else{
  364. $entry['editor'] = "";
  365. }
  366. $entry['editor'] .= " ".$authors[1]." ".$authors[0];
  367. }
  368. // year
  369. if(preg_match('/Y1\s*-\s*(.*)\s*/',$line,$matches)){
  370. $entry['year'] = $matches[1];
  371. }
  372. // publisher
  373. if(preg_match('/BP\s*-\s*(.*)\s*/',$line,$matches)){
  374. $entry['publisher'] = $matches[1];
  375. }
  376. // address
  377. if(preg_match('/AD\s*-\s*(.*)\s*/',$line,$matches)){
  378. $entry['address'] = $matches[1];
  379. }
  380. // note
  381. if(preg_match('/N1\s*-\s*(.*)\s*/',$line,$matches)){
  382. $entry['note'] = $matches[1];
  383. }
  384. // abstract
  385. if(preg_match('/N2\s*-\s*(.*)\s*/',$line,$matches)){
  386. $entry['abstract'] = $matches[1];
  387. }
  388. // keywords
  389. if(preg_match('/KW\s*-\s*(.*)\s*/',$line,$matches)){
  390. if(isset($entry['keywords'])){
  391. $entry['keywords'] = ", ";
  392. }
  393. else{
  394. $entry['keywords'] = "";
  395. }
  396. $entry['keywords'] .= $matches[1];
  397. }
  398. // url
  399. if(preg_match('/UR\s*-\s*(.*)\s*/',$line,$matches)){
  400. $entry['url'] = $matches[1];
  401. }
  402. // end
  403. if(preg_match('/ER\s*-\s*(.*)\s*/',$line,$matches)){
  404. if(isset($entry['author']))
  405. $entry['author'] = trim(preg_replace("/\s+/"," ",$entry['author']));
  406. if(isset($entry['editor']))
  407. $entry['editor'] = trim(preg_replace("/s+/"," ",$entry['editor']));
  408. if(isset($entry['SP'])){
  409. $entry['pages'] = $entry['SP'];
  410. unset($entry['SP']);
  411. }
  412. if(isset($entry['EP'])){
  413. if(isset($entry['page'])){
  414. $entry['pages'] .= "--";
  415. }
  416. else{
  417. $entry['pages'] = "";
  418. }
  419. $entry['pages'] .= $entry['EP'];
  420. unset($entry['EP']);
  421. }
  422. $entries[] = $entry;
  423. }
  424. }
  425. return $entries;
  426. }
  427. /**
  428. * Export an array of references to DocBook.
  429. * @param $tab An array of entries.
  430. * @return A DocBook string.
  431. */
  432. function array_to_DocBook($tab){
  433. $pc = new PARSECREATORS();
  434. $export = "<?xml version='1.0'?>\n";
  435. $export .= "<bibliography>\n";
  436. foreach($tab as $entry){
  437. $export .= sprintf("\t<biblioentry xreflabel='%s' id='%s'>\n",$entry['id'],$entry['id']);
  438. $export .= sprintf("\t\t<abbrev>%s</abbrev>\n",$entry['id']);
  439. // authors
  440. if(array_key_exists('author',$entry)){
  441. $authors = $pc->parse($entry['author']);
  442. $export .= "\t\t<authorgroup>\n";
  443. foreach($authors as $author){
  444. $export .= "\t\t\t<author>\n";
  445. $export .= "\t\t\t\t<firstname>".$author[0]."</firstname>\n";
  446. $export .= "\t\t\t\t<othername role='mi'>".$author[1]."</othername>\n";
  447. $export .= "\t\t\t\t<surname>".$author[2]."</surname>\n";
  448. $export .= "\t\t\t</author>\n";
  449. }
  450. $export .= "\t\t</authorgroup>\n";
  451. }
  452. // title
  453. if(array_key_exists('title',$entry)){
  454. $type = $entry['___type'];
  455. if($type != 'article' && $type != 'book' && $type != 'journal'){
  456. $type = 'article';
  457. }
  458. $export .= sprintf("\t\t<citetitle pubwork='%s'>%s</citetitle>\n",$type,$entry['title']);
  459. }
  460. // journal
  461. if(array_key_exists('jounrnal',$entry)){
  462. $export .= sprintf("\t\t<citetitle pubwork='%s'>%s</citetitle>\n",'journal',$entry['journal']);
  463. }
  464. // publisher
  465. if(array_key_exists('publisher',$entry)){
  466. $export .= sprintf("\t\t<publisher>\n\t\t\t<publishername>%s</publishername>\n\t\t</publisher>\n",$entry['publisher']);
  467. }
  468. // volume
  469. if(array_key_exists('volume',$entry)){
  470. $export .= sprintf("\t\t<volumenum>%s</volumenum>\n",$entry['volume']);
  471. }
  472. // year
  473. if(array_key_exists('year',$entry)){
  474. $export .= sprintf("\t\t<pubdate>%s</pubdate>\n",$entry['year']);
  475. }
  476. // pages
  477. if(array_key_exists('pages',$entry)){
  478. $export .= sprintf("\t\t<artpagenums>%s</artpagenums>\n",$entry['pages']);
  479. }
  480. // number
  481. if(array_key_exists('number',$entry)){
  482. $export .= sprintf("\t\t<issuenum>%s</issuenum>\n",$entry['number']);
  483. }
  484. // editor
  485. if(array_key_exists('editor',$entry)){
  486. $export .= sprintf("\t\t<editor>%s</editor>\n",$entry['editor']);
  487. }
  488. // abstract
  489. if(array_key_exists('abstract',$entry)){
  490. $export .= "\t\t<abstract>\n";
  491. $export .= "\t\t\t<para>".$entry['abstract']."\n\t\t\t</para>\n";
  492. $export .= "\t\t</abstract>\n";
  493. }
  494. $export .= "\t</biblioentry>\n";
  495. }
  496. $export .= "</bibliography>\n";
  497. return $export;
  498. }
  499. /**
  500. * Import a docbook string.
  501. * @param $docbook The DocBook string.
  502. * @return An array of entries present in the DocBook string.
  503. */
  504. function DocBook_to_array($docbook)
  505. {
  506. // clean up before starting
  507. unset($this->entries);
  508. unset($this->currentEntry);
  509. // create an xml parser
  510. $this->xp = xml_parser_create() or trigger_error("Unable to create an XML Parser!.",ERROR);
  511. xml_set_object($this->xp,$this);
  512. xml_set_element_handler($this->xp,"docbook_start_tag","docbook_end_tag");
  513. xml_set_character_data_handler($this->xp,"docbook_cdata");
  514. if( !xml_parse($this->xp,$docbook,true)){
  515. trigger_error("XML Parsing error:\n".xml_error_string(xml_get_error_code($this->xp))."\nError at line: ".xml_get_current_line_number($this->xp),ERROR);
  516. }
  517. for($i=0;$i<count($this->entries);$i++){
  518. // set title and journal fields
  519. if(array_key_exists('citetitle',$this->entries[$i])){
  520. if(array_key_exists('article',$this->entries[$i]['citetitle'])){
  521. $this->entries[$i]['title'] = $this->entries[$i]['citetitle']['article'];
  522. }
  523. if(array_key_exists('journal',$this->entries[$i]['citetitle'])){
  524. $this->entries[$i]['journal'] = $this->entries[$i]['citetitle']['journal'];
  525. }
  526. unset($this->entries[$i]['citetitle']);
  527. }
  528. // remove spaces
  529. foreach($this->entries[$i] as $key=>$value){
  530. $val = trim($value);
  531. if($val != ""){
  532. $this->entries[$i][$key] = $val;
  533. }
  534. else{
  535. unset($this->entries[$i][$key]);
  536. }
  537. }
  538. }
  539. xml_parser_free($this->xp);
  540. return $this->entries;
  541. }
  542. /**
  543. * Called when a docbook start tag is parsed.
  544. */
  545. function docbook_start_tag($parser, $name, $att)
  546. {
  547. $name = strtolower($name);
  548. switch($name){
  549. case 'bibliography':
  550. // start a new bibliography
  551. $this->entries = array();
  552. break;
  553. case 'biblioentry':
  554. // start a new entry
  555. $this->currentEntry = array();
  556. if(array_key_exists('XREFLABEL',$att)){
  557. $this->currentEntry['id'] = $att['XREFLABEL'];
  558. }
  559. break;
  560. case 'authorgroup':
  561. // waiting for an author
  562. $this->currentAuthor = array();
  563. break;
  564. case 'author':
  565. // an author
  566. // if it isn't the first author, add an 'and' to the string
  567. if(isset($this->currentEntry['author'])){
  568. $this->currentEntry['author'] .= " and ";
  569. }
  570. else{
  571. $this->currentEntry['author'] = "";
  572. }
  573. $this->currentAuthor = array();
  574. break;
  575. case 'citetitle':
  576. //title
  577. $this->currentTag = $name;
  578. if(array_key_exists('PUBWORK',$att)){
  579. $this->currentTitle = $att['PUBWORK'];
  580. $this->currentEntry['citetitle'][$att['PUBWORK']] = "";
  581. }
  582. break;
  583. }
  584. if($name == 'othername' && isset($att['role']) && $att['role'] == 'mi'){
  585. $this->currentTag = $name;
  586. }
  587. if($name != 'para'){
  588. $this->currentTag = $name;
  589. }
  590. }
  591. /**
  592. * Called when DocBook and tag is parsed.
  593. */
  594. function docbook_end_tag($parser,$name)
  595. {
  596. $name = strtolower($name);
  597. switch($name){
  598. case 'biblioentry':
  599. // add the entry
  600. $this->entries[] = $this->currentEntry;
  601. break;
  602. case 'author':
  603. // add the author
  604. if(isset($this->currentAuthor[0]))
  605. $this->currentEntry['author'] .= " ".$this->currentAuthor[0];
  606. if(isset($this->currentAuthor[1]))
  607. $this->currentEntry['author'] .= " ".$this->currentAuthor[1];
  608. if(isset($this->currentAuthor[2]))
  609. $this->currentEntry['author'] .= " ".$this->currentAuthor[2];
  610. $this->currentEntry['author'] = preg_replace('/\s+/',' ',$this->currentEntry['author']);
  611. unset($this->currentAuthor);
  612. break;
  613. }
  614. }
  615. /**
  616. * CDATA values for DocBook parsing.
  617. */
  618. function docbook_cdata($parser,$data)
  619. {
  620. switch($this->currentTag){
  621. case 'firstname':
  622. if(isset($this->currentAuthor[0])){
  623. $this->currentAuthor[0] .= $data;
  624. }
  625. else{
  626. $this->currentAuthor[0] = $data;
  627. }
  628. break;
  629. case 'surname':
  630. if(isset($this->currentAuthor[2])){
  631. $this->currentAuthor[2] .= $data;
  632. }
  633. else{
  634. $this->currentAuthor[2] = $data;
  635. }
  636. break;
  637. case 'othername':
  638. if(isset($this->currentAuthor[1])){
  639. $this->currentAuthor[1] .= $data;
  640. }
  641. else{
  642. $this->currentAuthor[1] = $data;
  643. }
  644. break;
  645. case 'citetitle':
  646. $this->currentEntry['citetitle'][$this->currentTitle] .= $data;
  647. break;
  648. case 'volumenum':
  649. if(isset($this->currentEntry['volume'])){
  650. $this->currentEntry['volume'] .= $data;
  651. }
  652. else{
  653. $this->currentEntry['volume'] = $data;
  654. }
  655. break;
  656. case 'pubdate':
  657. if(isset($this->currentEntry['year'])){
  658. $this->currentEntry['year'] .= $data;
  659. }
  660. else{
  661. $this->currentEntry['year'] = $data;
  662. }
  663. break;
  664. case 'artpagenums':
  665. if(isset($this->currentEntry['pages'])){
  666. $this->currentEntry['pages'] .= $data;
  667. }
  668. else{
  669. $this->currentEntry['pages'] = $data;
  670. }
  671. break;
  672. case 'issuenum':
  673. if(isset($this->currentEntry['number'])){
  674. $this->currentEntry['number'] .= $data;
  675. }
  676. else{
  677. $this->currentEntry['number'] = $data;
  678. }
  679. break;
  680. case 'editor':
  681. if(isset($this->currentEntry['editor'])){
  682. $this->currentEntry['editor'] .= $data;
  683. }
  684. else{
  685. $this->currentEntry['editor'] = $data;
  686. }
  687. break;
  688. case 'publishername':
  689. if(isset($this->currentEntry['publishername'])){
  690. $this->currentEntry['publishername'] .= $data;
  691. }
  692. else{
  693. $this->currentEntry['publishername'] = $data;
  694. }
  695. break;
  696. case 'abstract':
  697. if(isset($this->currentEntry['abstract'])){
  698. $this->currentEntry['abstract'] .= $data;
  699. }
  700. else{
  701. $this->currentEntry['abstract'] = $data;
  702. }
  703. break;
  704. }
  705. }
  706. /**
  707. * Extract some information from a reference.
  708. * Remove from $tab all elements whose key is not in $extract.
  709. * @param $iTab The array from which to extract values.
  710. * @param $iExtract The fields to extract.
  711. */
  712. function extract_bibtex_data($tab,$extract)
  713. {
  714. $result = array();
  715. foreach($tab as $key => $value){
  716. $val = trim($value);
  717. if(in_array($key,$extract) && $val != ''){
  718. $result[$key] = $val;
  719. }
  720. }
  721. return $result;
  722. }
  723. }
  724. /**
  725. * Convert LaTeX code to HTML
  726. * @param $latex A string with LaTeX macros
  727. */
  728. function latex_macro_to_html($latex)
  729. {
  730. $latex_conversion_table =
  731. array("\'a" => "á", "\`a" => "à", "\^a" => "â", "\~a" => "ã", "\\\"a" => "ä", "\aa" => "å", "\ae" => "æ",
  732. "\c{c}" => "ç",
  733. "\'e" => "é", "\^e" => "ê", "\`e" => "è", "\\\"e" => "ë",
  734. "\'i" => "í", "\`i" => "ì", "\^i" => "î", "\\\"i" => "ï",
  735. "\~n" => "ñ",
  736. "\'o" => "ó", "\^o" => "ô", "\`o" => "ò", "\\\"o" => "ö", "\~o" => "õ",
  737. "\'u" => "ú", "\`u" => "ù", "\^u" => "û", "\\\"u" => "ü",
  738. "\'y" => "ý", "\\\"y" => "ÿ");
  739. return str_replace(array_keys($latex_conversion_table),
  740. array_values($latex_conversion_table),
  741. $latex);
  742. }
  743. ?>