}
},
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(
+ <input type="checkbox" name="host" value="$name" />
+ );
+ $host->{select} = \$html;
+ }
+ $struct->[0]{wrapper} = sub {
+ use HTML::Tags;
+ ' ', <form>, "\n",
+ (map /^\s*$/ ? " $_" : $_, @_),
+ ' ', <input type="submit" value="Now Multify" />, "\n",
+ ' ', </form>, "\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') {
[ 200, [ 'Content-type' => 'text/html' ], [
HTML::Tags::to_html_string(
<html>, <body>, "\n",
- <table>, "\n",
- (map { my $el = $_;
- ' ', <tr>,
- (map {
- <td>, $self->render_el($el, $_), </td>
- } @{$el}{@{$data->{columns}}}),
- </tr>, "\n"
- } @rows),
- </table>, "\n",
+ ($data->{wrapper}||sub{@_})->(
+ '', <table>, "\n",
+ (map { my $el = $_;
+ ' ', <tr>,
+ (map {
+ <td>, $self->render_el($el, $_, $el->{$_}), </td>
+ } @{$data->{columns}}),
+ </tr>, "\n"
+ } @rows),
+ '', </table>, "\n",
+ ),
</body>, </html>, "\n",
)
] ];