lib: Properly escape quotes in csv output.

This commit is contained in:
Stephen Morgan 2021-03-23 23:04:30 +11:00 committed by Simon Michael
parent 4609e79f2c
commit 4cb9dfb5b8
2 changed files with 13 additions and 2 deletions

View File

@ -813,8 +813,8 @@ parseResultToCsv = toListList . unpackFields
printCSV :: CSV -> TL.Text printCSV :: CSV -> TL.Text
printCSV = TB.toLazyText . unlinesB . map printRecord printCSV = TB.toLazyText . unlinesB . map printRecord
where printRecord = mconcat . map TB.fromText . intersperse "," . map printField where printRecord = foldMap TB.fromText . intersperse "," . map printField
printField = wrap "\"" "\"" . T.replace "\"" "\\\"\\\"" printField = wrap "\"" "\"" . T.replace "\"" "\"\""
-- | Return the cleaned up and validated CSV data (can be empty), or an error. -- | Return the cleaned up and validated CSV data (can be empty), or an error.
validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord] validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord]

View File

@ -143,3 +143,14 @@ hledger -f - balance -b 2016/10 -e 2016/11
>>>2 >>>2
>>>= 0 >>>= 0
# 7. Test csv output
hledger -f - balance -N --output-format=csv
<<<
2021-01-01 Test
Assets:ABC "AB.C" 1
Assets:Cash -$1
>>>
"account","balance"
"Assets:ABC","""AB.C"" 1"
"Assets:Cash","$-1"
>>>= 0