From: Matt S Trout Date: Fri, 15 Jun 2012 16:53:12 +0000 (+0000) Subject: Now Multify X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5951d9d33813caabe17eee048b521ae55295fef5;p=scpubgit%2FJSON-Tree-Viewer.git Now Multify --- diff --git a/br.pl b/br.pl index 613687b..887e0df 100644 --- a/br.pl +++ b/br.pl @@ -25,14 +25,81 @@ sub dispatch_request { } }, sub (/) { - [ $self->structure($self->root) ]; + sub (?@host=) { + [ 302, [ 'Location', '/'.join('+', @{$_[1]}) ], [] ]; + }, + sub () { + $self->root_structure; + }, }, sub (/**/) { - [ $self->structure($self->descend($self->root, split '/', $_[1])) ]; + $self->structure(split '/', $_[1]); }, } +sub root_structure { + my ($self) = @_; + my $struct = $self->mangle_structure($self->root); + push @{$struct->[0]{columns}}, 'select'; + foreach my $host (@{$struct->[0]{data}}) { + use HTML::Tags; + my $name = $host->{name}; + my $html = join '', HTML::Tags::to_html_string( + + ); + $host->{select} = \$html; + } + $struct->[0]{wrapper} = sub { + use HTML::Tags; + ' ',
, "\n", + (map /^\s*$/ ? " $_" : $_, @_), + ' ', , "\n", + ' ',
, "\n" + }; + return $struct; +} + sub structure { + my ($self, @parts) = @_; + if ($parts[0] =~ /\+/) { + ($parts[0], my @extra) = split /\+/, $parts[0]; + my $struct = $self->mangle_structure($self->descend($self->root, @parts)); + return $struct unless $struct->[0]{show_columns}; + my $first = shift @parts; + my @rest = map [ $_, $self->mangle_structure( + $self->descend($self->root, $_, @parts) + )->[0] ], @extra; + my %by_name; + my %host_cols; + my %complex_cols; + foreach my $thing ([ $first, $struct->[0] ], @rest) { + foreach my $el (@{$thing->[1]{data}}) { + my $by = $by_name{$el->{name}} ||= { name => $el->{name} }; + foreach my $key (keys %$el) { + next if $key eq 'name'; + if (ref($el->{$key}) eq 'HASH') { + $complex_cols{$key} = 1; + $by->{$key} = {}; + } else { + my $full_key = $key.' ('.$thing->[0].')'; + $host_cols{$full_key} = 1; + $by->{$full_key} = $el->{$key}; + } + } + } + } + return [{ + columns => [ 'name', sort(keys %host_cols), sort(keys %complex_cols) ], + show_columns => 1, + data => [ + map $by_name{$_}, sort keys %by_name + ], + }]; + } + return $self->mangle_structure($self->descend($self->root, @parts)); +} + +sub mangle_structure { my ($self, $data) = @_; return unless $data; if (ref($data) eq 'HASH') { @@ -100,15 +167,17 @@ sub render_table { [ 200, [ 'Content-type' => 'text/html' ], [ HTML::Tags::to_html_string( , , "\n", - , "\n", - (map { my $el = $_; - ' ', , - (map { - - } @{$el}{@{$data->{columns}}}), - , "\n" - } @rows), -
, $self->render_el($el, $_),
, "\n", + ($data->{wrapper}||sub{@_})->( + '', , "\n", + (map { my $el = $_; + ' ', , + (map { + + } @{$data->{columns}}), + , "\n" + } @rows), + '',
, $self->render_el($el, $_, $el->{$_}),
, "\n", + ), , , "\n", ) ] ];