26 require_once
'PEAR.php' ;
151 $this->_delimiters = array(
'"'=>
'"',
153 $this->data = array();
157 $this->warnings = array();
158 $this->_options = array(
159 'stripDelimiter' =>
true,
162 'wordWrapWidth' =>
false,
163 'wordWrapBreak' =>
"\n",
165 'removeCurlyBraces' =>
false,
166 'extractAuthors' =>
true,
168 foreach (
$options as $option => $value) {
174 $this->rtfstring =
'AUTHORS, "{\b TITLE}", {\i JOURNAL}, YEAR';
175 $this->htmlstring =
'AUTHORS, "<strong>TITLE</strong>", <em>JOURNAL</em>, YEAR<br />';
176 $this->allowedEntryTypes = array(
192 $this->authorstring =
'VON LAST, JR, FIRST';
206 if (array_key_exists($option, $this->_options)) {
207 $this->_options[$option] = $value;
224 if (($this->content = @file_get_contents(
$filename)) ===
false) {
246 $this->warnings = array();
247 $this->data = array();
254 for ($i = 0; $i < strlen($this->content); $i++) {
255 $char = substr($this->content, $i, 1);
256 if ((0 != $open) && (
'@' == $char)) {
264 if ((0 == $open) && (
'@' == $char)) {
266 } elseif ($entry && (
'{' == $char) && (
'\\' != $lastchar)) {
268 } elseif ($entry && (
'}' == $char) && (
'\\' != $lastchar)) {
284 $this->data[] = $entrydata;
300 $this->data[] = $entrydata;
310 if ($this->_options[
'validate']) {
312 foreach ($this->data as $entry) {
313 $cites[] = $entry[
'cite'];
315 $unique = array_unique($cites);
316 if (
sizeof($cites) !=
sizeof($unique)) {
317 $notuniques = array();
318 for ($i = 0; $i <
sizeof($cites); $i++) {
319 if (
'' == $unique[$i]) {
320 $notuniques[] = $cites[$i];
323 $this->
_generateWarning(
'WARNING_MULTIPLE_ENTRIES', implode(
',',$notuniques));
357 if ($this->_options[
'validate']) {
361 if (
'@string' == strtolower(substr($entry, 0, 7))) {
363 if ($this->_options[
'validate']) {
366 } elseif (
'@preamble' == strtolower(substr($entry, 0, 9))) {
368 if ($this->_options[
'validate']) {
369 $this->
_generateWarning(
'PREAMBLE_ENTRY_NOT_YET_SUPPORTED',
'', $entry.
'}');
373 while (strrpos($entry,
'=') !==
false) {
374 $position = strrpos($entry,
'=');
377 if (substr($entry, $position-1, 1) ==
'\\') {
384 $substring = substr($entry, 0, $position);
385 $position = strrpos($substring,
'=');
387 if (substr($entry, $position-1, 1) ==
'\\') {
395 $value = trim(substr($entry, $position+1));
396 $entry = substr($entry, 0, $position);
398 if (
',' == substr($value, strlen($value)-1, 1)) {
399 $value = substr($value, 0, -1);
401 if ($this->_options[
'validate']) {
404 if ($this->_options[
'stripDelimiter']) {
407 if ($this->_options[
'unwrap']) {
408 $value = $this->
_unwrap($value);
410 if ($this->_options[
'removeCurlyBraces']) {
413 $position = strrpos($entry,
',');
414 $field = strtolower(trim(substr($entry, $position+1)));
415 $ret[$field] = $value;
416 $entry = substr($entry, 0, $position);
419 $arr = explode(
'{', $entry);
420 $ret[
'cite'] = trim($arr[1]);
421 $ret[
'entryType'] = strtolower(trim($arr[0]));
422 if (
'@' ==
$ret[
'entryType']{0}) {
423 $ret[
'entryType'] = substr(
$ret[
'entryType'], 1);
425 if ($this->_options[
'validate']) {
431 if (in_array(
'author', array_keys(
$ret)) && $this->_options[
'extractAuthors']) {
456 $length = strlen($entry);
458 for ($i = $length-1; $i >= $position; $i--) {
459 $precedingchar = substr($entry, $i-1, 1);
460 $char = substr($entry, $i, 1);
461 if ((
'{' == $char) && (
'\\' != $precedingchar)) {
464 if ((
'}' == $char) && (
'\\' != $precedingchar)) {
474 $entrycopy = trim($entry);
475 $lastchar = $entrycopy{strlen($entrycopy)-1};
476 if (
',' == $lastchar) {
477 $lastchar = $entrycopy{strlen($entrycopy)-2};
479 if (
'"' == $lastchar) {
485 for ($i = $length; $i >= $position; $i--) {
486 $precedingchar = substr($entry, $i-1, 1);
487 $char = substr($entry, $i, 1);
488 if ((
'"' == $char) && (
'\\' != $precedingchar)) {
510 return in_array($entry, $this->allowedEntryTypes);
527 $opening = array_keys($this->_delimiters);
528 $closing = array_values($this->_delimiters);
530 if (strrpos($entry,
'=') !==
false) {
531 $position = strrpos($entry,
'=');
533 if (substr($entry, $position-1, 1) ==
'\\') {
537 $substring = substr($entry, 0, $position);
538 $position = strrpos($substring,
'=');
540 if (substr($entry, $position-1, 1) ==
'\\') {
544 $value = trim(substr($entry, $position+1));
548 for ($i = 0; $i < strlen($value); $i++) {
549 $char = substr($this->content, $i, 1);
550 if (in_array($char, $opening) && (
'\\' != $lastchar)) {
552 } elseif (in_array($char, $closing) && (
'\\' != $lastchar)) {
574 $beginningdels = array_keys($this->_delimiters);
575 $length = strlen($entry);
576 $firstchar = substr($entry, 0, 1);
577 $lastchar = substr($entry, -1, 1);
578 while (in_array($firstchar, $beginningdels)) {
579 if ($lastchar == $this->_delimiters[$firstchar]) {
580 $entry = substr($entry, 1, -1);
584 $firstchar = substr($entry, 0, 1);
585 $lastchar = substr($entry, -1, 1);
599 $entry = preg_replace(
'/\s+/',
' ', $entry);
612 if ( (
''!=$entry) && (is_string($entry)) ) {
613 $entry = wordwrap($entry, $this->_options[
'wordWrapWidth'], $this->_options[
'wordWrapBreak'], $this->_options[
'wordWrapCut']);
626 $entry = $this->
_unwrap($entry);
627 $authorarray = array();
628 $authorarray = explode(
' and ', $entry);
629 for ($i = 0; $i <
sizeof($authorarray); $i++) {
630 $author = trim($authorarray[$i]);
637 if (strpos($author,
',') ===
false) {
640 $tmparray = explode(
' |~', $author);
641 $size =
sizeof($tmparray);
643 $last = $tmparray[0];
644 } elseif (2 ==
$size) {
645 $first = $tmparray[0];
646 $last = $tmparray[1];
650 for ($j=0; $j<(
$size-1); $j++) {
652 $last .=
' '.$tmparray[$j];
657 } elseif ((0 == $case) || (-1 == $case)) {
660 for ($k=($j+1); $k<(
$size-1); $k++) {
664 } elseif (0 == $futurecase) {
671 $last .=
' '.$tmparray[$j];
673 $von .=
' '.$tmparray[$j];
676 $von .=
' '.$tmparray[$j];
679 $von .=
' '.$tmparray[$j];
685 } elseif (0 == $case) {
687 $von .=
' '.$tmparray[$j];
689 $first .=
' '.$tmparray[$j];
694 $last .=
' '.$tmparray[
$size-1];
698 $tmparray = explode(
',', $author);
700 $vonlastarray = array();
701 $vonlastarray = explode(
' ', $tmparray[0]);
702 $size =
sizeof($vonlastarray);
704 $last = $vonlastarray[0];
707 for ($j=0; $j<(
$size-1); $j++) {
709 $last .=
' '.$vonlastarray[$j];
713 for ($k=($j+1); $k<(
$size-1); $k++) {
718 } elseif (0 == $case) {
724 $last .=
' '.$vonlastarray[$j];
726 $von .=
' '.$vonlastarray[$j];
729 $von .=
' '.$vonlastarray[$j];
733 $last .=
' '.$vonlastarray[
$size-1];
736 if (3==
sizeof($tmparray)) {
740 $first = $tmparray[
sizeof($tmparray)-1];
742 $authorarray[$i] = array(
'first'=>trim($first),
'von'=>trim($von),
'last'=>trim($last),
'jr'=>trim($jr));
762 $trimmedword = trim ($word);
765 if (is_string($word) && (strlen($trimmedword) > 0)) {
769 while (!$found && ($i <= strlen($word))) {
770 $letter = substr($trimmedword, $i, 1);
778 if (($ord>=65) && ($ord<=90) && (0==$openbrace)) {
781 } elseif ( ($ord>=97) && ($ord<=122) && (0==$openbrace) ) {
809 if (preg_match(
'/^{.*@.*}$/', $entry)) {
813 if (preg_match(
'/^\".*\\".*\"$/', $entry)) {
814 $this->
_generateWarning(
'WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES', $entry, $wholeentry);
820 for ($i = 0; $i < strlen($entry); $i++) {
821 $char = substr($entry, $i, 1);
822 if ((
'{' == $char) && (
'\\' != $lastchar)) {
825 if ((
'}' == $char) && (
'\\' != $lastchar)) {
831 $this->
_generateWarning(
'WARNING_UNBALANCED_AMOUNT_OF_BRACES', $entry, $wholeentry);
845 $beginningdels = array_keys($this->_delimiters);
846 $firstchar = substr($entry, 0, 1);
847 $lastchar = substr($entry, -1, 1);
850 while (in_array($firstchar, $beginningdels)) {
851 if ($lastchar == $this->_delimiters[$firstchar]) {
852 $begin .= $firstchar;
854 $value = substr($value, 1, -1);
858 $firstchar = substr($value, 0, 1);
859 $lastchar = substr($value, -1, 1);
862 $pattern =
'/([^\\\\])\{(.*?[^\\\\])\}/';
863 $replacement =
'$1$2';
864 $value = preg_replace($pattern, $replacement, $value);
866 $value = $begin.$value.$end;
880 $warning[
'warning'] = $type;
881 $warning[
'entry'] = $entry;
882 $warning[
'wholeentry'] = $wholeentry;
883 $this->warnings[] = $warning;
893 $this->warnings = array();
904 if (
sizeof($this->warnings)>0)
return true;
930 if (!array_key_exists(
'von', $array)) {
933 $array[
'von'] = trim($array[
'von']);
935 if (!array_key_exists(
'last', $array)) {
938 $array[
'last'] = trim($array[
'last']);
940 if (!array_key_exists(
'jr', $array)) {
943 $array[
'jr'] = trim($array[
'jr']);
945 if (!array_key_exists(
'first', $array)) {
946 $array[
'first'] =
'';
948 $array[
'first'] = trim($array[
'first']);
951 $ret = str_replace(
"VON", $array[
'von'],
$ret);
952 $ret = str_replace(
"LAST", $array[
'last'],
$ret);
953 $ret = str_replace(
"JR", $array[
'jr'],
$ret);
954 $ret = str_replace(
"FIRST", $array[
'first'],
$ret);
969 foreach ($this->data as $entry) {
971 $bibtex .=
'@'.strtolower($entry[
'entryType']).
' { '.$entry[
'cite'].
",\n";
973 foreach ($entry as $key=>$val) {
974 if ($this->_options[
'wordWrapWidth']>0) {
975 $val = $this->_wordWrap($val);
977 if (!in_array($key, array(
'cite',
'entryType',
'author'))) {
978 $bibtex .=
"\t".$key.
' = {'.$val.
"},\n";
982 if (array_key_exists(
'author', $entry)) {
983 if ($this->_options[
'extractAuthors']) {
985 foreach ($entry[
'author'] as $authorentry) {
988 $author = join(
' and ', $tmparray);
990 $author = $entry[
'author'];
995 $bibtex .=
"\tauthor = {".$author.
"}\n";
1010 $this->data[] = $newentry;
1025 foreach ($this->data as $entry) {
1026 if (array_key_exists($entry[
'entryType'],
$ret)) {
1027 $ret[$entry[
'entryType']]++;
1029 $ret[$entry[
'entryType']] = 1;
1052 foreach ($this->data as $entry) {
1058 if (array_key_exists(
'title', $entry)) {
1059 $title = $this->
_unwrap($entry[
'title']);
1061 if (array_key_exists(
'journal', $entry)) {
1062 $journal = $this->
_unwrap($entry[
'journal']);
1064 if (array_key_exists(
'year', $entry)) {
1065 $year = $this->
_unwrap($entry[
'year']);
1067 if (array_key_exists(
'author', $entry)) {
1068 if ($this->_options[
'extractAuthors']) {
1069 $tmparray = array();
1070 foreach ($entry[
'author'] as $authorentry) {
1073 $authors = join(
', ', $tmparray);
1075 $authors = $entry[
'author'];
1078 if ((
''!=$title) || (
''!=$journal) || (
''!=$year) || (
''!=$authors)) {
1079 $line = str_replace(
"TITLE", $title, $line);
1080 $line = str_replace(
"JOURNAL", $journal, $line);
1081 $line = str_replace(
"YEAR", $year, $line);
1082 $line = str_replace(
"AUTHORS", $authors, $line);
1083 $line .=
"\n\\par\n";
1086 $this->
_generateWarning(
'WARNING_LINE_WAS_NOT_CONVERTED',
'', print_r($entry,1));
1109 foreach ($this->data as $entry) {
1115 if (array_key_exists(
'title', $entry)) {
1116 $title = $this->
_unwrap($entry[
'title']);
1118 if (array_key_exists(
'journal', $entry)) {
1119 $journal = $this->
_unwrap($entry[
'journal']);
1121 if (array_key_exists(
'year', $entry)) {
1122 $year = $this->
_unwrap($entry[
'year']);
1124 if (array_key_exists(
'author', $entry)) {
1125 if ($this->_options[
'extractAuthors']) {
1126 $tmparray = array();
1127 foreach ($entry[
'author'] as $authorentry) {
1130 $authors = join(
', ', $tmparray);
1132 $authors = $entry[
'author'];
1135 if ((
''!=$title) || (
''!=$journal) || (
''!=$year) || (
''!=$authors)) {
1136 $line = str_replace(
"TITLE", $title, $line);
1137 $line = str_replace(
"JOURNAL", $journal, $line);
1138 $line = str_replace(
"YEAR", $year, $line);
1139 $line = str_replace(
"AUTHORS", $authors, $line);
1143 $this->
_generateWarning(
'WARNING_LINE_WAS_NOT_CONVERTED',
'', print_r($entry,1));
_unwrap($entry)
Unwrapping entry.
_determineCase($word)
Case Determination according to the needs of BibTex.
rtf()
Returns the stored data in RTF format.
_wordwrap($entry)
Wordwrap an entry.
Structures_BibTex($options=array())
Constructor.
_parseEntry($entry)
Extracting the data of one content.
_formatAuthor($array)
Returns the author formatted.
setOption($option, $value)
Sets run-time configuration options.
html()
Returns the stored data in HTML format.
hasWarning()
Is there a warning?
_stripDelimiter($entry)
Stripping Delimiter.
_validateValue($entry, $wholeentry)
Validation of a value.
_checkEqualSign($entry, $position)
Checking whether the position of the '=' is correct.
getStatistic()
Returns statistic.
bibTex()
Converts the stored BibTex entries to a BibTex String.
if(!is_array($argv)) $options
_checkAt($entry)
Checking whether an at is outside an entry.
clearWarnings()
Cleares all warnings.
_extractAuthors($entry)
Extracting the authors.
amount()
Returns the amount of available BibTex entries.
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
parse()
Parses what is stored in content and clears the content if the parsing is successfull.
addEntry($newentry)
Adds a new BibTex entry to the data.
_removeCurlyBraces($value)
Remove curly braces from entry.
loadFile($filename)
Reads a give BibTex File.
_checkAllowedEntryType($entry)
Checking if the entry type is allowed.
_generateWarning($type, $entry, $wholeentry='')
Generates a warning.
isError($data, $code=null)
Tell whether a value is a PEAR error.