From 07329500b7d9a2e8fcfd9eb97f485deaea9f48f7 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Sun, 18 Aug 2019 11:51:12 -0600 Subject: [PATCH] add support for formatting metadata --- lib/App/HomeBank2Ledger/Formatter/Beancount.pm | 15 +++++++++++++++ lib/App/HomeBank2Ledger/Formatter/Ledger.pm | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/App/HomeBank2Ledger/Formatter/Beancount.pm b/lib/App/HomeBank2Ledger/Formatter/Beancount.pm index 659283a..748a0bf 100644 --- a/lib/App/HomeBank2Ledger/Formatter/Beancount.pm +++ b/lib/App/HomeBank2Ledger/Formatter/Beancount.pm @@ -16,6 +16,7 @@ use warnings; use strict; use App::HomeBank2Ledger::Util qw(commify rtrim); +use Scalar::Util qw(looks_like_number); use parent 'App::HomeBank2Ledger::Formatter'; @@ -197,6 +198,13 @@ sub _format_transaction { $out[-1] .= ' '.join(' ', @tags); } + my $metadata = $transaction->{metadata} || {}; + for my $key (sort keys %$metadata) { + my $value = looks_like_number($metadata->{$key}) ? $metadata->{$key} + : $self->_format_string($metadata->{$key}); + push @out, " ; ${key}: ${value}"; + } + for my $posting (@postings) { my @line; @@ -233,6 +241,13 @@ sub _format_transaction { } push @out, join('', @line); + + my $metadata = $posting->{metadata} || {}; + for my $key (sort keys %$metadata) { + my $value = looks_like_number($metadata->{$key}) ? $metadata->{$key} + : $self->_format_string($metadata->{$key}); + push @out, " ; ${key}: ${value}"; + } } push @out, ''; diff --git a/lib/App/HomeBank2Ledger/Formatter/Ledger.pm b/lib/App/HomeBank2Ledger/Formatter/Ledger.pm index 8f05441..464fa04 100644 --- a/lib/App/HomeBank2Ledger/Formatter/Ledger.pm +++ b/lib/App/HomeBank2Ledger/Formatter/Ledger.pm @@ -234,6 +234,12 @@ sub _format_transaction { $memo && " ; $memo", ); + my $metadata = $transaction->{metadata} || {}; + for my $key (sort keys %$metadata) { + my $value = $self->_format_string($metadata->{$key}); + push @out, " ; ${key}: ${value}"; + } + for my $posting (@postings) { my @line; @@ -271,6 +277,12 @@ sub _format_transaction { push @out, join('', @line); + my $metadata = $posting->{metadata} || {}; + for my $key (sort keys %$metadata) { + my $value = $self->_format_string($metadata->{$key}); + push @out, " ; ${key}: ${value}"; + } + if (my $posting_payee = $posting->{payee}) { $posting_payee = $self->_format_string($posting_payee); push @out, " ; Payee: $posting_payee" if $posting_payee ne $payee; -- 2.45.2