1 |
package Frey::Dumper; |
2 |
use Moose; |
3 |
|
4 |
with 'Frey::Escape'; |
5 |
|
6 |
use Data::Dump qw/dump/; |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
Frey::Dumper - dump perl data as tree |
11 |
|
12 |
=cut |
13 |
|
14 |
has data => ( |
15 |
is => 'rw', |
16 |
required => 1, |
17 |
); |
18 |
|
19 |
sub markup { |
20 |
my ($self) = @_; |
21 |
qq|<div class="frey-dumper">| . $self->unroll( $self->data ) . qq|</div>|; |
22 |
} |
23 |
|
24 |
sub unroll { |
25 |
my ($self,$data,$ref,$key) = @_; |
26 |
my $out; |
27 |
my $title = $ref ? qq| title="$ref"| : ''; |
28 |
if ( ref($data) eq 'ARRAY' ) { |
29 |
$out .= "<li>$key ↓</li>" if $key; |
30 |
$out .= qq|<ol${title} start=0>\n|; |
31 |
$out .= $self->unroll($_,ref($_)) foreach @$data; |
32 |
$out .= "</ol>\n"; |
33 |
} elsif ( ref($data) eq 'HASH' ) { |
34 |
$out .= "<li>$key ↓</li>" if $key; |
35 |
$out .= qq|<ul${title}>\n|; |
36 |
$out .= $self->unroll($data->{$_},ref($data->{$_}),$_) foreach keys %$data; |
37 |
$out .= "</ul>\n"; |
38 |
} else { |
39 |
$out .= qq|<li${title}>|; |
40 |
$out .= "$key → " if $key; |
41 |
if ( $data =~ m{<(\w+)>.+</\1>} ) { |
42 |
$out .= qq|<div style="background: #eee; color: #000">$data</div></li>|; |
43 |
} else { |
44 |
$out .= "<span>" . $self->html_dump( $data ) . "</span></li>"; |
45 |
} |
46 |
} |
47 |
return $out; |
48 |
} |
49 |
|
50 |
1; |