fix(parsing): keep more-indented folded lines literal
A folded block scalar folded every non-blank line into the surrounding text, including more-indented lines, which YAML keeps literal with their line breaks. Detect a more-indented line by its retained leading whitespace and keep the breaks adjacent to it instead of folding them. Plain and blank-line folding are unchanged.
This commit is contained in:
parent
d3ff3823a5
commit
4358cf02cc
@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
paths (which had duplicated the loop), and `blockScalarValue` applies literal
|
||||
versus folded line semantics and clip/strip/keep chomping correctly. A block
|
||||
scalar without blank lines is unchanged.
|
||||
- Keep a more-indented line inside a folded `>` block literal, with its line
|
||||
breaks preserved, rather than folding it into the surrounding text.
|
||||
|
||||
### Documentation
|
||||
|
||||
|
||||
@ -274,6 +274,11 @@ extension PureYAML.Parsing.TokenEventParser {
|
||||
result += lines[index]
|
||||
index += 1
|
||||
}
|
||||
} else if isMoreIndentedBlockLine(lines[index]) || isMoreIndentedBlockLine(lines[index - 1]) {
|
||||
// A line break adjacent to a more-indented line is kept literal,
|
||||
// so the more-indented line and its breaks survive folding.
|
||||
result += "\n" + lines[index]
|
||||
index += 1
|
||||
} else {
|
||||
result += " " + lines[index]
|
||||
index += 1
|
||||
@ -282,6 +287,12 @@ extension PureYAML.Parsing.TokenEventParser {
|
||||
return result + "\n"
|
||||
}
|
||||
|
||||
func isMoreIndentedBlockLine(_ line: String) -> Bool {
|
||||
// In a block scalar the scanner strips content lines to the block's
|
||||
// indentation, so only a more-indented line retains leading whitespace.
|
||||
line.first == " " || line.first == "\t"
|
||||
}
|
||||
|
||||
func chompedBlockScalar(
|
||||
_ body: String,
|
||||
chomping: PureYAML.Parsing.BlockScalarChomping,
|
||||
|
||||
@ -36,6 +36,12 @@ struct BlockScalarChompingTests {
|
||||
#expect(try value("k: >\n a\n\n\n b\nnext: 1\n") == .string("a\n\nb\n"))
|
||||
}
|
||||
|
||||
@Test("A more-indented line inside a folded block stays literal")
|
||||
func test_foldedMoreIndentedLineStaysLiteral() throws {
|
||||
#expect(try value("k: >\n normal\n indented\n back\nnext: 1\n") == .string("normal\n indented\nback\n"))
|
||||
#expect(try value("k: >\n a\n code\nnext: 1\n") == .string("a\n code\n"))
|
||||
}
|
||||
|
||||
@Test("A block scalar with no blank lines is unaffected")
|
||||
func test_blockScalarWithoutBlankLinesUnchanged() throws {
|
||||
#expect(try value("k: |\n a\n b\nnext: 1\n") == .string("a\nb\n"))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user