blib/lib/Test/CPAN/Health/Reporter/HTML.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Report.pm ↑ Summary blib/lib/Test/CPAN/Health/Reporter/JSON.pm »
1 package Test::CPAN::Health::Reporter::HTML;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Carp qw(croak);
8 use Readonly;
9 use Params::Validate::Strict qw(validate_strict);
10 use Scalar::Util qw(blessed);
11  
12 our $VERSION = '0.1.0';
13  
14 # Status badge colours: background and foreground.
15 Readonly::Hash my %STATUS_BG => (
16 pass => '#d1fae5',
17 warn => '#fef3c7',
18 fail => '#fee2e2',
19 skip => '#f3f4f6',
20 error => '#f3e8ff',
21 );
22  
23 Readonly::Hash my %STATUS_FG => (
24 pass => '#065f46',
25 warn => '#92400e',
26 fail => '#991b1b',
27 skip => '#374151',
28 error => '#5b21b6',
29 );
30  
31 # Unicode glyphs expressed as HTML entities (source stays ASCII).
32 Readonly::Hash my %STATUS_GLYPH => (
33 pass => '✓', # CHECK MARK
34 warn => '⚠', # WARNING SIGN
35 fail => '✗', # BALLOT X
36 skip => '—', # EM DASH
37 error => '⁉', # EXCLAMATION QUESTION MARK
38 );
39  
40 =head1 NAME
41  
42 Test::CPAN::Health::Reporter::HTML - Render a health report as an HTML document
43  
44 =head1 SYNOPSIS
45  
46 use Test::CPAN::Health::Reporter::HTML;
47  
48 my $reporter = Test::CPAN::Health::Reporter::HTML->new;
49 my $html = $reporter->render($report);
50 open my $fh, '>', 'report.html';
51 print {$fh} $html;
52  
53 =head1 DESCRIPTION
54  
55 Produces a self-contained HTML5 document from a
56 L<Test::CPAN::Health::Report>. The document includes all CSS inline and
57 has no external dependencies; it may be opened directly in a browser or
58 saved to disk.
59  
60 Each check result is a colour-coded table row. Detail strings appear
61 below the summary within the same row. The overall score and status
62 summary are shown prominently at the top of the page.
63  
64 =head1 LIMITATIONS
65  
66 =over 4
67  
68 =item * No JavaScript is used; the output is not interactive.
69  
70 =item * Long detail strings are not wrapped.
71  
72 =back
73  
74 =cut
75  
76 sub new {
77 my ($class, %args) = @_;
78  
79 %args = %{ validate_strict(
80 schema => {
81 title => { type => 'string', optional => 1, default => 'CPAN Health Report' },
82 },
83 input => \%args,
84 ) };
85  
86 return bless { _title => $args{title} }, $class;
87 }
88  
89 =head2 render
90  
91 =head3 PURPOSE
92  
93 Render a Report to a self-contained HTML5 string.
94  
95 =head3 API SPECIFICATION
96  
97 =head4 INPUT
98  
99 report Test::CPAN::Health::Report required
100  
101 =head4 OUTPUT
102  
103 Scalar string: valid HTML5 document (UTF-8).
104  
105 =head3 MESSAGES
106  
107 Code | Severity | Message | Resolution
108 ------+----------+------------------------------------+---------------------
109 HTM01 | FATAL | report must be a Report object | Pass a Report instance
110  
111 =head3 FORMAL SPECIFICATION
112  
113 -- Z schema (placeholder) --
114 RenderOp
115 report : Report
116 html! : String
117 -------------------------------------------------------
118 html! contains report.overall_score
119 valid_html5(html!)
120  
121 =head3 SIDE EFFECTS
122  
123 None.
124  
125 =head3 USAGE EXAMPLE
126  
127 my $html = $reporter->render($report);
128 open my $fh, '>', 'report.html' or die $!;
129 print {$fh} $html;
130  
131 =cut
132  
133 sub render {
134 my ($self, $report) = @_;
135  
136 croak 'report must be a Test::CPAN::Health::Report'
137 unless blessed($report) && $report->isa('Test::CPAN::Health::Report');
138  
139 my $score = $report->overall_score;
140 my $score_colour = $score >= 90 ? '#065f46'
141 : $score >= 70 ? '#92400e'
142 : '#991b1b';
143 my $score_bg = $score >= 90 ? '#d1fae5'
144 : $score >= 70 ? '#fef3c7'
145 : '#fee2e2';
146  
147 my $title = _esc($self->{_title});
148 my $score_str = _esc(sprintf('%d/100', $score));
149 my $summary = _esc(sprintf(
150 'Passed: %d Warned: %d Failed: %d Skipped: %d',
151 $report->pass_count,
152 $report->warn_count,
153 $report->fail_count,
154 $report->skip_count,
155 ));
156  
157 my $rows = '';
158 for my $result (sort { $a->check_id cmp $b->check_id } @{$report->results}) {
159 $rows .= $self->_render_row($result);
160 }
161  
162 return <<"END_HTML";
163 <!DOCTYPE html>
164 <html lang="en">
165 <head>
166 <meta charset="UTF-8">
167 <meta name="viewport" content="width=device-width, initial-scale=1.0">
168 <title>$title</title>
169 <style>
170 *{box-sizing:border-box;margin:0;padding:0}
171 body{font-family:system-ui,sans-serif;background:#f9fafb;color:#111827;padding:2rem}
172 h1{font-size:1.5rem;margin-bottom:1.5rem}
173 .score-box{display:inline-block;padding:.75rem 1.5rem;border-radius:.5rem;font-size:2rem;font-weight:700;background:$score_bg;color:$score_colour;margin-bottom:1rem}
174 .summary{color:#6b7280;margin-bottom:1.5rem;font-size:.9rem}
175 table{width:100%;border-collapse:collapse;background:#fff;border-radius:.5rem;overflow:hidden;box-shadow:0 1px 3px rgba(0,0,0,.1)}
176 thead th{background:#374151;color:#fff;padding:.75rem 1rem;text-align:left;font-size:.85rem;font-weight:600}
177 tbody tr{border-bottom:1px solid #e5e7eb}
178 tbody tr:last-child{border-bottom:none}
179 td{padding:.75rem 1rem;vertical-align:top;font-size:.9rem}
180 .badge{display:inline-block;padding:.2rem .6rem;border-radius:.25rem;font-size:.8rem;font-weight:600}
181 .detail{font-size:.8rem;color:#6b7280;margin-top:.25rem}
182 </style>
183 </head>
184 <body>
185 <h1>$title</h1>
186 <div class="score-box">$score_str</div>
187 <div class="summary">$summary</div>
188 <table>
189 <thead><tr><th>Check</th><th>Status</th><th>Score</th><th>Summary</th></tr></thead>
190 <tbody>
191 $rows</tbody>
192 </table>
193 </body>
194 </html>
195 END_HTML
196 }
197  
198 # ---------------------------------------------------------------------------
199 # Private helpers
200 # ---------------------------------------------------------------------------
201  
202 sub _render_row {
203 my ($self, $result) = @_;
204  
205 my $status = $result->status;
206 my $bg = $STATUS_BG{$status} // '#f9fafb';
207 my $fg = $STATUS_FG{$status} // '#111827';
208 my $glyph = $STATUS_GLYPH{$status} // '?';
209  
210 my $name = _esc($result->data->{name} // $result->check_id);
211 my $summary = _esc($result->summary);
212 my $score = defined $result->score ? _esc($result->score . '/100') : '';
213  
214 my $details_html = '';
215 for my $d (@{$result->details}) {
216 $details_html .= '<div class="detail">' . _esc($d) . '</div>';
217 }
218  
219 return <<"END_ROW";
220 <tr style="background:$bg">
221 <td>$name</td>
222 <td><span class="badge" style="background:$bg;color:$fg">$glyph $status</span></td>
223 <td>$score</td>
224 <td>$summary$details_html</td>
225 </tr>
226 END_ROW
227 }
228  
229 # Escape the five HTML-special characters to prevent XSS.
230 sub _esc {
231 my ($str) = @_;
232 return '' unless defined $str;
233 $str =~ s/ & /&amp;/gx;
234 $str =~ s/ < /&lt;/gx;
235 $str =~ s/ > /&gt;/gx;
236 $str =~ s/ " /&quot;/gx;
237 $str =~ s/ ' /&#39;/gx;
238 return $str;
239 }
240  
241 =head1 AUTHOR
242  
243 Nigel Horne, C<< <njh at nigelhorne.com> >>
244  
245 =head1 LICENSE AND COPYRIGHT
246  
247 Copyright (C) 2025-2026 Nigel Horne.
248  
249 This program is free software; you can redistribute it and/or modify it
250 under the terms of the GNU General Public License as published by the Free
251 Software Foundation; either version 2 of the License, or (at your option)
252 any later version.
253  
254 =cut
255  
256 1;
« blib/lib/Test/CPAN/Health/Report.pm ↑ Summary blib/lib/Test/CPAN/Health/Reporter/JSON.pm »