From: Matt S Trout Date: Fri, 15 Jun 2012 11:10:26 +0000 (+0000) Subject: descent into madness X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2ff9773be4f6c4684628fa14a0a56fabd7c49760;p=scpubgit%2FJSON-Tree-Viewer.git descent into madness --- diff --git a/br.pl b/br.pl index 6ac30cc..8f7a436 100644 --- a/br.pl +++ b/br.pl @@ -24,32 +24,49 @@ sub dispatch_request { ref($_[0][0]) eq 'HASH' ? $self->render_table($_[0][0]) : $_[0] } }, + sub (/) { + [ $self->structure($self->root) ]; + }, sub (/**/) { - $self->structure($self->descend($self->root, split '/', $_[1])); + [ $self->structure($self->descend($self->root, split '/', $_[1])) ]; }, } sub structure { my ($self, $data) = @_; if (ref($data) eq 'HASH') { - $data = [ @{$data}{sort keys %$data} ]; - my @cols = sort keys %{$data->[0]}; - return [ { - columns => \@cols, - data => $data, - } ]; + return { + columns => [ 'key', 'value' ], + data => [ map +{ key => $_, value => $data->{$_} }, sort keys %$data ], + }; + } elsif (blessed($data) and $data->isa('IO::All::Dir')) { + return { + columns => [ 'name', 'explore' ], + data => [ + map +{ name => $_, explore => $self->link_to($_) }, keys %$data, + ] + }; } else { die "Confused by $data"; } } +sub link_to { + my ($self, $to) = @_; + use HTML::Tags; + my $html = join '', HTML::Tags::to_html_string( + , "Explore $to", + ); + return \$html; +} + sub descend { my ($self, $target, @path) = @_; - return $target unless @path; - my $step = shift @path; 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); } @@ -57,16 +74,18 @@ sub render_table { my ($self, $data) = @_; use HTML::Tags; my @rows = ( - $data->{columns}, - map [ @{$_}{@{$data->{columns}}} ], @{$data->{data}} + $data->{show_columns} ? { map +($_ => $_), @{$data->{columns}} } : (), + @{$data->{data}} ); [ 200, [ 'Content-type' => 'text/html' ], [ HTML::Tags::to_html_string( , , "\n", , "\n", - (map {; + (map { my $el = $_; ' ', , - (map { } @$_), + (map { + + } @{$el}{@{$data->{columns}}}), , "\n" } @rows),
, $self->render_el($_), , $self->render_el($el, $_),
, "\n", @@ -75,6 +94,15 @@ sub render_table { ] ]; } -sub render_el { ref($_[1]) eq 'ARRAY' ? join(', ', @{$_[1]}) : $_[1] } +sub render_el { + my ($self, $whole, $part) = @_; + if (ref($part) eq 'ARRAY') { + return join(', ', @$part); + } + if (ref($part) eq 'HASH') { + return $self->link_to($whole->{key}); + } + return $part; +} __PACKAGE__->run_if_script;