From d925c8a0422f325fda863c39aaeffe09983ad696 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 8 Nov 2024 23:27:02 -1000 Subject: [PATCH] imp:print:beancount: add a second account name part if needed Beancount requires account names to have at least two parts. --- hledger-lib/Hledger/Write/Beancount.hs | 26 +++++++++++++------------- hledger/hledger.m4.md | 3 ++- hledger/test/print/beancount.test | 18 +++++++++--------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/hledger-lib/Hledger/Write/Beancount.hs b/hledger-lib/Hledger/Write/Beancount.hs index c8e149742..e0e1d3302 100644 --- a/hledger-lib/Hledger/Write/Beancount.hs +++ b/hledger-lib/Hledger/Write/Beancount.hs @@ -139,34 +139,34 @@ type BeancountAccountNameComponent = AccountName -- | Convert a hledger account name to a valid Beancount account name. -- It replaces spaces with dashes and other non-supported characters with C; --- prepends the letter A- to any part which doesn't begin with a letter or number; +-- prepends the letter A to any part which doesn't begin with a letter or number; +-- adds a second :A part if there is only one part; -- and capitalises each part. -- It also checks that the first part is one of the required english -- account names Assets, Liabilities, Equity, Income, or Expenses, and if not -- raises an informative error. -- Ref: https://beancount.github.io/docs/beancount_language_syntax.html#accounts accountNameToBeancount :: AccountName -> BeancountAccountName -accountNameToBeancount a = - dbg9 "beancount account name" $ - accountNameFromComponents bs' +accountNameToBeancount a = b where - bs = + cs1 = map accountNameComponentToBeancount $ accountNameComponents $ - dbg9 "hledger account name " $ - a - bs' = - case bs of - b:_ | b `notElem` beancountTopLevelAccounts -> error' e + dbg9 "hledger account name " a + cs2 = + case cs1 of + c:_ | c `notElem` beancountTopLevelAccounts -> error' e where e = T.unpack $ T.unlines [ - "bad top-level account: " <> b - ,"in beancount account name: " <> accountNameFromComponents bs + "bad top-level account: " <> c + ,"in beancount account name: " <> accountNameFromComponents cs1 ,"converted from hledger account name: " <> a ,"For Beancount, top-level accounts must be (or be --alias'ed to)" ,"one of " <> T.intercalate ", " beancountTopLevelAccounts <> "." -- ,"and not: " <> b ] - cs -> cs + [c] -> [c, "A"] + cs -> cs + b = dbg9 "beancount account name" $ accountNameFromComponents cs2 accountNameComponentToBeancount :: AccountName -> BeancountAccountNameComponent accountNameComponentToBeancount acctpart = diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 8dcbd569b..98d0faac9 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -842,7 +842,8 @@ Here are more details hledger will adjust your account names when needed, to make valid [Beancount account names](https://beancount.github.io/docs/beancount_language_syntax.html#accounts) (capitalising, replacing spaces with `-`, replacing other unsupported characters with `C`, -and prepending `A` to account name parts which don't begin with a letter or digit). +prepending `A` to account name parts which don't begin with a letter or digit, +and appending `:A` to account names which have only one part). However, you must ensure that all top level account names are one of the five required by Beancount: `Assets`, `Liabilities`, `Equity`, `Income`, or `Expenses`. If yours are named differently, you can use [account aliases](#alias-directive), diff --git a/hledger/test/print/beancount.test b/hledger/test/print/beancount.test index e87cd1c7e..6c9002cb3 100644 --- a/hledger/test/print/beancount.test +++ b/hledger/test/print/beancount.test @@ -9,10 +9,10 @@ $ hledger -f- print -O beancount >2 /bad top-level account/ >=1 -# ** 2. Otherwise, accounts are encoded to suit beancount, and open directives are added. +# ** 2. Otherwise, accounts are modified/encoded to suit beancount, and open directives are added. < 2000-01-01 - assets:a 0 ABC + assets 0 ABC equity:$-€:$ 0 USD $ hledger -f- print -O beancount @@ -34,13 +34,13 @@ $ hledger -f- print -O beancount assets 0 "size 2 pencils" $ hledger -f- print -O beancount -2000-01-01 open Assets +2000-01-01 open Assets:A 2000-01-01 * - Assets 0 USD - Assets 0 C - Assets 0 C21 - Assets 0 SIZE-2-PENCILS + Assets:A 0 USD + Assets:A 0 C + Assets:A 0 C21 + Assets:A 0 SIZE-2-PENCILS >= @@ -53,9 +53,9 @@ $ hledger -f- print -O beancount [c] $ hledger -f- print -O beancount -2000-01-01 open Assets +2000-01-01 open Assets:A 2000-01-01 * - Assets 0 USD + Assets:A 0 USD >=