diff --git a/Parser.php b/Parser.php index be0a406a..14147a6a 100644 --- a/Parser.php +++ b/Parser.php @@ -295,6 +295,42 @@ public function parse($value, $flags = 0) return $value; } + // try to parse the value as a multi-line string as a last resort + if (0 === $this->currentLineNb) { + $parseError = false; + $previousLineWasNewline = false; + $value = ''; + + foreach ($this->lines as $line) { + try { + $parsedLine = Inline::parse($line, $flags, $this->refs); + + if (!is_string($value)) { + $parseError = true; + break; + } + + if ('' === trim($parsedLine)) { + $value .= "\n"; + $previousLineWasNewline = true; + } elseif ($previousLineWasNewline) { + $value .= trim($parsedLine); + $previousLineWasNewline = false; + } else { + $value .= ' '.trim($parsedLine); + $previousLineWasNewline = false; + } + } catch (ParseException $e) { + $parseError = true; + break; + } + } + + if (!$parseError) { + return trim($value); + } + } + switch (preg_last_error()) { case PREG_INTERNAL_ERROR: $error = 'Internal PCRE error.'; diff --git a/Tests/ParserTest.php b/Tests/ParserTest.php index 0e4fb14a..5ba71f71 100644 --- a/Tests/ParserTest.php +++ b/Tests/ParserTest.php @@ -1465,6 +1465,32 @@ public function testParseMultiLineUnquotedString() $this->assertSame(array('foo' => 'bar baz foobar foo', 'bar' => 'baz'), $this->parser->parse($yaml)); } + + public function testParseMultiLineString() + { + $this->assertEquals("foo bar\nbaz", $this->parser->parse("foo\nbar\n\nbaz")); + } + + public function testParseMultiLineMappingValue() + { + $yaml = <<<'EOF' +foo: +- bar: + one + + two + three +EOF; + $expected = array( + 'foo' => array( + array( + 'bar' => "one\ntwo three", + ), + ), + ); + + $this->assertEquals($expected, $this->parser->parse($yaml)); + } } class B