commit: 22dc9b867e10ff2c56891561e59517542a4ffba4
parent: 602ad2ecdfe40d03778887f666bfae5aa27146d1
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Tue, 5 May 2020 22:51:08 +0200
ap-represent.pl: Add indentation and proper recurse
Diffstat:
M | ap-represent.pl | 70 | ++++++++++++++++++++++++++++++++++++++++++---------------------------- |
1 file changed, 42 insertions(+), 28 deletions(-)
diff --git a/ap-represent.pl b/ap-represent.pl
@@ -2,6 +2,7 @@
use strict;
use utf8;
no warnings; # Wide Character…
+use Scalar::Util qw(reftype);
use JSON::MaybeXS;
use Data::Dumper;
@@ -9,46 +10,59 @@ use Data::Dumper;
my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
sub print_object {
- my $object = shift;
+ my ($indent, $object) = @_;
- printf '⇒ %s', $object->{"type"};
+ printf '%*s %s', $indent, "⇒", $object->{"type"};
printf ' <%s>', $object->{"id"} if $object->{"id"};
printf ' “%s”', $object->{"name"} if $object->{"name"};
printf ' @%s', $object->{"preferredUsername"} if $object->{"preferredUsername"};
- printf "\n";
- printf "URL: <%s>\n", $object->{"url"} if $object->{"url"};
- printf "Context: %s\n", $object->{"context"} if $object->{"context"};
- printf "Inbox: <%s>\n", $object->{"inbox"} if $object->{"inbox"};
- printf "Outbox: <%s>\n", $object->{"outbox"} if $object->{"outbox"};
- printf "Previous Page: <%s>\n", $object->{"prev"} if $object->{"prev"};
- printf "Next Page: <%s>\n", $object->{"next"} if $object->{"next"};
- printf "Published: %s\n", $object->{"published"} if $object->{"published"};
- printf "Updated: %s\n", $object->{"updated"} if $object->{"updated"};
- printf "Summary: %s\n", $object->{"summary"} if $object->{"summary"};
- printf "Content: %s\n", $object->{"content"} if $object->{"content"};
- if($object->{"object"}) {
- printf "Object: ";
- print_object($object->{"object"});
+ print "\n";
+ printf "%*sURL: <%s>\n", $indent, ' ', $object->{"url"} if $object->{"url"};
+ printf "%*sContext: %s\n", $indent, ' ', $object->{"context"} if $object->{"context"};
+ printf "%*sInbox: <%s>\n", $indent, ' ', $object->{"inbox"} if $object->{"inbox"};
+ printf "%*sOutbox: <%s>\n", $indent, ' ', $object->{"outbox"} if $object->{"outbox"};
+ printf "%*sPrevious Page: <%s>\n", $indent, ' ', $object->{"prev"} if $object->{"prev"};
+ printf "%*sNext Page: <%s>\n", $indent, ' ', $object->{"next"} if $object->{"next"};
+ printf "%*sPublished: %s\n", $indent, ' ', $object->{"published"} if $object->{"published"};
+ printf "%*sUpdated: %s\n", $indent, ' ', $object->{"updated"} if $object->{"updated"};
+ printf "%*sSummary: %s\n", $indent, ' ', $object->{"summary"} if $object->{"summary"};
+ printf "%*sContent: %s\n", $indent, ' ', $object->{"content"} if $object->{"content"};
+ if(exists $object->{"object"}) {
+ printf "%*sobject: ", $indent, ' ';
+ print_ref($indent+4, $object->{"object"});
}
- if($object->{"tag"}) {
- printf "Tags: ";
- print_objects($object->{"tag"});
+ if(exists $object->{"attachment"}) {
+ printf "%*sattachment: ", $indent, ' ';
+ print_ref($indent+4, $object->{"attachment"});
}
- if($object->{"attachment"}) {
- printf "Attachments: ";
- print_objects($object->{"attachment"});
+ if(exists $object->{"tag"}) {
+ printf "%*stag: ", $indent, ' ';
+ print_ref($indent+4, $object->{"tag"});
+ }
+ if(exists $object->{"orderedItems"}) {
+ printf "%*sorderedItems: ", $indent, ' ';
+ print_ref($indent+4, $object->{"orderedItems"});
}
}
-sub print_objects {
- my $objects = shift;
+sub print_ref {
+ my ($indent, $object) = @_;
+
+ my $ref_type = reftype($object);
- printf "\n";
- foreach(@$objects) {
- print_object($_);
+ if($ref_type eq "HASH") {
+ print "\n";
+ print_object($indent, $object);
+ } elsif($ref_type eq "ARRAY") {
+ print "\n";
+ foreach(@{$object}) {
+ print_object($indent, $_);
+ }
+ } else {
+ print $object, "\n";
}
}
my $blob = $json->decode(<STDIN>);
-print_object($blob);
+print_object(1, $blob);