Markdent/Parser/BlockParser.pm

filter: Statementstmt 96.5% 312 / 323 Branchbran 79.3% 73 / 92 Conditioncond 65.3% 17 / 26 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 100% 49 / 49 Podpod 100% 1 / 1 totaltotal 90.7% 460 / 507 CC 62 SCAR 16.0
CC
62
Cov
91%
_parse_text
15.0
_handle_list_items
10.1
_split_chunks_on_regex
9.1
CRAP 5.0
« Markdent/Dialect/Theory/BlockParser.pm ↑ Summary Markdent/Role/DebugPrinter.pm »
1 package Markdent::Parser::BlockParser;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.41';
8  
9 35 ▶use Digest::SHA qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Markdent::Event::StartDocument;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Markdent::Event::EndDocument;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Markdent::Event::StartBlockquote;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Markdent::Event::EndBlockquote;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use Markdent::Event::StartHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use Markdent::Event::EndHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 35 ▶use Markdent::Event::StartListItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 35 ▶use Markdent::Event::EndListItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 35 ▶use Markdent::Event::StartOrderedList;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 35 ▶use Markdent::Event::EndOrderedList;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 35 ▶use Markdent::Event::StartParagraph;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 35 ▶use Markdent::Event::EndParagraph;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 35 ▶use Markdent::Event::StartUnorderedList;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use Markdent::Event::EndUnorderedList;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use Markdent::Event::HorizontalRule;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 35 ▶use Markdent::Event::HTMLBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 35 ▶use Markdent::Event::HTMLCommentBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Markdent::Event::Preformatted;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use Markdent::Regexes qw( :block $HTMLComment );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 35 ▶use Markdent::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 35 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 35 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 35 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 35 with 'Markdent::Role::BlockParser';
37  
38 has __html_blocks => (
39 traits => ['Hash'],
40 is => 'ro',
41 isa => t( 'HashRef', of => t('Str') ),
42 184 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 35 init_arg => undef,
44 handles => {
45 _save_html_block => 'set',
46 _get_html_block => 'get',
47 },
48 );
49  
50 35 has _list_level => (
51 traits => ['Counter'],
52 is => 'rw',
53 isa => t('Int'),
54 default => 0,
55 init_arg => undef,
56 handles => {
57 '_inc_list_level' => 'inc',
58 '_dec_list_level' => 'dec',
59 },
60 );
61  
62 35 has _list_item_is_paragraph => (
63 traits => ['Bool'],
64 is => 'ro',
65 isa => t('Bool'),
66 default => 0,
67 init_arg => undef,
68 handles => {
69 _treat_list_item_as_paragraph => 'set',
70 _treat_list_item_as_line => 'unset',
71 },
72 );
73  
74 180 ▶sub parse_document {
Subroutine parse_document: called CC=1 SCAR=0.0
Pod documented
75 180 my $self = shift;
76 180 my $text = shift;
77  
78 180 $self->_treat_list_item_as_line;
79  
80 180 $self->_hash_html_blocks($text);
81  
82 180 $self->_span_parser->extract_link_ids($text);
83  
84 180 $self->_parse_text($text);
85 }
86  
87 {
88  
89 # Stolen from Text::Markdown, along with the whole "extract and replace
90 # with hash" concept.
91 35 my $block_names_re = qr{
92 p | div | h[1-6] | blockquote | pre | table |
93 dl | ol | ul | script | noscript | form |
94 fieldset | iframe | math | ins | del
95 }xi;
96  
97 180 ▶ sub _hash_html_blocks {
Subroutine _hash_html_blocks: called CC=3 SCAR=11.3
98 180 my $self = shift;
99 180 my $text = shift;
100  
101 180 ${$text} =~ s{
102 ( $BlockStart )
103 (
104 ^ < ($block_names_re) [^>]* >
105 (?s: .+? )
106 (?: </ \3 > \n )+ # This catches repetitions of the final closing block
107 )
108 $BlockEnd
109 }
110 14 ▶ { ( $1 || q{} ) . $self->_hash_and_save_html($2) }egxm;
Condition100%2/2
$1 || ''
l!l
113
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
111  
112 # We need to treat <hr/> tags as blocks as well, but they don't have
113 # an ending delimiter.
114 180 ${$text} =~ s{
115 ( $BlockStart )
116 (<hr.*\ */?>)
117 }
118 0 ▶ { ( $1 || q{} ) . $self->_hash_and_save_html($2) }egxm;
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
119  
120 180 return;
121 }
122 }
123  
124 35 ▶sub _hash_and_save_html {
Subroutine _hash_and_save_html: called CC=1 SCAR=0.0
125 14 my $self = shift;
126 14 my $html = shift;
127  
128 14 my $sha1 = lc sha1_hex( encode( 'UTF-8', $html ) );
129  
130 14 $self->_save_html_block( $sha1 => $html );
131  
132 14 return 'html:' . $sha1 . "\n";
133 }
134  
135 910 ▶sub _parse_text {
Subroutine _parse_text: called CC=10 SCAR=27.1
136 910 my $self = shift;
137 910 my $text = shift;
138  
139 910 my $last_pos;
140 PARSE:
141 910 while (1) {
142 0 ▶ if ( $self->debug && pos ${$text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and pos ${$text;}) 0 2263 2263
Condition33%1/3
$self->debug and pos ${$text;}
!ll&&!rl&&r
226300
MC/DC0%0/2
$self->debug and pos ${$text;}
$self->debug pos ${$text;}
Truth table33%1/3
A and B
A= $self->debugB= pos ${$text;}
ABresult
0X0
100
111
143 $self->_print_debug( "Remaining text:\n[\n"
144 0 . substr( ${$text}, pos ${$text} )
145 . "\n]\n" );
146 }
147  
148 2263 ▶ if ( ${$text} =~ / \G \p{Space}* \z /xgc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$text;} =~ / \G \p{Space}* \z /cgx) 908 1355 2263
149 908 last;
150 }
151  
152 1355 ▶ my $current_pos = pos ${$text} || 0;
Condition100%2/2
pos ${$text;} || 0
l!l
445910
MC/DC100%1/1
pos ${$text;} || 0
pos ${$text;}
Truth table100%2/2
A
A= pos ${$text;}
Aresult
11
00
153 1355 ▶ if ( defined $last_pos && $last_pos == $current_pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_pos and $last_pos == $current_pos) 0 1355 1355
Condition33%1/3
defined $last_pos and $last_pos == $current_pos
!ll&&!rl&&r
135500
MC/DC0%0/2
defined $last_pos and $last_pos == $current_pos
defined $last_pos $last_pos == $current_pos
Truth table33%1/3
A and B
A= defined $last_posB= $last_pos == $current_pos
ABresult
0X0
100
111
154 0 my $msg
155 = "About to enter an endless loop (pos = $current_pos)!\n";
156 0 $msg .= "\n";
157 0 $msg .= substr( ${$text}, $last_pos );
158 0 $msg .= "\n";
159  
160 0 die $msg;
161 }
162  
163 1355 my @look_for = $self->_possible_block_matches;
164  
165 1355 $self->_debug_look_for(@look_for);
166  
167 1355 for my $block (@look_for) {
168 11783 my $meth = '_match_' . $block;
169  
170 11783 ▶ $self->$meth($text)
Branch: 1 branch
BranchTrueFalseTotal
$self->$meth($text) and next PARSE 733 11048 11781
171 and next PARSE;
172 }
173  
174 620 ▶ $last_pos = pos ${$text} || 0;
Condition50%1/2
pos ${$text;} || 0
l!l
6200
MC/DC0%0/1
pos ${$text;} || 0
pos ${$text;}
Truth table50%1/2
A
A= pos ${$text;}
Aresult
11
00
175 }
176 }
177  
178 1355 ▶sub _possible_block_matches {
Subroutine _possible_block_matches: called CC=3 SCAR=11.0
179 1355 my $self = shift;
180  
181 1355 my @look_for;
182  
183 1355 ▶ push @look_for, qw( hashed_html horizontal_rule )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_list_level 1265 90 1355
184 unless $self->_list_level;
185  
186 1355 push @look_for, qw(
187 html_comment
188 atx_header
189 two_line_header
190 blockquote
191 preformatted
192 list
193 );
194  
195 1355 ▶ push @look_for, 'list_item'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_list_level 90 1265 1355
196 if $self->_list_level;
197  
198 1355 push @look_for, 'paragraph';
199  
200 1355 return @look_for;
201 }
202  
203 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
204 1258 ▶sub _match_hashed_html {
Subroutine _match_hashed_html: called CC=4 SCAR=14.0
205 1258 my $self = shift;
206 1258 my $text = shift;
207  
208 1258 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::BlockParser::BlockStart ^ ( html:([0-9a-f]{40}) \n ) $Markdent::Parser::BlockParser::BlockEnd /cgmx 1244 14 1258
209 $BlockStart
210 ^
211 (
212 html:([0-9a-f]{40})
213 \n
214 )
215 $BlockEnd
216 /xmgc;
217  
218 14 my $html = $self->_get_html_block($2);
219  
220 14 ▶ return unless defined $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 14 14
221  
222 14 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 14 14
223 $1,
224 'hashed html',
225 ) if $self->debug;
226  
227 14 $self->_send_event(
228 HTMLBlock => html => $html,
229 );
230  
231 14 return 1;
232 }
233  
234 1328 ▶sub _match_html_comment {
Subroutine _match_html_comment: called CC=3 SCAR=11.0
235 1328 my $self = shift;
236 1328 my $text = shift;
237  
238 1328 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::BlockParser::EmptyLine*? ^ \p{SpaceSeparator}{0,3} $Markdent::Parser::BlockParser::HTMLComment $Markdent::Parser::BlockParser::HorizontalWS* \n /cgmx 1325 3 1328
239 $EmptyLine*?
240 ^
241 \p{SpaceSeparator}{0,3}
242 $HTMLComment
243 $HorizontalWS*
244 \n
245 /xmgc;
246  
247 3 my $comment = $1;
248  
249 3 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 3 3
250 $comment,
251 'html comment block',
252 ) if $self->debug;
253  
254 3 $self->_detab_text( \$comment );
255  
256 3 $self->_send_event( HTMLCommentBlock => text => $comment );
257  
258 3 return 1;
259 }
260  
261 35 my $AtxHeader = qr/ ^
262 (\#{1,6})
263 (
264 $HorizontalWS*
265 \S
266 .+?
267 )
268 (?:
269 $HorizontalWS*
270 \#+
271 )?
272 \n
273 /xm;
274  
275 1325 ▶sub _match_atx_header {
Subroutine _match_atx_header: called CC=3 SCAR=11.0
276 1325 my $self = shift;
277 1325 my $text = shift;
278  
279 1325 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? ($AtxHeader) /cgmx 1282 43 1325
280 (?:$EmptyLines)?
281 ($AtxHeader)
282 /xmgc;
283  
284 43 my $level = length $2;
285 43 my $header_text = $3 . "\n";
286  
287 43 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 43 43
288 $1,
289 'atx header',
290 [ level => $level ],
291 ) if $self->debug;
292  
293 43 $header_text =~ s/^$HorizontalWS*//;
294  
295 43 $self->_header( $level, $header_text );
296  
297 43 return 1;
298 }
299  
300 35 my $TwoLineHeader = qr/ ^
301 (
302 $HorizontalWS*
303 \S # must have some non-ws
304 .+ # anything else
305 \n
306 )
307 ^(=+|-+) # underline marking a header
308 \n
309 /xm;
310  
311 1282 ▶sub _match_two_line_header {
Subroutine _match_two_line_header: called CC=4 SCAR=13.9
312 1282 my $self = shift;
313 1282 my $text = shift;
314  
315 1282 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? ($TwoLineHeader) /cgmx 1263 19 1282
316 (?:$EmptyLines)?
317 ($TwoLineHeader)
318 /xmgc;
319  
320 19 ▶ my $level = substr( $3, 0, 1 ) eq '=' ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
substr($3, 0, 1) eq '=' ? : 13 6 19
321  
322 19 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 19 19
323 $1,
324 'two-line header',
325 [ level => $level ],
326 ) if $self->debug;
327  
328 19 $self->_header( $level, $2 );
329  
330 19 return 1;
331 }
332  
333 62 ▶sub _header {
Subroutine _header: called CC=1 SCAR=0.0
334 62 my $self = shift;
335 62 my $level = shift;
336 62 my $text = shift;
337  
338 62 $self->_send_event( StartHeader => level => $level );
339  
340 62 $self->_span_parser->parse_block($text);
341  
342 62 $self->_send_event( EndHeader => level => $level );
343  
344 62 return 1;
345 }
346  
347 35 my $HorizontalRule = qr/ ^
348 (
349 \p{SpaceSeparator}{0,3}
350 (?:
351 (?: \* \p{SpaceSeparator}? ){3,}
352 |
353 (?: - \p{SpaceSeparator}? ){3,}
354 |
355 (?: _ \p{SpaceSeparator}? ){3,}
356 )
357 \n
358 )
359 /xm;
360  
361 1244 ▶sub _match_horizontal_rule {
Subroutine _match_horizontal_rule: called CC=3 SCAR=11.0
362 1244 my $self = shift;
363 1244 my $text = shift;
364  
365 1244 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? $HorizontalRule /cgmx 1238 6 1244
366 (?:$EmptyLines)?
367 $HorizontalRule
368 /xmgc;
369  
370 6 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 6 6
371 $1,
372 'horizontal rule',
373 ) if $self->debug;
374  
375 6 $self->_send_event('HorizontalRule');
376  
377 6 return 1;
378 }
379  
380 1263 ▶sub _match_blockquote {
Subroutine _match_blockquote: called CC=4 SCAR=13.9
381 1263 my $self = shift;
382 1263 my $text = shift;
383  
384 1263 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::BlockParser::BlockStart ( ^ > $Markdent::Parser::BlockParser::HorizontalWS* \S (?: .* \n )+? ) (?= $Markdent::Parser::BlockParser::EmptyLine # ... an empty line ^ (?= \S # ... followed by content in column 1 ) (?! # ... which is not > # ... a blockquote $Markdent::Parser::BlockParser::HorizontalWS* \S ) | \s* # or end of the document \z ) /cgmx 1242 21 1263
385 $BlockStart
386 (
387 ^
388 >
389 $HorizontalWS*
390 \S
391 (?:
392 .*
393 \n
394 )+?
395 )
396 (?=
397 $EmptyLine # ... an empty line
398 ^
399 (?=
400 \S # ... followed by content in column 1
401 )
402 (?! # ... which is not
403 > # ... a blockquote
404 $HorizontalWS*
405 \S
406 )
407 |
408 \s* # or end of the document
409 \z
410 )
411 /xmgc;
412  
413 21 my $bq = $1;
414  
415 21 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 21 21
416 $bq,
417 'blockquote',
418 ) if $self->debug;
419  
420 21 $self->_send_event('StartBlockquote');
421  
422 21 $bq =~ s/^>$HorizontalWS?//gxm;
423  
424 # Even if the blockquote is inside a list, we want to look for paragraphs,
425 # not list items.
426 21 my $list_level = $self->_list_level;
427 21 $self->_set_list_level(0);
428  
429 # Dingus treats a new blockquote level as starting a new paragraph as
430 # well. If we treat each change of blockquote level as starting a new
431 # sub-document, we get the same behavior.
432 21 for my $chunk (
433 $self->_split_chunks_on_regex( $bq, qr/^>$HorizontalWS*\S/xm ) ) {
434  
435 29 $self->_parse_text( \$chunk );
436 }
437  
438 21 $self->_set_list_level($list_level);
439  
440 21 $self->_send_event('EndBlockquote');
441  
442 21 return 1;
443 }
444 ## use critic
445  
446 21 ▶sub _split_chunks_on_regex {
Subroutine _split_chunks_on_regex: called CC=9 SCAR=22.1
447 21 my $self = shift;
448 21 my $text = shift;
449 21 my $regex = shift;
450  
451 21 my @chunks;
452 my @chunk;
453 21 my $in_regex = 0;
454  
455 21 for my $line ( split /\n/, $text ) {
456 57 my $new_chunk;
457  
458 57 ▶ if ( $in_regex && $line !~ $regex ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($in_regex and not $line =~ /$regex/) { } 3 54 57
elsif ($line =~ /$regex/ and not $in_regex) { } 5 98 103
Condition66%2/3
$in_regex and not $line =~ /$regex/
!ll&&!rl&&r
5403
MC/DC50%1/2
$in_regex and not $line =~ /$regex/
$in_regex not $line =~ /$regex/
Truth table66%2/3
A and not(B)
A= $in_regexB= not $line =~ /$regex/
ABresult
0X0
100
111
459 3 $in_regex = 0;
460 3 $new_chunk = 1;
461 }
462 ▶ elsif ( $line =~ $regex && !$in_regex ) {
Condition66%2/3
$line =~ /$regex/ and not $in_regex
!ll&&!rl&&r
4905
MC/DC50%1/2
$line =~ /$regex/ and not $in_regex
$line =~ /$regex/ not $in_regex
Truth table66%2/3
A and not(B)
A= $line =~ /$regex/B= not $in_regex
ABresult
0X0
100
111
463 5 $in_regex = 1;
464 5 $new_chunk = 1;
465 }
466  
467 57 ▶ if ($new_chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_chunk) 8 49 57
468 8 ▶ push @chunks, join q{}, map { $_ . "\n" } @chunk
Branch: 1 branch
BranchTrueFalseTotal
if @chunk 8 0 8
469 if @chunk;
470 8 @chunk = ();
471 }
472  
473 57 push @chunk, $line;
474 }
475  
476 21 ▶ push @chunks, join q{}, map { $_ . "\n" } @chunk
Branch: 1 branch
BranchTrueFalseTotal
if @chunk 21 0 21
477 if @chunk;
478  
479 21 return @chunks;
480 }
481  
482 35 my $PreLine = qr/ ^
483 (?:
484 \p{spaceSeparator}{4,}
485 |
486 \t
487 )
488 $HorizontalWS*
489 \S
490 .*
491 \n
492 /xm;
493  
494 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
495 1242 ▶sub _match_preformatted {
Subroutine _match_preformatted: called CC=3 SCAR=11.0
496 1242 my $self = shift;
497 1242 my $text = shift;
498  
499 1242 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::BlockParser::BlockStart ( (?: $PreLine (?:$Markdent::Parser::BlockParser::EmptyLine)* )* $PreLine ) /cgmx 1100 142 1242
500 $BlockStart
501 (
502 (?:
503 $PreLine
504 (?:$EmptyLine)*
505 )*
506 $PreLine
507 )
508 /xmgc;
509  
510 142 my $pre = $1;
511  
512 142 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 142 142
513 $pre,
514 'preformatted',
515 ) if $self->debug;
516  
517 142 $pre =~ s/^(?:\p{SpaceSeparator}{4}|\t)//gm;
518  
519 142 $self->_detab_text( \$pre );
520  
521 142 $self->_send_event( Preformatted => text => $pre );
522  
523 142 return 1;
524 }
525 ## use critic
526  
527 35 my $Bullet = qr/ (?:
528 \p{SpaceSeparator}{0,3}
529 (
530 [\+\*\-] # unordered list bullet
531 |
532 \d+\. # ordered list number
533 )
534 )
535 $HorizontalWS+
536 /xm;
537  
538 2072 ▶sub _list_re {
Subroutine _list_re: called CC=2 SCAR=6.9
539 2072 my $self = shift;
540  
541 2072 my $block_start;
542  
543 2072 ▶ if ( $self->_list_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_list_level) { } 98 1974 2072
544 98 $block_start = qr/(?: (?<= \n ) | $EmptyLines )/xm;
545 }
546 else {
547 1974 $block_start = qr/ $BlockStart /xm;
548 }
549  
550 2072 my $list = qr/ $block_start
551 (
552 $Bullet
553 (?: .* \n )+?
554 )
555 /xm;
556  
557 2072 return $list;
558 }
559  
560 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
561 1100 ▶sub _match_list {
Subroutine _match_list: called CC=4 SCAR=13.9
562 1100 my $self = shift;
563 1100 my $text = shift;
564  
565 1100 my $list_re = $self->_list_re;
566  
567 1100 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $list_re (?= # list ends with $Markdent::Parser::BlockParser::EmptyLine # ... an empty line (?: (?= $HorizontalRule # ... followed by a horizontal rule ) | (?= \S # ... or followed by content in column 1 ) (?! # ... which is not $Bullet # ... a bullet ) ) | \s* # or end of the document \z ) /cgmx 1061 39 1100
568 $list_re
569 (?= # list ends with
570 $EmptyLine # ... an empty line
571 (?:
572 (?=
573 $HorizontalRule # ... followed by a horizontal rule
574 )
575 |
576 (?=
577 \S # ... or followed by content in column 1
578 )
579 (?! # ... which is not
580 $Bullet # ... a bullet
581 )
582 )
583 |
584 \s* # or end of the document
585 \z
586 )
587 /xmgc;
588  
589 39 my $list = $1;
590 39 my $bullet = $2;
591  
592 39 ▶ my $type = $bullet =~ /\d/ ? 'OrderedList' : 'UnorderedList';
Branch: 1 branch
BranchTrueFalseTotal
$bullet =~ /\d/ ? : 7 32 39
593  
594 39 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 39 39
595 $list,
596 $type,
597 ) if $self->debug;
598  
599 39 $self->_send_event( 'Start' . $type );
600  
601 39 $self->_inc_list_level;
602  
603 39 my @items = $self->_split_list_items($list);
604  
605 39 $self->_handle_list_items( $type, @items );
606  
607 39 $self->_dec_list_level;
608  
609 39 $self->_send_event( 'End' . $type );
610  
611 39 return 1;
612 }
613 ## use critic
614  
615 39 ▶sub _split_list_items {
Subroutine _split_list_items: called CC=5 SCAR=16.1
616 39 my $self = shift;
617 39 my $list = shift;
618  
619 39 my @items;
620 my @chunk;
621  
622 39 for my $line ( split /\n/, $list ) {
623 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
624 113 ▶ if ( $line =~ /^$Bullet/ && @chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$Bullet/ and @chunk) 36 77 113
Condition100%3/3
$line =~ /^$Bullet/ and @chunk
!ll&&!rl&&r
383936
MC/DC100%2/2
$line =~ /^$Bullet/ and @chunk
$line =~ /^$Bullet/ @chunk
Truth table100%3/3
A and B
A= $line =~ /^$Bullet/B= @chunk
ABresult
0X0
100
111
625 ## use critic
626 36 push @items, join q{}, map { $_ . "\n" } @chunk;
627  
628 36 @chunk = ();
629 }
630  
631 113 push @chunk, $line;
632 }
633  
634 39 ▶ push @items, join q{}, map { $_ . "\n" } @chunk
Branch: 1 branch
BranchTrueFalseTotal
if @chunk 39 0 39
635 if @chunk;
636  
637 39 return @items;
638 }
639  
640 39 ▶sub _handle_list_items {
Subroutine _handle_list_items: called CC=10 SCAR=23.1
641 39 my $self = shift;
642 39 my $type = shift;
643 39 my @items = @_;
644  
645 39 my $ordinal_list_num = 1;
646 39 for my $item (@items) {
647 75 $item =~ s/^$Bullet//;
648  
649 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
650 75 ▶ my $bullet
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'OrderedList' ? : 20 55 75
651 = $type eq 'OrderedList' ? ( $ordinal_list_num++ ) . q{.} : $1;
652 ## use critic
653  
654 75 $self->_send_event( StartListItem => bullet => $bullet );
655  
656 # This strips out indentation from any lines beyond the first. This
657 # causes the block parser to see a sub-list as starting a new list
658 # when it parses the entire item for blocks.
659 75 $item =~ s/(?<=\n)^ (?: \p{SpaceSeparator}{4} | \t )//xgm;
660  
661 75 ▶ $self->_print_debug("Parsing list item for blocks:\n[$item]\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 75 75
662 if $self->debug;
663  
664 # This is a hack to ensure that the last item in a loose list (each
665 # item is a paragraph) also is treated as a paragraph, not just a list
666 # item.
667 75 ▶ if ( $item eq $items[-1] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item eq $items[-1]) { } 39 36 75
elsif ($item =~ /^$Markdent::Parser::BlockParser::EmptyLine\z/m) { } 6 30 36
668 39 ▶ if ( @items > 1
Branch: 1 branch
BranchTrueFalseTotal
if (@items > 1 and $items[-2] =~ /^$Markdent::Parser::BlockParser::EmptyLine\z/m) { } 5 34 39
Condition100%3/3
@items > 1 and $items[-2] =~ /^$Markdent::Parser::BlockParser::EmptyLine\z/m
!ll&&!rl&&r
12225
MC/DC100%2/2
@items > 1 and $items[-2] =~ /^$Markdent::Parser::BlockParser::EmptyLine\z/m
@items > 1 $items[-2] =~ /^$Markdent::Parser::BlockParser::EmptyLine\z/m
Truth table100%3/3
A and B
A= @items > 1B= $items[-2] =~ /^$Markdent::Parser::BlockParser::EmptyLine\z/m
ABresult
0X0
100
111
669 && $items[-2] =~ /^$EmptyLine\z/m ) {
670  
671 5 ▶ $self->_print_debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 5 5
672 "Treating last list item as a paragraph because previous item ends with empty line\n"
673 ) if $self->debug;
674  
675 5 $self->_treat_list_item_as_paragraph;
676 }
677 else {
678 34 $self->_treat_list_item_as_line;
679 }
680 }
681 elsif ( $item =~ /^$EmptyLine\z/m ) {
682 6 ▶ $self->_print_debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 6 6
683 "Treating item as a paragraph because it ends with empty line\n"
684 ) if $self->debug;
685  
686 6 $self->_treat_list_item_as_paragraph;
687 }
688 else {
689 30 $self->_treat_list_item_as_line;
690 }
691  
692 75 $self->_parse_text( \$item );
693  
694 75 $self->_send_event('EndListItem');
695 }
696 }
697  
698 # A list item matches multiple lines of text without any separating
699 # newlines. These lines stop when we see a blockquote or indented list
700 # bullet. This match is only done inside a list, and lets us distinguish
701 # between list items which contain paragraphs and those which don't.
702 #
703 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
704 78 ▶sub _match_list_item {
Subroutine _match_list_item: called CC=5 SCAR=16.1
705 78 my $self = shift;
706 78 my $text = shift;
707  
708 78 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G ((?: ^ \p{SpaceSeparator}* \S .* \n )+?) (?= ^ $Bullet | ^ > \p{SpaceSeparator}* \S .* \n | \z ) /cgmx 10 68 78
709 ((?:
710 ^
711 \p{SpaceSeparator}*
712 \S
713 .*
714 \n
715 )+?)
716 (?=
717 ^
718 $Bullet
719 |
720 ^
721 > \p{SpaceSeparator}*
722 \S
723 .*
724 \n
725 |
726 \z
727 )
728 /xmgc;
729  
730 68 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 68 68
731 $1,
732 'list_item',
733 ) if $self->debug;
734  
735 68 ▶ $self->_send_event('StartParagraph')
Branch: 1 branch
BranchTrueFalseTotal
if $self->_list_item_is_paragraph 6 62 68
736 if $self->_list_item_is_paragraph;
737  
738 68 $self->_span_parser->parse_block($1);
739  
740 68 ▶ $self->_send_event('EndParagraph')
Branch: 1 branch
BranchTrueFalseTotal
if $self->_list_item_is_paragraph 6 62 68
741 if $self->_list_item_is_paragraph;
742  
743 68 return 1;
744 }
745  
746 972 ▶sub _match_paragraph {
Subroutine _match_paragraph: called CC=3 SCAR=11.0
747 972 my $self = shift;
748 972 my $text = shift;
749  
750 972 my $list_re = $self->_list_re;
751  
752 # At this point anything that is not an empty line must be a paragraph.
753 972 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? ((?: ^ $Markdent::Parser::BlockParser::HorizontalWS* \S .* \n )+?) (?: $Markdent::Parser::BlockParser::BlockEnd | (?= $HorizontalRule ) | (?= $TwoLineHeader ) | (?= $AtxHeader ) | (?= $list_re ) ) /cgmx 620 352 972
754 (?:$EmptyLines)?
755 ((?:
756 ^
757 $HorizontalWS*
758 \S
759 .*
760 \n
761 )+?)
762 (?:
763 $BlockEnd
764 |
765 (?= $HorizontalRule )
766 |
767 (?= $TwoLineHeader )
768 |
769 (?= $AtxHeader )
770 |
771 (?= $list_re )
772 )
773 /xmgc;
774  
775 352 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 352 352
776 $1,
777 'paragraph',
778 ) if $self->debug;
779  
780 352 $self->_send_event('StartParagraph');
781  
782 350 $self->_span_parser->parse_block($1);
783  
784 350 $self->_send_event('EndParagraph');
785  
786 350 return 1;
787 }
788 ## use critic
789  
790 35 __PACKAGE__->meta->make_immutable;
791  
792 35 1;
793  
794 # ABSTRACT: Block parser for standard Markdown
795  
796 __END__
« Markdent/Dialect/Theory/BlockParser.pm ↑ Summary Markdent/Role/DebugPrinter.pm »