20
20
*/
21
21
class Parser
22
22
{
23
- const FOLDED_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)? ' ;
23
+ const BLOCK_SCALAR_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)? ' ;
24
24
25
25
private $ offset = 0 ;
26
26
private $ lines = array ();
@@ -331,7 +331,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
331
331
$ isItUnindentedCollection = $ this ->isStringUnIndentedCollectionItem ($ this ->currentLine );
332
332
333
333
// Comments must not be removed inside a string block (ie. after a line ending with "|")
334
- $ removeCommentsPattern = '~ ' .self ::FOLDED_SCALAR_PATTERN .'$~ ' ;
334
+ $ removeCommentsPattern = '~ ' .self ::BLOCK_SCALAR_PATTERN .'$~ ' ;
335
335
$ removeComments = !preg_match ($ removeCommentsPattern , $ this ->currentLine );
336
336
337
337
while ($ this ->moveToNextLine ()) {
@@ -420,10 +420,10 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
420
420
return $ this ->refs [$ value ];
421
421
}
422
422
423
- if (preg_match ('/^ ' .self ::FOLDED_SCALAR_PATTERN .'$/ ' , $ value , $ matches )) {
423
+ if (preg_match ('/^ ' .self ::BLOCK_SCALAR_PATTERN .'$/ ' , $ value , $ matches )) {
424
424
$ modifiers = isset ($ matches ['modifiers ' ]) ? $ matches ['modifiers ' ] : '' ;
425
425
426
- return $ this ->parseFoldedScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
426
+ return $ this ->parseBlockScalar ($ matches ['separator ' ], preg_replace ('#\d+# ' , '' , $ modifiers ), (int ) abs ($ modifiers ));
427
427
}
428
428
429
429
try {
@@ -437,15 +437,15 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport)
437
437
}
438
438
439
439
/**
440
- * Parses a folded scalar.
440
+ * Parses a block scalar.
441
441
*
442
- * @param string $separator The separator that was used to begin this folded scalar (| or >)
443
- * @param string $indicator The indicator that was used to begin this folded scalar (+ or -)
444
- * @param int $indentation The indentation that was used to begin this folded scalar
442
+ * @param string $separator The separator that was used to begin this block scalar (| or >)
443
+ * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
444
+ * @param int $indentation The indentation indicator that was used to begin this block scalar
445
445
*
446
446
* @return string The text value
447
447
*/
448
- private function parseFoldedScalar ($ separator , $ indicator = '' , $ indentation = 0 )
448
+ private function parseBlockScalar ($ separator , $ chomping = '' , $ indentation = 0 )
449
449
{
450
450
$ notEOF = $ this ->moveToNextLine ();
451
451
if (!$ notEOF ) {
@@ -500,17 +500,25 @@ private function parseFoldedScalar($separator, $indicator = '', $indentation = 0
500
500
$ this ->moveToPreviousLine ();
501
501
}
502
502
503
- // replace all non-trailing single newlines with spaces in folded blocks
503
+ // folded style
504
504
if ('> ' === $ separator ) {
505
+ // folded lines
506
+ // replace all non-leading/non-trailing single newlines with spaces
505
507
preg_match ('/(\n*)$/ ' , $ text , $ matches );
506
- $ text = preg_replace ('/(?<!\n)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
508
+ $ text = preg_replace ('/(?<!\n|^)\n(?!\n)/ ' , ' ' , rtrim ($ text , "\n" ));
509
+ $ text .= $ matches [1 ];
510
+
511
+ // empty separation lines
512
+ // remove one newline from each group of non-leading/non-trailing newlines
513
+ preg_match ('/(\n*)$/ ' , $ text , $ matches );
514
+ $ text = preg_replace ('/[^\n]\n+\K\n/ ' , '$1 ' , rtrim ($ text , "\n" ));
507
515
$ text .= $ matches [1 ];
508
516
}
509
517
510
- // deal with trailing newlines as indicated
511
- if ('' === $ indicator ) {
518
+ // deal with trailing newlines
519
+ if ('' === $ chomping ) {
512
520
$ text = preg_replace ('/\n+$/ ' , "\n" , $ text );
513
- } elseif ('- ' === $ indicator ) {
521
+ } elseif ('- ' === $ chomping ) {
514
522
$ text = preg_replace ('/\n+$/ ' , '' , $ text );
515
523
}
516
524
0 commit comments