[Yaml] fix indented line handling in folded blocks
This commit is contained in:
parent
0c2f1d94de
commit
756834c8dd
@ -468,13 +468,13 @@ class Parser
|
|||||||
}
|
}
|
||||||
|
|
||||||
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
||||||
$text = '';
|
$blockLines = array();
|
||||||
|
|
||||||
// leading blank lines are consumed before determining indentation
|
// leading blank lines are consumed before determining indentation
|
||||||
while ($notEOF && $isCurrentLineBlank) {
|
while ($notEOF && $isCurrentLineBlank) {
|
||||||
// newline only if not EOF
|
// newline only if not EOF
|
||||||
if ($notEOF = $this->moveToNextLine()) {
|
if ($notEOF = $this->moveToNextLine()) {
|
||||||
$text .= "\n";
|
$blockLines[] = '';
|
||||||
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -495,37 +495,59 @@ class Parser
|
|||||||
preg_match($pattern, $this->currentLine, $matches)
|
preg_match($pattern, $this->currentLine, $matches)
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
if ($isCurrentLineBlank) {
|
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
|
||||||
$text .= substr($this->currentLine, $indentation);
|
$blockLines[] = substr($this->currentLine, $indentation);
|
||||||
|
} elseif ($isCurrentLineBlank) {
|
||||||
|
$blockLines[] = '';
|
||||||
} else {
|
} else {
|
||||||
$text .= $matches[1];
|
$blockLines[] = $matches[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
// newline only if not EOF
|
// newline only if not EOF
|
||||||
if ($notEOF = $this->moveToNextLine()) {
|
if ($notEOF = $this->moveToNextLine()) {
|
||||||
$text .= "\n";
|
|
||||||
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
$isCurrentLineBlank = $this->isCurrentLineBlank();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ($notEOF) {
|
} elseif ($notEOF) {
|
||||||
$text .= "\n";
|
$blockLines[] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($notEOF) {
|
if ($notEOF) {
|
||||||
|
$blockLines[] = '';
|
||||||
$this->moveToPreviousLine();
|
$this->moveToPreviousLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
// folded style
|
// folded style
|
||||||
if ('>' === $style) {
|
if ('>' === $style) {
|
||||||
// folded lines
|
$text = '';
|
||||||
// replace all non-leading/non-trailing single newlines with spaces
|
$previousLineIndented = false;
|
||||||
preg_match('/(\n*)$/', $text, $matches);
|
$previousLineBlank = false;
|
||||||
$text = preg_replace('/(?<!\n|^)\n(?!\n)/', ' ', rtrim($text, "\n"));
|
|
||||||
$text .= $matches[1];
|
|
||||||
|
|
||||||
// empty separation lines
|
for ($i = 0; $i < count($blockLines); $i++) {
|
||||||
// remove one newline from each group of non-leading/non-trailing newlines
|
if ('' === $blockLines[$i]) {
|
||||||
$text = preg_replace('/[^\n]\n+\K\n(?=[^\n])/', '', $text);
|
$text .= "\n";
|
||||||
|
$previousLineIndented = false;
|
||||||
|
$previousLineBlank = true;
|
||||||
|
} elseif (' ' === $blockLines[$i][0]) {
|
||||||
|
$text .= "\n".$blockLines[$i];
|
||||||
|
$previousLineIndented = true;
|
||||||
|
$previousLineBlank = false;
|
||||||
|
} elseif ($previousLineIndented) {
|
||||||
|
$text .= "\n".$blockLines[$i];
|
||||||
|
$previousLineIndented = false;
|
||||||
|
$previousLineBlank = false;
|
||||||
|
} elseif ($previousLineBlank || 0 === $i) {
|
||||||
|
$text .= $blockLines[$i];
|
||||||
|
$previousLineIndented = false;
|
||||||
|
$previousLineBlank = false;
|
||||||
|
} else {
|
||||||
|
$text .= ' '.$blockLines[$i];
|
||||||
|
$previousLineIndented = false;
|
||||||
|
$previousLineBlank = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$text = implode("\n", $blockLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
// deal with trailing newlines
|
// deal with trailing newlines
|
||||||
|
@ -820,6 +820,57 @@ EOT
|
|||||||
array($yaml2, $expected2),
|
array($yaml2, $expected2),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testBlankLinesAreParsedAsNewLinesInFoldedBlocks()
|
||||||
|
{
|
||||||
|
$yaml = <<<EOT
|
||||||
|
test: >
|
||||||
|
<h2>A heading</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>a list</li>
|
||||||
|
<li>may be a good example</li>
|
||||||
|
</ul>
|
||||||
|
EOT;
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
array(
|
||||||
|
'test' => <<<EOT
|
||||||
|
<h2>A heading</h2>
|
||||||
|
<ul> <li>a list</li> <li>may be a good example</li> </ul>
|
||||||
|
EOT
|
||||||
|
,
|
||||||
|
),
|
||||||
|
$this->parser->parse($yaml)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
|
||||||
|
{
|
||||||
|
$yaml = <<<EOT
|
||||||
|
test: >
|
||||||
|
<h2>A heading</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>a list</li>
|
||||||
|
<li>may be a good example</li>
|
||||||
|
</ul>
|
||||||
|
EOT;
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
array(
|
||||||
|
'test' => <<<EOT
|
||||||
|
<h2>A heading</h2>
|
||||||
|
<ul>
|
||||||
|
<li>a list</li>
|
||||||
|
<li>may be a good example</li>
|
||||||
|
</ul>
|
||||||
|
EOT
|
||||||
|
,
|
||||||
|
),
|
||||||
|
$this->parser->parse($yaml)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class B
|
class B
|
||||||
|
Reference in New Issue
Block a user