blib/lib/Simple/Html.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 77 CC 4 SCAR 17.6
CC
4
Cov
0%
make_tr
12.0
make_html
6.0
make_html_table
2.0
CRAP 5.8
1 #ABSTRACT: make simple html, without install Template
2 package Simple::Html;
3  
4 require Exporter;
5 @ISA = qw(Exporter);
6 @EXPORT = qw(
7 make_html_table
8 make_html
9 );
10 our $VERSION = '0.01';
11 our @TR_COLORS = ( '#d2eaf1', '#ffffff', );
12 our $TR_COLOR_NUM = scalar(@TR_COLORS);
13 our $BORDER_COLOR = '#4bacc6';
14  
15 sub make_html_table {
16  
17  
18 my ( $head, $data ) = @_;
19  
20 my $th = make_th($head);
21  
22 my $tbody;
23 my $tr_color_sub = get_tr_color_sub();
24 my $max_colnum = $#$head;
25 for my $tr_data (@$data) {
26 my $color = $tr_color_sub->();
27 my $tr = make_tr(
28 $tr_data,
29 color => $color,
30 max_colnum => $max_colnum,
31 );
32 $tbody .= $tr . "\n";
33 }
34  
35 return <<__TABLE__;
36 <table class='tbmain'>
37 $th
38 $tbody
39 </table>
40 __TABLE__
41  
42 }
43  
44 sub make_th {
45 my ($head) = @_;
46  
47 my @temp = map { "<td>$_</td>" } @$head;
48 my $th = join( "\n", @temp );
49 return "<tr class='tbhead'> $th </tr>";
50 }
51  
52 sub get_tr_color_sub {
53 my $i = 0;
54 my $tr_color_sub = sub {
55 my $c = $TR_COLORS[ $i % $TR_COLOR_NUM ];
56 $i++;
57 return $c;
58 };
59 return $tr_color_sub;
60 }
61  
62 sub make_tr {
63 my ( $d, %opt ) = @_;
64 $opt{max_colnum} ||= $#$d;
65  
66 my $tr = '';
67 for ( @{$d}[ 0 .. $opt{max_colnum} ] ) {
68 $_ = '' unless ( defined $_ );
69  
70 ( my $trim = $_ ) =~ s/<[^>]+>//gs;
71 my $td_h =
72 $trim !~ /^(\d+|N\/A)$/
73 ? "<td>"
74 : "<td style='text-align:center'>";
75  
76 $tr .= "$td_h $_ </td>\n";
77 }
78  
79 return "<tr style='background-color:$opt{color}'>$tr</tr>\n";
80 }
81  
82 sub make_html {
83 my ( $html, %opt ) = @_;
84  
85 my $h = ref($html) ? $html : \$html;
86 my $css = $opt{css} || default_css();
87  
88 return <<__HTML__;
89 <html>
90 <head>
91 <style type='text/css'>
92 $css
93 </style>
94 </head>
95 <body>
96 $$h
97 </body>
98 </html>
99 __HTML__
100 }
101  
102 sub default_css {
103 my $css = <<__REPORT_CSS__;
104 body {
105 font-family: Calibri,Arial, sans-serif;
106 font-size: 90%;
107 line-height: 110%;
108 }
109 p {
110 text-indent:2em;
111 margin:6px;
112 }
113 .tbmain {
114 margin-left:36.0pt;
115 border-color:$BORDER_COLOR;
116 border-style:solid;
117 border-width:1px;
118 text-align:left;
119 border-collapse:collapse;
120 word-break:break-all;
121 }
122  
123 .tbhead {
124 font-weight:bold;
125 }
126 td {
127 padding:0.15cm;
128 border-width:1px;
129 border-color:$BORDER_COLOR;
130 border-style:solid;
131 }
132 .alarm {
133 font-size : 110%;
134 font-weight:bold;
135 color : red;
136 }
137 .strike {
138 font-size : 110%;
139 font-weight:bold;
140 color : #0070c0;
141 }
142 .section {
143 font-size : 110%;
144 font-weight:bold;
145 line-height:150%;
146 }
147 __REPORT_CSS__
148 return $css;
149 }
150  
151 1;