diff --git a/CHANGELOG.md b/CHANGELOG.md index 21e16f4..4005e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Sources/Parsing/TokenEventParser.Block.swift b/Sources/Parsing/TokenEventParser.Block.swift index c8ea178..89192c1 100644 --- a/Sources/Parsing/TokenEventParser.Block.swift +++ b/Sources/Parsing/TokenEventParser.Block.swift @@ -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, diff --git a/Tests/BlockScalarChompingTests.swift b/Tests/BlockScalarChompingTests.swift index d351630..d0b8c0a 100644 --- a/Tests/BlockScalarChompingTests.swift +++ b/Tests/BlockScalarChompingTests.swift @@ -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"))