]> Dogcows Code - chaz/homebank2ledger/commitdiff
decode entities in attribute values
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 Jun 2019 04:05:15 +0000 (22:05 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Thu, 13 Jun 2019 04:05:15 +0000 (22:05 -0600)
lib/File/HomeBank.pm

index 31ca39d097ea8ab771418d117d7f37085fdec86c..c786fddcf419a607d8c07f0f3eb78280a51bd93a 100644 (file)
@@ -30,15 +30,13 @@ use App::HomeBank2Ledger::Util qw(commify);
 use Exporter qw(import);
 use Scalar::Util qw(refaddr);
 use Time::Piece;
+use XML::Entities;
 use XML::Parser::Lite;
 
 our $VERSION = '9999.999'; # VERSION
 
 our @EXPORT_OK = qw(parse_string parse_file);
 
-sub _croak { require Carp; Carp::croak(@_) }
-sub _usage { _croak("Usage: @_\n") }
-
 my %ACCOUNT_TYPES = (
     0   => 'none',
     1   => 'bank',
@@ -104,6 +102,9 @@ my %TRANSACTION_PAYMODES = (
     11  => 'directdebit',
 );
 
+sub _croak { require Carp; Carp::croak(@_) }
+sub _usage { _croak("Usage: @_\n") }
+
 =method new
 
     $homebank = File::HomeBank->new(string => $str);
@@ -415,7 +416,7 @@ sub find_transaction_transfer_pair {
 
     $transations = $homebank->sorted_transactions;
 
-Get an arrayref of transactions sorted by date (earliest first).
+Get an arrayref of transactions sorted by date (oldest first).
 
 =cut
 
@@ -532,6 +533,12 @@ sub parse_string {
                 shift;
                 my $node = shift;
                 my %attr = @_;
+
+                # decode all attribute values
+                for my $key (keys %attr) {
+                    $attr{$key} = _decode_xml_entities($attr{$key});
+                }
+
                 if ($node eq 'properties') {
                     $attr{currency} = delete $attr{curr} if $attr{curr};
                     %properties = %attr;
@@ -603,6 +610,14 @@ sub parse_string {
     };
 }
 
+sub _decode_xml_entities {
+    my $str = shift;
+    # decoding entities can be extremely slow, so don't bother if it doesn't look like there are any
+    # entities to decode
+    return $str if $str !~ /&(?:#\d+)|[A-Za-z0-9]+;/;
+    return XML::Entities::decode('all', $str);
+}
+
 sub _rdn_to_unix_epoch {
     my $rdn = shift;
     my $jan01_1970 = 719163;
This page took 0.02534 seconds and 4 git commands to generate.