blib/lib/Text/MarkdownAdoc/Refs.pm

filter: Statementstmt 84.1% 85 / 101 Branchbran 73.3% 22 / 30 Conditioncond 54.5% 6 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 7 totaltotal 75.0% 126 / 168 CC 21 SCAR 21.9
CC
21
Cov
80%
resolve
16.5
_format_resolved_link
6.0
generate_id
3.0
CRAP 9.0
« blib/lib/Text/MarkdownAdoc/Parser.pm ↑ Summary script/markdown-adoc »
1 package Text::MarkdownAdoc::Refs;
2  
3 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #===========================================================================
8 # Reference and cross-reference resolver
9 #===========================================================================
10  
11 sub new
12 186 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 186 my ($class, %opts) = @_;
14  
15 186 my $self = {
16 options => {%opts},
17 link_defs => {}, # normalized label → { url, title }
18 image_defs => {}, # normalized label → { url, title }
19 heading_ids => {}, # normalized anchor → heading title
20 footnote_defs => {}, # label → text (raw, not normalized)
21 footnote_seen => {}, # label → count of times referenced
22 };
23  
24 186 bless $self, $class;
25 186 return $self;
26 }
27  
28 # Register a link reference definition
29 sub add_link_def
30 4 ▶{
Subroutine add_link_def: called CC=1 SCAR=0.0
Pod undocumented
31 4 my ($self, $label, $url, $title) = @_;
32  
33 4 my $normalized = _normalize_label($label);
34 4 $self->{link_defs}{$normalized} = {
35 url => $url,
36 title => $title,
37 };
38 }
39  
40 # Register an image reference definition
41 sub add_image_def
42 0 ▶{
Subroutine add_image_def: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my ($self, $label, $url, $title) = @_;
44  
45 0 my $normalized = _normalize_label($label);
46 0 $self->{image_defs}{$normalized} = {
47 url => $url,
48 title => $title,
49 };
50 }
51  
52 # Register a heading ID and its title text
53 sub register_heading
54 6 ▶{
Subroutine register_heading: called CC=2 SCAR=7.2
Pod undocumented
55 6 my ($self, $id, $title) = @_;
56  
57 6 ▶ $self->{heading_ids}{$id} = $title // '';
Condition50%1/2
$title // ""
l!l
60
MC/DC0%0/1
$title // ""
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
58 }
59  
60 # Generate an AsciiDoc-compatible ID from heading text
61 sub generate_id
62 8 ▶{
Subroutine generate_id: called CC=3 SCAR=11.1
Pod undocumented
63 8 my ($self, $text) = @_;
64  
65 8 ▶ my $idprefix = $self->{options}{idprefix} // '_';
Condition50%1/2
$self->{"options"}{"idprefix"} // "_"
l!l
08
MC/DC0%0/1
$self->{"options"}{"idprefix"} // "_"
$self->{"options"}{"idprefix"}
Truth table50%1/2
A
A= $self->{"options"}{"idprefix"}
Aresult
11
00
66 8 ▶ my $idseparator = $self->{options}{idseparator} // '_';
Condition50%1/2
$self->{"options"}{"idseparator"} // "_"
l!l
08
MC/DC0%0/1
$self->{"options"}{"idseparator"} // "_"
$self->{"options"}{"idseparator"}
Truth table50%1/2
A
A= $self->{"options"}{"idseparator"}
Aresult
11
00
67  
68 8 my $id = lc($text);
69 8 $id =~ s/[^a-z0-9]+/$idseparator/g;
70 8 $id =~ s/^$idseparator+//;
71 8 $id =~ s/$idseparator+$//;
72  
73 8 return $idprefix . $id;
74 }
75  
76 # Register a footnote definition
77 sub add_footnote_def
78 10 ▶{
Subroutine add_footnote_def: called CC=1 SCAR=0.0
Pod undocumented
79 10 my ($self, $label, $text, $inline) = @_;
80  
81 # Process inline formatting inside footnote text
82 10 my $processed = $inline->process($text);
83 10 $self->{footnote_defs}{$label} = $processed;
84 }
85  
86 # Build a footnote ID from the label (fn + non-word chars → _)
87 sub _footnote_id
88 10 ▶{
Subroutine _footnote_id: called CC=1 SCAR=0.0
89 10 my ($label) = @_;
90  
91 10 my $id = 'fn' . $label;
92 10 $id =~ s/[^\w]/_/g;
93 10 return $id;
94 }
95  
96 # Resolve all placeholders in the output text
97 sub resolve
98 182 ▶{
Subroutine resolve: called CC=15 SCAR=28.1
Pod undocumented
99 182 my ($self, $text) = @_;
100  
101 182 ▶ my $input = $text // '';
Condition50%1/2
$text // ""
l!l
1820
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
102 182 ▶ return '' if $input eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $input eq "" 0 182 182
103  
104 # Resolve FOOTNOTE placeholders
105 182 $input =~ s{\x00FOOTNOTE:([^\x00]+)\x00}{
106 11 my $label = $1;
107  
108 11 my $def = $self->{footnote_defs}{$label};
109 11 ▶ if ($def) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def) { } 10 1 11
110 10 $self->{footnote_seen}{$label}++;
111 10 my $count = $self->{footnote_seen}{$label};
112  
113 10 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 9 1 10
114 9 my $id = _footnote_id($label);
115 9 "footnote:$id\[$def\]";
116 }
117 else {
118 1 my $id = _footnote_id($label);
119 1 "footnote:$id\[\]";
120 }
121 }
122 else {
123 # Unresolved footnote: literal fallback
124 1 "[^$label]";
125 }
126 }eg;
127  
128 # Resolve XREF placeholders (deferred cross-reference resolution)
129 182 $input =~ s{\x00XREF:([^\x00]+)\x00}{
130 4 my $payload = $1;
131  
132 # Format: id:display
133 4 ▶ if ($payload =~ m/^([^:]+):(.*)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload =~ /^([^:]+):(.*)/su) { } 4 0 4
134 4 my $anchor = $1;
135 4 my $display = $2;
136  
137 # Try to find the heading by exact anchor match first,
138 # then try the generated ID form.
139 4 my $resolved_anchor;
140 4 ▶ if (exists $self->{heading_ids}{$anchor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'heading_ids'}{$anchor}) { } 0 4 4
141 0 $resolved_anchor = $anchor;
142 }
143 else {
144 # Try generating the ID from the raw anchor
145 4 my $gen_id = $self->generate_id($anchor);
146 4 ▶ if (exists $self->{heading_ids}{$gen_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"heading_ids"}{$gen_id}) 3 1 4
147 3 $resolved_anchor = $gen_id;
148 }
149 }
150  
151 4 ▶ if ($resolved_anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolved_anchor) { } 3 1 4
152 # If display text matches the heading title for this anchor,
153 # emit bare <<anchor>> without explicit text.
154 3 my $title = $self->{heading_ids}{$resolved_anchor};
155 3 ▶ if (defined $title && $display eq $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title and $display eq $title) { } 2 1 3
Condition66%2/3
defined $title and $display eq $title
!ll&&!rl&&r
012
MC/DC50%1/2
defined $title and $display eq $title
defined $title $display eq $title
Truth table66%2/3
A and B
A= defined $titleB= $display eq $title
ABresult
0X0
100
111
156 2 "<<$resolved_anchor>>";
157 }
158 else {
159 1 "<<$resolved_anchor,$display>>";
160 }
161 }
162 else {
163 # Unresolved xref: emit the display text as a fallback
164 1 "<<$anchor,$display>>";
165 }
166 }
167 else {
168 0 $payload;
169 }
170 }eg;
171  
172 # Resolve REFLINK placeholders
173 182 $input =~ s{\x00REFLINK:([^\x02\x00]+)\x02([^\x02\x00]*)\x02([^\x00]*)\x00}{
174 12 my $label = $1;
175 12 my $display = $2;
176 12 my $form = $3;
177 12 ▶ my $suffix = $form eq q{c} ? q{[]} : $form =~ m{^f(.*)\z}s ? "[$1]" : q{};
Branch: 2 branches
BranchTrueFalseTotal
$form eq 'c' ? : 2 10 12
$form =~ /^f(.*)\z/su ? : 2 8 10
178  
179 12 my $def = $self->{link_defs}{$label};
180 12 ▶ if ($def) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def) { } 4 8 12
181 4 _format_resolved_link($display, $def->{url});
182 }
183 else {
184 # Unresolved: not a link, so the brackets are literal text
185 8 "[$display]$suffix";
186 }
187 }eg;
188  
189 # Resolve REFIMG placeholders
190 182 $input =~ s{\x00REFIMG:([^:]+):([^\x00]*)\x00}{
191 0 my $label = $1;
192 0 my $alt = $2;
193  
194 0 my $def = $self->{image_defs}{$label};
195 0 ▶ if ($def) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def) { } 0 0 0
196 0 "image:$def->{url}\[$alt\]";
197 }
198 else {
199 # Unresolved: emit literal alt text
200 0 $alt;
201 }
202 }eg;
203  
204 182 return $input;
205 }
206  
207 #===========================================================================
208 # Internal helpers
209 #===========================================================================
210  
211 sub _normalize_label
212 4 ▶{
Subroutine _normalize_label: called CC=1 SCAR=0.0
213 4 my ($label) = @_;
214  
215 4 $label = lc($label);
216 4 $label =~ s/\s+/ /g;
217 4 $label =~ s/^\s+|\s+$//g;
218  
219 4 return $label;
220 }
221  
222 sub _format_resolved_link
223 4 ▶{
Subroutine _format_resolved_link: called CC=4 SCAR=17.9
224 4 my ($text, $url) = @_;
225  
226 # Anchor link
227 4 ▶ if ($url =~ m/^#(.+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^#(.+)/u) 0 4 4
228 {
229 0 my $anchor = $1;
230 0 return "<<$anchor,$text>>";
231 }
232  
233 # .md file → .adoc xref
234 4 ▶ if ($url =~ m/^(.+)\.md$/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /^(.+)\.md$/iu) 0 4 4
235 {
236 0 my $base = $1;
237 0 return "xref:$base.adoc\[$text\]";
238 }
239  
240 # URL-only link where text equals URL → bare URL
241 4 ▶ if ($text eq $url)
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq $url) 0 4 4
242 {
243 0 return $url;
244 }
245  
246 # Standard link
247 4 return "$url\[$text\]";
248 }
249  
250 17 1;
251  
252 __END__
« blib/lib/Text/MarkdownAdoc/Parser.pm ↑ Summary script/markdown-adoc »