blib/lib/Text/MarkdownAdoc/Parser.pm

filter: Statementstmt 92.7% 836 / 901 Branchbran 79.7% 343 / 430 Conditioncond 66.8% 131 / 196 MC/DCmcdc 40.7% 44 / 108 Subroutinesub 100% 48 / 48 Podpod 0.0% 0 / 2 totaltotal 83.2% 1402 / 1685 CC 299 SCAR 42.0
CC
299
Cov
86%
_parse_body
174.4
__ANON__
27.7
_parse_block_html
23.1
CRAP 66.7
↑ Summary blib/lib/Text/MarkdownAdoc/Refs.pm »
1 package Text::MarkdownAdoc::Parser;
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 17 ▶use Text::MarkdownAdoc::Inline;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Text::MarkdownAdoc::Refs;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #===========================================================================
11 # Line-oriented state machine for Markdown-to-AsciiDoc block conversion
12 #===========================================================================
13  
14 sub new
15 186 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
16 186 my ($class, %opts) = @_;
17  
18 my $self = {
19 options => {%opts},
20 inline => Text::MarkdownAdoc::Inline->new(%opts),
21 refs => Text::MarkdownAdoc::Refs->new(%opts),
22 186 ▶ diagram_languages => $opts{diagram_languages} || ['plantuml', 'mermaid'],
Condition50%1/2
$opts{'diagram_languages'} || ['plantuml', 'mermaid']
l!l
0186
MC/DC0%0/1
$opts{'diagram_languages'} || ['plantuml', 'mermaid']
$opts{'diagram_languages'}
Truth table50%1/2
A
A= $opts{'diagram_languages'}
Aresult
11
00
23 has_math => 0,
24 };
25  
26 186 bless $self, $class;
27 186 return $self;
28 }
29  
30 sub parse
31 186 ▶{
Subroutine parse: called CC=14 SCAR=26.4
Pod undocumented
32 186 my ($self, $text) = @_;
33  
34 186 ▶ my $input = $text // '';
Condition50%1/2
$text // ""
l!l
1860
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
35  
36 186 $input = _normalize_input($input);
37  
38 186 my @lines = split /\n/, $input;
39 186 ▶ return "" unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 4 182 186
40  
41 182 my ($front_matter_keys, $body_start) = _extract_front_matter(\@lines);
42  
43 182 my @body_lines = @lines[$body_start .. $#lines];
44 182 my $body_output = _parse_body($self, \@body_lines);
45  
46 # Collect has_math from inline processor
47 182 ▶ if ($self->{inline}->{has_math})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inline"}{"has_math"}) 2 180 182
48 {
49 2 $self->{has_math} = 1;
50 }
51  
52 182 my $body_has_h1 = _body_has_level1_heading(\@body_lines);
53  
54 182 my @output;
55  
56 182 ▶ if ($front_matter_keys && $front_matter_keys->{title} && !$body_has_h1)
Branch: 1 branch
BranchTrueFalseTotal
if ($front_matter_keys and $front_matter_keys->{"title"} and not $body_has_h1) 2 180 182
Condition100%6/6
$front_matter_keys and $front_matter_keys->{"title"} and not $body_has_h1
!ll&&!rl&&r
17822
$front_matter_keys and $front_matter_keys->{"title"}
!ll&&!rl&&r
17624
MC/DC100%3/3
$front_matter_keys and $front_matter_keys->{"title"} and not $body_has_h1
$front_matter_keys $front_matter_keys->{"title"} not $body_has_h1
Truth table100%4/4
A and B and not(C)
A= $front_matter_keysB= $front_matter_keys->{"title"}C= not $body_has_h1
ABCresult
0XX0
10X0
1100
1111
57 {
58 2 push @output, '= ' . $front_matter_keys->{title};
59 }
60  
61 182 ▶ if ($front_matter_keys)
Branch: 1 branch
BranchTrueFalseTotal
if ($front_matter_keys) 6 176 182
62 {
63 6 for my $key (sort keys %$front_matter_keys)
64 {
65 8 ▶ if ($key eq 'title')
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "title") 4 4 8
66 {
67 4 next;
68 }
69 4 push @output, ':' . $key . ': ' . $front_matter_keys->{$key};
70 }
71 }
72  
73 182 ▶ if ($self->{has_math})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_math"}) 5 177 182
74 {
75 5 push @output, ':stem: latexmath';
76 }
77  
78 182 ▶ if (@output && $body_output ne '')
Branch: 1 branch
BranchTrueFalseTotal
if (@output and $body_output ne "") 9 173 182
Condition66%2/3
@output and $body_output ne ""
!ll&&!rl&&r
17309
MC/DC50%1/2
@output and $body_output ne ""
@output $body_output ne ""
Truth table66%2/3
A and B
A= @outputB= $body_output ne ""
ABresult
0X0
100
111
79 {
80 9 push @output, '';
81 }
82  
83 182 ▶ if ($body_output ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($body_output ne "") 182 0 182
84 {
85 182 push @output, $body_output;
86 }
87  
88 182 my $result = join("\n", @output);
89 182 $result = $self->{refs}->resolve($result);
90 182 $result = _normalize_output($result);
91 182 $result = _apply_wrap($result, $self->{options}{wrap});
92  
93 182 return $result;
94 }
95  
96 #===========================================================================
97 # Internal helpers
98 #===========================================================================
99  
100 sub _normalize_input
101 186 ▶{
Subroutine _normalize_input: called CC=1 SCAR=0.0
102 186 my ($text) = @_;
103  
104 186 $text =~ s/\r\n/\n/g;
105 186 $text =~ s/\r/\n/g;
106 186 $text =~ s/^(?:[ \t]*\n)+//;
107  
108 186 return $text;
109 }
110  
111 sub _normalize_output
112 182 ▶{
Subroutine _normalize_output: called CC=2 SCAR=7.1
113 182 my ($text) = @_;
114  
115 182 $text =~ s/[ \t]+$//gm;
116 182 $text =~ s/\n{3,}/\n\n/g;
117 182 $text =~ s/\n*$/\n/;
118  
119 182 ▶ if ($text =~ m/^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^\s*$/u) 0 182 182
120 {
121 0 return '';
122 }
123  
124 182 return $text;
125 }
126  
127 sub _extract_front_matter
128 182 ▶{
Subroutine _extract_front_matter: called CC=8 SCAR=20.8
129 182 my ($lines) = @_;
130  
131 182 ▶ return (undef, 0) unless @$lines && $lines->[0] =~ m/^---$/;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines and $lines->[0] =~ /^---$/u 175 7 182
Condition66%2/3
@$lines and $lines->[0] =~ /^---$/u
!ll&&!rl&&r
01757
MC/DC50%1/2
@$lines and $lines->[0] =~ /^---$/u
@$lines $lines->[0] =~ /^---$/u
Truth table66%2/3
A and B
A= @$linesB= $lines->[0] =~ /^---$/u
ABresult
0X0
100
111
132  
133 7 my $end = -1;
134 7 ▶ for (my $i = 1; $i < @$lines; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 14 1 15
135 {
136 14 ▶ if ($lines->[$i] =~ m/^---$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($lines->[$i] =~ /^---$/u) 6 8 14
137 {
138 6 $end = $i;
139 6 last;
140 }
141 }
142  
143 7 ▶ return (undef, 0) if $end == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $end == -1 1 6 7
144  
145 6 my %keys;
146 6 ▶ for (my $i = 1; $i < $end; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 8 6 14
147 {
148 8 my $line = $lines->[$i];
149 8 ▶ if ($line =~ m/^([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*:[ \t]*(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([a-zA-Z_][a-zA-Z0-9_]*)[ \t]*:[ \t]*(.*)/u) 8 0 8
150 {
151 8 my $key = $1;
152 8 my $value = $2;
153 8 $value =~ s/[ \t]+$//;
154 8 $keys{$key} = $value;
155 }
156 }
157  
158 6 return (\%keys, $end + 1);
159 }
160  
161 sub _body_has_level1_heading
162 182 ▶{
Subroutine _body_has_level1_heading: called CC=8 SCAR=20.8
163 182 my ($lines) = @_;
164  
165 182 ▶ for (my $i = 0; $i < @$lines; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 351 164 515
166 {
167 351 my $line = $lines->[$i];
168  
169 351 ▶ if ($line =~ m/^[ \t]*#[ \t]/ || $line =~ m/^[ \t]*#$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]*#[ \t]/u or $line =~ /^[ \t]*#$/u) 15 336 351
Condition66%2/3
$line =~ /^[ \t]*#[ \t]/u or $line =~ /^[ \t]*#$/u
l!l&&r!l&&!r
150336
MC/DC50%1/2
$line =~ /^[ \t]*#[ \t]/u or $line =~ /^[ \t]*#$/u
$line =~ /^[ \t]*#[ \t]/u $line =~ /^[ \t]*#$/u
Truth table66%2/3
A or B
A= $line =~ /^[ \t]*#[ \t]/uB= $line =~ /^[ \t]*#$/u
ABresult
1X1
011
000
170 {
171 15 return 1;
172 }
173  
174 336 ▶ if ($i + 1 < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < @$lines) 172 164 336
175 {
176 172 my $next = $lines->[$i + 1];
177 172 ▶ if ($line ne '' && $next =~ m/^=+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "" and $next =~ /^=+$/u) 3 169 172
Condition100%3/3
$line ne "" and $next =~ /^=+$/u
!ll&&!rl&&r
341353
MC/DC100%2/2
$line ne "" and $next =~ /^=+$/u
$line ne "" $next =~ /^=+$/u
Truth table100%3/3
A and B
A= $line ne ""B= $next =~ /^=+$/u
ABresult
0X0
100
111
178 {
179 3 ▶ if ($line !~ m/^[ \t]*[#=\-*_]/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^[ \t]*[#=\-*_]/u) 3 0 3
180 {
181 3 return 1;
182 }
183 }
184 }
185 }
186  
187 164 return 0;
188 }
189  
190 sub _is_thematic_break
191 243 ▶{
Subroutine _is_thematic_break: called CC=4 SCAR=13.9
192 243 my ($line, $prev_was_para) = @_;
193  
194 243 ▶ return 0 unless $line =~ m/^[ \t]*([-*_])[ \t]*\1[ \t]*\1[ \t]*(\1[ \t]*)*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^[ \t]*([-*_])[ \t]*\1[ \t]*\1[ \t]*(\1[ \t]*)*$/u 233 10 243
195  
196 10 ▶ if ($1 eq '-' && $prev_was_para)
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "-" and $prev_was_para) 3 7 10
Condition100%3/3
$1 eq "-" and $prev_was_para
!ll&&!rl&&r
343
MC/DC100%2/2
$1 eq "-" and $prev_was_para
$1 eq "-" $prev_was_para
Truth table100%3/3
A and B
A= $1 eq "-"B= $prev_was_para
ABresult
0X0
100
111
197 {
198 3 return 0;
199 }
200  
201 7 return 1;
202 }
203  
204 sub _is_setext_underline
205 146 ▶{
Subroutine _is_setext_underline: called CC=3 SCAR=11.0
206 146 my ($line) = @_;
207  
208 146 ▶ return 1 if $line =~ m/^=+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^=+$/u 3 143 146
209 143 ▶ return 1 if $line =~ m/^-+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^-+$/u 3 140 143
210  
211 140 return 0;
212 }
213  
214 sub _is_atx_heading
215 177 ▶{
Subroutine _is_atx_heading: called CC=3 SCAR=11.0
216 177 my ($line) = @_;
217  
218 177 ▶ return 1 if $line =~ m/^[ \t]*#{1,6}[ \t]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]*#{1,6}[ \t]/u 30 147 177
219 147 ▶ return 1 if $line =~ m/^[ \t]*#{1,6}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]*#{1,6}$/u 0 147 147
220  
221 147 return 0;
222 }
223  
224 sub _convert_atx_heading
225 30 ▶{
Subroutine _convert_atx_heading: called CC=5 SCAR=16.1
226 30 my ($self, $line) = @_;
227  
228 30 $line =~ m/^[ \t]*(#{1,6})[ \t]*(.*?)[ \t]*$/;
229  
230 30 my $hashes = $1;
231 30 ▶ my $text = $2 // '';
Condition50%1/2
$2 // ""
l!l
300
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
232  
233 30 my $level = length($hashes);
234 30 my $prefix = '=' x $level;
235  
236 30 $text =~ s/[ \t]+#+[ \t]*$//;
237 30 $text =~ s/[ \t]+$//;
238  
239 # Extract explicit anchor: <a name="id"></a> or <a id="id"></a>
240 30 my $explicit_anchor;
241 30 ▶ if ($text =~ s/^<a\s+(?:name|id)\s*=\s*"([^"]+)"\s*><\/a>\s*//)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s[^<a\s+(?:name|id)\s*=\s*"([^"]+)"\s*></a>\s*][]u) 2 28 30
242 {
243 2 $explicit_anchor = $1;
244 }
245  
246 # Strip any remaining inline HTML from heading text (handled by inline)
247 30 $text = $self->{inline}->process($text);
248  
249 # Generate heading ID if auto_ids is enabled or if explicit anchor present
250 30 my @heading_out;
251 30 ▶ if ($explicit_anchor)
Branch: 2 branches
BranchTrueFalseTotal
if ($explicit_anchor) { } 2 28 30
elsif ($self->{'options'}{'auto_ids'}) { } 4 24 28
252 {
253 2 push @heading_out, "[[$explicit_anchor]]";
254 2 $self->{refs}->register_heading($explicit_anchor, $text);
255 }
256 elsif ($self->{options}{auto_ids})
257 {
258 4 my $id = $self->{refs}->generate_id($text);
259 4 push @heading_out, "[[$id]]";
260 4 $self->{refs}->register_heading($id, $text);
261 }
262  
263 30 push @heading_out, $prefix . ' ' . $text;
264  
265 30 return join("\n", @heading_out);
266 }
267  
268 sub _is_ref_definition
269 178 ▶{
Subroutine _is_ref_definition: called CC=2 SCAR=6.9
270 178 my ($line) = @_;
271  
272 178 ▶ return 1 if $line =~ m/^[ \t]*\[[^\]]+\]:[ \t]*\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]*\[[^\]]+\]:[ \t]*\S/u 4 174 178
273  
274 174 return 0;
275 }
276  
277 sub _parse_ref_definition
278 4 ▶{
Subroutine _parse_ref_definition: called CC=2 SCAR=7.1
279 4 my ($self, $line) = @_;
280  
281 4 ▶ if ($line =~ m/^[ \t]*\[([^\]]+)\]:[ \t]*(\S+)(?:[ \t]+"([^"]*)")?/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]*\[([^\]]+)\]:[ \t]*(\S+)(?:[ \t]+"([^"]*)")?/u) 4 0 4
282 {
283 4 my $label = $1;
284 4 my $url = $2;
285 4 my $title = $3;
286 4 $self->{refs}->add_link_def($label, $url, $title);
287 4 return 1;
288 }
289  
290 0 return 0;
291 }
292  
293 #===========================================================================
294 # Footnote definition detection and parsing
295 #===========================================================================
296  
297 # Check if a line is a footnote definition: [^label]: text
298 sub _is_footnote_definition
299 188 ▶{
Subroutine _is_footnote_definition: called CC=2 SCAR=6.9
300 188 my ($line) = @_;
301  
302 188 ▶ return 1 if $line =~ m/^[ \t]*\[\^[^\]]+\]:[ \t]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]*\[\^[^\]]+\]:[ \t]/u 10 178 188
303 178 return 0;
304 }
305  
306 # Parse a footnote definition and its multi-line continuations.
307 # Continuation lines are indented by 4+ spaces.
308 # Returns { label, text, next_idx }
309 sub _parse_footnote_definition
310 10 ▶{
Subroutine _parse_footnote_definition: called CC=5 SCAR=16.1
311 10 my ($self, $lines, $start_idx) = @_;
312  
313 10 my $i = $start_idx;
314 10 my $line = $lines->[$i];
315  
316 10 my ($label, $text);
317 10 ▶ if ($line =~ m/^[ \t]*\[\^([^\]]+)\]:[ \t]+(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]*\[\^([^\]]+)\]:[ \t]+(.*)/u) { } 10 0 10
318 {
319 10 $label = $1;
320 10 $text = $2;
321 }
322 else
323 {
324 0 return undef;
325 }
326  
327 10 $text =~ s/[ \t]+$//;
328 10 $i++;
329  
330 # Collect continuation lines (indented by 4+ spaces)
331 10 my @continuation;
332 10 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 3 9 12
333 {
334 3 my $cl = $lines->[$i];
335 3 ▶ if ($cl =~ m/^[ \t]{4,}(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cl =~ /^[ \t]{4,}(.*)/u) 2 1 3
336 {
337 2 my $cont_text = $1;
338 2 $cont_text =~ s/[ \t]+$//;
339 2 push @continuation, $cont_text;
340 2 $i++;
341 2 next;
342 }
343 1 last;
344 }
345  
346 10 ▶ if (@continuation)
Branch: 1 branch
BranchTrueFalseTotal
if (@continuation) 1 9 10
347 {
348 1 $text .= ' ' . join(' ', @continuation);
349 }
350  
351 return {
352 10 label => $label,
353 text => $text,
354 next_idx => $i,
355 };
356 }
357  
358 sub _emit_paragraph
359 113 ▶{
Subroutine _emit_paragraph: called CC=4 SCAR=13.9
360 113 my ($self, $para_lines) = @_;
361  
362 113 my @processed;
363 113 for my $line (@$para_lines)
364 {
365 130 ▶ if ($line =~ s/[ \t]{2,}$//)
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ s/[ \t]{2,}$//u) { } 2 128 130
elsif ($line =~ s/\\$//u) { } 1 127 128
366 {
367 2 push @processed, $line . ' +';
368 }
369 elsif ($line =~ s/\\$//)
370 {
371 1 push @processed, $line . ' +';
372 }
373 else
374 {
375 127 push @processed, $line;
376 }
377 }
378  
379 113 my $text = join("\n", @processed);
380 113 $text = $self->{inline}->process($text);
381  
382 113 return $text;
383 }
384  
385 sub _is_block_image
386 115 ▶{
Subroutine _is_block_image: called CC=4 SCAR=13.9
387 115 my ($para_lines) = @_;
388  
389 115 ▶ return 0 unless @$para_lines == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$para_lines == 1 10 105 115
390  
391 105 my $line = $para_lines->[0];
392  
393 105 ▶ return 1 if $line =~ m/^!\[.*\]\([^)]+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^!\[.*\]\([^)]+\)$/u 2 103 105
394 103 ▶ return 1 if $line =~ m/^!\[.*\]\[[^\]]*\]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^!\[.*\]\[[^\]]*\]$/u 0 103 103
395  
396 103 return 0;
397 }
398  
399 sub _convert_block_image
400 2 ▶{
Subroutine _convert_block_image: called CC=3 SCAR=14.8
401 2 my ($self, $line) = @_;
402  
403 2 ▶ if ($line =~ m/^!\[(.*)\]\(([^)]+)\)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^!\[(.*)\]\(([^)]+)\)$/u) 2 0 2
404 {
405 2 my $alt = $1;
406 2 my $src = $2;
407 2 return "image::$src\[$alt\]";
408 }
409  
410 0 my $processed = $self->{inline}->process($line);
411  
412 0 ▶ if ($processed =~ m/^\x00REFIMG:([^:]+):([^\x00]*)\x00$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($processed =~ /^\x00REFIMG:([^:]+):([^\x00]*)\x00$/u) 0 0 0
413 {
414 0 return $processed;
415 }
416  
417 0 return $processed;
418 }
419  
420 #===========================================================================
421 # Fenced code block detection
422 #===========================================================================
423  
424 sub _is_opening_fence
425 273 ▶{
Subroutine _is_opening_fence: called CC=3 SCAR=11.0
426 273 my ($line) = @_;
427  
428 273 ▶ return 1 if $line =~ m/^[ \t]*(\x60{3,})(\S*)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]*(\x60{3,})(\S*)\s*$/u 13 260 273
429 260 ▶ return 1 if $line =~ m/^[ \t]*(~{3,})(\S*)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[ \t]*(~{3,})(\S*)\s*$/u 1 259 260
430  
431 259 return 0;
432 }
433  
434 sub _get_fence_info
435 14 ▶{
Subroutine _get_fence_info: called CC=3 SCAR=11.2
436 14 my ($line) = @_;
437  
438 14 ▶ if ($line =~ m/^[ \t]*(\x60{3,})(\S*)\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]*(\x60{3,})(\S*)\s*$/u) 13 1 14
439 {
440 13 return ('`', length($1), $2);
441 }
442 1 ▶ if ($line =~ m/^[ \t]*(~{3,})(\S*)\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]*(~{3,})(\S*)\s*$/u) 1 0 1
443 {
444 1 return ('~', length($1), $2);
445 }
446  
447 0 return (undef, 0, '');
448 }
449  
450 sub _is_diagram_language
451 9 ▶{
Subroutine _is_diagram_language: called CC=4 SCAR=13.9
452 9 my ($self, $lang) = @_;
453  
454 9 ▶ return 0 unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 0 9 9
455  
456 9 my $lc = lc($lang);
457 9 for my $dl (@{$self->{diagram_languages}})
458 {
459 17 ▶ return 1 if lc($dl) eq $lc;
Branch: 1 branch
BranchTrueFalseTotal
if lc $dl eq $lc 2 15 17
460 }
461  
462 7 return 0;
463 }
464  
465 #===========================================================================
466 # Blockquote processing
467 #===========================================================================
468  
469 sub _count_blockquote_depth
470 18 ▶{
Subroutine _count_blockquote_depth: called CC=5 SCAR=16.1
471 18 my ($line) = @_;
472  
473 18 my $depth = 0;
474 18 my $pos = 0;
475 18 my $len = length($line);
476  
477 18 ▶ while ($pos < $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 37 2 39
478 {
479 37 my $ch = substr($line, $pos, 1);
480 37 ▶ if ($ch eq '>')
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq '>') { } 21 16 37
481 {
482 21 $depth++;
483 21 $pos++;
484 21 ▶ if ($pos < $len && substr($line, $pos, 1) eq ' ')
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($line, $pos, 1) eq " ") 19 2 21
Condition66%2/3
$pos < $len and substr($line, $pos, 1) eq " "
!ll&&!rl&&r
2019
MC/DC50%1/2
$pos < $len and substr($line, $pos, 1) eq " "
$pos < $len substr($line, $pos, 1) eq " "
Truth table66%2/3
A and B
A= $pos < $lenB= substr($line, $pos, 1) eq " "
ABresult
0X0
100
111
485 {
486 19 $pos++;
487 }
488 }
489 else
490 {
491 16 last;
492 }
493 }
494  
495 18 return $depth;
496 }
497  
498 sub _parse_blockquote
499 12 ▶{
Subroutine _parse_blockquote: called CC=16 SCAR=30.8
500 12 my ($self, $lines, $start_idx) = @_;
501  
502 12 my @content_lines;
503 12 my $i = $start_idx;
504 12 my $depth = 0;
505 12 my $first = 1;
506  
507 12 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 21 9 30
508 {
509 21 my $line = $lines->[$i];
510  
511 21 ▶ if ($line eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 3 18 21
512 {
513 3 my $j = $i + 1;
514 3 ▶ while ($j < @$lines && $lines->[$j] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$lines and $lines->[$j] eq "") 0 3 3
Condition33%1/3
$j < @$lines and $lines->[$j] eq ""
!ll&&!rl&&r
030
MC/DC0%0/2
$j < @$lines and $lines->[$j] eq ""
$j < @$lines $lines->[$j] eq ""
Truth table33%1/3
A and B
A= $j < @$linesB= $lines->[$j] eq ""
ABresult
0X0
100
111
515 {
516 0 $j++;
517 }
518 3 ▶ if ($j < @$lines && $lines->[$j] =~ m/^>/)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$lines and $lines->[$j] =~ /^>/u) 0 3 3
Condition33%1/3
$j < @$lines and $lines->[$j] =~ /^>/u
!ll&&!rl&&r
030
MC/DC0%0/2
$j < @$lines and $lines->[$j] =~ /^>/u
$j < @$lines $lines->[$j] =~ /^>/u
Truth table33%1/3
A and B
A= $j < @$linesB= $lines->[$j] =~ /^>/u
ABresult
0X0
100
111
519 {
520 0 push @content_lines, '';
521 0 $i++;
522 0 next;
523 }
524 3 last;
525 }
526  
527 18 ▶ if ($line =~ m/^>/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^>/u) 18 0 18
528 {
529 18 push @content_lines, $line;
530 18 $i++;
531 18 next;
532 }
533  
534 0 last;
535 }
536  
537 # Determine minimum depth across all non-blank lines
538 12 my $min_depth = undef;
539 12 for my $rl (@content_lines)
540 {
541 18 ▶ next if $rl eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rl eq "" 0 18 18
542 18 my $d = _count_blockquote_depth($rl);
543 18 ▶ $min_depth = $d if !defined($min_depth) || $d < $min_depth;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min_depth or $d < $min_depth 12 6 18
Condition66%2/3
not defined $min_depth or $d < $min_depth
l!l&&r!l&&!r
1206
MC/DC50%1/2
not defined $min_depth or $d < $min_depth
not defined $min_depth $d < $min_depth
Truth table66%2/3
not(A) or B
A= not defined $min_depthB= $d < $min_depth
ABresult
1X1
011
000
544 }
545 12 ▶ $min_depth //= 1;
Condition50%1/2
$min_depth //= 1
l!l
120
MC/DC0%0/1
$min_depth //= 1
$min_depth
Truth table50%1/2
A
A= $min_depth
Aresult
11
00
546  
547 # Strip min_depth levels of '>' from every non-blank line
548 12 my @stripped;
549 12 for my $rl (@content_lines)
550 {
551 18 ▶ if ($rl eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($rl eq "") 0 18 18
552 {
553 0 push @stripped, '';
554 0 next;
555 }
556 18 my $s = $rl;
557 18 for (1 .. $min_depth)
558 {
559 20 $s =~ s/^>[ ]?//;
560 }
561 18 push @stripped, $s;
562 }
563  
564 12 return {lines => \@stripped, depth => $min_depth, next_idx => $i};
565 }
566  
567 #===========================================================================
568 # List processing
569 #===========================================================================
570  
571 sub _get_list_item_info
572 244 ▶{
Subroutine _get_list_item_info: called CC=7 SCAR=19.6
573 244 my ($line) = @_;
574  
575 # Unordered (with optional task checkbox)
576 244 ▶ if ($line =~ m/^([ \t]*)([-*+])([ \t]+)(\[[ xX]\]\s*)?(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([ \t]*)([-*+])([ \t]+)(\[[ xX]\]\s*)?(.*)/u) 39 205 244
577 {
578 39 my $leading = $1;
579 39 ▶ my $info = {
Condition50%1/2
$5 // ""
l!l
390
MC/DC0%0/1
$5 // ""
$5
Truth table50%1/2
A
A= $5
Aresult
11
00
580 type => 'ul',
581 indent => length($leading),
582 marker => $2,
583 text => $5 // '',
584 task => '',
585 };
586 39 ▶ if (defined $4 && $4 ne '')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $4 and $4 ne "") 3 36 39
Condition66%2/3
defined $4 and $4 ne ""
!ll&&!rl&&r
3603
MC/DC50%1/2
defined $4 and $4 ne ""
defined $4 $4 ne ""
Truth table66%2/3
A and B
A= defined $4B= $4 ne ""
ABresult
0X0
100
111
587 {
588 3 my $cb = $4;
589 3 $cb =~ s/\s+$//;
590 3 $info->{task} = $cb;
591 }
592 39 return $info;
593 }
594  
595 # Ordered
596 205 ▶ if ($line =~ m/^([ \t]*)(\d+)([.)])([ \t]+)(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([ \t]*)(\d+)([.)])([ \t]+)(.*)/u) 11 194 205
597 {
598 11 my $leading = $1;
599 return {
600 11 ▶ type => 'ol',
Condition50%1/2
$5 // ""
l!l
110
MC/DC0%0/1
$5 // ""
$5
Truth table50%1/2
A
A= $5
Aresult
11
00
601 indent => length($leading),
602 marker => $2 . $3,
603 text => $5 // '',
604 task => '',
605 };
606 }
607  
608 194 return undef;
609 }
610  
611 sub _get_list_depth
612 42 ▶{
Subroutine _get_list_depth: called CC=1 SCAR=0.0
613 42 my ($indent) = @_;
614  
615 42 return int($indent / 2);
616 }
617  
618 #===========================================================================
619 # Admonition detection
620 #===========================================================================
621  
622 sub _is_admonition_label
623 177 ▶{
Subroutine _is_admonition_label: called CC=3 SCAR=11.0
624 177 my ($text) = @_;
625  
626 177 my %labels = (
627 'note' => 'NOTE',
628 'tip' => 'TIP',
629 'important' => 'IMPORTANT',
630 'warning' => 'WARNING',
631 'caution' => 'CAUTION',
632 );
633  
634 177 ▶ if ($text =~ m/^\*\*([Nn]ote|[Tt]ip|[Ii]mportant|[Ww]arning|[Cc]aution):\*\*\s*(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^\*\*([Nn]ote|[Tt]ip|[Ii]mportant|[Ww]arning|[Cc]aution):\*\*\s*(.*)/u) 2 175 177
635 {
636 2 my $label = lc($1);
637 2 my $rest = $2;
638 2 return ($labels{$label}, $rest);
639 }
640  
641 175 ▶ if ($text =~ m/^([Nn]ote|[Tt]ip|[Ii]mportant|[Ww]arning|[Cc]aution):\s*(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^([Nn]ote|[Tt]ip|[Ii]mportant|[Ww]arning|[Cc]aution):\s*(.*)/u) 1 174 175
642 {
643 1 my $label = lc($1);
644 1 my $rest = $2;
645 1 return ($labels{$label}, $rest);
646 }
647  
648 174 return ();
649 }
650  
651 #===========================================================================
652 # Process inline text for list items — must protect [ ] from ref link parsing
653 #===========================================================================
654  
655 sub _process_list_item_text
656 42 ▶{
Subroutine _process_list_item_text: called CC=2 SCAR=6.9
657 42 my ($self, $info) = @_;
658  
659 42 my $text = $info->{text};
660  
661 # Process text content through inline formatting first
662 42 $text = $self->{inline}->process($text);
663  
664 # Prepend task checkbox after inline processing to avoid
665 # [ ] / [x] being consumed by reference link matching in Inline.pm.
666 # Normalize [X] → [x] for consistency.
667 42 ▶ if ($info->{task} ne '')
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"task"} ne "") 3 39 42
668 {
669 3 my $task = $info->{task};
670 3 $task =~ s/\[X\]/[x]/;
671 3 $text = $task . ' ' . $text;
672 }
673  
674 42 return $text;
675 }
676  
677 #===========================================================================
678 # Build an AsciiDoc list marker from the list stack
679 #===========================================================================
680  
681 sub _build_list_marker
682 42 ▶{
Subroutine _build_list_marker: called CC=2 SCAR=6.9
683 42 my ($list_stack) = @_;
684  
685 # Build AsciiDoc marker: repeat the current type's character
686 # for the total nesting depth (stack size).
687 # E.g., ul at depth 2 → '**', ol at depth 2 → '..'
688 42 my $depth = scalar @$list_stack;
689 42 my $type = $list_stack->[-1]{type};
690  
691 42 ▶ my $ch = ($type eq 'ul') ? '*' : '.';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'ul' ? : 33 9 42
692 42 return $ch x $depth;
693 }
694  
695 #===========================================================================
696 # GFM Table detection and conversion
697 #===========================================================================
698  
699 # Check if a line looks like a table header (starts and ends with |)
700 sub _is_table_row
701 294 ▶{
Subroutine _is_table_row: called CC=3 SCAR=11.0
702 294 my ($line) = @_;
703  
704 294 ▶ return 1 if $line =~ m/^\|.*\|$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\|.*\|$/u 21 273 294
705 273 ▶ return 1 if $line =~ m/^\|/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\|/u 0 273 273
706  
707 273 return 0;
708 }
709  
710 # Check if a line is a table delimiter row (only |, -, :, spaces)
711 sub _is_table_delimiter
712 7 ▶{
Subroutine _is_table_delimiter: called CC=2 SCAR=8.1
713 7 my ($line) = @_;
714  
715 7 ▶ return 1 if $line =~ m/^\|[-\s:|]+\|$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\|[-\s:|]+\|$/u 7 0 7
716  
717 0 return 0;
718 }
719  
720 # Parse a table row into cells, stripping leading/trailing whitespace
721 sub _parse_table_cells
722 21 ▶{
Subroutine _parse_table_cells: called CC=2 SCAR=6.9
723 21 my ($line) = @_;
724  
725 # Remove leading and trailing |
726 21 my $inner = $line;
727 21 $inner =~ s/^\|//;
728 21 $inner =~ s/\|$//;
729  
730 # Split on | (not on escaped \|)
731 21 my @cells = split /(?<!\\)\|/, $inner;
732  
733 # Strip leading/trailing whitespace and escape pipes
734 21 for my $cell (@cells)
735 {
736 45 $cell =~ s/^[ \t]+|[ \t]+$//g;
737 45 $cell =~ s/\\\|/|/g;
738 }
739  
740 21 return @cells;
741 }
742  
743 # Determine column alignment from delimiter cell
744 sub _parse_alignment
745 15 ▶{
Subroutine _parse_alignment: called CC=7 SCAR=19.5
746 15 my ($cell) = @_;
747  
748 15 my $trimmed = $cell;
749 15 $trimmed =~ s/^[ \t]+|[ \t]+$//g;
750  
751 15 ▶ my $left = ($trimmed =~ m/^:/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$trimmed =~ /^:/u ? : 2 13 15
752 15 ▶ my $right = ($trimmed =~ m/:$/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$trimmed =~ /:$/u ? : 2 13 15
753  
754 15 ▶ if ($left && $right)
Branch: 3 branches
BranchTrueFalseTotal
if ($left and $right) { } 1 14 15
elsif ($right) { } 1 13 14
elsif ($left) { } 1 12 13
Condition100%3/3
$left and $right
!ll&&!rl&&r
1311
MC/DC100%2/2
$left and $right
$left $right
Truth table100%3/3
A and B
A= $leftB= $right
ABresult
0X0
100
111
755 {
756 1 return '^';
757 }
758 elsif ($right)
759 {
760 1 return '>';
761 }
762 elsif ($left)
763 {
764 1 return '<';
765 }
766  
767 # Default
768 12 return '<';
769 }
770  
771 # Check if a sequence of lines starting at index i forms a GFM table
772 # Returns { header_cells, alignments, body_rows, next_idx } or undef
773 sub _parse_table
774 7 ▶{
Subroutine _parse_table: called CC=11 SCAR=25.7
775 7 my ($self, $lines, $start_idx) = @_;
776  
777 7 my $i = $start_idx;
778  
779 # Must have at least header + delimiter rows
780 7 ▶ return undef unless $i + 1 < @$lines;
Branch: 1 branch
BranchTrueFalseTotal
unless $i + 1 < @$lines 0 7 7
781  
782 # First line must be a table row (potential header)
783 7 ▶ return undef unless _is_table_row($lines->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_table_row($lines->[$i]) 0 7 7
784  
785 7 my $header_line = $lines->[$i];
786 7 $i++;
787  
788 # Second line must be a delimiter row
789 7 ▶ return undef unless $i < @$lines && _is_table_delimiter($lines->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless $i < @$lines and _is_table_delimiter($lines->[$i]) 0 7 7
Condition33%1/3
$i < @$lines and _is_table_delimiter($lines->[$i])
!ll&&!rl&&r
007
MC/DC0%0/2
$i < @$lines and _is_table_delimiter($lines->[$i])
$i < @$lines _is_table_delimiter($lines->[$i])
Truth table33%1/3
A and B
A= $i < @$linesB= _is_table_delimiter($lines->[$i])
ABresult
0X0
100
111
790  
791 7 my $delimiter_line = $lines->[$i];
792 7 $i++;
793  
794 7 my @header_cells = _parse_table_cells($header_line);
795 7 my @delim_cells = _parse_table_cells($delimiter_line);
796  
797 # Column count must match
798 7 ▶ return undef unless @header_cells == @delim_cells;
Branch: 1 branch
BranchTrueFalseTotal
unless @header_cells == @delim_cells 0 7 7
799  
800 7 my @alignments;
801 7 for my $dc (@delim_cells)
802 {
803 15 push @alignments, _parse_alignment($dc);
804 }
805  
806 # Collect body rows
807 7 my @body_rows;
808 7 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 7 7 14
809 {
810 7 my $row_line = $lines->[$i];
811  
812 7 ▶ if ($row_line eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($row_line eq "") 0 7 7
813 {
814 0 $i++;
815 0 last;
816 }
817  
818 7 ▶ return undef unless _is_table_row($row_line);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_table_row($row_line) 0 7 7
819  
820 7 my @cells = _parse_table_cells($row_line);
821  
822 # Allow rows with fewer cells than header (pad with empty)
823 7 ▶ while (@cells < @header_cells)
Branch: 1 branch
BranchTrueFalseTotal
if (@cells < @header_cells) 0 7 7
824 {
825 0 push @cells, '';
826 }
827  
828 7 push @body_rows, \@cells;
829 7 $i++;
830 }
831  
832 return {
833 7 header_cells => \@header_cells,
834 alignments => \@alignments,
835 body_rows => \@body_rows,
836 next_idx => $i,
837 };
838 }
839  
840 # Convert parsed table to AsciiDoc
841 sub _convert_table
842 7 ▶{
Subroutine _convert_table: called CC=8 SCAR=20.8
843 7 my ($self, $table) = @_;
844  
845 7 my $header_cells = $table->{header_cells};
846 7 my $alignments = $table->{alignments};
847 7 my $body_rows = $table->{body_rows};
848 7 my $num_cols = scalar @$header_cells;
849  
850 7 my @out;
851  
852 # Determine if we need [cols=...]
853 7 my $all_default = 1;
854 7 for my $a (@$alignments)
855 {
856 14 ▶ if ($a ne '<')
Branch: 1 branch
BranchTrueFalseTotal
if ($a ne "<") 1 13 14
857 {
858 1 $all_default = 0;
859 1 last;
860 }
861 }
862  
863 # Only emit [cols=...] when not all default AND more than 1 column
864 7 ▶ if (!$all_default && $num_cols > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (not $all_default and $num_cols > 1) 1 6 7
Condition66%2/3
not $all_default and $num_cols > 1
!ll&&!rl&&r
601
MC/DC50%1/2
not $all_default and $num_cols > 1
not $all_default $num_cols > 1
Truth table66%2/3
not(A) and B
A= not $all_defaultB= $num_cols > 1
ABresult
0X0
100
111
865 {
866 1 my $cols = '"' . join(',', @$alignments) . '"';
867 1 push @out, "[cols=$cols]";
868 }
869  
870 7 push @out, '|===';
871  
872 # Header row (compact: all cells on one line)
873 7 my @header_processed;
874 7 for my $cell (@$header_cells)
875 {
876 15 my $processed = $self->{inline}->process($cell);
877 15 push @header_processed, $processed;
878 }
879 7 push @out, '| ' . join(' | ', @header_processed);
880  
881 # Blank line between header and first body row (required by Asciidoctor)
882 7 push @out, '';
883  
884 # Body rows (compact format)
885 7 for my $row (@$body_rows)
886 {
887 7 my @row_processed;
888 7 for my $cell (@$row)
889 {
890 15 my $processed = $self->{inline}->process($cell);
891 15 $processed =~ s/\|/\\\|/g;
892 15 push @row_processed, $processed;
893 }
894 7 push @out, '| ' . join(' | ', @row_processed);
895 }
896  
897 7 push @out, '|===';
898  
899 7 return join("\n", @out);
900 }
901  
902 #===========================================================================
903 # Block-level HTML detection and conversion
904 #===========================================================================
905  
906 # Check if a line starts a block-level HTML element or comment
907 sub _is_block_html_start
908 287 ▶{
Subroutine _is_block_html_start: called CC=7 SCAR=21.1
909 287 my ($line) = @_;
910  
911 # HTML comment
912 287 ▶ return 1 if $line =~ m/^<!--/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^<!--/u 3 284 287
913  
914 # Only treat actual block-level tags as block HTML
915 # Inline tags (even if at line start) go through paragraph/inline processing
916 284 my %block_tags = map { $_ => 1 } qw(
917 div details table script iframe embed
918 pre nav section article header footer main aside
919 form fieldset figure figcaption ol ul dl
920 );
921  
922 # Must be a real HTML tag: after the tag name, next char must be
923 # space, >, /, or /; — not a character that would make it a URL or text.
924 284 ▶ if ($line =~ m/^<(\/?)([a-zA-Z][a-zA-Z0-9]*)(?:\s|>|\/>|$)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^<(/?)([a-zA-Z][a-zA-Z0-9]*)(?:\s|>|/>|$)]u) 7 277 284
925 {
926 7 my $closing = $1;
927 7 my $tag = lc($2);
928  
929 7 ▶ return 1 if $block_tags{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $block_tags{$tag} 2 5 7
930  
931 # Also treat unknown/not-inline tags as block HTML
932 # Inline tags: known inline elements or tags already handled by Inline.pm
933 5 my %inline_tags = map { $_ => 1 } qw(
934 br strong b em i code del s mark sup sub
935 a span small abbr cite dfn kbd q samp var
936 img input select textarea button label
937 );
938  
939 5 ▶ return 0 if $inline_tags{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $inline_tags{$tag} 5 0 5
940 0 ▶ return 0 if $tag =~ m/^h[1-6]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^h[1-6]$/u 0 0 0
941  
942 # Closing tags of block elements
943 0 ▶ return 1 if $closing;
Branch: 1 branch
BranchTrueFalseTotal
if $closing 0 0 0
944 0 return 1;
945 }
946  
947 277 return 0;
948 }
949  
950 # Parse a block HTML element or comment, consuming lines
951 # Returns { type, content, next_idx }
952 sub _parse_block_html
953 5 ▶{
Subroutine _parse_block_html: called CC=18 SCAR=31.4
954 5 my ($self, $lines, $start_idx) = @_;
955  
956 5 my $i = $start_idx;
957 5 my $line = $lines->[$i];
958  
959 # HTML comment
960 5 ▶ if ($line =~ m/^<!--/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^<!--/u) 3 2 5
961 {
962 # Check if it's a single-line comment
963 3 ▶ if ($line =~ m/^<!--\s*(.*?)\s*-->$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^<!--\s*(.*?)\s*-->$/u) 2 1 3
964 {
965 2 my $content = $1;
966  
967 # Check for ! prefix (directive)
968 2 ▶ if ($content =~ s/^!\s*//)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/^!\s*//u) 1 1 2
969 {
970 1 return {type => 'comment_directive', content => $content, next_idx => $i + 1};
971 }
972  
973 1 return {type => 'comment_single', content => $content, next_idx => $i + 1};
974 }
975  
976 # Multi-line comment: content between <!-- and -->
977 1 my @comment_lines;
978  
979 # Extract content after <!--
980 1 my $stripped = $line;
981 1 $stripped =~ s/^<!--\s*//;
982 1 ▶ if ($stripped =~ s/\s*-->$//)
Branch: 1 branch
BranchTrueFalseTotal
if ($stripped =~ s/\s*-->$//u) 0 1 1
983 {
984 # Comment ends on same line
985 0 return {type => 'comment_single', content => $stripped, next_idx => $i + 1};
986 }
987 1 ▶ push @comment_lines, $stripped if $stripped ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $stripped ne "" 0 1 1
988 1 $i++;
989  
990 1 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 3 0 3
991 {
992 3 my $cl = $lines->[$i];
993 3 ▶ if ($cl =~ m/^(.*?)\s*-->$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cl =~ /^(.*?)\s*-->$/u) 1 2 3
994 {
995 1 my $last = $1;
996 1 ▶ push @comment_lines, $last if $last ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $last ne "" 0 1 1
997 1 $i++;
998 1 last;
999 }
1000 2 push @comment_lines, $cl;
1001 2 $i++;
1002 }
1003  
1004 1 my $content = join("\n", @comment_lines);
1005  
1006 # Check for ! prefix on first line (directive)
1007 1 ▶ if (@comment_lines && $comment_lines[0] =~ s/^!\s*//)
Branch: 1 branch
BranchTrueFalseTotal
if (@comment_lines and $comment_lines[0] =~ s/^!\s*//u) 0 1 1
Condition33%1/3
@comment_lines and $comment_lines[0] =~ s/^!\s*//u
!ll&&!rl&&r
010
MC/DC0%0/2
@comment_lines and $comment_lines[0] =~ s/^!\s*//u
@comment_lines $comment_lines[0] =~ s/^!\s*//u
Truth table33%1/3
A and B
A= @comment_linesB= $comment_lines[0] =~ s/^!\s*//u
ABresult
0X0
100
111
1008 {
1009 0 $comment_lines[0] = $comment_lines[0];
1010 0 $content = join("\n", @comment_lines);
1011  
1012 # Single-line after all: if only one line, use // ; otherwise ////
1013 0 ▶ if (@comment_lines == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@comment_lines == 1) 0 0 0
1014 {
1015 0 return {type => 'comment_directive', content => $content, next_idx => $i};
1016 }
1017 0 return {type => 'comment_directive', content => $content, next_idx => $i};
1018 }
1019  
1020 1 ▶ if (@comment_lines == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@comment_lines == 1) 0 1 1
1021 {
1022 0 return {type => 'comment_single', content => $content, next_idx => $i};
1023 }
1024  
1025 1 return {type => 'comment_multi', content => $content, next_idx => $i};
1026 }
1027  
1028 # Block HTML element (div, script, table, etc.)
1029 2 my @html_lines;
1030 2 push @html_lines, $line;
1031 2 $i++;
1032  
1033 # Collect until blank line or matching closing tag
1034 # Simple heuristic: collect lines until we see a blank line
1035 # or a closing tag on its own line that matches depth
1036 2 my $tag_name;
1037 2 ▶ if ($line =~ m/^<([a-zA-Z][a-zA-Z0-9]*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^<([a-zA-Z][a-zA-Z0-9]*)/u) 2 0 2
1038 {
1039 2 $tag_name = $1;
1040 }
1041  
1042 2 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 4 0 4
1043 {
1044 4 my $hl = $lines->[$i];
1045  
1046 4 ▶ if ($hl eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($hl eq "") 0 4 4
1047 {
1048 0 last;
1049 }
1050  
1051 # Check for closing tag
1052 4 ▶ if ($tag_name && $hl =~ m/^<\/$tag_name>/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_name and $hl =~ m[^</$tag_name>]u) 2 2 4
Condition66%2/3
$tag_name and $hl =~ m[^</$tag_name>]u
!ll&&!rl&&r
022
MC/DC50%1/2
$tag_name and $hl =~ m[^</$tag_name>]u
$tag_name $hl =~ m[^</$tag_name>]u
Truth table66%2/3
A and B
A= $tag_nameB= $hl =~ m[^</$tag_name>]u
ABresult
0X0
100
111
1053 {
1054 2 push @html_lines, $hl;
1055 2 $i++;
1056 2 last;
1057 }
1058  
1059 2 push @html_lines, $hl;
1060 2 $i++;
1061 }
1062  
1063 return {
1064 2 type => 'html_block',
1065 content => join("\n", @html_lines),
1066 next_idx => $i,
1067 };
1068 }
1069  
1070 #===========================================================================
1071 # Definition list detection and conversion
1072 #===========================================================================
1073  
1074 # Check if a line is a definition list bold term with colons form
1075 # Returns { term, level } or undef
1076 sub _is_def_list_bold_term
1077 211 ▶{
Subroutine _is_def_list_bold_term: called CC=2 SCAR=6.9
1078 211 my ($line) = @_;
1079  
1080 # Match **term** with 2+ colons and optional space before definition
1081 211 ▶ if ($line =~ m/^\*\*(.+?)\*\*(:{2,})\s*(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\*\*(.+?)\*\*(:{2,})\s*(.*)/u) 3 208 211
1082 {
1083 3 my $term = $1;
1084 3 my $colons = $2;
1085 3 my $def = $3;
1086 return {
1087 3 term => $term,
1088 level => length($colons),
1089 def => $def,
1090 };
1091 }
1092  
1093 208 return undef;
1094 }
1095  
1096 # Check if the next line(s) start with ': ' (definition list continuation)
1097 sub _is_def_list_standard
1098 4 ▶{
Subroutine _is_def_list_standard: called CC=3 SCAR=11.3
1099 4 my ($lines, $start_idx) = @_;
1100  
1101 4 ▶ return 0 if $start_idx >= @$lines;
Branch: 1 branch
BranchTrueFalseTotal
if $start_idx >= @$lines 0 4 4
1102  
1103 # Next line must start with ': '
1104 4 my $next = $lines->[$start_idx];
1105 4 ▶ return 1 if $next =~ m/^:[ \t]/;
Branch: 1 branch
BranchTrueFalseTotal
if $next =~ /^:[ \t]/u 0 4 4
1106  
1107 4 return 0;
1108 }
1109  
1110 # Parse a standard definition list entry
1111 # Returns { term, definitions, next_idx }
1112 sub _parse_def_list_standard
1113 2 ▶{
Subroutine _parse_def_list_standard: called CC=3 SCAR=11.0
1114 2 my ($self, $lines, $start_idx) = @_;
1115  
1116 2 my $i = $start_idx;
1117  
1118 # First line is the term
1119 2 my $term = $lines->[$i];
1120 2 $i++;
1121  
1122 # Collect definition lines (starting with ': ')
1123 2 my @defs;
1124 2 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 3 2 5
1125 {
1126 3 my $dl = $lines->[$i];
1127  
1128 3 ▶ if ($dl =~ m/^:[ \t]+(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($dl =~ /^:[ \t]+(.*)/u) 3 0 3
1129 {
1130 3 push @defs, $1;
1131 3 $i++;
1132 3 next;
1133 }
1134  
1135 0 last;
1136 }
1137  
1138 return {
1139 2 term => $term,
1140 definitions => \@defs,
1141 next_idx => $i,
1142 };
1143 }
1144  
1145 #===========================================================================
1146 # Main body parser
1147 #===========================================================================
1148  
1149 sub _parse_body
1150 192 ▶{
Subroutine _parse_body: called CC=118 SCAR=51.6
1151 192 my ($self, $lines) = @_;
1152  
1153 192 my @out;
1154 my @para;
1155 192 my $prev_was_para = 0;
1156 192 my $in_list = 0;
1157 192 my @list_stack; # {type, depth}
1158 my @list_items; # Collected list item strings
1159 192 my $list_is_loose = 0; # Whether current list has blank lines between items
1160  
1161 375 ▶ my $flush_para = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1162 375 ▶ if (@para)
Branch: 1 branch
BranchTrueFalseTotal
if (@para) 115 260 375
1163 {
1164 115 ▶ if (_is_block_image(\@para))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_block_image(\@para)) { } 2 113 115
1165 {
1166 2 push @out, $self->_convert_block_image($para[0]);
1167 }
1168 else
1169 {
1170 113 push @out, $self->_emit_paragraph(\@para);
1171 }
1172 115 @para = ();
1173 }
1174 192 };
1175  
1176 297 ▶ my $flush_list = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1177 297 ▶ if (@list_items)
Branch: 1 branch
BranchTrueFalseTotal
if (@list_items) 18 279 297
1178 {
1179 18 ▶ my $sep = $list_is_loose ? "\n\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$list_is_loose ? : 3 15 18
1180 18 push @out, join($sep, @list_items);
1181 18 @list_items = ();
1182 18 $list_is_loose = 0;
1183 }
1184 192 };
1185  
1186 192 my $i = 0;
1187 192 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 335 192 527
1188 {
1189 335 my $line = $lines->[$i];
1190  
1191 # --- Blank line ---
1192 335 ▶ if ($line eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 50 285 335
1193 {
1194 50 ▶ if ($in_list)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_list) 8 42 50
1195 {
1196 8 my $j = $i + 1;
1197 8 ▶ while ($j < @$lines && $lines->[$j] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$lines and $lines->[$j] eq "") 0 8 8
Condition33%1/3
$j < @$lines and $lines->[$j] eq ""
!ll&&!rl&&r
080
MC/DC0%0/2
$j < @$lines and $lines->[$j] eq ""
$j < @$lines $lines->[$j] eq ""
Truth table33%1/3
A and B
A= $j < @$linesB= $lines->[$j] eq ""
ABresult
0X0
100
111
1198 {
1199 0 $j++;
1200 }
1201 8 ▶ if ($j < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$lines) 8 0 8
1202 {
1203 8 my $next_line = $lines->[$j];
1204 8 my $next_info = _get_list_item_info($next_line);
1205  
1206 # Check if this blank line precedes a definition list
1207 8 ▶ if (!$next_info && !$in_list)
Branch: 1 branch
BranchTrueFalseTotal
unless ($next_info or $in_list) 0 8 8
Condition66%2/3
$next_info or $in_list
l!l&&r!l&&!r
440
MC/DC0%0/2
$next_info or $in_list
$next_info $in_list
Truth table66%2/3
A or B
A= $next_infoB= $in_list
ABresult
1X1
011
000
1208 {
1209 # Not in list context, let default blank-line handling run
1210 }
1211  
1212 8 ▶ if ($next_info)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_info) 4 4 8
1213 {
1214 # Loose list: mark as loose, skip blank
1215 4 $list_is_loose = 1;
1216 4 $i++;
1217 4 next;
1218 }
1219  
1220 # Check for definition list continuation after blank line
1221 # (definition lists can have blank lines between term and def)
1222 4 ▶ if (_is_def_list_standard($lines, $j))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_def_list_standard($lines, $j)) 0 4 4
1223 {
1224 # Blank line before a definition list entry — it's a new entry
1225 # handled by the def list detection below (when processing term)
1226 0 $flush_para->();
1227 0 $prev_was_para = 0;
1228 0 $i++;
1229 0 next;
1230 }
1231 }
1232  
1233 # End of list
1234 4 $flush_list->();
1235 4 $in_list = 0;
1236 4 @list_stack = ();
1237 4 $prev_was_para = 0;
1238 4 $i++;
1239 4 next;
1240 }
1241  
1242 42 $flush_para->();
1243 42 $prev_was_para = 0;
1244 42 $i++;
1245 42 next;
1246 }
1247  
1248 # --- HTML block (comment or block-level element) ---
1249 285 ▶ if (_is_block_html_start($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_block_html_start($line)) 5 280 285
1250 {
1251 5 $flush_para->();
1252 5 $flush_list->();
1253 5 $in_list = 0;
1254 5 @list_stack = ();
1255  
1256 5 my $html = $self->_parse_block_html($lines, $i);
1257 5 $i = $html->{next_idx};
1258  
1259 5 ▶ if ($html->{type} eq 'comment_single')
Branch: 4 branches
BranchTrueFalseTotal
if ($html->{'type'} eq 'comment_single') { } 1 4 5
elsif ($html->{'type'} eq 'comment_multi') { } 1 3 4
elsif ($html->{'type'} eq 'comment_directive') { } 1 2 3
elsif ($html->{'type'} eq 'html_block') { } 2 0 2
1260 {
1261 1 push @out, '// ' . $html->{content};
1262 }
1263 elsif ($html->{type} eq 'comment_multi')
1264 {
1265 1 push @out, "////\n" . $html->{content} . "\n////";
1266 }
1267 elsif ($html->{type} eq 'comment_directive')
1268 {
1269 1 my $content = $html->{content};
1270  
1271 # Single-line directive
1272 1 ▶ if ($content !~ m/\n/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $content =~ /\n/u) { } 1 0 1
1273 {
1274 1 push @out, '// ' . $content;
1275 }
1276 else
1277 {
1278 # Multi-line: strip ! prefix from first line
1279 0 my @dlines = split /\n/, $content;
1280 0 $dlines[0] =~ s/^!\s*//;
1281  
1282 # After stripping, if only one non-empty line, use //
1283 0 my @non_empty = grep { $_ ne '' } @dlines;
1284 0 ▶ if (@non_empty <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@non_empty <= 1) { } 0 0 0
1285 {
1286 0 push @out, '// ' . join(' ', @dlines);
1287 }
1288 else
1289 {
1290 0 push @out, "////\n" . join("\n", @dlines) . "\n////";
1291 }
1292 }
1293 }
1294 elsif ($html->{type} eq 'html_block')
1295 {
1296 2 push @out, "++++\n" . $html->{content} . "\n++++";
1297 }
1298  
1299 5 $prev_was_para = 0;
1300 5 next;
1301 }
1302  
1303 # --- GFM Table ---
1304 280 ▶ if (_is_table_row($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_table_row($line)) 7 273 280
1305 {
1306 7 my $table = $self->_parse_table($lines, $i);
1307 7 ▶ if ($table)
Branch: 1 branch
BranchTrueFalseTotal
if ($table) 7 0 7
1308 {
1309 7 $flush_para->();
1310 7 $flush_list->();
1311 7 $in_list = 0;
1312 7 @list_stack = ();
1313  
1314 7 push @out, $self->_convert_table($table);
1315 7 $i = $table->{next_idx};
1316 7 $prev_was_para = 0;
1317 7 next;
1318 }
1319  
1320 # Not a valid table, treat as paragraph
1321 0 push @para, $line;
1322 0 $prev_was_para = 1;
1323 0 $i++;
1324 0 next;
1325 }
1326  
1327 # --- Block math ($$...$$) ---
1328 273 ▶ if ($line =~ m/^\$\$$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\$\$$/u) 1 272 273
1329 {
1330 1 $flush_para->();
1331 1 $flush_list->();
1332 1 $in_list = 0;
1333 1 @list_stack = ();
1334 1 $self->{has_math} = 1;
1335  
1336 1 $i++;
1337 1 my @math_lines;
1338 1 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 2 0 2
1339 {
1340 2 my $ml = $lines->[$i];
1341 2 ▶ if ($ml =~ m/^\$\$$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($ml =~ /^\$\$$/u) 1 1 2
1342 {
1343 1 $i++;
1344 1 last;
1345 }
1346 1 push @math_lines, $ml;
1347 1 $i++;
1348 }
1349  
1350 1 ▶ if (@math_lines && $math_lines[-1] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if (@math_lines and $math_lines[-1] eq "") 0 1 1
Condition33%1/3
@math_lines and $math_lines[-1] eq ""
!ll&&!rl&&r
010
MC/DC0%0/2
@math_lines and $math_lines[-1] eq ""
@math_lines $math_lines[-1] eq ""
Truth table33%1/3
A and B
A= @math_linesB= $math_lines[-1] eq ""
ABresult
0X0
100
111
1351 {
1352 0 pop @math_lines;
1353 }
1354  
1355 1 my $math_text = join("\n", @math_lines);
1356 1 ▶ push @out, "[stem]\n++++\n" . ($math_text ne '' ? "$math_text\n" : '') . "++++";
Branch: 1 branch
BranchTrueFalseTotal
$math_text ne '' ? : 1 0 1
1357  
1358 1 $prev_was_para = 0;
1359 1 next;
1360 }
1361  
1362 # --- Fenced code block ---
1363 272 ▶ if (_is_opening_fence($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_opening_fence($line)) 14 258 272
1364 {
1365 14 $flush_para->();
1366 14 $flush_list->();
1367 14 $in_list = 0;
1368 14 @list_stack = ();
1369  
1370 14 my ($fence_char, $fence_len, $lang) = _get_fence_info($line);
1371 14 $i++;
1372  
1373 14 my @code_lines;
1374 14 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 33 2 35
1375 {
1376 33 my $cl = $lines->[$i];
1377 33 ▶ if ($cl =~ m/^[ \t]*(\Q$fence_char\E{$fence_len,})\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cl =~ /^[ \t]*(\Q$fence_char\E{$fence_len,})\s*$/u) 12 21 33
1378 {
1379 12 $i++;
1380 12 last;
1381 }
1382 21 push @code_lines, $cl;
1383 21 $i++;
1384 }
1385  
1386 14 ▶ if (@code_lines && $code_lines[-1] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if (@code_lines and $code_lines[-1] eq "") 0 14 14
Condition33%1/3
@code_lines and $code_lines[-1] eq ""
!ll&&!rl&&r
0140
MC/DC0%0/2
@code_lines and $code_lines[-1] eq ""
@code_lines $code_lines[-1] eq ""
Truth table33%1/3
A and B
A= @code_linesB= $code_lines[-1] eq ""
ABresult
0X0
100
111
1387 {
1388 0 pop @code_lines;
1389 }
1390  
1391 14 my $code_text = join("\n", @code_lines);
1392  
1393 # Fenced math block: ```math → [stem] ++++
1394 14 ▶ if ($lang && lc($lang) eq 'math')
Branch: 3 branches
BranchTrueFalseTotal
if ($lang and lc $lang eq 'math') { } 2 12 14
elsif ($lang and $self->_is_diagram_language($lang)) { } 2 10 12
elsif ($lang) { } 6 4 10
Condition100%3/3
$lang and lc $lang eq "math"
!ll&&!rl&&r
482
MC/DC100%2/2
$lang and lc $lang eq "math"
$lang lc $lang eq "math"
Truth table100%3/3
A and B
A= $langB= lc $lang eq "math"
ABresult
0X0
100
111
1395 {
1396 2 $self->{has_math} = 1;
1397 2 ▶ push @out, "[stem]\n++++\n" . ($code_text ne '' ? "$code_text\n" : '') . "++++";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 2 0 2
1398 }
1399 ▶ elsif ($lang && $self->_is_diagram_language($lang))
Condition100%3/3
$lang and $self->_is_diagram_language($lang)
!ll&&!rl&&r
462
MC/DC100%2/2
$lang and $self->_is_diagram_language($lang)
$lang $self->_is_diagram_language($lang)
Truth table100%3/3
A and B
A= $langB= $self->_is_diagram_language($lang)
ABresult
0X0
100
111
1400 {
1401 2 ▶ push @out, "[$lang]\n....\n" . ($code_text ne '' ? "$code_text\n" : '') . "....";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 2 0 2
1402 }
1403 elsif ($lang)
1404 {
1405 6 ▶ push @out, "[source,$lang]\n----\n" . ($code_text ne '' ? "$code_text\n" : '') . "----";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 6 0 6
1406 }
1407 else
1408 {
1409 4 ▶ push @out, "----\n" . ($code_text ne '' ? "$code_text\n" : '') . "----";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 4 0 4
1410 }
1411  
1412 14 $prev_was_para = 0;
1413 14 next;
1414 }
1415  
1416 # --- Indented code block (outside list context only) ---
1417 258 ▶ if (!$in_list && $line =~ m/^[ \t]{4,}/ && $line !~ m/^[ \t]*[-*+]\s/ && $line !~ m/^[ \t]*\d+[.)]\s/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_list and $line =~ /^[ \t]{4,}/u and not $line =~ /^[ \t]*[-*+]\s/u and not $line =~ /^[ \t]*\d+[.)]\s/u) 3 255 258
Condition77%7/9
not $in_list and $line =~ /^[ \t]{4,}/u and not $line =~ /^[ \t]*[-*+]\s/u and not $line =~ /^[ \t]*\d+[.)]\s/u
!ll&&!rl&&r
25503
not $in_list and $line =~ /^[ \t]{4,}/u and not $line =~ /^[ \t]*[-*+]\s/u
!ll&&!rl&&r
25503
not $in_list and $line =~ /^[ \t]{4,}/u
!ll&&!rl&&r
252303
MC/DC50%2/4
not $in_list and $line =~ /^[ \t]{4,}/u and not $line =~ /^[ \t]*[-*+]\s/u and not $line =~ /^[ \t]*\d+[.)]\s/u
not $in_list $line =~ /^[ \t]{4,}/u not $line =~ /^[ \t]*[-*+]\s/u not $line =~ /^[ \t]*\d+[.)]\s/u
Truth table60%3/5
not(A) and B and not(C) and not(D)
A= not $in_listB= $line =~ /^[ \t]{4,}/uC= not $line =~ /^[ \t]*[-*+]\s/uD= not $line =~ /^[ \t]*\d+[.)]\s/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1418 {
1419 3 $flush_para->();
1420  
1421 3 my @code_lines;
1422 3 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 4 3 7
1423 {
1424 4 my $cl = $lines->[$i];
1425 4 ▶ if ($cl eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($cl eq "") 0 4 4
1426 {
1427 0 push @code_lines, '';
1428 0 $i++;
1429 0 next;
1430 }
1431 4 ▶ if ($cl =~ m/^[ \t]{4,}(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cl =~ /^[ \t]{4,}(.*)/u) 4 0 4
1432 {
1433 4 push @code_lines, $1;
1434 4 $i++;
1435 4 next;
1436 }
1437 0 last;
1438 }
1439  
1440 3 ▶ while (@code_lines && $code_lines[-1] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if (@code_lines and $code_lines[-1] eq "") 0 3 3
Condition33%1/3
@code_lines and $code_lines[-1] eq ""
!ll&&!rl&&r
030
MC/DC0%0/2
@code_lines and $code_lines[-1] eq ""
@code_lines $code_lines[-1] eq ""
Truth table33%1/3
A and B
A= @code_linesB= $code_lines[-1] eq ""
ABresult
0X0
100
111
1441 {
1442 0 pop @code_lines;
1443 }
1444  
1445 3 my $code_text = join("\n", @code_lines);
1446 3 ▶ push @out, "....\n" . ($code_text ne '' ? "$code_text\n" : '') . "....";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 3 0 3
1447  
1448 3 $prev_was_para = 0;
1449 3 next;
1450 }
1451  
1452 # --- Blockquote ---
1453 255 ▶ if ($line =~ m/^>/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^>/u) 12 243 255
1454 {
1455 12 $flush_para->();
1456 12 $flush_list->();
1457 12 $in_list = 0;
1458 12 @list_stack = ();
1459  
1460 12 my $bq = $self->_parse_blockquote($lines, $i);
1461 12 $i = $bq->{next_idx};
1462 12 my $bq_lines = $bq->{lines};
1463 12 my $depth = $bq->{depth};
1464  
1465 # Check for admonition
1466 12 my $is_admonition = 0;
1467 12 my $admonition_label = '';
1468 12 my $admonition_text = '';
1469  
1470 12 ▶ if (@$bq_lines)
Branch: 1 branch
BranchTrueFalseTotal
if (@$bq_lines) 12 0 12
1471 {
1472 12 my $first = $bq_lines->[0];
1473 12 my ($label, $rest) = _is_admonition_label($first);
1474 12 ▶ if ($label)
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 2 10 12
1475 {
1476 2 $is_admonition = 1;
1477 2 $admonition_label = $label;
1478 2 $admonition_text = $rest;
1479 }
1480 }
1481  
1482 12 ▶ if ($is_admonition)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_admonition) { } 2 10 12
1483 {
1484 2 ▶ if (@$bq_lines > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@$bq_lines > 1) { } 1 1 2
1485 {
1486 1 my $block = "[$admonition_label]\n====\n";
1487 1 $block .= $admonition_text . "\n";
1488 1 ▶ for (my $k = 1; $k < @$bq_lines; $k++)
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @$bq_lines) 2 1 3
1489 {
1490 2 my $pl = $bq_lines->[$k];
1491 2 ▶ if ($pl eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($pl eq '') { } 1 1 2
1492 {
1493 1 $block .= "\n";
1494 }
1495 else
1496 {
1497 1 $block .= $self->{inline}->process($pl) . "\n";
1498 }
1499 }
1500 1 $block .= "====";
1501 1 push @out, $block;
1502 }
1503 else
1504 {
1505 1 push @out, "$admonition_label: $admonition_text";
1506 }
1507 }
1508 else
1509 {
1510 10 my $bq_content = _parse_body($self, $bq_lines);
1511 10 my $delim = '_' x (4 + ($depth - 1) * 2);
1512 10 ▶ push @out, "$delim\n" . ($bq_content ne '' ? "$bq_content\n" : '') . $delim;
Branch: 1 branch
BranchTrueFalseTotal
$bq_content ne '' ? : 10 0 10
1513 }
1514  
1515 12 $prev_was_para = 0;
1516 12 next;
1517 }
1518  
1519 # --- Thematic break (before list check) ---
1520 243 ▶ if (_is_thematic_break($line, $prev_was_para))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_thematic_break($line, $prev_was_para)) 7 236 243
1521 {
1522 7 $flush_para->();
1523 7 $flush_list->();
1524 7 $in_list = 0;
1525 7 @list_stack = ();
1526 7 push @out, "'''";
1527 7 $prev_was_para = 0;
1528 7 $i++;
1529 7 next;
1530 }
1531  
1532 # --- Definition list (bold term :: form) ---
1533 236 ▶ if (!$in_list)
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_list) 211 25 236
1534 {
1535 211 my $bold_term = _is_def_list_bold_term($line);
1536 211 ▶ if ($bold_term)
Branch: 1 branch
BranchTrueFalseTotal
if ($bold_term) 3 208 211
1537 {
1538 3 $flush_para->();
1539 3 $flush_list->();
1540 3 $in_list = 0;
1541 3 @list_stack = ();
1542  
1543 3 my $level = $bold_term->{level};
1544 3 my $prefix = ':' x $level;
1545  
1546 3 my $term_processed = $self->{inline}->process($bold_term->{term});
1547 3 my $def_processed = $self->{inline}->process($bold_term->{def});
1548  
1549 3 my @dl_out;
1550 3 push @dl_out, "${term_processed}${prefix}";
1551 3 push @dl_out, $def_processed;
1552 3 push @out, join("\n", @dl_out);
1553  
1554 3 $prev_was_para = 0;
1555 3 $i++;
1556 3 next;
1557 }
1558 }
1559  
1560 # --- Definition list (standard form: Term\n: Definition) ---
1561 # Check if this is a term followed by definition(s)
1562 233 ▶ if (!$in_list && !@para && $i + 1 < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_list || @para and $i + 1 < @$lines) 71 162 233
Condition100%6/6
not $in_list || @para and $i + 1 < @$lines
!ll&&!rl&&r
4811471
$in_list || @para
l!l&&r!l&&!r
2523185
MC/DC100%3/3
not $in_list || @para and $i + 1 < @$lines
$in_list @para $i + 1 < @$lines
Truth table100%4/4
not(A || B) and C
A= $in_listB= @paraC= $i + 1 < @$lines
ABCresult
1XX0
01X0
0000
0011
1563 {
1564 71 my $next_line = $lines->[$i + 1];
1565 71 ▶ if ($next_line =~ m/^:[ \t]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line =~ /^:[ \t]/u) 2 69 71
1566 {
1567 2 ▶ my $prev_blank = ($i == 0 || $lines->[$i - 1] eq '');
Condition33%1/3
$i == 0 || $$lines[$i - 1] eq ''
l!l&&r!l&&!r
200
MC/DC0%0/2
$i == 0 || $$lines[$i - 1] eq ''
$i == 0 $$lines[$i - 1] eq ''
Truth table33%1/3
A || B
A= $i == 0B= $$lines[$i - 1] eq ''
ABresult
1X1
011
000
1568  
1569 # Don't treat a line starting with '> ' as a def list term
1570 2 ▶ if ($line !~ m/^>/ &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^>/u or $line =~ /^[ \t]*[-*+#]/u or _is_atx_heading($line) or _is_setext_underline($line) or _is_block_html_start($line)) 2 0 2
Condition33%4/12
$line =~ /^>/u or $line =~ /^[ \t]*[-*+#]/u or _is_atx_heading($line) or _is_setext_underline($line) or _is_block_html_start($line)
l!l&&r!l&&!r
002
$line =~ /^>/u or $line =~ /^[ \t]*[-*+#]/u or _is_atx_heading($line) or _is_setext_underline($line)
l!l&&r!l&&!r
002
$line =~ /^>/u or $line =~ /^[ \t]*[-*+#]/u or _is_atx_heading($line)
l!l&&r!l&&!r
002
$line =~ /^>/u or $line =~ /^[ \t]*[-*+#]/u
l!l&&r!l&&!r
002
MC/DC0%0/5
$line =~ /^>/u or $line =~ /^[ \t]*[-*+#]/u or _is_atx_heading($line) or _is_setext_underline($line) or _is_block_html_start($line)
$line =~ /^>/u $line =~ /^[ \t]*[-*+#]/u _is_atx_heading($line) _is_setext_underline($line) _is_block_html_start($line)
Truth table16%1/6
A or B or C or D or E
A= $line =~ /^>/uB= $line =~ /^[ \t]*[-*+#]/uC= _is_atx_heading($line)D= _is_setext_underline($line)E= _is_block_html_start($line)
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
1571 $line !~ m/^[ \t]*[-*+#]/ &&
1572 !_is_atx_heading($line) &&
1573 !_is_setext_underline($line) &&
1574 !_is_block_html_start($line))
1575 {
1576  
1577 2 $flush_para->();
1578 2 $flush_list->();
1579 2 $in_list = 0;
1580 2 @list_stack = ();
1581  
1582 2 my $def = $self->_parse_def_list_standard($lines, $i);
1583 2 $i = $def->{next_idx};
1584  
1585 2 my $term_processed = $self->{inline}->process($def->{term});
1586  
1587 2 my @dl_out;
1588 2 push @dl_out, "${term_processed}::";
1589  
1590 2 my @defs = @{$def->{definitions}};
1591 2 ▶ for (my $d = 0; $d < @defs; $d++)
Branch: 1 branch
BranchTrueFalseTotal
if ($d < @defs) 3 2 5
1592 {
1593 3 my $def_text = $self->{inline}->process($defs[$d]);
1594 3 ▶ if ($d == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($d == 0) { } 2 1 3
1595 {
1596 2 push @dl_out, $def_text;
1597 }
1598 else
1599 {
1600 # Multiple definitions: use list continuation
1601 1 push @dl_out, '+';
1602 1 push @dl_out, $def_text;
1603 }
1604 }
1605 2 push @out, join("\n", @dl_out);
1606  
1607 2 $prev_was_para = 0;
1608 2 next;
1609 }
1610 }
1611 }
1612  
1613 # --- List item ---
1614 231 my $item_info = _get_list_item_info($line);
1615 231 ▶ if ($item_info)
Branch: 1 branch
BranchTrueFalseTotal
if ($item_info) 42 189 231
1616 {
1617 42 $flush_para->();
1618  
1619 42 my $depth = _get_list_depth($item_info->{indent});
1620  
1621 # Pop from stack if depth decreased
1622 42 ▶ while (@list_stack && $list_stack[-1]{depth} > $depth)
Branch: 1 branch
BranchTrueFalseTotal
if (@list_stack and $list_stack[-1]{"depth"} > $depth) 4 42 46
Condition100%3/3
@list_stack and $list_stack[-1]{"depth"} > $depth
!ll&&!rl&&r
18244
MC/DC100%2/2
@list_stack and $list_stack[-1]{"depth"} > $depth
@list_stack $list_stack[-1]{"depth"} > $depth
Truth table100%3/3
A and B
A= @list_stackB= $list_stack[-1]{"depth"} > $depth
ABresult
0X0
100
111
1623 {
1624 4 pop @list_stack;
1625 }
1626  
1627 # Update or push stack entry
1628 42 ▶ if (!@list_stack)
Branch: 2 branches
BranchTrueFalseTotal
if (not @list_stack) { } 18 24 42
elsif ($list_stack[-1]{'depth'} < $depth) { } 4 20 24
1629 {
1630 18 push @list_stack, {type => $item_info->{type}, depth => $depth};
1631 }
1632 elsif ($list_stack[-1]{depth} < $depth)
1633 {
1634 4 push @list_stack, {type => $item_info->{type}, depth => $depth};
1635 }
1636 else
1637 {
1638 # Same depth — update type if switching (ul→ol or ol→ul)
1639 20 $list_stack[-1]{type} = $item_info->{type};
1640 }
1641  
1642 42 my $marker = _build_list_marker(\@list_stack);
1643 42 my $item_text = $self->_process_list_item_text($item_info);
1644  
1645 # Collect continuation lines
1646 42 $i++;
1647 42 my @continuation;
1648 42 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 30 13 43
1649 {
1650 30 my $cl = $lines->[$i];
1651 30 ▶ if ($cl eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($cl eq "") 8 22 30
1652 {
1653 8 last;
1654 }
1655  
1656 # Continuation: indented at least 2 spaces, not a new list item,
1657 # not a block-level construct
1658 22 ▶ if ($cl =~ m/^[ \t]{2,}/ &&
Branch: 1 branch
BranchTrueFalseTotal
if ($cl =~ /^[ \t]{2,}/u and not _get_list_item_info($cl) and not _is_atx_heading($cl) and not $cl =~ /^>/u and not _is_opening_fence($cl)) 1 21 22
Condition75%9/12
$cl =~ /^[ \t]{2,}/u and not _get_list_item_info($cl) and not _is_atx_heading($cl) and not $cl =~ /^>/u and not _is_opening_fence($cl)
!ll&&!rl&&r
2101
$cl =~ /^[ \t]{2,}/u and not _get_list_item_info($cl) and not _is_atx_heading($cl) and not $cl =~ /^>/u
!ll&&!rl&&r
2101
$cl =~ /^[ \t]{2,}/u and not _get_list_item_info($cl) and not _is_atx_heading($cl)
!ll&&!rl&&r
2101
$cl =~ /^[ \t]{2,}/u and not _get_list_item_info($cl)
!ll&&!rl&&r
1741
MC/DC40%2/5
$cl =~ /^[ \t]{2,}/u and not _get_list_item_info($cl) and not _is_atx_heading($cl) and not $cl =~ /^>/u and not _is_opening_fence($cl)
$cl =~ /^[ \t]{2,}/u not _get_list_item_info($cl) not _is_atx_heading($cl) not $cl =~ /^>/u not _is_opening_fence($cl)
Truth table50%3/6
A and not(B) and not(C) and not(D) and not(E)
A= $cl =~ /^[ \t]{2,}/uB= not _get_list_item_info($cl)C= not _is_atx_heading($cl)D= not $cl =~ /^>/uE= not _is_opening_fence($cl)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1659 !_get_list_item_info($cl) &&
1660 !_is_atx_heading($cl) &&
1661 $cl !~ m/^>/ &&
1662 !_is_opening_fence($cl))
1663 {
1664 # Strip minimal indentation (the list indent + 2 spaces)
1665 1 push @continuation, $cl;
1666 1 $i++;
1667 1 next;
1668 }
1669 21 last;
1670 }
1671  
1672 42 ▶ if (@continuation)
Branch: 1 branch
BranchTrueFalseTotal
if (@continuation) 1 41 42
1673 {
1674 # Append continuation lines to the item text
1675 1 my $cont_text = join("\n", @continuation);
1676 1 $item_text .= "\n" . $cont_text;
1677 }
1678  
1679 # Check for a block attachment after a blank line:
1680 # If the next non-blank line is an indented fenced code block or
1681 # an indented code block (4+ spaces), attach it to this item with '+'.
1682 42 ▶ if ($i < @$lines && $lines->[$i] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines and $lines->[$i] eq "") 8 34 42
Condition100%3/3
$i < @$lines and $lines->[$i] eq ""
!ll&&!rl&&r
13218
MC/DC100%2/2
$i < @$lines and $lines->[$i] eq ""
$i < @$lines $lines->[$i] eq ""
Truth table100%3/3
A and B
A= $i < @$linesB= $lines->[$i] eq ""
ABresult
0X0
100
111
1683 {
1684 8 my $j = $i + 1;
1685 8 ▶ while ($j < @$lines && $lines->[$j] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$lines and $lines->[$j] eq "") 0 8 8
Condition33%1/3
$j < @$lines and $lines->[$j] eq ""
!ll&&!rl&&r
080
MC/DC0%0/2
$j < @$lines and $lines->[$j] eq ""
$j < @$lines $lines->[$j] eq ""
Truth table33%1/3
A and B
A= $j < @$linesB= $lines->[$j] eq ""
ABresult
0X0
100
111
1686 {
1687 0 $j++;
1688 }
1689 8 ▶ if ($j < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$lines) 8 0 8
1690 {
1691 8 my $peek = $lines->[$j];
1692 # Only attach indented fenced code blocks (2+ spaces then ``` or ~~~)
1693 # Plain 4-space indented code after a list blank line is treated as
1694 # a detached code block (existing behavior, per t/03 test 24).
1695 8 ▶ if ($peek =~ m/^[ \t]{2,}```/ || $peek =~ m/^[ \t]{2,}~~~/)
Branch: 1 branch
BranchTrueFalseTotal
if ($peek =~ /^[ \t]{2,}```/u or $peek =~ /^[ \t]{2,}~~~/u) 2 6 8
Condition66%2/3
$peek =~ /^[ \t]{2,}```/u or $peek =~ /^[ \t]{2,}~~~/u
l!l&&r!l&&!r
206
MC/DC50%1/2
$peek =~ /^[ \t]{2,}```/u or $peek =~ /^[ \t]{2,}~~~/u
$peek =~ /^[ \t]{2,}```/u $peek =~ /^[ \t]{2,}~~~/u
Truth table66%2/3
A or B
A= $peek =~ /^[ \t]{2,}```/uB= $peek =~ /^[ \t]{2,}~~~/u
ABresult
1X1
011
000
1696 {
1697 # Determine the continuation indent (2 spaces for top-level items)
1698 2 ▶ my $cont_indent = ($item_info->{indent} || 0) + 2;
Condition50%1/2
$item_info->{'indent'} || 0
l!l
02
MC/DC0%0/1
$item_info->{'indent'} || 0
$item_info->{'indent'}
Truth table50%1/2
A
A= $item_info->{'indent'}
Aresult
11
00
1699  
1700 # Fenced code block: collect until closing fence
1701 2 $i = $j;
1702 2 my $fence_line = $lines->[$i];
1703 2 $fence_line =~ m/^[ \t]*((`{3,})|(~{3,}))(\S*)/;
1704 2 my $fence_char = substr($1, 0, 1);
1705 2 my $fence_len = length($1);
1706 2 ▶ my $lang = $4 // '';
Condition50%1/2
$4 // ""
l!l
20
MC/DC0%0/1
$4 // ""
$4
Truth table50%1/2
A
A= $4
Aresult
11
00
1707  
1708 2 $i++;
1709 2 my @code_lines;
1710 2 ▶ while ($i < @$lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$lines) 4 0 4
1711 {
1712 4 my $cl = $lines->[$i];
1713 4 ▶ if ($cl =~ m/^[ \t]*\Q$fence_char\E{$fence_len,}\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($cl =~ /^[ \t]*\Q$fence_char\E{$fence_len,}\s*$/u) 2 2 4
1714 {
1715 2 $i++;
1716 2 last;
1717 }
1718 # Dedent by cont_indent
1719 2 $cl =~ s/^[ \t]{1,$cont_indent}//;
1720 2 push @code_lines, $cl;
1721 2 $i++;
1722 }
1723 0 ▶ while (@code_lines && $code_lines[-1] eq '') { pop @code_lines }
Branch: 1 branch
BranchTrueFalseTotal
if (@code_lines and $code_lines[-1] eq "") 0 2 2
Condition33%1/3
@code_lines and $code_lines[-1] eq ""
!ll&&!rl&&r
020
MC/DC0%0/2
@code_lines and $code_lines[-1] eq ""
@code_lines $code_lines[-1] eq ""
Truth table33%1/3
A and B
A= @code_linesB= $code_lines[-1] eq ""
ABresult
0X0
100
111
1724 2 my $code_text = join("\n", @code_lines);
1725 2 my $block_str;
1726 2 ▶ if ($lang && $self->_is_diagram_language($lang))
Branch: 2 branches
BranchTrueFalseTotal
if ($lang and $self->_is_diagram_language($lang)) { } 0 2 2
elsif ($lang) { } 1 1 2
Condition66%2/3
$lang and $self->_is_diagram_language($lang)
!ll&&!rl&&r
110
MC/DC0%0/2
$lang and $self->_is_diagram_language($lang)
$lang $self->_is_diagram_language($lang)
Truth table66%2/3
A and B
A= $langB= $self->_is_diagram_language($lang)
ABresult
0X0
100
111
1727 {
1728 0 ▶ $block_str = "[$lang]\n....\n" . ($code_text ne '' ? "$code_text\n" : '') . "....";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 0 0 0
1729 }
1730 elsif ($lang)
1731 {
1732 1 ▶ $block_str = "[source,$lang]\n----\n" . ($code_text ne '' ? "$code_text\n" : '') . "----";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 1 0 1
1733 }
1734 else
1735 {
1736 1 ▶ $block_str = "----\n" . ($code_text ne '' ? "$code_text\n" : '') . "----";
Branch: 1 branch
BranchTrueFalseTotal
$code_text ne '' ? : 1 0 1
1737 }
1738 2 push @list_items, "$marker $item_text";
1739 2 push @list_items, "+\n$block_str";
1740 2 $in_list = 1;
1741 2 $prev_was_para = 0;
1742 2 next;
1743 }
1744 }
1745 }
1746  
1747 40 push @list_items, "$marker $item_text";
1748 40 $in_list = 1;
1749 40 $prev_was_para = 0;
1750 40 next;
1751 }
1752  
1753 # --- Admonition paragraph ---
1754 189 ▶ if (!@para && !$in_list)
Branch: 1 branch
BranchTrueFalseTotal
unless (@para or $in_list) 165 24 189
Condition100%3/3
@para or $in_list
l!l&&r!l&&!r
231165
MC/DC100%2/2
@para or $in_list
@para $in_list
Truth table100%3/3
A or B
A= @paraB= $in_list
ABresult
1X1
011
000
1755 {
1756 165 my ($label, $rest) = _is_admonition_label($line);
1757 165 ▶ if ($label)
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 1 164 165
1758 {
1759 1 $flush_para->();
1760 1 push @out, "$label: $rest";
1761 1 $prev_was_para = 0;
1762 1 $i++;
1763 1 next;
1764 }
1765 }
1766  
1767 # --- Footnote definition ---
1768 188 ▶ if (_is_footnote_definition($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_footnote_definition($line)) 10 178 188
1769 {
1770 10 $flush_para->();
1771 10 $flush_list->();
1772 10 $in_list = 0;
1773 10 @list_stack = ();
1774  
1775 10 my $fn = $self->_parse_footnote_definition($lines, $i);
1776 10 ▶ if ($fn)
Branch: 1 branch
BranchTrueFalseTotal
if ($fn) { } 10 0 10
1777 {
1778 10 $self->{refs}->add_footnote_def($fn->{label}, $fn->{text}, $self->{inline});
1779 10 $i = $fn->{next_idx};
1780 }
1781 else
1782 {
1783 0 $i++;
1784 }
1785 10 $prev_was_para = 0;
1786 10 next;
1787 }
1788  
1789 # --- Reference definition ---
1790 178 ▶ if (_is_ref_definition($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_ref_definition($line)) 4 174 178
1791 {
1792 4 $flush_para->();
1793 4 $flush_list->();
1794 4 $in_list = 0;
1795 4 @list_stack = ();
1796 4 $self->_parse_ref_definition($line);
1797 4 $prev_was_para = 0;
1798 4 $i++;
1799 4 next;
1800 }
1801  
1802 # --- ATX heading ---
1803 174 ▶ if (_is_atx_heading($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_atx_heading($line)) 30 144 174
1804 {
1805 30 $flush_para->();
1806 30 $flush_list->();
1807 30 $in_list = 0;
1808 30 @list_stack = ();
1809 30 push @out, $self->_convert_atx_heading($line);
1810 30 $prev_was_para = 0;
1811 30 $i++;
1812 30 next;
1813 }
1814  
1815 # --- Setext underline ---
1816 144 ▶ if (_is_setext_underline($line))
Branch: 1 branch
BranchTrueFalseTotal
if (_is_setext_underline($line)) 6 138 144
1817 {
1818 6 ▶ if (@para == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@para == 1) 6 0 6
1819 {
1820 6 my $heading_text = $para[0];
1821 6 @para = ();
1822 6 $flush_list->();
1823 6 $in_list = 0;
1824 6 @list_stack = ();
1825  
1826 6 ▶ my $level = ($line =~ m/^=+$/) ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^=+$/u ? : 3 3 6
1827 6 my $prefix = '=' x $level;
1828  
1829 # Extract explicit anchor
1830 6 my $explicit_anchor;
1831 6 ▶ if ($heading_text =~ s/^<a\s+(?:name|id)\s*=\s*"([^"]+)"\s*><\/a>\s*//)
Branch: 1 branch
BranchTrueFalseTotal
if ($heading_text =~ s[^<a\s+(?:name|id)\s*=\s*"([^"]+)"\s*></a>\s*][]u) 0 6 6
1832 {
1833 0 $explicit_anchor = $1;
1834 }
1835  
1836 6 $heading_text = $self->{inline}->process($heading_text);
1837  
1838 6 ▶ if ($explicit_anchor)
Branch: 2 branches
BranchTrueFalseTotal
if ($explicit_anchor) { } 0 6 6
elsif ($self->{'options'}{'auto_ids'}) { } 0 6 6
1839 {
1840 0 push @out, "[[$explicit_anchor]]";
1841 0 $self->{refs}->register_heading($explicit_anchor, $heading_text);
1842 }
1843 elsif ($self->{options}{auto_ids})
1844 {
1845 0 my $id = $self->{refs}->generate_id($heading_text);
1846 0 push @out, "[[$id]]";
1847 0 $self->{refs}->register_heading($id, $heading_text);
1848 }
1849  
1850 6 push @out, $prefix . ' ' . $heading_text;
1851 6 $prev_was_para = 0;
1852 6 $i++;
1853 6 next;
1854 }
1855 }
1856  
1857 # --- Paragraph line ---
1858 138 push @para, $line;
1859 138 $prev_was_para = 1;
1860 138 $i++;
1861 }
1862  
1863 192 $flush_para->();
1864 192 $flush_list->();
1865  
1866 192 return join("\n\n", @out);
1867 }
1868  
1869 #===========================================================================
1870 # Wrap mode
1871 #===========================================================================
1872  
1873 sub _apply_wrap
1874 182 ▶{
Subroutine _apply_wrap: called CC=9 SCAR=22.1
1875 182 my ($text, $mode) = @_;
1876  
1877 182 ▶ return $text if (!$mode || $mode eq 'preserve');
Branch: 1 branch
BranchTrueFalseTotal
if not $mode or $mode eq "preserve" 171 11 182
Condition100%3/3
not $mode or $mode eq "preserve"
l!l&&r!l&&!r
170111
MC/DC100%2/2
not $mode or $mode eq "preserve"
not $mode $mode eq "preserve"
Truth table100%3/3
not(A) or B
A= not $modeB= $mode eq "preserve"
ABresult
1X1
011
000
1878  
1879 # Split the output into blocks separated by blank lines, then process
1880 # each block. Blocks that are inside a verbatim delimiter (----, ....,
1881 # ++++, ////) are passed through unchanged. All other blocks have their
1882 # lines joined and optionally re-broken according to $mode.
1883  
1884 11 my @lines = split /\n/, $text, -1;
1885 11 my @result;
1886  
1887 # Delimiters that open/close verbatim regions (exact-match on trimmed line)
1888 11 my %verbatim_open = map { $_ => 1 } qw(---- .... ++++ ////);
1889  
1890 11 my $in_verbatim = 0;
1891 11 my $verbatim_delim = '';
1892  
1893 11 my @para_buf; # accumulates lines of the current paragraph block
1894  
1895 27 ▶ my $flush_para = sub {
Subroutine __ANON__: called CC=24 SCAR=33.2
1896 27 ▶ return unless @para_buf;
Branch: 1 branch
BranchTrueFalseTotal
unless @para_buf 16 11 27
1897  
1898 # Check whether this "paragraph" is actually a non-wrappable structural
1899 # line (heading, list marker, attribute list, block title, admonition,
1900 # thematic break, table row, etc.). We only wrap plain prose paragraphs.
1901 11 my $first = $para_buf[0];
1902 11 ▶ my $is_structural =
Condition73%22/30
$first =~ /^=+ /u || $first =~ /^\[/u || $first =~ /^[*.]+ /u || $first =~ /^\|/u || $first =~ /^'{3}/u || $first =~ /^:[\w]/u || $first =~ /^[A-Z]+: /u || $first =~ /^\+$/u || $first =~ /^={4}/u || $first =~ /^_{4}/u || $first =~ m[^// ]u
l!l&&r!l&&!r
407
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u or $first =~ /^'{3}/u or $first =~ /^:[\w]/u or $first =~ /^[A-Z]+: /u or $first =~ /^\+$/u or $first =~ /^={4}/u or $first =~ /^_{4}/u
l!l&&r!l&&!r
407
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u or $first =~ /^'{3}/u or $first =~ /^:[\w]/u or $first =~ /^[A-Z]+: /u or $first =~ /^\+$/u or $first =~ /^={4}/u
l!l&&r!l&&!r
407
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u or $first =~ /^'{3}/u or $first =~ /^:[\w]/u or $first =~ /^[A-Z]+: /u or $first =~ /^\+$/u
l!l&&r!l&&!r
407
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u or $first =~ /^'{3}/u or $first =~ /^:[\w]/u or $first =~ /^[A-Z]+: /u
l!l&&r!l&&!r
407
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u or $first =~ /^'{3}/u or $first =~ /^:[\w]/u
l!l&&r!l&&!r
317
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u or $first =~ /^'{3}/u
l!l&&r!l&&!r
308
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u or $first =~ /^\|/u
l!l&&r!l&&!r
128
$first =~ /^=+ /u or $first =~ /^\[/u or $first =~ /^[*.]+ /u
l!l&&r!l&&!r
1010
$first =~ /^=+ /u or $first =~ /^\[/u
l!l&&r!l&&!r
0110
MC/DC27%3/11
$first =~ /^=+ /u || $first =~ /^\[/u || $first =~ /^[*.]+ /u || $first =~ /^\|/u || $first =~ /^'{3}/u || $first =~ /^:[\w]/u || $first =~ /^[A-Z]+: /u || $first =~ /^\+$/u || $first =~ /^={4}/u || $first =~ /^_{4}/u || $first =~ m[^// ]u
$first =~ /^=+ /u $first =~ /^\[/u $first =~ /^[*.]+ /u $first =~ /^\|/u $first =~ /^'{3}/u $first =~ /^:[\w]/u $first =~ /^[A-Z]+: /u $first =~ /^\+$/u $first =~ /^={4}/u $first =~ /^_{4}/u $first =~ m[^// ]u
Truth table33%4/12
A or B or C or D or E or F or G or H or I or J || K
A= $first =~ /^=+ /uB= $first =~ /^\[/uC= $first =~ /^[*.]+ /uD= $first =~ /^\|/uE= $first =~ /^'{3}/uF= $first =~ /^:[\w]/uG= $first =~ /^[A-Z]+: /uH= $first =~ /^\+$/uI= $first =~ /^={4}/uJ= $first =~ /^_{4}/uK= $first =~ m[^// ]u
ABCDEFGHIJKresult
1XXXXXXXXXX1
01XXXXXXXXX1
001XXXXXXXX1
0001XXXXXXX1
00001XXXXXX1
000001XXXXX1
0000001XXXX1
00000001XXX1
000000001XX1
0000000001X1
000000000011
000000000000
1903 $first =~ m/^=+ / # heading
1904 || $first =~ m/^\[/ # attribute list / admonition label
1905 || $first =~ m/^[*.]+ / # list item
1906 || $first =~ m/^\|/ # table row
1907 || $first =~ m/^'{3}/ # thematic break
1908 || $first =~ m/^:[\w]/ # document attribute
1909 || $first =~ m/^[A-Z]+: / # admonition paragraph (NOTE: ...)
1910 || $first =~ m/^\+$/ # list continuation marker
1911 || $first =~ m/^={4}/ # admonition block delimiter
1912 || $first =~ m/^_{4}/ # quote block delimiter
1913 || $first =~ m/^\/\/ / # single-line comment
1914 ;
1915  
1916 11 ▶ if ($is_structural || @para_buf == 1 && $para_buf[0] eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($is_structural or @para_buf == 1 and $para_buf[0] eq '') { } 4 7 11
Condition66%4/6
$is_structural or @para_buf == 1 and $para_buf[0] eq ""
l!l&&r!l&&!r
407
@para_buf == 1 and $para_buf[0] eq ""
!ll&&!rl&&r
340
MC/DC33%1/3
$is_structural or @para_buf == 1 and $para_buf[0] eq ""
$is_structural @para_buf == 1 $para_buf[0] eq ""
Truth table75%3/4
A or B and C
A= $is_structuralB= @para_buf == 1C= $para_buf[0] eq ""
ABCresult
1XX1
0111
00X0
0100
1917 {
1918 4 push @result, @para_buf;
1919 }
1920 else
1921 {
1922 # Wrap the paragraph lines
1923 # First, split on hard-break markers (' +') to preserve them
1924 7 my @segments;
1925 my @cur_seg;
1926 7 for my $pl (@para_buf)
1927 {
1928 12 ▶ if ($pl =~ m/ \+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($pl =~ / \+$/u) { } 1 11 12
1929 {
1930 1 push @cur_seg, $pl;
1931 1 push @segments, [@cur_seg];
1932 1 @cur_seg = ();
1933 }
1934 else
1935 {
1936 11 push @cur_seg, $pl;
1937 }
1938 }
1939 7 ▶ push @segments, [@cur_seg] if @cur_seg;
Branch: 1 branch
BranchTrueFalseTotal
if @cur_seg 7 0 7
1940  
1941 7 my @wrapped_lines;
1942 7 for my $seg (@segments)
1943 {
1944 # Join segment lines into one string
1945 8 my $joined = join(' ', @$seg);
1946 8 $joined =~ s/ \+$//; # remove trailing hard-break marker before join
1947  
1948 8 ▶ if ($mode eq 'none')
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'none') { } 5 3 8
elsif ($mode eq 'ventilate') { } 3 0 3
1949 {
1950 5 push @wrapped_lines, $joined;
1951 }
1952 elsif ($mode eq 'ventilate')
1953 {
1954 # Re-break after sentence-ending punctuation followed by space
1955 3 $joined =~ s/([.?!;]) +(?=\S)/$1\n/g;
1956 3 push @wrapped_lines, split(/\n/, $joined);
1957 }
1958 else
1959 {
1960 0 push @wrapped_lines, split(/\n/, $joined);
1961 }
1962  
1963 # Re-attach hard-break marker to last line of segment if original had one
1964 8 ▶ if ($seg->[-1] =~ m/ \+$/ && @wrapped_lines)
Branch: 1 branch
BranchTrueFalseTotal
if ($seg->[-1] =~ / \+$/u and @wrapped_lines) 1 7 8
Condition66%2/3
$seg->[-1] =~ / \+$/u and @wrapped_lines
!ll&&!rl&&r
701
MC/DC50%1/2
$seg->[-1] =~ / \+$/u and @wrapped_lines
$seg->[-1] =~ / \+$/u @wrapped_lines
Truth table66%2/3
A and B
A= $seg->[-1] =~ / \+$/uB= @wrapped_lines
ABresult
0X0
100
111
1965 {
1966 1 ▶ $wrapped_lines[-1] .= ' +' unless $wrapped_lines[-1] =~ m/ \+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $wrapped_lines[-1] =~ / \+$/u 1 0 1
1967 }
1968 }
1969  
1970 7 push @result, @wrapped_lines;
1971 }
1972  
1973 11 @para_buf = ();
1974 11 };
1975  
1976 11 for my $line (@lines)
1977 {
1978 # Track verbatim block delimiters
1979 41 my $trimmed = $line;
1980 41 $trimmed =~ s/\s+$//;
1981  
1982 41 ▶ if (!$in_verbatim && $verbatim_open{$trimmed})
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_verbatim and $verbatim_open{$trimmed}) 3 38 41
Condition100%3/3
not $in_verbatim and $verbatim_open{$trimmed}
!ll&&!rl&&r
7313
MC/DC100%2/2
not $in_verbatim and $verbatim_open{$trimmed}
not $in_verbatim $verbatim_open{$trimmed}
Truth table100%3/3
not(A) and B
A= not $in_verbatimB= $verbatim_open{$trimmed}
ABresult
0X0
100
111
1983 {
1984 3 $flush_para->();
1985 3 $in_verbatim = 1;
1986 3 $verbatim_delim = $trimmed;
1987 3 push @result, $line;
1988 3 next;
1989 }
1990  
1991 38 ▶ if ($in_verbatim)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_verbatim) 7 31 38
1992 {
1993 7 push @result, $line;
1994 7 ▶ if ($trimmed eq $verbatim_delim)
Branch: 1 branch
BranchTrueFalseTotal
if ($trimmed eq $verbatim_delim) 3 4 7
1995 {
1996 3 $in_verbatim = 0;
1997 3 $verbatim_delim = '';
1998 }
1999 7 next;
2000 }
2001  
2002 # Blank line: flush current paragraph, emit blank
2003 31 ▶ if ($trimmed eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($trimmed eq "") 13 18 31
2004 {
2005 13 $flush_para->();
2006 13 push @result, '';
2007 13 next;
2008 }
2009  
2010 18 push @para_buf, $line;
2011 }
2012  
2013 11 $flush_para->();
2014  
2015 11 return join("\n", @result);
2016 }
2017  
2018 17 1;
2019  
2020 __END__
↑ Summary blib/lib/Text/MarkdownAdoc/Refs.pm »