File Coverage

blib/lib/Data/Format/Pretty/HTML.pm
Criterion Covered Total %
statement 80 83 96.3
branch 11 14 78.5
condition 7 10 70.0
subroutine 16 18 88.8
pod 1 3 33.3
total 115 128 89.8


line stmt bran cond sub pod time code
1             package Data::Format::Pretty::HTML;
2              
3 1     1   25138 use 5.010001;
  1         4  
  1         37  
4 1     1   5 use strict;
  1         2  
  1         29  
5 1     1   5 use warnings;
  1         1  
  1         29  
6 1     1   827 use Log::Any '$log';
  1         1974  
  1         4  
7              
8 1     1   878 use Data::Format::Pretty::Console 0.24;
  1         208057  
  1         81  
9 1     1   1066 use HTML::Entities;
  1         14996  
  1         112  
10 1     1   10 use Scalar::Util qw(looks_like_number);
  1         2  
  1         41  
11 1     1   1057 use URI::Find::Schemeless;
  1         40802  
  1         191  
12 1     1   16 use YAML::Any;
  1         2  
  1         15  
13              
14             require Exporter;
15             our @ISA = qw(Exporter Data::Format::Pretty::Console);
16             our @EXPORT_OK = qw(format_pretty);
17              
18             our $VERSION = '0.09'; # VERSION
19              
20 0     0 0 0 sub content_type { "text/html" }
21              
22             sub format_pretty {
23 5     5 1 3999 my ($data, $opts) = @_;
24 5   50     20 $opts //= {};
25 5         17 __PACKAGE__->new($opts)->_format($data);
26             }
27              
28             # OO interface is hidden
29             sub new {
30 9     9 0 6091 my ($class, $opts) = @_;
31 9         62 my $obj = $class->SUPER::new($opts);
32             #my $obj = Data::Format::Pretty::Console->new($opts);
33 9   50     274 $obj->{opts}{linkify_urls_in_text} //= 1;
34 9         20 $obj->{opts}{interactive} = 1;
35 9         19 $obj->{opts}{list_max_columns} = 1;
36             #bless $class, $obj;
37 9         41 $obj;
38             }
39              
40             sub _htmlify {
41 11     11   24 my ($self, $text) = @_;
42              
43 11         39 $text = encode_entities($text);
44 11 50       196 if ($self->{opts}{linkify_urls_in_text}) {
45             URI::Find::Schemeless->new(
46             sub {
47             #my $uri = encode_entities $_[0];
48             #my $uri = $_[0];
49 1     1   10908 my $uri = decode_entities $_[0];
50 1         14 return qq|$uri|;
51 11         170 })->find(\$text);
52             }
53 11 100       10415 if ($text =~ /\R/) {
54 1         9 return "
$text
";
55             } else {
56 10         66 return $text;
57             }
58             }
59              
60             sub _render_table {
61 3     3   12884 my ($self, $t) = @_;
62 3         9 my @t = ("\n"); \n"; "; \n"; \n"; \n"; "; "; ", \n"; \n";
63              
64 3         8 my $sh = $t->{at_opts}{show_header};
65 3 100 66     17 unless (defined($sh) && !$sh) {
66 2         5 push @t, "
67 2         4 push @t, "
68 2         2 for my $c (@{$t->{cols}}) {
  2         6  
69 3 50       55 push @t, (
70             "",
71             $self->_htmlify($c),
72             "",
73             );
74             }
75 2         4 push @t, "
76 2         4 push @t, "
77             }
78              
79 3         8 push @t, "
80 3         5 for my $r (@{$t->{rows}}) {
  3         9  
81 5         8 push @t, "
82 5         29 my $cidx = 0;
83 5         9 for my $c (@$r) {
84 7 100 100     37 if ($t->{html_cols} && $t->{html_cols}[$cidx]) {
85 2         14 push @t, "", $c, "
86             } else {
87 5 100       27 push @t, (
88             "",
89             $self->_htmlify($c),
90             "
91             );
92             }
93 7         21 $cidx++;
94             }
95 5         13 push @t, "
96             }
97 3         6 push @t, "
98 3         6 push @t, "
\n";
99 3         41 join "", @t;
100             }
101              
102             # format unknown structure, the default is to dump YAML structure
103             sub _format_unknown {
104 0     0   0 my ($self, $data) = @_;
105 0         0 $self->_htmlify(Dump $data);
106             }
107              
108             sub _format_scalar {
109 3     3   58 my ($self, $data) = @_;
110              
111 3 50       15 my $sdata = defined($data) ? "$data" : "";
112 3         11 $self->_htmlify($sdata);
113             }
114              
115             sub _format_hot {
116 1     1   84 my ($self, $data) = @_;
117 1         2 my @t;
118             # format as 2-column table of key/value
119 1         7 my $t = {cols=>[qw/key value/], html_cols=>[0, 1], rows=>[]};
120 1         6 for my $k (sort keys %$data) {
121 2         36 push @{ $t->{rows} }, [$k, $self->_format($data->{$k})];
  2         11  
122             }
123 1         4 $self->_render_table($t);
124             }
125              
126             1;
127             # ABSTRACT: Pretty-print data structure for HTML output
128              
129             __END__