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(
+ <a href="${to}/">, "Explore $to", </a>
+ );
+ 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);
}
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(
<html>, <body>, "\n",
<table>, "\n",
- (map {;
+ (map { my $el = $_;
' ', <tr>,
- (map { <td>, $self->render_el($_), </td> } @$_),
+ (map {
+ <td>, $self->render_el($el, $_), </td>
+ } @{$el}{@{$data->{columns}}}),
</tr>, "\n"
} @rows),
</table>, "\n",
] ];
}
-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;