blib/lib/CPAN/Changes/Parser/KeepAChangeLog.pm

filter: Statementstmt 100% 52 / 52 Branchbran 86.3% 19 / 22 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 94.0% 79 / 84 CC 14 SCAR 21.1
CC
14
Cov
95%
_kac_to_cpan_changes_spec
12.0
parse_string
3.0
BEGIN
1.0
CRAP 8.3
↑ Summary
1 package CPAN::Changes::Parser::KeepAChangeLog;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.1.1';
7  
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 extends 'CPAN::Changes::Parser';
10  
11 has '+version_like' => (
12 is => 'ro',
13 1 ▶ default => sub { qr/Unreleased/i },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 );
15  
16 2 ▶sub parse_string {
Subroutine parse_string: called CC=3 SCAR=11.0
Pod documented
17 2 my ($self, $string, @rest) = @_;
18 2 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 2 2
19  
20 2 my $transformed = _kac_to_cpan_changes_spec($string);
21 2 ▶ return undef unless defined $transformed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $transformed 1 1 2
22  
23 1 return $self->SUPER::parse_string($transformed, @rest);
24 }
25  
26 # Transform Keep a Changelog 1.1.0-ish Markdown into CPAN::Changes::Spec-ish text.
27 # Returns transformed string, or undef if it doesn't look like KaC / can't be transformed.
28 2 ▶sub _kac_to_cpan_changes_spec {
Subroutine _kac_to_cpan_changes_spec: called CC=12 SCAR=24.9
29 2 my ($in) = @_;
30  
31 # Quick “does this plausibly look like Keep a Changelog?” gate.
32 # We require at least one release heading of the KaC form.
33 2 ▶ return undef unless $in =~ /^\s*##\s+\[(?:Unreleased|[^\]]+)\]/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $in =~ /^\s*##\s+\[(?:Unreleased|[^\]]+)\]/m 1 1 2
34  
35 1 my @out;
36 1 my $saw_release = 0;
37  
38 1 for my $line (split /\n/, $in, -1) {
39 # Drop CR if file is CRLF
40 18 $line =~ s/\r\z//;
41  
42 # Drop KaC link reference definitions (commonly at bottom), e.g.:
43 # [1.0.0]: https://example/compare/v0.9.0...v1.0.0
44 # [Unreleased]: https://example/compare/v1.0.0...HEAD
45 18 ▶ if ($line =~ /^\s*\[[^\]]+\]:\s+\S+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\[[^\]]+\]:\s+\S+/) 2 16 18
46 2 next;
47 }
48  
49 # Release headings:
50 # ## [1.1.0] - 2024-01-31
51 # ## [1.1.0]
52 # ## [Unreleased]
53 16 ▶ if ($line =~ /^\s*##\s+\[([^\]]+)\]\s*(?:-\s*([0-9]{4}-[0-9]{2}-[0-9]{2}))?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*##\s+\[([^\]]+)\]\s*(?:-\s*([0-9]{4}-[0-9]{2}-[0-9]{2}))?\s*$/) 2 14 16
54 2 my ($ver, $date) = ($1, $2);
55  
56 # Normalise Unreleased to a CPAN::Changes-ish release line with an allowed “date” token.
57 2 ▶ if ($ver =~ /\AUnreleased\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver =~ /\AUnreleased\z/i) { } 1 1 2
58 1 push @out, "Unreleased Not Released";
59 }
60 else {
61 # If date is missing, KaC is still KaC, but CPAN::Changes parser expects something date-like.
62 # We'll accept missing date and set it to "Unknown" (allowed by CPAN::Changes::Spec).
63 1 ▶ $date //= 'Unknown';
Condition50%1/2
$date //= "Unknown"
l!l
10
MC/DC0%0/1
$date //= "Unknown"
$date
Truth table50%1/2
A
A= $date
Aresult
11
00
64 1 push @out, "$ver $date";
65 }
66  
67 2 $saw_release = 1;
68 2 next;
69 }
70  
71 # Category headings:
72 # ### Added
73 # ### Fixed
74 # Map to CPAN group marker:
75 # [Added]
76 14 ▶ if ($line =~ /^\s*###\s+(.+?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*###\s+(.+?)\s*$/) 3 11 14
77 3 my $group = $1;
78  
79 # Be conservative: ignore empty/odd headings that are likely Markdown scaffolding.
80 3 $group =~ s/\s+\z//;
81 3 $group =~ s/\A\s+//;
82  
83 # If it’s something like “Links” or “Changelog” we still *could* treat it as a group,
84 # but KaC categories are the main use. We'll accept any text, as CPAN::Changes groups are free-form.
85 3 push @out, "[$group]";
86 3 next;
87 }
88  
89 # Top-level title:
90 # # Changelog
91 # Treat as preamble. CPAN::Changes parser supports preamble, so keep it (as plain text).
92 11 ▶ if ($line =~ /^\s*#\s+(.+?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*#\s+(.+?)\s*$/) 1 10 11
93 1 push @out, $1;
94 1 next;
95 }
96  
97 # Keep bullets but normalise to "- " for consistency.
98 # Preserve indentation for nested bullets.
99 10 ▶ if ($line =~ /^(\s*)[*-]\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)[*-]\s+(.*)$/) 3 7 10
100 3 push @out, $1 . "- " . $2;
101 3 next;
102 }
103  
104 # Otherwise: pass through line as-is.
105 7 push @out, $line;
106 }
107  
108 1 ▶ return undef unless $saw_release;
Branch: 1 branch
BranchTrueFalseTotal
unless $saw_release 0 1 1
109  
110 # Basic sanity: after transform, we should still have at least one release line
111 # that the base parser is likely to recognise. We can't call its private regex here,
112 # but we can assert we emitted something release-like.
113 1 my $out = join("\n", @out);
114 1 ▶ return undef unless $out =~ /^\s*(?:Unreleased|[0-9A-Za-z_.]+)\s+(?:\d{4}-\d{2}-\d{2}|Unknown|Not Released)\b/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $out =~ /^\s*(?:Unreleased|[0-9A-Za-z_.]+)\s+(?:\d{4}-\d{2}-\d{2}|Unknown|Not Released)\b/m 0 1 1
115  
116 1 return $out;
117 }
118  
119 1 1;
120  
121 __END__
↑ Summary