From f7f86a709b13da0dd398e0d104c5dc179d50d113 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 2 May 2023 17:03:03 -1000 Subject: [PATCH] imp: timedot: fix day description/comment parsing; parse posting comments/tags --- hledger-lib/Hledger/Read/TimedotReader.hs | 29 +++++++++++++------- hledger/hledger.m4.md | 18 +++++-------- hledger/test/timedot.test | 33 ++++++++++++++++++----- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/hledger-lib/Hledger/Read/TimedotReader.hs b/hledger-lib/Hledger/Read/TimedotReader.hs index 84ed45dbe..2a28019df 100644 --- a/hledger-lib/Hledger/Read/TimedotReader.hs +++ b/hledger-lib/Hledger/Read/TimedotReader.hs @@ -123,23 +123,24 @@ preamblep = do dayp :: JournalParser m () dayp = label "timedot day entry" $ do lift $ traceparse "dayp" - (d,desc) <- datelinep + (date,desc,comment,tags) <- datelinep commentlinesp ts <- many $ entryp <* commentlinesp - modify' $ addTransactions $ map (\t -> t{tdate=d, tdescription=desc}) ts + modify' $ addTransactions $ map (\t -> t{tdate=date, tdescription=desc, tcomment=comment, ttags=tags}) ts lift $ traceparse' "dayp" where addTransactions :: [Transaction] -> Journal -> Journal addTransactions ts j = foldl' (flip ($)) j (map addTransaction ts) -datelinep :: JournalParser m (Day,Text) +datelinep :: JournalParser m (Day,Text,Text,[Tag]) datelinep = do lift $ traceparse "datelinep" lift $ optional orgheadingprefixp - d <- datep - desc <- strip <$> lift restofline + date <- datep + desc <- T.strip <$> lift descriptionp + (comment, tags) <- lift transactioncommentp lift $ traceparse' "datelinep" - return (d, T.pack desc) + return (date, desc, comment, tags) -- | Zero or more empty lines or hash/semicolon comment lines -- or org headlines which do not start a new day. @@ -172,10 +173,16 @@ entryp = do lift $ optional $ choice [orgheadingprefixp, skipNonNewlineSpaces1] a <- modifiedaccountnamep lift skipNonNewlineSpaces - hours <- - try (lift followingcommentp >> return 0) - <|> (lift durationp <* - (try (lift followingcommentp) <|> (newline >> return ""))) + (hours, comment, tags) <- + try (do + (c,ts) <- lift transactioncommentp -- or postingp, but let's not bother supporting date:/date2: + return (0, c, ts) + ) + <|> (do + h <- lift durationp + (c,ts) <- try (lift transactioncommentp) <|> (newline >> return ("",[])) + return (h,c,ts) + ) mcs <- getDefaultCommodityAndStyle let (c,s) = case mcs of @@ -188,6 +195,8 @@ entryp = do nullposting{paccount=a ,pamount=mixedAmount $ nullamt{acommodity=c, aquantity=hours, astyle=s} ,ptype=VirtualPosting + ,pcomment=comment + ,ptags=tags ,ptransaction=Just t } ] diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 4c0595bc6..329082581 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -4201,19 +4201,15 @@ A day entry begins with a date line: Optionally this can be followed on the same line by -- a common **transaction description** for this day -- a common **transaction comment** for this day, after a semicolon (`;`). +- a **common description** for this day's transactions. +- a **common comment** for this day's transactions, following a semicolon (`;`). -After the date line are zero or more optionally-indented -time transaction lines, consisting of: +After the date line are zero or more optionally-indented time transactions, consisting of: -- an **account name** - any word or phrase, usually a - hledger-style [account name](#account-names). -- **two or more spaces** - a field separator, - required if there is an amount (as in journal format). -- a **timedot amount** - dots representing quarter hours, - or a number representing hours. -- an optional **comment** beginning with semicolon. This is ignored. +- an **account name** - any word or phrase, usually a hledger-style [account name](#account-names). +- **two or more spaces** - a field separator, required if there is an amount (as in journal format). +- a **timedot amount** - dots representing quarter hours, or a number representing hours, optionally with a unit suffix. +- an **posting comment** for this transaction, following with semicolon. In more detail, timedot amounts can be: diff --git a/hledger/test/timedot.test b/hledger/test/timedot.test index 1525fe26a..622e56c68 100644 --- a/hledger/test/timedot.test +++ b/hledger/test/timedot.test @@ -1,13 +1,11 @@ -# Note since 1.17 we need to specify stdin's format explicitly. - # 1. basic timedot entry < -# comment +# file comment +; another file comment -; another comment 2020-01-01 - a:aa 1 - b:bb 2 +a:aa 1 +b:bb 2 $ hledger -ftimedot:- print 2020-01-01 * @@ -35,3 +33,26 @@ $ hledger -ftimedot:- print --alias a=b (b:aa) 1.00 >=0 + +# 4. A common day description and comment, and posting comments are supported. +< +2023-01-01 day description ; day comment, day-tag: +a .... +b .... ; posting comment, posting-tag: + +$ hledger -ftimedot:- print +2023-01-01 * day description ; day comment, day-tag: + (a) 1.00 + +2023-01-01 * day description ; day comment, day-tag: + (b) 1.00 ; posting comment, posting-tag: + +>= + +# 5. Transaction descriptions, comments and tags are parsed properly. +$ hledger -ftimedot:- descriptions tag:day-tag +day description + +# 6. Posting comments and tags are parsed properly. +$ hledger -ftimedot:- reg tag:posting-tag +2023-01-01 day description (b) 1.00 1.00