@@ -509,13 +509,13 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
509
509
}
510
510
511
511
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
512
- $ text = '' ;
512
+ $ blockLines = array () ;
513
513
514
514
// leading blank lines are consumed before determining indentation
515
515
while ($ notEOF && $ isCurrentLineBlank ) {
516
516
// newline only if not EOF
517
517
if ($ notEOF = $ this ->moveToNextLine ()) {
518
- $ text .= "\n" ;
518
+ $ blockLines [] = '' ;
519
519
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
520
520
}
521
521
}
@@ -536,37 +536,59 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
536
536
preg_match ($ pattern , $ this ->currentLine , $ matches )
537
537
)
538
538
) {
539
- if ($ isCurrentLineBlank ) {
540
- $ text .= substr ($ this ->currentLine , $ indentation );
539
+ if ($ isCurrentLineBlank && strlen ($ this ->currentLine ) > $ indentation ) {
540
+ $ blockLines [] = substr ($ this ->currentLine , $ indentation );
541
+ } elseif ($ isCurrentLineBlank ) {
542
+ $ blockLines [] = '' ;
541
543
} else {
542
- $ text . = $ matches [1 ];
544
+ $ blockLines [] = $ matches [1 ];
543
545
}
544
546
545
547
// newline only if not EOF
546
548
if ($ notEOF = $ this ->moveToNextLine ()) {
547
- $ text .= "\n" ;
548
549
$ isCurrentLineBlank = $ this ->isCurrentLineBlank ();
549
550
}
550
551
}
551
552
} elseif ($ notEOF ) {
552
- $ text .= "\n" ;
553
+ $ blockLines [] = '' ;
553
554
}
554
555
555
556
if ($ notEOF ) {
557
+ $ blockLines [] = '' ;
556
558
$ this ->moveToPreviousLine ();
557
559
}
558
560
559
561
// folded style
560
562
if ('> ' === $ style ) {
561
- // folded lines
562
- // replace all non-leading/non-trailing single newlines with spaces
563
- preg_match ('/(\n*)$/ ' , $ text , $ matches );
564
- $ text = preg_replace ('/(?<!\n|^)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
565
- $ text .= $ matches [1 ];
566
-
567
- // empty separation lines
568
- // remove one newline from each group of non-leading/non-trailing newlines
569
- $ text = preg_replace ('/[^\n]\n+\K\n(?=[^\n])/ ' , '' , $ text );
563
+ $ text = '' ;
564
+ $ previousLineIndented = false ;
565
+ $ previousLineBlank = false ;
566
+
567
+ for ($ i = 0 ; $ i < count ($ blockLines ); $ i ++) {
568
+ if ('' === $ blockLines [$ i ]) {
569
+ $ text .= "\n" ;
570
+ $ previousLineIndented = false ;
571
+ $ previousLineBlank = true ;
572
+ } elseif (' ' === $ blockLines [$ i ][0 ]) {
573
+ $ text .= "\n" .$ blockLines [$ i ];
574
+ $ previousLineIndented = true ;
575
+ $ previousLineBlank = false ;
576
+ } elseif ($ previousLineIndented ) {
577
+ $ text .= "\n" .$ blockLines [$ i ];
578
+ $ previousLineIndented = false ;
579
+ $ previousLineBlank = false ;
580
+ } elseif ($ previousLineBlank || 0 === $ i ) {
581
+ $ text .= $ blockLines [$ i ];
582
+ $ previousLineIndented = false ;
583
+ $ previousLineBlank = false ;
584
+ } else {
585
+ $ text .= ' ' .$ blockLines [$ i ];
586
+ $ previousLineIndented = false ;
587
+ $ previousLineBlank = false ;
588
+ }
589
+ }
590
+ } else {
591
+ $ text = implode ("\n" , $ blockLines );
570
592
}
571
593
572
594
// deal with trailing newlines
0 commit comments