Skip to content

Commit

Permalink
Preserve whitespace between amount and comment for inline maths
Browse files Browse the repository at this point in the history
In commit 80ad237 ("Preserve whitespace between amount and comment")
we added better handling for whitespace.   Unfortunately, the function
that parses inline maths was not updated accordingly, resulting in this
warning because because $amount->[WHITESPACE] wasn't defined.

    Use of uninitialized value in concatenation (.) or string at ../bin/ledger2beancount line 802.

Fixes #261
  • Loading branch information
tbm committed Dec 2, 2022
1 parent 5706846 commit 88a3582
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bin/ledger2beancount
Original file line number Diff line number Diff line change
Expand Up @@ -1469,13 +1469,14 @@ sub get_default_commodity() {
sub get_value_expr($) {
my ($l) = @_;
if ($l !~ /^(?<math>$RE{balanced}{-parens=>'()'})\s*(?<rest>.*)/) {
if ($l !~ /^(?<math>$RE{balanced}{-parens=>'()'})(?<space>\s*)(?<rest>.*)/) {
die "Cannot parse inline math from $l\n";
}
my $amount;
my $math = $+{math};
$amount->[WHITESPACE] = $+{space};
my $rest = $+{rest};
my $amount;
$amount->[ADJUST_WHITESPACE] = length($math) - 1; # we subtract the new length later
# Strip the outer () since beancount doesn't require them.
$math =~ s/^\(//;
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Improve pre-processing example in documentation
* Add support for amounts with plus sign
* Set option `name_income` for top-level account `Revenue`
* Fix whitespace handling for inline maths

## 2.6 (2021-04-15)

Expand Down
13 changes: 13 additions & 0 deletions tests/amounts.beancount
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
1970-01-01 open Assets:Rewards
1970-01-01 open Assets:Test
1970-01-01 open Equity:Exchange
1970-01-01 open Expenses:Drinks
1970-01-01 open Expenses:Housing:Rent
1970-01-01 open Expenses:Travel:Airfare
1970-01-01 open Income:401k:Match
Expand Down Expand Up @@ -290,3 +291,15 @@
Assets:Test 1 FOO
Assets:Test -1 FOO

2022-12-02 * "Inline math and whitespace"
Expenses:Drinks 1/1.159 GBP ; comment
Assets:Test

2022-12-02 * "Inline math and whitespace"
Expenses:Drinks 1/1.159 GBP; comment
Assets:Test

2022-12-02 * "Inline math and whitespace"
Expenses:Drinks 1/1.159 GBP ; comment
Assets:Test

12 changes: 12 additions & 0 deletions tests/amounts.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,15 @@ commodity $
Assets:Test 1
Assets:Test -1

2022-12-02 * Inline math and whitespace
Expenses:Drinks (1/1.159 GBP) ; comment
Assets:Test

2022-12-02 * Inline math and whitespace
Expenses:Drinks (1/1.159 GBP); comment
Assets:Test

2022-12-02 * Inline math and whitespace
Expenses:Drinks (1/1.159 GBP) ; comment
Assets:Test

0 comments on commit 88a3582

Please sign in to comment.