@@ -468,13 +468,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
468
468
}
469
469
470
470
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
471
- $ text = '' ;
471
+ $ blockLines = array () ;
472
472
473
473
// leading blank lines are consumed before determining indentation
474
474
while ($ notEOF && $ isCurrentLineBlank ) {
475
475
// newline only if not EOF
476
476
if ($ notEOF = $ this ->moveToNextLine ()) {
477
- $ text .= "\n" ;
477
+ $ blockLines [] = '' ;
478
478
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
479
479
}
480
480
}
@@ -495,37 +495,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
495
495
preg_match ($ pattern , $ this ->currentLine , $ matches )
496
496
)
497
497
) {
498
- if ($ isCurrentLineBlank ) {
499
- $ text .= substr ($ this ->currentLine , $ indentation );
498
+ if ($ isCurrentLineBlank && strlen ($ this ->currentLine ) > $ indentation ) {
499
+ $ blockLines [] = substr ($ this ->currentLine , $ indentation );
500
+ } elseif ($ isCurrentLineBlank ) {
501
+ $ blockLines [] = '' ;
500
502
} else {
501
- $ text . = $ matches [1 ];
503
+ $ blockLines [] = $ matches [1 ];
502
504
}
503
505
504
506
// newline only if not EOF
505
507
if ($ notEOF = $ this ->moveToNextLine ()) {
506
- $ text .= "\n" ;
507
508
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
508
509
}
509
510
}
510
511
} elseif ($ notEOF ) {
511
- $ text .= "\n" ;
512
+ $ blockLines [] = '' ;
512
513
}
513
514
514
515
if ($ notEOF ) {
516
+ $ blockLines [] = '' ;
515
517
$ this ->moveToPreviousLine ();
516
518
}
517
519
518
520
// folded style
519
521
if ('> ' === $ style ) {
520
- // folded lines
521
- // replace all non-leading/non-trailing single newlines with spaces
522
- preg_match ('/(\n*)$/ ' , $ text , $ matches );
523
- $ text = preg_replace ('/(?<!\n|^)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
524
- $ text .= $ matches [1 ];
525
-
526
- // empty separation lines
527
- // remove one newline from each group of non-leading/non-trailing newlines
528
- $ text = preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' , '' , $ text );
522
+ $ text = '' ;
523
+ $ previousLineIndented = false ;
524
+ $ previousLineBlank = false ;
525
+
526
+ for ($ i = 0 ; $ i < count ($ blockLines ); $ i ++) {
527
+ if ('' === $ blockLines [$ i ]) {
528
+ $ text .= "\n" ;
529
+ $ previousLineIndented = false ;
530
+ $ previousLineBlank = true ;
531
+ } elseif (' ' === $ blockLines [$ i ][0 ]) {
532
+ $ text .= "\n" .$ blockLines [$ i ];
533
+ $ previousLineIndented = true ;
534
+ $ previousLineBlank = false ;
535
+ } elseif ($ previousLineIndented ) {
536
+ $ text .= "\n" .$ blockLines [$ i ];
537
+ $ previousLineIndented = false ;
538
+ $ previousLineBlank = false ;
539
+ } elseif ($ previousLineBlank || 0 === $ i ) {
540
+ $ text .= $ blockLines [$ i ];
541
+ $ previousLineIndented = false ;
542
+ $ previousLineBlank = false ;
543
+ } else {
544
+ $ text .= ' ' .$ blockLines [$ i ];
545
+ $ previousLineIndented = false ;
546
+ $ previousLineBlank = false ;
547
+ }
548
+ }
549
+ } else {
550
+ $ text = implode ("\n" , $ blockLines );
529
551
}
530
552
531
553
// deal with trailing newlines
0 commit comments