imp:print:beancount: add a second account name part if needed

Beancount requires account names to have at least two parts.
This commit is contained in:
Simon Michael 2024-11-08 23:27:02 -10:00
parent 6e3dfd6703
commit d925c8a042
3 changed files with 24 additions and 23 deletions

View File

@ -139,34 +139,34 @@ type BeancountAccountNameComponent = AccountName
-- | Convert a hledger account name to a valid Beancount account name. -- | Convert a hledger account name to a valid Beancount account name.
-- It replaces spaces with dashes and other non-supported characters with C<HEXBYTES>; -- It replaces spaces with dashes and other non-supported characters with C<HEXBYTES>;
-- 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. -- and capitalises each part.
-- It also checks that the first part is one of the required english -- It also checks that the first part is one of the required english
-- account names Assets, Liabilities, Equity, Income, or Expenses, and if not -- account names Assets, Liabilities, Equity, Income, or Expenses, and if not
-- raises an informative error. -- raises an informative error.
-- Ref: https://beancount.github.io/docs/beancount_language_syntax.html#accounts -- Ref: https://beancount.github.io/docs/beancount_language_syntax.html#accounts
accountNameToBeancount :: AccountName -> BeancountAccountName accountNameToBeancount :: AccountName -> BeancountAccountName
accountNameToBeancount a = accountNameToBeancount a = b
dbg9 "beancount account name" $
accountNameFromComponents bs'
where where
bs = cs1 =
map accountNameComponentToBeancount $ accountNameComponents $ map accountNameComponentToBeancount $ accountNameComponents $
dbg9 "hledger account name " $ dbg9 "hledger account name " a
a cs2 =
bs' = case cs1 of
case bs of c:_ | c `notElem` beancountTopLevelAccounts -> error' e
b:_ | b `notElem` beancountTopLevelAccounts -> error' e
where where
e = T.unpack $ T.unlines [ e = T.unpack $ T.unlines [
"bad top-level account: " <> b "bad top-level account: " <> c
,"in beancount account name: " <> accountNameFromComponents bs ,"in beancount account name: " <> accountNameFromComponents cs1
,"converted from hledger account name: " <> a ,"converted from hledger account name: " <> a
,"For Beancount, top-level accounts must be (or be --alias'ed to)" ,"For Beancount, top-level accounts must be (or be --alias'ed to)"
,"one of " <> T.intercalate ", " beancountTopLevelAccounts <> "." ,"one of " <> T.intercalate ", " beancountTopLevelAccounts <> "."
-- ,"and not: " <> b -- ,"and not: " <> b
] ]
cs -> cs [c] -> [c, "A"]
cs -> cs
b = dbg9 "beancount account name" $ accountNameFromComponents cs2
accountNameComponentToBeancount :: AccountName -> BeancountAccountNameComponent accountNameComponentToBeancount :: AccountName -> BeancountAccountNameComponent
accountNameComponentToBeancount acctpart = accountNameComponentToBeancount acctpart =

View File

@ -842,7 +842,8 @@ Here are more details
hledger will adjust your account names when needed, to make valid hledger will adjust your account names when needed, to make valid
[Beancount account names](https://beancount.github.io/docs/beancount_language_syntax.html#accounts) [Beancount account names](https://beancount.github.io/docs/beancount_language_syntax.html#accounts)
(capitalising, replacing spaces with `-`, replacing other unsupported characters with `C<HEXBYTES>`, (capitalising, replacing spaces with `-`, replacing other unsupported characters with `C<HEXBYTES>`,
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: However, you must ensure that all top level account names are one of the five required by Beancount:
`Assets`, `Liabilities`, `Equity`, `Income`, or `Expenses`. `Assets`, `Liabilities`, `Equity`, `Income`, or `Expenses`.
If yours are named differently, you can use [account aliases](#alias-directive), If yours are named differently, you can use [account aliases](#alias-directive),

View File

@ -9,10 +9,10 @@ $ hledger -f- print -O beancount
>2 /bad top-level account/ >2 /bad top-level account/
>=1 >=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 2000-01-01
assets:a 0 ABC assets 0 ABC
equity:$-€:$ 0 USD equity:$-€:$ 0 USD
$ hledger -f- print -O beancount $ hledger -f- print -O beancount
@ -34,13 +34,13 @@ $ hledger -f- print -O beancount
assets 0 "size 2 pencils" assets 0 "size 2 pencils"
$ hledger -f- print -O beancount $ hledger -f- print -O beancount
2000-01-01 open Assets 2000-01-01 open Assets:A
2000-01-01 * 2000-01-01 *
Assets 0 USD Assets:A 0 USD
Assets 0 C Assets:A 0 C
Assets 0 C21 Assets:A 0 C21
Assets 0 SIZE-2-PENCILS Assets:A 0 SIZE-2-PENCILS
>= >=
@ -53,9 +53,9 @@ $ hledger -f- print -O beancount
[c] [c]
$ hledger -f- print -O beancount $ hledger -f- print -O beancount
2000-01-01 open Assets 2000-01-01 open Assets:A
2000-01-01 * 2000-01-01 *
Assets 0 USD Assets:A 0 USD
>= >=