1 | <?php |
||
27 | class BibtexParser { |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $undefinedStrings = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $strings = []; |
||
38 | |||
39 | /** |
||
40 | * @since 1.0 |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | 7 | public function parse( $bibtex ) { |
|
45 | |||
46 | 7 | if ( ( $matches = $this->findBibtexFormatMatches( $bibtex ) ) === [] ) { |
|
47 | 2 | return []; |
|
48 | } |
||
49 | |||
50 | $head = [ |
||
51 | 5 | 'type' => strtolower( trim( $matches[1] ) ), |
|
52 | 5 | 'reference' => $matches[2] |
|
53 | ]; |
||
54 | |||
55 | 5 | return $head + $this->parseFields( $matches[3] ); |
|
56 | } |
||
57 | |||
58 | 7 | private function findBibtexFormatMatches( $bibtex ) { |
|
59 | |||
60 | 7 | $matches = preg_split("/@(.*)[{(](.*),/U", $bibtex, 2, PREG_SPLIT_DELIM_CAPTURE ); |
|
61 | |||
62 | // Silently retreat from processing |
||
63 | 7 | if ( !isset( $matches[2] ) ) { |
|
64 | 2 | return []; |
|
65 | } |
||
66 | |||
67 | 5 | if( preg_match("/=/", $matches[2] ) ) { |
|
68 | $matches = preg_split("/@(.*)\s*[{(](.*)/U", $bibtex, 2, PREG_SPLIT_DELIM_CAPTURE ); |
||
69 | } |
||
70 | |||
71 | 5 | return $matches; |
|
72 | } |
||
73 | |||
74 | 5 | private function parseFields( $content ) { |
|
75 | 5 | $elements = []; |
|
76 | 5 | $values = []; |
|
77 | |||
78 | 5 | $length = strlen( $content ); |
|
79 | |||
80 | 5 | if( $content[$length - 1] == "}" || $content[$length - 1] == ")" || $content[$length - 1] == ",") { |
|
81 | 5 | $content = substr( $content, 0, $length - 1 ); |
|
82 | } |
||
83 | |||
84 | 5 | $split = preg_split("/=/", $content, 2 ); |
|
85 | 5 | $string = $split[1]; |
|
86 | |||
87 | 5 | while( $string ) { |
|
88 | 5 | list( $entry, $string ) = $this->splitField( $string ); |
|
89 | 5 | $values[] = $entry; |
|
90 | } |
||
91 | |||
92 | 5 | foreach( $values as $value ) { |
|
93 | 5 | $pos = strpos( $content, $value); |
|
94 | 5 | $content = substr_replace( $content, '', $pos, strlen( $value ) ); |
|
95 | } |
||
96 | |||
97 | 5 | $rev = strrev( trim( $content ) ); |
|
98 | |||
99 | 5 | if( $rev{0} != ',') { |
|
100 | 5 | $content .= ','; |
|
101 | } |
||
102 | |||
103 | 5 | $keys = preg_split("/=,/", $content ); |
|
104 | 5 | array_pop($keys); |
|
105 | |||
106 | 5 | foreach( $keys as $key ) { |
|
107 | 5 | $value = trim( array_shift( $values ) ); |
|
108 | 5 | $rev = strrev( $value ); |
|
109 | |||
110 | // remove any dangling ',' left on final field of entry |
||
111 | 5 | if($rev{0} == ',') { |
|
112 | 1 | $value = rtrim($value, ","); |
|
113 | } |
||
114 | |||
115 | 5 | if(!$value) { |
|
116 | continue; |
||
117 | } |
||
118 | |||
119 | 5 | $key = strtolower(trim($key)); |
|
120 | 5 | $value = trim($value); |
|
121 | 5 | $elements[$key] = $this->removeDelimiters( $value ); |
|
122 | } |
||
123 | |||
124 | 5 | return $elements; |
|
125 | } |
||
126 | |||
127 | 5 | private function splitField( $seg ) { |
|
137 | |||
138 | 5 | private function removeDelimiters( $string ) { |
|
139 | |||
140 | 5 | if( $string && ( $string{0} == "\"") ) { |
|
157 | } |
||
158 |