1 |
package Frey::View::Dumper; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Escape'; |
7 |
|
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
=head1 DESCRIPTION |
11 |
|
12 |
dump C<data> as html tree |
13 |
|
14 |
=cut |
15 |
|
16 |
has data => ( |
17 |
is => 'rw', |
18 |
required => 1, |
19 |
documentation => "input data", |
20 |
); |
21 |
|
22 |
sub as_markup { |
23 |
my ($self) = @_; |
24 |
qq|<div class="frey-dumper">| . $self->unroll( $self->data ) . qq|</div>|; |
25 |
} |
26 |
|
27 |
sub unroll { |
28 |
my ($self,$data,$ref,$key) = @_; |
29 |
my $out; |
30 |
my $title = $ref ? qq| title="$ref"| : ''; |
31 |
|
32 |
$out .= qq|<li${title}>|; |
33 |
my $mark = $key || '♦'; |
34 |
$out .= $ref && $ref !~ m{^(ARRAY|HASH)$} ? qq|<a href="/$ref" class="blessed" target="blessed" title="$ref">$mark</a>| : ( $key || '' ); |
35 |
|
36 |
if ( ref($data) eq 'ARRAY' ) { |
37 |
$out .= " ↓</li>" if $key; |
38 |
$out .= qq|<ol${title} start=0>\n|; |
39 |
$out .= $self->unroll($_,ref($_)) foreach @$data; |
40 |
$out .= "</ol>\n"; |
41 |
} elsif ( ref($data) && eval { %$data } ) { |
42 |
$out .= " ↓</li>" if $key; |
43 |
$out .= qq|<ul${title}>\n|; |
44 |
$out .= $self->unroll($data->{$_},ref($data->{$_}),$_) foreach keys %$data; |
45 |
$out .= "</ul>\n"; |
46 |
} else { |
47 |
$out .= " → " if $key; |
48 |
if ( defined $data && $data =~ m{<(\w+).*>.+</\1>} ) { |
49 |
$out .= qq|<div style="background: #eee; color: #000" title="HTML">$data</div></li>|; |
50 |
} else { |
51 |
$out .= "<span>" . $self->html_dump( $data ) . "</span></li>"; |
52 |
} |
53 |
} |
54 |
$out .= "</li>"; |
55 |
|
56 |
return $out; |
57 |
} |
58 |
|
59 |
1; |