From 823be7c5659e6b227cc00f4e4d9324948c7ddf54 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 28 Sep 2024 18:54:43 -1000 Subject: [PATCH] fix: csv: tags on following lines, and posting dates, also work now [#2241] Follow-on work from #2214. --- hledger-lib/Hledger/Read/RulesReader.hs | 25 ++++++++++++++++++++----- hledger/hledger.m4.md | 2 ++ hledger/test/csv.test | 16 ++++++++++------ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/hledger-lib/Hledger/Read/RulesReader.hs b/hledger-lib/Hledger/Read/RulesReader.hs index ddfdb7a1a..3a29b348b 100644 --- a/hledger-lib/Hledger/Read/RulesReader.hs +++ b/hledger-lib/Hledger/Read/RulesReader.hs @@ -76,7 +76,7 @@ import Text.Printf (printf) import Hledger.Data import Hledger.Utils -import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep, commenttagsp ) +import Hledger.Read.Common (aliasesFromOpts, Reader(..), InputOpts(..), amountp, statusp, journalFinalise, accountnamep, transactioncommentp, postingcommentp ) import Hledger.Write.Csv import System.Directory (doesFileExist, getHomeDirectory) import Data.Either (fromRight) @@ -1111,7 +1111,13 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t code = maybe "" singleline' $ fieldval "code" description = maybe "" singleline' $ fieldval "description" comment = maybe "" unescapeNewlines $ fieldval "comment" - ttags = fromRight [] $ rtp commenttagsp comment + + -- Convert some parsed comment text back into following comment syntax, + -- with the semicolons and indents, so it can be parsed again for tags. + textToFollowingComment :: Text -> Text + textToFollowingComment = T.stripStart . T.unlines . map (" ;"<>) . T.lines + + ttags = fromRight [] $ fmap snd $ rtp transactioncommentp $ textToFollowingComment comment precomment = maybe "" unescapeNewlines $ fieldval "precomment" singleline' = T.unwords . filter (not . T.null) . map T.strip . T.lines @@ -1124,7 +1130,15 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t p1IsVirtual = (accountNamePostingType <$> fieldval "account1") == Just VirtualPosting ps = [p | n <- [1..maxpostings] ,let cmt = maybe "" unescapeNewlines $ fieldval ("comment"<> T.pack (show n)) - ,let ptags = fromRight [] $ rtp commenttagsp cmt + -- Tags in the comment will be parsed and attached to the posting. + -- A posting date, in the date: tag or in brackets, will also be parsed and applied to the posting. + -- But it must have a year, or it will be ignored. + -- A secondary posting date will also be ignored. + ,let (tags,mdate) = + fromRight ([],Nothing) $ + fmap (\(_,ts,md,_)->(ts,md)) $ + rtp (postingcommentp Nothing) $ + textToFollowingComment cmt ,let currency = fromMaybe "" (fieldval ("currency"<> T.pack (show n)) <|> fieldval "currency") ,let mamount = getAmount rules record currency p1IsVirtual n ,let mbalance = getBalance rules record currency n @@ -1132,12 +1146,13 @@ transactionFromCsvRecord timesarezoned mtzin tzout sourcepos rules record = t ,let acct' | not isfinal && acct==unknownExpenseAccount && fromMaybe False (mamount >>= isNegativeMixedAmount) = unknownIncomeAccount | otherwise = acct - ,let p = nullposting{paccount = accountNameWithoutPostingType acct' + ,let p = nullposting{pdate = mdate + ,paccount = accountNameWithoutPostingType acct' ,pamount = fromMaybe missingmixedamt mamount ,ptransaction = Just t ,pbalanceassertion = mkBalanceAssertion rules record <$> mbalance ,pcomment = cmt - ,ptags = ptags + ,ptags = tags ,ptype = accountNamePostingType acct } ] diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 10f0a0316..fabed60ce 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -3351,6 +3351,8 @@ You can assign multi-line comments by writing literal `\n` in the code. A commen Comments can contain [tags](#tags), as usual. +Posting comments can also contain a [posting date](#posting-dates). A secondary date, or a year-less date, will be ignored. + ### account field Assigning to `accountN`, where N is 1 to 99, diff --git a/hledger/test/csv.test b/hledger/test/csv.test index 82de24722..1734d5312 100644 --- a/hledger/test/csv.test +++ b/hledger/test/csv.test @@ -1094,26 +1094,30 @@ $ ./csvtest.sh >2 /unexpected space/ >=1 -# ** 56. match transaction by posting tag (#2114) +# ** 56. make sure transaction tags are functional (#2114), including ones on subsequent lines (#2241) < 2020-01-01, 1 RULES fields date, desc if %desc 1 - comment ttag:tval - comment1 ptag:pval + comment ttag:tval\nttag2: + comment1 ptag:pval\ndate:2022-02-02 account1 a -$ ./csvtest.sh tag:ptag +$ ./csvtest.sh tag:ttag2 2020-01-01 ; ttag:tval + ; ttag2: a ; ptag:pval + ; date:2022-02-02 >= -# ** 57. match transaction by transaction tag (#2114) -$ ./csvtest.sh tag:ttag +# ** 57. and also posting tags, and (primary, yearful) posting dates (#2114, #2241) +$ ./csvtest.sh tag:date 2020-01-01 ; ttag:tval + ; ttag2: a ; ptag:pval + ; date:2022-02-02 >=