From: Matt S Trout Date: Fri, 15 Jun 2012 16:12:42 +0000 (+0000) Subject: handle descent into complex data X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fa101d7f40cecdd61953df050936688d0dde4082;p=scpubgit%2FJSON-Tree-Viewer.git handle descent into complex data --- diff --git a/br.pl b/br.pl index 3bb6466..613687b 100644 --- a/br.pl +++ b/br.pl @@ -34,6 +34,7 @@ sub dispatch_request { sub structure { my ($self, $data) = @_; + return unless $data; if (ref($data) eq 'HASH') { if (keys %$data > 1 and values %$data == grep ref($_) eq 'HASH', values %$data) { @@ -43,46 +44,50 @@ sub structure { $data->{$_}->{name} ||= $_ for keys %$data; my @cols = grep $_ ne 'name', sort keys %{(values %$data)[0]}; unshift @cols, 'name'; - return { + return [{ columns => \@cols, show_columns => 1, data => [ @{$data}{sort keys %$data} ], - } + }] } } - return { + return [{ columns => [ 'key', 'value' ], data => [ map +{ key => $_, value => $data->{$_} }, sort keys %$data ], - }; + }]; } elsif (blessed($data) and $data->isa('IO::All::Dir')) { - return { + return [{ columns => [ 'name', 'explore' ], data => [ map +{ name => $_, explore => $self->link_to($_) }, keys %$data, ] - }; + }]; } else { die "Confused by $data"; } } sub link_to { - my ($self, $to) = @_; + my ($self, @to) = @_; use HTML::Tags; + s/\//\./g for my @link = @to; + my $link = join('/', @link, ''); + my $to = $to[-1]; my $html = join '', HTML::Tags::to_html_string( - , "Explore $to", + , "Explore $to", ); return \$html; } sub descend { my ($self, $target, @path) = @_; + return unless $target; if (blessed($target) and $target->isa('IO::All::File')) { $target = $self->json->decode(scalar $target->all); } return $target unless @path; - my $step = shift @path; - $self->descend($target->{$step}, @path); + (my $undot = my $step = shift @path) =~ s/\./\//g; + $self->descend($target->{$step}||$target->{$undot}, @path); } sub render_table { @@ -110,13 +115,15 @@ sub render_table { } sub render_el { - my ($self, $whole, $part) = @_; + my ($self, $whole, $key, $part) = @_; if (ref($part) eq 'ARRAY') { return join(', ', @$part); } if (ref($part) eq 'HASH') { if ($whole->{key}) { return $self->link_to($whole->{key}) + } elsif ($whole->{name}) { + return $self->link_to($whole->{name}, $key); } $part = '(complex)'; }