| 1 | package Test::CPAN::Health::Reporter::Markdown; | ||
| 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 | # Glyphs -- Unicode escape sequences keep the source file strictly ASCII while | ||
| 15 | # producing the expected UTF-8 glyphs at runtime. | ||
| 16 | Readonly::Scalar my $GLYPH_PASS => "\x{2705}"; # U+2705 CHECK MARK BUTTON | ||
| 17 | Readonly::Scalar my $GLYPH_WARN => "\x{26A0}"; # U+26A0 WARNING SIGN | ||
| 18 | Readonly::Scalar my $GLYPH_FAIL => "\x{2717}"; # U+2717 BALLOT X | ||
| 19 | Readonly::Scalar my $GLYPH_SKIP => "\x{2014}"; # U+2014 EM DASH | ||
| 20 | Readonly::Scalar my $GLYPH_ERROR => "\x{2049}"; # U+2049 EXCLAMATION QUESTION MARK | ||
| 21 | |||
| 22 | Readonly::Hash my %STATUS_GLYPH => ( | ||
| 23 | pass => $GLYPH_PASS, | ||
| 24 | warn => $GLYPH_WARN, | ||
| 25 | fail => $GLYPH_FAIL, | ||
| 26 | skip => $GLYPH_SKIP, | ||
| 27 | error => $GLYPH_ERROR, | ||
| 28 | ); | ||
| 29 | |||
| 30 | # Score thresholds for badge colour selection. | ||
| 31 | Readonly::Scalar my $SCORE_GREAT => 90; | ||
| 32 | Readonly::Scalar my $SCORE_OK => 70; | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Test::CPAN::Health::Reporter::Markdown - Render a health report as GitHub-Flavoured Markdown | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use Test::CPAN::Health::Reporter::Markdown; | ||
| 41 | |||
| 42 | my $reporter = Test::CPAN::Health::Reporter::Markdown->new; | ||
| 43 | my $md = $reporter->render($report); | ||
| 44 | print $md; | ||
| 45 | |||
| 46 | =head1 DESCRIPTION | ||
| 47 | |||
| 48 | Produces GitHub-Flavoured Markdown from a L<Test::CPAN::Health::Report>. | ||
| 49 | The output is suitable for PR comments, README badge sections, or wiki pages. | ||
| 50 | |||
| 51 | The document includes a header, a weighted score line with an emoji badge, | ||
| 52 | a summary counts line, a Markdown results table, collapsible detail blocks | ||
| 53 | for warn/fail/error results, and a trailing shields.io badge URL comment. | ||
| 54 | |||
| 55 | =head1 LIMITATIONS | ||
| 56 | |||
| 57 | =over 4 | ||
| 58 | |||
| 59 | =item * Pipe characters and backslashes in check summaries are escaped for | ||
| 60 | GFM table compatibility; other Markdown metacharacters are not escaped. | ||
| 61 | |||
| 62 | =item * Detail strings are rendered verbatim inside C<< <details> >> blocks; | ||
| 63 | HTML embedded in detail strings is not escaped. | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub new { | ||
| 70 | my ($class, %args) = @_; | ||
| 71 | |||
| 72 | %args = %{ validate_strict( | ||
| 73 | schema => {}, | ||
| 74 | input => \%args, | ||
| 75 | ) }; | ||
| 76 | |||
| 77 | return bless {}, $class; | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 render | ||
| 81 | |||
| 82 | =head3 PURPOSE | ||
| 83 | |||
| 84 | Render a Report to a GitHub-Flavoured Markdown string. | ||
| 85 | |||
| 86 | =head3 API SPECIFICATION | ||
| 87 | |||
| 88 | =head4 INPUT | ||
| 89 | |||
| 90 | report Test::CPAN::Health::Report required | ||
| 91 | |||
| 92 | =head4 OUTPUT | ||
| 93 | |||
| 94 | Scalar string (UTF-8). Ends with a trailing newline. | ||
| 95 | |||
| 96 | =head3 MESSAGES | ||
| 97 | |||
| 98 | Code | Severity | Message | Resolution | ||
| 99 | ------+----------+------------------------------------+--------------------- | ||
| 100 | MDR01 | FATAL | report must be a Report object | Pass a Report instance | ||
| 101 | |||
| 102 | =head3 FORMAL SPECIFICATION | ||
| 103 | |||
| 104 | -- Z schema (placeholder) -- | ||
| 105 | RenderOp | ||
| 106 | report : Report | ||
| 107 | output : String | ||
| 108 | ------------------------------------------------------- | ||
| 109 | #output > 0 | ||
| 110 | output contains report.overall_score | ||
| 111 | |||
| 112 | =head3 SIDE EFFECTS | ||
| 113 | |||
| 114 | None. | ||
| 115 | |||
| 116 | =head3 USAGE EXAMPLE | ||
| 117 | |||
| 118 | my $md = $reporter->render($report); | ||
| 119 | print $md; | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub render { | ||
| 124 | my ($self, $report) = @_; | ||
| 125 | |||
| 126 | croak 'report must be a Test::CPAN::Health::Report' | ||
| 127 | unless blessed($report) && $report->isa('Test::CPAN::Health::Report'); | ||
| 128 | |||
| 129 | my $score = $report->overall_score; | ||
| 130 | |||
| 131 | my $score_badge = $score >= $SCORE_GREAT ? $GLYPH_PASS | ||
| 132 | : $score >= $SCORE_OK ? $GLYPH_WARN | ||
| 133 | : $GLYPH_FAIL; | ||
| 134 | |||
| 135 | my $badge_color = $score >= $SCORE_GREAT ? 'brightgreen' | ||
| 136 | : $score >= $SCORE_OK ? 'yellow' | ||
| 137 | : 'red'; | ||
| 138 | |||
| 139 | my @lines; | ||
| 140 | |||
| 141 | # 1. Header | ||
| 142 | push @lines, '## CPAN Health Report'; | ||
| 143 | push @lines, ''; | ||
| 144 | |||
| 145 | # 2. Score line | ||
| 146 | push @lines, sprintf( '**Overall score: %d/100** %s', $score, $score_badge ); | ||
| 147 | push @lines, ''; | ||
| 148 | |||
| 149 | # 3. Summary counts | ||
| 150 | push @lines, sprintf( | ||
| 151 | 'Passed: %d | Warned: %d | Failed: %d | Skipped: %d', | ||
| 152 | $report->pass_count, | ||
| 153 | $report->warn_count, | ||
| 154 | $report->fail_count, | ||
| 155 | $report->skip_count, | ||
| 156 | ); | ||
| 157 | push @lines, ''; | ||
| 158 | |||
| 159 | # 4. Results table | ||
| 160 | push @lines, '| Status | Check | Score | Summary |'; | ||
| 161 | push @lines, '|--------|-------|-------|---------|'; | ||
| 162 | |||
| 163 | my @results = sort { $a->check_id cmp $b->check_id } @{ $report->results }; | ||
| 164 | for my $result (@results) { | ||
| 165 | push @lines, $self->_render_table_row($result); | ||
| 166 | } | ||
| 167 | push @lines, ''; | ||
| 168 | |||
| 169 | # 5. Collapsible detail blocks for warn / fail / error results with details. | ||
| 170 | for my $result (@results) { | ||
| 171 | my $status = $result->status; | ||
| 172 | next unless $status eq 'warn' || $status eq 'fail' || $status eq 'error'; | ||
| 173 | next unless @{ $result->details }; | ||
| 174 | push @lines, $self->_render_details_block($result); | ||
| 175 | } | ||
| 176 | |||
| 177 | # 6. Footer: shields.io badge URL as an HTML comment. | ||
| 178 | push @lines, sprintf( | ||
| 179 | '<!-- badge: https://img.shields.io/badge/cpan--health-%d%%2F100-%s?style=flat-square -->', | ||
| 180 | $score, | ||
| 181 | $badge_color, | ||
| 182 | ); | ||
| 183 | |||
| 184 | return join( "\n", @lines ) . "\n"; | ||
| 185 | } | ||
| 186 | |||
| 187 | # --------------------------------------------------------------------------- | ||
| 188 | # Private rendering helpers | ||
| 189 | # --------------------------------------------------------------------------- | ||
| 190 | |||
| 191 | sub _render_table_row { | ||
| 192 | my ($self, $result) = @_; | ||
| 193 | |||
| 194 | my $status = $result->status; | ||
| 195 | my $glyph = $STATUS_GLYPH{$status} // '?'; | ||
| 196 | my $name = _esc_md( $result->data->{name} // $result->check_id ); | ||
| 197 | my $score = defined $result->score | ||
| 198 | ? $result->score . '/100' | ||
| 199 | : $GLYPH_SKIP; | ||
| 200 | my $summary = _esc_md( $result->summary ); | ||
| 201 | |||
| 202 | return sprintf( '| %s | %s | %s | %s |', $glyph, $name, $score, $summary ); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _render_details_block { | ||
| 206 | my ($self, $result) = @_; | ||
| 207 | |||
| 208 | my $status = $result->status; | ||
| 209 | my $glyph = $STATUS_GLYPH{$status} // '?'; | ||
| 210 | my $name = $result->data->{name} // $result->check_id; | ||
| 211 | |||
| 212 | my @lines; | ||
| 213 | push @lines, '<details>'; | ||
| 214 | push @lines, sprintf( '<summary>%s %s details</summary>', $glyph, $name ); | ||
| 215 | push @lines, ''; | ||
| 216 | for my $detail ( @{ $result->details } ) { | ||
| 217 | push @lines, '- ' . $detail; | ||
| 218 | } | ||
| 219 | push @lines, ''; | ||
| 220 | push @lines, '</details>'; | ||
| 221 | push @lines, ''; | ||
| 222 | |||
| 223 | return @lines; | ||
| 224 | } | ||
| 225 | |||
| 226 | # Escape GFM table cell special characters. | ||
| 227 | # Backslash must be escaped before pipe so the escaping backslash is not | ||
| 228 | # itself subsequently escaped. | ||
| 229 | sub _esc_md { | ||
| 230 | my ($str) = @_; | ||
| 231 | return '' unless defined $str; | ||
| 232 | $str =~ s/ [\\] /\\\\/gx; | ||
| 233 | $str =~ s/ [|] /\\|/gx; | ||
| 234 | return $str; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head1 AUTHOR | ||
| 238 | |||
| 239 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 240 | |||
| 241 | =head1 LICENSE AND COPYRIGHT | ||
| 242 | |||
| 243 | Copyright (C) 2025-2026 Nigel Horne. | ||
| 244 | |||
| 245 | This program is free software; you can redistribute it and/or modify it | ||
| 246 | under the terms of the GNU General Public License as published by the Free | ||
| 247 | Software Foundation; either version 2 of the License, or (at your option) | ||
| 248 | any later version. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | 1; | ||