Skip to content

Commit f79fc43

Browse files
committed
Line break fix for bibtexParse
1 parent 85b34c2 commit f79fc43

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

includes/bibtexParse/PARSEENTRIES.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
- When an undefined string is found in function removeDelimiters return the empty string
6262
- Return $this->undefinedStrings in the last position to allow compatibility with previous versions
6363
- Fix management of preamble in function returnArrays
64+
65+
28/12/2015 Michael Winkler
66+
- Outdated PHP methods replaced
67+
- Fix for saving line breaks in abstracts and other fields
68+
6469
*/
6570

6671
/**
@@ -93,14 +98,52 @@ function openBib($file) {
9398
$this->parseFile = TRUE;
9499
}
95100

101+
/**
102+
* Sets a control character for saving line breaks
103+
* @param array $line_array
104+
* @return array
105+
* @since 2.3
106+
* @author Michael Winkler <[email protected]>
107+
*/
108+
static function SetLineBreak($line_array) {
109+
$max = count($line_array);
110+
111+
// Ignore the first line
112+
for ( $i = 1; $i < $max; $i++ ) {
113+
$line_before = mb_substr(trim( $line_array[$i-1] ), -2, 2);
114+
115+
if ( strpos($line_array[$i], '@') === false && // No '@' in the line
116+
strpos($line_array[$i], '=') === false && // No '=' in the line
117+
$line_before !== '},' && // No '},' at the end of the line before
118+
$line_array[$i][0] !== '}' // No '}' at the beginning of the line
119+
) {
120+
$line_array[$i] = '<LineBreak>' . $line_array[$i];
121+
}
122+
}
123+
return $line_array;
124+
}
125+
126+
/**
127+
* Replaces the control character with the original line break
128+
* @param string $string
129+
* @return string
130+
* @since 2.3
131+
* @author Michael Winkler <[email protected]>
132+
*/
133+
static function ReplaceLineBreak($string) {
134+
$return = str_replace('<LineBreak>', chr(13) . chr(10), $string);
135+
return $return;
136+
}
137+
96138
/**
97139
* Load a bibtex string to parse it
98140
*
99141
* @param string $bibtex_string
100142
*/
101143
function loadBibtexString($bibtex_string) {
102144
if(is_string($bibtex_string)) {
103-
$this->bibtexString = explode("\n",$bibtex_string);
145+
$line_array = explode("\n",$bibtex_string);
146+
$this->bibtexString = self::SetLineBreak($line_array);
104147
}
105148
else {
106149
$this->bibtexString = $bibtex_string;
@@ -223,6 +266,11 @@ function reduceFields($oldString){
223266
if(!$value) {
224267
continue;
225268
}
269+
270+
// 28/12/2015 Michael Winkler
271+
// Replace the control character for line breaks
272+
$value = self::ReplaceLineBreak($value);
273+
226274
// 21/08/2004 G.Gardey -> expand macro
227275
// Don't remove delimiters now needs to know if the value is a string macro
228276
// $this->entries[$this->count][strtolower(trim($key))] = trim($this->removeDelimiters(trim($value)));
@@ -235,7 +283,7 @@ function reduceFields($oldString){
235283
* Start splitting a bibtex entry into component fields.
236284
* Store the entry type and citation.
237285
*
238-
* @param $string $entry
286+
* @param string $entry
239287
*/
240288
function fullSplit($entry){
241289
$matches = preg_split("/@(.*)[{(](.*),/U", $entry, 2, PREG_SPLIT_DELIM_CAPTURE);
@@ -495,5 +543,4 @@ function returnArrays() {
495543
}
496544
return array($this->preamble, $this->strings, $this->entries, $this->undefinedStrings);
497545
}
498-
}
499-
?>
546+
}

0 commit comments

Comments
 (0)