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