diff --git a/ledger_pyreport/__init__.py b/ledger_pyreport/__init__.py
index 268b235..69a4138 100644
--- a/ledger_pyreport/__init__.py
+++ b/ledger_pyreport/__init__.py
@@ -368,9 +368,9 @@ def filter_amount_positive(amt):
@app.template_filter('bc')
def filter_commodity_positive(amt):
if amt.commodity.is_prefix:
- return flask.Markup('{}{:,.2f}'.format(amt.tostr(False), amt.commodity.name, amt.amount).replace(',', ' '))
+ return flask.Markup('{}{}{:,.2f}'.format(amt.tostr(False), amt.commodity.name, ' ' if amt.commodity.is_space else '', amt.amount).replace(',', ' '))
else:
- return flask.Markup('{:,.2f} {}'.format(amt.tostr(False), amt.amount, amt.commodity.name).replace(',', ' '))
+ return flask.Markup('{:,.2f}{}{}'.format(amt.tostr(False), amt.amount, ' ' if amt.commodity.is_space else '', amt.commodity.name).replace(',', ' '))
@app.template_filter('bt')
def filter_commodity_table_positive(amt, show_price, link=None):
diff --git a/ledger_pyreport/ledger.py b/ledger_pyreport/ledger.py
index e756ea0..870c313 100644
--- a/ledger_pyreport/ledger.py
+++ b/ledger_pyreport/ledger.py
@@ -53,6 +53,9 @@ def financial_year(date):
csv.register_dialect('ledger', doublequote=False, escapechar='\\', strict=True)
+RE_COMMODITY1 = re.compile(r'([0123456789.,-]+)( *)(.+)')
+RE_COMMODITY2 = re.compile(r'(.+?)( *)([0123456789.,-]+)')
+
def parse_amount(amount):
if '{' in amount:
amount_str = amount[:amount.index('{')].strip()
@@ -63,13 +66,14 @@ def parse_amount(amount):
if amount_str[0] in list('0123456789-'):
# Commodity follows number
- bits = amount_str.split()
- amount_num = Decimal(bits[0].replace(',', ''))
- commodity = Commodity(bits[1].strip('"'), False)
+ result = RE_COMMODITY1.match(amount_str)
+ amount_num = Decimal(result.group(1).replace(',', ''))
+ commodity = Commodity(result.group(3).strip('"'), False, len(result.group(2)) > 0)
else:
# Commodity precedes number
- commodity = Commodity(amount_str[0], True)
- amount_num = Decimal(amount_str[1:].replace(',', ''))
+ result = RE_COMMODITY2.match(amount_str)
+ amount_num = Decimal(result.group(3).replace(',', ''))
+ commodity = Commodity(result.group(1).strip('"'), True, len(result.group(2)) > 0)
if price_str:
commodity.price = parse_amount(price_str)
diff --git a/ledger_pyreport/model.py b/ledger_pyreport/model.py
index 990db9c..3856673 100644
--- a/ledger_pyreport/model.py
+++ b/ledger_pyreport/model.py
@@ -435,9 +435,10 @@ class Balance:
return self + (-other)
class Commodity:
- def __init__(self, name, is_prefix, price=None):
+ def __init__(self, name, is_prefix, is_space, price=None):
self.name = name
self.is_prefix = is_prefix
+ self.is_space = is_space
self.price = price
def __repr__(self):
@@ -449,7 +450,7 @@ class Commodity:
return self.name == other.name and self.price == other.price
def strip_price(self):
- return Commodity(self.name, self.is_prefix)
+ return Commodity(self.name, self.is_prefix, self.is_space)
class TrialBalance:
def __init__(self, ledger, date, pstart, label=None):