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.39';
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 182 ▶ 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 sub parse_document {
75 178 ▶ my $self = shift;
Subroutine parse_document: called CC=1 SCAR=0.0
Pod documented
76 178 my $text = shift;
77  
78 178 $self->_treat_list_item_as_line;
79  
80 178 $self->_hash_html_blocks($text);
81  
82 178 $self->_span_parser->extract_link_ids($text);
83  
84 178 $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 sub _hash_html_blocks {
98 178 ▶ my $self = shift;
Subroutine _hash_html_blocks: called CC=3 SCAR=11.3
99 178 my $text = shift;
100  
101 178 ${$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 178 ${$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 178 return;
121 }
122 }
123  
124 35 sub _hash_and_save_html {
125 14 ▶ my $self = shift;
Subroutine _hash_and_save_html: called CC=1 SCAR=0.0
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 sub _parse_text {
136 908 ▶ my $self = shift;
Subroutine _parse_text: called CC=10 SCAR=27.1
137 908 my $text = shift;
138  
139 908 my $last_pos;
140 PARSE:
141 908 while (1) {
142 2259 ▶ if ( $self->debug && pos ${$text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and pos ${$text;}) 0 2259 2259
Condition33%1/3
$self->debug and pos ${$text;}
!ll&&!rl&&r
225900
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 2259 ▶ if ( ${$text} =~ / \G \p{Space}* \z /xgc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$text;} =~ / \G \p{Space}* \z /cgx) 906 1353 2259
149 906 last;
150 }
151  
152 1353 ▶ my $current_pos = pos ${$text} || 0;
Condition100%2/2
pos ${$text;} || 0
l!l
445908
MC/DC100%1/1
pos ${$text;} || 0
pos ${$text;}
Truth table100%2/2
A
A= pos ${$text;}
Aresult
11
00
153 1353 ▶ if ( defined $last_pos && $last_pos == $current_pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_pos and $last_pos == $current_pos) 0 1353 1353
Condition33%1/3
defined $last_pos and $last_pos == $current_pos
!ll&&!rl&&r
135300
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 1353 my @look_for = $self->_possible_block_matches;
164  
165 1353 $self->_debug_look_for(@look_for);
166  
167 1353 for my $block (@look_for) {
168 11765 my $meth = '_match_' . $block;
169  
170 11765 ▶ $self->$meth($text)
Branch: 1 branch
BranchTrueFalseTotal
$self->$meth($text) and next PARSE 731 11032 11763
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 sub _possible_block_matches {
179 1353 ▶ my $self = shift;
Subroutine _possible_block_matches: called CC=3 SCAR=11.0
180  
181 1353 my @look_for;
182  
183 1353 ▶ push @look_for, qw( hashed_html horizontal_rule )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_list_level 1263 90 1353
184 unless $self->_list_level;
185  
186 1353 push @look_for, qw(
187 html_comment
188 atx_header
189 two_line_header
190 blockquote
191 preformatted
192 list
193 );
194  
195 1353 ▶ push @look_for, 'list_item'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_list_level 90 1263 1353
196 if $self->_list_level;
197  
198 1353 push @look_for, 'paragraph';
199  
200 1353 return @look_for;
201 }
202  
203 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
204 sub _match_hashed_html {
205 1256 ▶ my $self = shift;
Subroutine _match_hashed_html: called CC=4 SCAR=14.0
206 1256 my $text = shift;
207  
208 1256 ▶ 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 1242 14 1256
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 sub _match_html_comment {
235 1326 ▶ my $self = shift;
Subroutine _match_html_comment: called CC=3 SCAR=11.0
236 1326 my $text = shift;
237  
238 1326 ▶ 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 1323 3 1326
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 sub _match_atx_header {
276 1323 ▶ my $self = shift;
Subroutine _match_atx_header: called CC=3 SCAR=11.0
277 1323 my $text = shift;
278  
279 1323 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? ($AtxHeader) /cgmx 1280 43 1323
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 sub _match_two_line_header {
312 1280 ▶ my $self = shift;
Subroutine _match_two_line_header: called CC=4 SCAR=13.9
313 1280 my $text = shift;
314  
315 1280 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? ($TwoLineHeader) /cgmx 1261 19 1280
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 sub _header {
334 62 ▶ my $self = shift;
Subroutine _header: called CC=1 SCAR=0.0
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 sub _match_horizontal_rule {
362 1242 ▶ my $self = shift;
Subroutine _match_horizontal_rule: called CC=3 SCAR=11.0
363 1242 my $text = shift;
364  
365 1242 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G (?:$Markdent::Parser::BlockParser::EmptyLines)? $HorizontalRule /cgmx 1236 6 1242
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 sub _match_blockquote {
381 1261 ▶ my $self = shift;
Subroutine _match_blockquote: called CC=4 SCAR=13.9
382 1261 my $text = shift;
383  
384 1261 ▶ 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 1240 21 1261
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 sub _split_chunks_on_regex {
447 21 ▶ my $self = shift;
Subroutine _split_chunks_on_regex: called CC=9 SCAR=22.1
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 sub _match_preformatted {
496 1240 ▶ my $self = shift;
Subroutine _match_preformatted: called CC=3 SCAR=11.0
497 1240 my $text = shift;
498  
499 1240 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::BlockParser::BlockStart ( (?: $PreLine (?:$Markdent::Parser::BlockParser::EmptyLine)* )* $PreLine ) /cgmx 1098 142 1240
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 sub _list_re {
539 2068 ▶ my $self = shift;
Subroutine _list_re: called CC=2 SCAR=6.9
540  
541 2068 my $block_start;
542  
543 2068 ▶ if ( $self->_list_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_list_level) { } 98 1970 2068
544 98 $block_start = qr/(?: (?<= \n ) | $EmptyLines )/xm;
545 }
546 else {
547 1970 $block_start = qr/ $BlockStart /xm;
548 }
549  
550 2068 my $list = qr/ $block_start
551 (
552 $Bullet
553 (?: .* \n )+?
554 )
555 /xm;
556  
557 2068 return $list;
558 }
559  
560 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
561 sub _match_list {
562 1098 ▶ my $self = shift;
Subroutine _match_list: called CC=4 SCAR=13.9
563 1098 my $text = shift;
564  
565 1098 my $list_re = $self->_list_re;
566  
567 1098 ▶ 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 1059 39 1098
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 sub _split_list_items {
616 39 ▶ my $self = shift;
Subroutine _split_list_items: called CC=5 SCAR=16.1
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 sub _handle_list_items {
641 39 ▶ my $self = shift;
Subroutine _handle_list_items: called CC=10 SCAR=23.1
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 sub _match_list_item {
705 78 ▶ my $self = shift;
Subroutine _match_list_item: called CC=5 SCAR=16.1
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 sub _match_paragraph {
747 970 ▶ my $self = shift;
Subroutine _match_paragraph: called CC=3 SCAR=11.0
748 970 my $text = shift;
749  
750 970 my $list_re = $self->_list_re;
751  
752 # At this point anything that is not an empty line must be a paragraph.
753 970 ▶ 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 350 970
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 350 ▶ $self->_debug_parse_result(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 350 350
776 $1,
777 'paragraph',
778 ) if $self->debug;
779  
780 350 $self->_send_event('StartParagraph');
781  
782 348 $self->_span_parser->parse_block($1);
783  
784 348 $self->_send_event('EndParagraph');
785  
786 348 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__
797  
798 =pod
799  
800 =encoding UTF-8
801  
802 =head1 NAME
803  
804 Markdent::Parser::BlockParser - Block parser for standard Markdown
805  
806 =head1 VERSION
807  
808 version 0.39
809  
810 =head1 DESCRIPTION
811  
812 This class parses blocks for the standard Markdown dialect (as defined by
813 Daring Fireball and mdtest).
814  
815 =head1 METHODS
816  
817 This class provides the following methods:
818  
819 =head2 Markdent::Parser::BlockParser->new( handler => $handler, span_parser => $span_parser )
820  
821 Creates a new block parser object. You must provide a span parser object.
822  
823 =head2 $block_parser->parse_document(\$markdown)
824  
825 This method takes a reference to a markdown string and parses it for
826 blocks. Each block which contains text (except preformatted text) will be
827 parsed for span-level markup using this object's C<span_parser>.
828  
829 =head1 ROLES
830  
831 This class does the L<Markdent::Role::BlockParser>,
832 L<Markdent::Role::AnyParser>, and L<Markdent::Role::DebugPrinter> roles.
833  
834 =head1 BUGS
835  
836 See L<Markdent> for bug reporting details.
837  
838 Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
839  
840 I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
841  
842 =head1 SOURCE
843  
844 The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
845  
846 =head1 AUTHOR
847  
848 Dave Rolsky <autarch@urth.org>
849  
850 =head1 COPYRIGHT AND LICENSE
851  
852 This software is copyright (c) 2021 by Dave Rolsky.
853  
854 This is free software; you can redistribute it and/or modify it under
855 the same terms as the Perl 5 programming language system itself.
856  
857 The full text of the license can be found in the
858 F<LICENSE> file included with this distribution.
859  
860 =cut
« Markdent/Dialect/Theory/BlockParser.pm ↑ Summary Markdent/Role/DebugPrinter.pm »