Skip to content

Commit

Permalink
Skip indentation for empty line in multiline encoding
Browse files Browse the repository at this point in the history
This change prevents yamllint error when encoding multilines
that contains empty lines.

Signed-off-by: Tristan Cacqueray <[email protected]>
  • Loading branch information
TristanCacqueray committed Mar 9, 2021
1 parent 4c89b1a commit bcff353
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ encodeLines :: Int -> [Text] -> Builder
encodeLines level ls =
mconcat $
(prefix :) $
intersperse (bs "\n" <> indent level) $ map (b . Text.Encoding.encodeUtf8) ls
intersperseOn (/= "") (bs "\n" <> indent level) (bs "\n") $ map Text.Encoding.encodeUtf8 ls
where
intersperseOn _ _ _ [] = []
intersperseOn cond thenClause elseClause (x:xs) = b x : map go xs
where
go x'
| cond x' = thenClause <> b x'
| otherwise = elseClause <> b x'
prefix =
mconcat
[ bs "|"
Expand Down
5 changes: 5 additions & 0 deletions test/Test/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tcDataTypes =
"leadingSymbol" : "!leading symbol",
"asteriskString": "*",
"multiLine": "The first line is followed by the\nsecond line\n",
"multiLineWithEmptyLines": "The first line is followed by an empty line\n\nthird line\n",
"multiLineWithSpaces": " This has extra\n spaces at the beginning\n",
"notMultiline": "This won't be\nmulti-lined",
"list": ["foo", "bar", "baz"],
Expand All @@ -86,6 +87,10 @@ listEmpty: []
multiLine: |
The first line is followed by the
second line
multiLineWithEmptyLines: |
The first line is followed by an empty line

third line
multiLineWithSpaces: |2
This has extra
spaces at the beginning
Expand Down

0 comments on commit bcff353

Please sign in to comment.