From 757a417836d8a7e8ed61c56eedb706e7ad228e9a Mon Sep 17 00:00:00 2001 From: Glenn Rice Date: Tue, 1 Aug 2023 09:08:30 -0500 Subject: [PATCH] Clean up the FormatRenderedProblem pretty_print method. --- lib/FormatRenderedProblem.pm | 62 ++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/FormatRenderedProblem.pm b/lib/FormatRenderedProblem.pm index 005b9c2c37..8e128d1bc6 100644 --- a/lib/FormatRenderedProblem.pm +++ b/lib/FormatRenderedProblem.pm @@ -403,20 +403,28 @@ EOS # Nice output for debugging sub pretty_print { my ($r_input, $level) = @_; + return 'undef' unless defined $r_input; + $level //= 4; $level--; - return '' unless $level > 0; # Only print three levels of hashes (safety feature) - my $out = ''; - if (!ref $r_input) { - $out = $r_input if defined $r_input; - $out =~ s/}; - + return 'too deep' unless $level > 0; + + my $ref = ref($r_input); + + if (!$ref) { + return xml_escape($r_input); + } elsif (eval { %$r_input || 1 }) { + # `eval { %$r_input || 1 }` will pick up all objectes that can be accessed like a hash and so works better than + # `ref $r_input`. Do not use `"$r_input" =~ /hash/i` because that will pick up strings containing the word + # hash, and that will cause an error below. + my $out = + '
' + . ($ref eq 'HASH' + ? '' + : '
' + . "$ref
") + . '
'; for my $key (sort keys %$r_input) { # Safety feature - we do not want to display the contents of %seed_ce which # contains the database password and lots of other things, and explicitly hide @@ -429,24 +437,24 @@ sub pretty_print { || ($key eq "externalPrograms") || ($key eq "permissionLevels") || ($key eq "seed_ce")); - $out .= "$key=> " . pretty_print($r_input->{$key}, $level) . ""; + $out .= + '
' + . xml_escape($key) + . '
' + . qq{
=>
} + . qq{
} + . pretty_print($r_input->{$key}, $level) + . '
'; } - $out .= ''; - } elsif (ref $r_input eq 'ARRAY') { - my @array = @$r_input; - $out .= '( '; - while (@array) { - $out .= pretty_print(shift @array, $level) . ' , '; - } - $out .= ' )'; - } elsif (ref $r_input eq 'CODE') { - $out = "$r_input"; + $out .= '
'; + return $out; + } elsif ($ref eq 'ARRAY') { + return '[ ' . join(', ', map { pretty_print($_, $level) } @$r_input) . ' ]'; + } elsif ($ref eq 'CODE') { + return 'CODE'; } else { - $out = $r_input; - $out =~ s/