-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
BibtexParser.php
157 lines (122 loc) · 3.81 KB
/
BibtexParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace SCI\Bibtex;
/**
* @note most of the parsing code has been copied from PARSEENTRIES therefore
* thanks goes to the authors of http://bibliophile.sourceforge.net
*
* Comments to the source code can be found at
* http://sourceforge.net/projects/bibliophile/files/bibtexParse/ and is
* released under the GPL license.
*
* @note There might be a better parser out there but I didn't want to spend to
* much time reviewing code therefore PARSEENTRIES does the job well.
*
* Any fancy macro stuff or other complicated string parsing isn't supported
* given that the bibtex format misses a proper specification. PARSEENTRIES
* surely allows to cover more edge cases but for what we want to achieve (to ease
* copy and paste of existing bibtex records) the current implementation is
* sufficient.
*
* BibtexParserTest provides the test interface to verify edge cases.
*
* @license GNU GPL v2+
* @since 1.0
*/
class BibtexParser {
/**
* @var array
*/
private $undefinedStrings = [];
/**
* @var array
*/
private $strings = [];
/**
* @since 1.0
*
* @return array
*/
public function parse( $bibtex ) {
if ( ( $matches = $this->findBibtexFormatMatches( $bibtex ) ) === [] ) {
return [];
}
$head = [
'type' => strtolower( trim( $matches[1] ) ),
'reference' => $matches[2]
];
return $head + $this->parseFields( $matches[3] );
}
private function findBibtexFormatMatches( $bibtex ) {
$matches = preg_split("/@(.*)[{(](.*),/U", $bibtex, 2, PREG_SPLIT_DELIM_CAPTURE );
// Silently retreat from processing
if ( !isset( $matches[2] ) ) {
return [];
}
if( preg_match("/=/", $matches[2] ) ) {
$matches = preg_split("/@(.*)\s*[{(](.*)/U", $bibtex, 2, PREG_SPLIT_DELIM_CAPTURE );
}
return $matches;
}
private function parseFields( $content ) {
$elements = [];
$values = [];
$length = strlen( $content );
if( $content[$length - 1] == "}" || $content[$length - 1] == ")" || $content[$length - 1] == ",") {
$content = substr( $content, 0, $length - 1 );
}
$split = preg_split("/=/", $content, 2 );
$string = $split[1];
while( $string ) {
list( $entry, $string ) = $this->splitField( $string );
$values[] = $entry;
}
foreach( $values as $value ) {
$pos = strpos( $content, $value);
$content = substr_replace( $content, '', $pos, strlen( $value ) );
}
$rev = strrev( trim( $content ) );
if( $rev[0] != ',') {
$content .= ',';
}
$keys = preg_split("/=,/", $content );
array_pop($keys);
foreach( $keys as $key ) {
$value = trim( array_shift( $values ) );
$rev = strrev( $value );
// remove any dangling ',' left on final field of entry
if( $rev[0] == ',') {
$value = rtrim($value, ",");
}
if(!$value) {
continue;
}
$key = strtolower(trim($key));
$value = trim($value);
$elements[$key] = $this->removeDelimiters( $value );
}
return $elements;
}
private function splitField( $seg ) {
$array = preg_split("/,\s*([-_.:,a-zA-Z0-9]+)\s*={1}\s*/U", $seg, PREG_SPLIT_DELIM_CAPTURE );
// if(!array_key_exists( 1, $array ) ) {
// return array( $array[0], FALSE);
// }
return isset( $array[1] ) ? [ $array[0], $array[1] ] : [ $array[0], false ];
}
private function removeDelimiters( $string ) {
if( $string && ( $string[0] == "\"") ) {
$string = substr($string, 1);
$string = substr($string, 0, -1);
} else if ( $string && ( $string[0] == "{") ) {
if( strlen( $string ) > 0 && $string[strlen($string)-1] == "}" ) {
$string = substr($string, 1);
$string = substr($string, 0, -1);
}
// } else if(!is_numeric($string) && !array_key_exists($string, $this->strings)
// && (array_search($string, $this->undefinedStrings) === FALSE ) ) {
// $this->undefinedStrings[] = $string; // Undefined string that is not a year etc.
// return '';
}
return $string;
}
}