Markdent/Parser/SpanParser.pm

filter: Statementstmt 95.5% 449 / 470 Branchbran 86.0% 117 / 136 Conditioncond 84.6% 22 / 26 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 69 / 69 Podpod 100% 2 / 2 totaltotal 93.3% 671 / 719 CC 89 SCAR 14.6
CC
89
Cov
93%
_convert_invalid_start_events_to_text
9.2
_look_for_strong_and_emphasis
9.0
_merge_consecutive_text_events
9.0
CRAP 4.3
« Markdent/Parser.pm ↑ Summary Markdent/Role/AnyParser.pm »
1 package Markdent::Parser::SpanParser;
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 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 35 our $VERSION = '0.41';
10  
11 35 ▶use List::AllUtils qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Markdent::Event::AutoLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Markdent::Event::EndCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Markdent::Event::EndEmphasis;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 35 ▶use Markdent::Event::EndHTMLTag;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 35 ▶use Markdent::Event::EndLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 35 ▶use Markdent::Event::EndStrong;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 35 ▶use Markdent::Event::HTMLComment;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 35 ▶use Markdent::Event::HTMLEntity;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 35 ▶use Markdent::Event::HTMLTag;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 35 ▶use Markdent::Event::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 35 ▶use Markdent::Event::LineBreak;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 35 ▶use Markdent::Event::StartCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use Markdent::Event::StartEmphasis;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use Markdent::Event::StartHTMLTag;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 35 ▶use Markdent::Event::StartLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 35 ▶use Markdent::Event::StartStrong;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Markdent::Event::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use Markdent::Regexes qw( $HorizontalWS $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::SpanParser';
37  
38 has __pending_events => (
39 traits => ['Array'],
40 is => 'rw',
41 isa => t( 'ArrayRef', of => t('EventObject') ),
42 186 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 35 init_arg => undef,
44 handles => {
45 _pending_events => 'elements',
46 _add_pending_event => 'push',
47 _clear_pending_events => 'clear',
48 },
49 );
50  
51 35 has _span_text_buffer => (
52 traits => ['String'],
53 is => 'ro',
54 isa => t('Str'),
55 default => q{},
56 init_arg => undef,
57 handles => {
58 _save_span_text => 'append',
59 _has_span_text_buffer => 'length',
60 _clear_span_text_buffer => 'clear',
61 },
62 );
63  
64 has _links_by_id => (
65 traits => ['Hash'],
66 is => 'ro',
67 isa => t( 'HashRef', of => t('ArrayRef') ),
68 186 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 35 init_arg => undef,
70 handles => {
71 _add_link_by_id => 'set',
72 _get_link_by_id => 'get',
73 },
74 );
75  
76 35 has _emphasis_start_delimiter_re => (
77 is => 'ro',
78 isa => t('RegexpRef'),
79 lazy => 1,
80 builder => '_build_emphasis_start_delimiter_re',
81 init_arg => undef,
82 );
83  
84 35 has _escape_re => (
85 is => 'ro',
86 isa => t('RegexpRef'),
87 lazy => 1,
88 builder => '_build_escape_re',
89 init_arg => undef,
90 );
91  
92 35 has _line_break_re => (
93 is => 'ro',
94 isa => t('RegexpRef'),
95 lazy => 1,
96 builder => '_build_line_break_re',
97 init_arg => undef,
98 );
99  
100 35 has _escapable_chars => (
101 is => 'ro',
102 isa => t( 'ArrayRef', of => t('Str') ),
103 lazy => 1,
104 builder => '_build_escapable_chars',
105 );
106  
107 180 ▶sub extract_link_ids {
Subroutine extract_link_ids: called CC=1 SCAR=0.0
Pod documented
108 180 my $self = shift;
109 180 my $text = shift;
110  
111 180 ${$text} =~ s/ ^
112 \p{SpaceSeparator}{0,3}
113 \[ ([^]]+) \]
114 :
115 \p{SpaceSeparator}*
116 \n?
117 \p{SpaceSeparator}*
118 (.+)
119 \n
120 /
121 25 $self->_process_id_for_link( $1, $2 );
122 /egxm;
123 }
124  
125 25 ▶sub _process_id_for_link {
Subroutine _process_id_for_link: called CC=1 SCAR=0.0
126 25 my $self = shift;
127 25 my $id = shift;
128 25 my $id_text = shift;
129  
130 25 $id_text =~ s/\s+$//;
131  
132 25 my ( $uri, $title ) = $self->_parse_uri_and_title($id_text);
133  
134 25 $self->_add_link_by_id( $id => [ $uri, $title ] );
135  
136 25 return q{};
137 }
138  
139 39 ▶sub _parse_uri_and_title {
Subroutine _parse_uri_and_title: called CC=3 SCAR=11.0
140 39 my $self = shift;
141 39 my $text = shift;
142  
143 39 $text =~ s/^\s+|\s+$//g;
144  
145 39 my ( $uri, $title ) = split /$HorizontalWS+/, $text, 2;
146  
147 39 ▶ $uri = q{}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 4 35 39
148 unless defined $uri;
149  
150 39 $uri =~ s/^<|>$//g;
151 39 ▶ $title =~ s/^"|"$//g
Branch: 1 branch
BranchTrueFalseTotal
if defined $title 16 23 39
152 if defined $title;
153  
154 39 return ( $uri, $title );
155 }
156  
157 1100 ▶sub parse_block {
Subroutine parse_block: called CC=3 SCAR=11.0
Pod documented
158 1100 my $self = shift;
159 1100 my $text = shift;
160  
161 1100 ▶ $self->_print_debug("Parsing text for span-level markup\n\n$text\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1100 1100
162 if $self->debug;
163  
164 # Note that we have to pass a _reference_ to text in order to make sure
165 # that we are matching the same variable with /g regexes each time.
166 1100 $self->_parse_text( \$text );
167  
168 # This catches any bad start events that were found after the last end
169 # event, or if there were _no_ end events at all.
170 1100 $self->_convert_invalid_start_events_to_text('is done');
171  
172 1100 $self->_debug_pending_events('before text merging');
173  
174 1100 $self->_merge_consecutive_text_events;
175  
176 1100 $self->_debug_pending_events('after text merging');
177  
178 1100 $self->handler->handle_event($_) for $self->_pending_events;
179  
180 1100 $self->_clear_pending_events;
181  
182 1100 return;
183 }
184  
185 1132 ▶sub _parse_text {
Subroutine _parse_text: called CC=7 SCAR=20.2
186 1132 my $self = shift;
187 1132 my $text = shift;
188  
189 PARSE:
190 1132 while (1) {
191 0 ▶ if ( $self->debug && pos ${$text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and pos ${$text;}) 0 3097 3097
Condition33%1/3
$self->debug and pos ${$text;}
!ll&&!rl&&r
309700
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
192 $self->_print_debug( "Remaining text:\n[\n"
193 0 . substr( ${$text}, pos ${$text} )
194 . "\n]\n" );
195 }
196  
197 3097 ▶ if ( ${$text} =~ /\G\z/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$text;} =~ /\G\z/cg) 1132 1965 3097
198 1132 $self->_event_for_text_buffer;
199 1132 last;
200 }
201  
202 1965 my @look_for = $self->_possible_span_matches;
203  
204 1965 $self->_debug_look_for(@look_for);
205  
206 1965 for my $span (@look_for) {
207 17554 ▶ my ( $markup, @args ) = ref $span ? @{$span} : $span;
Branch: 1 branch
BranchTrueFalseTotal
ref $span ? : 358 17196 17554
208  
209 17554 my $meth = '_match_' . $markup;
210  
211 17554 ▶ $self->$meth( $text, @args )
Branch: 1 branch
BranchTrueFalseTotal
$self->$meth($text, @args) and next PARSE 401 17153 17554
212 and next PARSE;
213 }
214  
215 1564 $self->_match_plain_text($text);
216 }
217 }
218  
219 1965 ▶sub _possible_span_matches {
Subroutine _possible_span_matches: called CC=3 SCAR=11.0
220 1965 my $self = shift;
221  
222 1965 my %open = $self->_open_start_events_for_span( 'code', 'link' );
223 1965 ▶ if ( my $event = $open{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $event = $open{"code"}) 199 1766 1965
224 199 return [ 'code_end', $event->delimiter ];
225 }
226  
227 1766 my @look_for = 'escape';
228  
229 1766 push @look_for, $self->_look_for_strong_and_emphasis;
230  
231 1766 push @look_for, 'code_start';
232  
233 1766 ▶ unless ( $open{link} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($open{"link"}) 1723 43 1766
234 1723 push @look_for, qw( auto_link link image );
235 }
236  
237 1766 push @look_for, 'html_comment', 'html_tag', 'html_entity', 'line_break';
238  
239 1766 return @look_for;
240 }
241  
242 1766 ▶sub _look_for_strong_and_emphasis {
Subroutine _look_for_strong_and_emphasis: called CC=9 SCAR=22.0
243 1766 my $self = shift;
244  
245 1766 my %open = $self->_open_start_events_for_span( 'strong', 'emphasis' );
246  
247 # If we are in both, we need to try to end the most recent one first.
248 1766 ▶ if ( $open{strong} && $open{emphasis} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($open{'strong'} and $open{'emphasis'}) { } 18 1748 1766
elsif ($open{'emphasis'}) { } 82 1666 1748
elsif ($open{'strong'}) { } 45 1621 1666
Condition100%3/3
$open{"strong"} and $open{"emphasis"}
!ll&&!rl&&r
17034518
MC/DC100%2/2
$open{"strong"} and $open{"emphasis"}
$open{"strong"} $open{"emphasis"}
Truth table100%3/3
A and B
A= $open{"strong"}B= $open{"emphasis"}
ABresult
0X0
100
111
249 18 my $last_saw;
250 18 for my $event ( $self->_pending_events ) {
251 86 my $event_name = $event->event_name;
252 86 ▶ if ( $event_name eq 'start_strong' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($event_name eq 'start_strong') { } 20 66 86
elsif ($event_name eq 'start_emphasis') { } 22 44 66
253 20 $last_saw = 'strong';
254 }
255 elsif ( $event_name eq 'start_emphasis' ) {
256 22 $last_saw = 'emphasis';
257 }
258 }
259  
260 my @order
261 18 ▶ = $last_saw eq 'strong'
Branch: 1 branch
BranchTrueFalseTotal
$last_saw eq 'strong' ? : 0 18 18
262 ? qw( strong emphasis )
263 : qw( emphasis strong );
264  
265 18 return map { [ $_ . '_end', $open{$_}->delimiter ] } @order;
266 }
267 elsif ( $open{emphasis} ) {
268 return (
269 'strong_start',
270 82 [ 'emphasis_end', $open{emphasis}->delimiter ]
271 );
272 }
273 elsif ( $open{strong} ) {
274 return (
275 45 [ 'strong_end', $open{strong}->delimiter ],
276 'emphasis_start'
277 );
278 }
279  
280 # We look for strong first since it's a longer version of emphasis (we
281 # need to try to match ** before *).
282 1621 return ( 'strong_start', 'emphasis_start' );
283 }
284  
285 3869 ▶sub _open_start_events_for_span {
Subroutine _open_start_events_for_span: called CC=4 SCAR=13.9
286 3869 my $self = shift;
287 3869 my %wanted_start = map { 'start_' . $_ => $_ } @_;
288 3869 my %wanted_end = map { 'end_' . $_ => $_ } @_;
289  
290 3869 my %open;
291 3869 for my $event ( $self->_pending_events ) {
292 7578 my $event_name = $event->event_name;
293 $open{ $wanted_start{$event_name} } = $event
294 7578 ▶ if $wanted_start{$event_name};
Branch: 1 branch
BranchTrueFalseTotal
if $wanted_start{$event_name} 1216 6362 7578
295  
296 delete $open{ $wanted_end{$event_name} }
297 7578 ▶ if $wanted_end{$event_name};
Branch: 1 branch
BranchTrueFalseTotal
if $wanted_end{$event_name} 809 6769 7578
298 }
299  
300 3869 return %open;
301 }
302  
303 146 ▶sub _build_emphasis_start_delimiter_re {
Subroutine _build_emphasis_start_delimiter_re: called CC=1 SCAR=0.0
304 146 my $self = shift;
305  
306 146 return qr/(?:\*|_)/;
307 }
308  
309 165 ▶sub _build_escapable_chars {
Subroutine _build_escapable_chars: called CC=1 SCAR=0.0
310 165 return [ qw( ` * _ { } [ ] ( ) + - . ! < > ~ ), '\\', '#' ];
311 }
312  
313 165 ▶sub _build_escape_re {
Subroutine _build_escape_re: called CC=1 SCAR=0.0
314 165 my $self = shift;
315  
316 165 my $chars = join q{}, uniq( @{ $self->_escapable_chars } );
317  
318 165 return qr/\\([\Q$chars\E])/;
319 }
320  
321 165 ▶sub _build_line_break_re {
Subroutine _build_line_break_re: called CC=1 SCAR=0.0
322 165 my $self = shift;
323  
324 165 return qr/\p{SpaceSeparator}{2}\n/;
325 }
326  
327 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
328 1766 ▶sub _match_escape {
Subroutine _match_escape: called CC=3 SCAR=11.0
329 1766 my $self = shift;
330 1766 my $text = shift;
331  
332 1766 my $escape_re = $self->_escape_re;
333  
334 1766 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G ($escape_re) /cgx 1753 13 1766
335 ($escape_re)
336 /xgc;
337  
338 13 ▶ $self->_print_debug("Interpreting as escaped character\n\n[$1]\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 13 13
339 if $self->debug;
340  
341 13 $self->_save_span_text($2);
342  
343 13 return 1;
344 }
345  
346 1690 ▶sub _match_strong_start {
Subroutine _match_strong_start: called CC=2 SCAR=6.9
347 1690 my $self = shift;
348 1690 my $text = shift;
349  
350 1690 ▶ my ($delim) = $self->_match_delimiter_start( $text, qr/(?:\*\*|__)/ )
Branch: 1 branch
BranchTrueFalseTotal
my($delim) = $self->_match_delimiter_start($text, qr/(?:\*\*|__)/) or return 18 1672 1690
351 or return;
352  
353 18 my $event = $self->_make_event( StartStrong => delimiter => $delim );
354  
355 18 $self->_markup_event($event);
356  
357 18 return 1;
358 }
359  
360 57 ▶sub _match_strong_end {
Subroutine _match_strong_end: called CC=2 SCAR=6.9
361 57 my $self = shift;
362 57 my $text = shift;
363 57 my $delim = shift;
364  
365 57 ▶ $self->_match_delimiter_end( $text, qr/\Q$delim\E/ )
Branch: 1 branch
BranchTrueFalseTotal
$self->_match_delimiter_end($text, qr/\Q$delim\E/) or return 12 45 57
366 or return;
367  
368 12 my $event = $self->_make_event( EndStrong => delimiter => $delim );
369  
370 12 $self->_markup_event($event);
371  
372 12 return 1;
373 }
374  
375 1623 ▶sub _match_emphasis_start {
Subroutine _match_emphasis_start: called CC=2 SCAR=6.9
376 1623 my $self = shift;
377 1623 my $text = shift;
378  
379 1623 ▶ my ($delim) = $self->_match_delimiter_start(
Branch: 1 branch
BranchTrueFalseTotal
my($delim) = $self->_match_delimiter_start($text, $self->_emphasis_start_delimiter_re) or return 50 1573 1623
380 $text,
381 $self->_emphasis_start_delimiter_re,
382 ) or return;
383  
384 50 my $event = $self->_make_event( StartEmphasis => delimiter => $delim );
385  
386 50 $self->_markup_event($event);
387  
388 50 return 1;
389 }
390  
391 100 ▶sub _match_emphasis_end {
Subroutine _match_emphasis_end: called CC=2 SCAR=6.9
392 100 my $self = shift;
393 100 my $text = shift;
394 100 my $delim = shift;
395  
396 100 ▶ $self->_match_delimiter_end(
Branch: 1 branch
BranchTrueFalseTotal
$self->_match_delimiter_end($text, $self->_emphasis_end_delimiter_re($delim)) or return 46 54 100
397 $text,
398 $self->_emphasis_end_delimiter_re($delim),
399 ) or return;
400  
401 46 my $event = $self->_make_event( EndEmphasis => delimiter => $delim );
402  
403 46 $self->_markup_event($event);
404  
405 46 return 1;
406 }
407 ## use critic
408  
409 90 ▶sub _emphasis_end_delimiter_re {
Subroutine _emphasis_end_delimiter_re: called CC=1 SCAR=0.0
410 90 my $self = shift;
411 90 my $delim = shift;
412  
413 90 return qr/\Q$delim\E/;
414 }
415  
416 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
417 1627 ▶sub _match_code_start {
Subroutine _match_code_start: called CC=2 SCAR=6.9
418 1627 my $self = shift;
419 1627 my $text = shift;
420  
421 1627 ▶ my ($delim)
Branch: 1 branch
BranchTrueFalseTotal
my($delim) = $self->_match_delimiter_start($text, qr/\`+\p{SpaceSeparator}*/) or return 92 1535 1627
422 = $self->_match_delimiter_start( $text, qr/\`+\p{SpaceSeparator}*/ )
423 or return;
424  
425 92 $delim =~ s/\p{SpaceSeparator}*$//;
426  
427 92 my $event = $self->_make_event( StartCode => delimiter => $delim );
428  
429 92 $self->_markup_event($event);
430  
431 92 return 1;
432 }
433  
434 199 ▶sub _match_code_end {
Subroutine _match_code_end: called CC=2 SCAR=6.9
435 199 my $self = shift;
436 199 my $text = shift;
437 199 my $delim = shift;
438  
439 199 ▶ $self->_match_delimiter_end( $text, qr/\p{SpaceSeparator}*\Q$delim/ )
Branch: 1 branch
BranchTrueFalseTotal
$self->_match_delimiter_end($text, qr/\p{SpaceSeparator}*\Q$delim\E/) or return 90 109 199
440 or return;
441  
442 90 my $event = $self->_make_event( EndCode => delimiter => $delim );
443  
444 90 $self->_markup_event($event);
445  
446 90 return 1;
447 }
448  
449 4997 ▶sub _match_delimiter_start {
Subroutine _match_delimiter_start: called CC=2 SCAR=6.9
450 4997 my $self = shift;
451 4997 my $text = shift;
452 4997 my $delim = shift;
453  
454 4997 ▶ return unless ${$text} =~ / \G ($delim)/xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G ($delim)/cgx 4836 161 4997
455  
456 161 return $1;
457 }
458  
459 358 ▶sub _match_delimiter_end {
Subroutine _match_delimiter_end: called CC=2 SCAR=6.9
460 358 my $self = shift;
461 358 my $text = shift;
462 358 my $delim = shift;
463  
464 358 ▶ return unless ${$text} =~ /\G $delim /xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G $delim /cgx 209 149 358
465  
466 149 return 1;
467 }
468  
469 1498 ▶sub _match_auto_link {
Subroutine _match_auto_link: called CC=3 SCAR=11.0
470 1498 my $self = shift;
471 1498 my $text = shift;
472  
473 1498 my $uri;
474  
475 1498 ▶ if ( ${$text} =~ /\G <( (?:https?|mailto|ftp): [^>]+ ) >/xgc ) {
Branch: 2 branches
BranchTrueFalseTotal
if (${$text;} =~ /\G <( (?:https?|mailto|ftp): [^>]+ ) >/cgx) { } 2 1496 1498
elsif (${$text;} =~ /\G <( [^>]+ \@ [^>]+ ) >/cgx) { } 1 1495 1496
476 2 $uri = $1;
477 }
478 1496 elsif ( ${$text} =~ /\G <( [^>]+ \@ [^>]+ ) >/xgc ) {
479 1 $uri = "mailto:$1";
480 }
481 else {
482 1495 return;
483 }
484  
485 3 my $link = $self->_make_event( AutoLink => uri => $uri );
486  
487 3 $self->_markup_event($link);
488  
489 3 return 1;
490 }
491  
492 # Stolen from Text::Markdown
493 35 my $nested_brackets;
494 $nested_brackets = qr{
495 (?> # Atomic matching
496 [^\[\]]+ # Anything other than brackets
497 |
498 \[
499 (??{ $nested_brackets }) # Recursive set of nested brackets
500 \]
501 )*
502 35 }x;
503  
504 # Also stolen from Text::Markdown
505 35 my $nested_parens;
506 $nested_parens = qr{
507 (?> # Atomic matching
508 [^()]+ # Anything other than parens
509 |
510 \(
511 (??{ $nested_parens }) # Recursive set of nested parens
512 \)
513 )*
514 35 }x;
515  
516 1495 ▶sub _match_link {
Subroutine _match_link: called CC=5 SCAR=16.1
517 1495 my $self = shift;
518 1495 my $text = shift;
519  
520 1495 ▶ my $pos = pos ${$text} || 0;
Condition100%2/2
pos ${$text;} || 0
l!l
4121083
MC/DC100%1/1
pos ${$text;} || 0
pos ${$text;}
Truth table100%2/2
A
A= pos ${$text;}
Aresult
11
00
521  
522 # For some inexplicable reason, this regex needs to be recreated each time
523 # the method is called or $nested_brackets && $nested_parens are
524 # undef. Presumably this has something to do with using it in a
525 # subroutine's lexical scope (resetting the stack on each invocation?)
526 return
527 1495 ▶ unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G \[ ($nested_brackets) \] # link or alt text (?: \( ($nested_parens) \) | \s* \[ ( [^]]* ) \] # an id (can be empty) )? # with no id or explicit uri, use text as id /cgx 1457 38 1495
528 \[ ($nested_brackets) \] # link or alt text
529 (?:
530 \( ($nested_parens) \)
531 |
532 \s*
533 \[ ( [^]]* ) \] # an id (can be empty)
534 )? # with no id or explicit uri, use text as id
535 /xgc;
536  
537 38 my ( $link_text, $attr ) = $self->_link_match_results( $1, $2, $3 );
538  
539 38 ▶ unless ( defined $attr->{uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr->{"uri"}) 6 32 38
540 6 ▶ pos ${$text} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 6 0 6
541 if defined $pos;
542  
543 6 return;
544 }
545  
546 32 my $start = $self->_make_event( StartLink => %{$attr} );
547  
548 32 $self->_markup_event($start);
549  
550 32 $self->_parse_text( \$link_text );
551  
552 32 my $end = $self->_make_event('EndLink');
553  
554 32 $self->_markup_event($end);
555  
556 32 return 1;
557 }
558  
559 1463 ▶sub _match_image {
Subroutine _match_image: called CC=5 SCAR=16.1
560 1463 my $self = shift;
561 1463 my $text = shift;
562  
563 1463 ▶ my $pos = pos ${$text} || 0;
Condition100%2/2
pos ${$text;} || 0
l!l
3821081
MC/DC100%1/1
pos ${$text;} || 0
pos ${$text;}
Truth table100%2/2
A
A= pos ${$text;}
Aresult
11
00
564  
565 return
566 1463 ▶ unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G ! \[ ($nested_brackets) \] # link or alt text (?: \( ($nested_parens) \) | \s* \[ ( [^]]* ) \] # an id (can be empty) )? # with no id or explicit uri, use text as id /cgx 1454 9 1463
567 !
568 \[ ($nested_brackets) \] # link or alt text
569 (?:
570 \( ($nested_parens) \)
571 |
572 \s*
573 \[ ( [^]]* ) \] # an id (can be empty)
574 )? # with no id or explicit uri, use text as id
575 /xgc;
576  
577 9 my ( $alt_text, $attr ) = $self->_link_match_results( $1, $2, $3 );
578  
579 9 ▶ unless ( defined $attr->{uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr->{"uri"}) 1 8 9
580 1 ▶ pos ${$text} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 1 0 1
581 if defined $pos;
582  
583 1 return;
584 }
585  
586 8 $attr->{alt_text} = $alt_text;
587  
588 8 my $image = $self->_make_event( Image => %{$attr} );
589  
590 8 $self->_markup_event($image);
591  
592 8 return 1;
593 }
594 ## use critic
595  
596 47 ▶sub _link_match_results {
Subroutine _link_match_results: called CC=7 SCAR=19.5
597 47 my $self = shift;
598 47 my $text = shift;
599 47 my $uri_and_title = shift;
600 47 my $id = shift;
601  
602 47 my %attr;
603 47 ▶ if ( defined $uri_and_title ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri_and_title) { } 14 33 47
604 14 my ( $uri, $title ) = $self->_parse_uri_and_title($uri_and_title);
605  
606 14 $attr{uri} = $uri;
607 14 ▶ $attr{title} = $title
Branch: 1 branch
BranchTrueFalseTotal
if defined $title 4 10 14
608 if defined $title;
609 }
610 else {
611 33 ▶ unless ( defined $id && length $id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id and length $id) 11 22 33
Condition100%3/3
defined $id and length $id
!ll&&!rl&&r
9222
MC/DC100%2/2
defined $id and length $id
defined $id length $id
Truth table100%3/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
612 11 $id = $text;
613 11 $attr{is_implicit_id} = 1;
614 }
615  
616 33 $id =~ s/\s+/ /g;
617  
618 33 ▶ my $link = $self->_get_link_by_id($id) || [];
Condition100%2/2
$self->_get_link_by_id($id) || []
l!l
267
MC/DC100%1/1
$self->_get_link_by_id($id) || []
$self->_get_link_by_id($id)
Truth table100%2/2
A
A= $self->_get_link_by_id($id)
Aresult
11
00
619  
620 33 $attr{uri} = $link->[0];
621 33 ▶ $attr{title} = $link->[1]
Branch: 1 branch
BranchTrueFalseTotal
if defined $link->[1] 12 21 33
622 if defined $link->[1];
623 33 $attr{id} = $id;
624 }
625  
626 47 return ( $text, \%attr );
627 }
628  
629 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
630 1492 ▶sub _match_html_comment {
Subroutine _match_html_comment: called CC=2 SCAR=6.9
631 1492 my $self = shift;
632 1492 my $text = shift;
633  
634 1492 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::SpanParser::HTMLComment /cgsx 1491 1 1492
635 $HTMLComment
636 /xgcs;
637  
638 1 my $comment = $1;
639  
640 1 $self->_detab_text( \$comment );
641  
642 1 my $event = $self->_make_event( HTMLComment => text => $comment );
643  
644 1 $self->_markup_event($event);
645  
646 1 return 1;
647 }
648  
649 35 my %InlineTags = map { $_ => 1 }
650 qw( area base basefont br col frame hr iframe img input link meta param );
651  
652 1491 ▶sub _match_html_tag {
Subroutine _match_html_tag: called CC=4 SCAR=13.9
653 1491 my $self = shift;
654 1491 my $text = shift;
655  
656 1491 ▶ return unless ${$text} =~ m{\G (< [^>]+ >)}xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G (< [^>]+ >)/cgx 1472 19 1491
657  
658 19 my $tag = $1;
659  
660 19 my $event;
661 19 ▶ if ( $tag =~ m{^</(\w+)>$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ m[^</(\w+)>$]) { } 8 11 19
662 8 $event = $self->_make_event( EndHTMLTag => tag => $1 );
663 }
664 else {
665 11 $tag =~ s{^<|/?>$}{}g;
666  
667 11 my ( $tag_name, $attr_text ) = split /\s+/, $tag, 2;
668  
669 11 my $attr = $self->_parse_attributes($attr_text);
670  
671 11 ▶ if ( $InlineTags{$tag_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($InlineTags{$tag_name}) { } 3 8 11
672 3 $event = $self->_make_event(
673 HTMLTag => (
674 tag => $tag_name,
675 attributes => $attr,
676 ),
677 );
678 }
679 else {
680 8 $event = $self->_make_event(
681 StartHTMLTag => (
682 tag => $tag_name,
683 attributes => $attr,
684 ),
685 );
686 }
687 }
688  
689 19 $self->_markup_event($event);
690  
691 19 return 1;
692 }
693  
694 # This parsing code is copied from HTML::Parser::Simple::Attributes by Ron
695 # Savage with some modifications & additions.
696 35 my @quote = (
697 qr{\G([a-zA-Z0-9_-]+)\s*=\s*["]([^"]+)["](?:\s+|\z)}s, # Double quotes.
698 qr{\G([a-zA-Z0-9_-]+)\s*=\s*[']([^']+)['](?:\s+|\z)}s, # Single quotes.
699 qr{\G([a-zA-Z0-9_-]+)\s*=\s*([^\s'"]+)(?:\s+|\z)}s, # Unquoted.
700 qr{\G([a-zA-Z0-9_-]+)(?:\s+|\z)}, # Empty attribute (name w/ no value).
701 );
702  
703 11 ▶sub _parse_attributes {
Subroutine _parse_attributes: called CC=7 SCAR=19.5
704 11 my $self = shift;
705 11 my $text = shift;
706  
707 # If the tag had no attributes there's nothing to parse.
708 11 ▶ return {} unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 4 7 11
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
407
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
709  
710 7 my %attrs;
711 OUTER:
712 7 ▶ while ( ( ( pos $text ) || 0 ) < length $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((pos $text || 0) < length $text) 12 7 19
Condition100%2/2
pos $text || 0
l!l
127
MC/DC100%1/1
pos $text || 0
pos $text
Truth table100%2/2
A
A= pos $text
Aresult
11
00
713 12 for my $q (@quote) {
714 18 ▶ if ( $text =~ /$q/cg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$q/cg) 12 6 18
715 12 $attrs{$1} = $2;
716 12 next OUTER;
717 }
718 }
719  
720 0 die "Can't parse $text - not a properly formed attribute string\n";
721 }
722  
723 7 return \%attrs;
724 }
725  
726 1472 ▶sub _match_html_entity {
Subroutine _match_html_entity: called CC=2 SCAR=6.9
727 1472 my $self = shift;
728 1472 my $text = shift;
729  
730 1472 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G &(\S+?); /cgsx 1465 7 1472
731 &(\S+?);
732 /xgcs;
733  
734 7 my $event = $self->_make_event( HTMLEntity => entity => $1 );
735  
736 7 $self->_markup_event($event);
737  
738 7 return 1;
739 }
740  
741 1465 ▶sub _match_line_break {
Subroutine _match_line_break: called CC=2 SCAR=6.9
742 1465 my $self = shift;
743 1465 my $text = shift;
744  
745 1465 my $line_break_re = $self->_line_break_re;
746  
747 1465 ▶ return unless ${$text} =~ /\G$line_break_re/gcs;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G$line_break_re/cgs 1462 3 1465
748  
749 3 my $event = $self->_make_event('LineBreak');
750  
751 3 $self->_markup_event($event);
752  
753 3 return 1;
754 }
755  
756 1564 ▶sub _match_plain_text {
Subroutine _match_plain_text: called CC=3 SCAR=11.1
757 1564 my $self = shift;
758 1564 my $text = shift;
759  
760 my $end_of_text_re = join '|',
761 1564 grep {defined} (
762 $self->_text_end_res,
763 );
764  
765 # Note that we're careful not to consume any of the characters marking the
766 # (possible) end of the plain text. If those things turn out to _not_ be
767 # markup, we'll get them on the next pass, because we always match at
768 # least one character, so we should never get stuck in a loop.
769 return
770 1564 ▶ unless ${$text} =~ /\G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G ( .+? ) # at least one character followed by ... (?= $end_of_text_re | \* # possible span markup - bold or italics | _ # possible span markup - bold or italics | \p{SpaceSeparator}* \` | !?\[ # possible image or link | < [^>]+ > # an HTML tag | &\S+; # an HTML entity | \z # or the end of the string ) /cgsx 0 1564 1564
771 ( .+? ) # at least one character followed by ...
772 (?=
773 $end_of_text_re
774 |
775 \* # possible span markup - bold or italics
776 |
777 _ # possible span markup - bold or italics
778 |
779 \p{SpaceSeparator}* \`
780 |
781 !?\[ # possible image or link
782 |
783 < [^>]+ > # an HTML tag
784 |
785 &\S+; # an HTML entity
786 |
787 \z # or the end of the string
788 )
789 /xgcs;
790  
791 1564 ▶ $self->_print_debug("Interpreting as plain text\n\n[$1]\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1564 1564
792 if $self->debug;
793  
794 1564 $self->_save_span_text($1);
795  
796 1564 return 1;
797 }
798 ## use critic
799  
800 1564 ▶sub _text_end_res {
Subroutine _text_end_res: called CC=1 SCAR=0.0
801 1564 my $self = shift;
802  
803 return (
804 1564 $self->_escape_re,
805 $self->_line_break_re,
806 );
807 }
808  
809 420 ▶sub _markup_event {
Subroutine _markup_event: called CC=4 SCAR=17.3
810 420 my $self = shift;
811 420 my $event = shift;
812  
813 420 $self->_event_for_text_buffer;
814  
815 420 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 420 420
816 0 my $msg = 'Found markup: ' . $event->event_name;
817  
818 0 ▶ if ( $event->can('delimiter') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->can("delimiter")) 0 0 0
819 0 $msg .= ' - delimiter: [' . $event->delimiter . ']';
820 }
821  
822 0 $msg .= "\n";
823  
824 0 $self->_print_debug($msg);
825 }
826  
827 420 $self->_add_pending_event($event);
828  
829 420 ▶ $self->_convert_invalid_start_events_to_text
Branch: 1 branch
BranchTrueFalseTotal
if $event->is_end 189 231 420
830 if $event->is_end;
831 }
832  
833 1552 ▶sub _event_for_text_buffer {
Subroutine _event_for_text_buffer: called CC=2 SCAR=6.9
834 1552 my $self = shift;
835  
836 1552 ▶ return unless $self->_has_span_text_buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_span_text_buffer 70 1482 1552
837  
838 1482 my $text = $self->_span_text_buffer;
839  
840 1482 $self->_detab_text( \$text );
841  
842 1482 my $event = $self->_make_event( Text => text => $text );
843  
844 1482 $self->_add_pending_event($event);
845  
846 1482 $self->_clear_span_text_buffer;
847 }
848  
849 1289 ▶sub _convert_invalid_start_events_to_text {
Subroutine _convert_invalid_start_events_to_text: called CC=9 SCAR=22.2
850 1289 my $self = shift;
851 1289 my $is_done = shift;
852  
853 # We want to operate directly on the reference so we can convert
854 # individual events in place
855 1289 my $events = $self->__pending_events;
856  
857 1289 my @starts;
858 EVENT:
859 1289 for my $i ( 0 .. $#{$events} ) {
860 3351 my $event = $events->[$i];
861  
862 3351 ▶ next unless $event->does('Markdent::Role::BalancedEvent');
Branch: 1 branch
BranchTrueFalseTotal
unless $event->does("Markdent::Role::BalancedEvent") 2252 1099 3351
863  
864 1099 ▶ if ( $event->is_start ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->is_start) { } 562 537 1099
elsif ($event->is_end) { } 537 0 537
865 562 push @starts, [ $i, $event ];
866 }
867 elsif ( $event->is_end ) {
868 537 ▶ while ( my $start = pop @starts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $start = pop @starts) 537 0 537
869 next EVENT
870 537 ▶ if $event->balances_event( $start->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $event->balances_event($start->[1]) 537 0 537
871  
872 0 $events->[ $start->[0] ]
873 = $self->_convert_start_event_to_text( $start->[1] );
874 }
875 }
876 }
877  
878 1289 ▶ return unless $is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_done 189 1100 1289
879  
880 1100 for my $start (@starts) {
881 12 $events->[ $start->[0] ]
882 = $self->_convert_start_event_to_text( $start->[1] );
883 }
884 }
885  
886 12 ▶sub _convert_start_event_to_text {
Subroutine _convert_start_event_to_text: called CC=3 SCAR=16.3
887 12 my $self = shift;
888 12 my $event = shift;
889  
890 12 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 12 12
891 0 my $msg = 'Found bad start event for ' . $event->name;
892  
893 0 ▶ if ( $event->can('delimiter') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->can("delimiter")) 0 0 0
894 0 $msg .= q{ with "} . $event->delimiter . q{" as the delimiter};
895 }
896  
897 0 $msg .= "\n";
898  
899 0 $self->_print_debug($msg);
900 }
901  
902 12 return $self->_make_event(
903 Text => (
904 text => $event->as_text,
905 _converted_from => $event->event_name,
906 )
907 );
908 }
909  
910 1100 ▶sub _merge_consecutive_text_events {
Subroutine _merge_consecutive_text_events: called CC=9 SCAR=22.0
911 1100 my $self = shift;
912  
913 1100 my $events = $self->__pending_events;
914  
915 1100 my $merge_start;
916  
917 my @to_merge;
918 1100 for my $i ( 0 .. $#{$events} ) {
919 1902 my $event = $events->[$i];
920  
921 1902 ▶ if ( $event->event_name eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->event_name eq 'text') { } 1494 408 1902
922 1494 ▶ $merge_start = $i
Branch: 1 branch
BranchTrueFalseTotal
unless defined $merge_start 1478 16 1494
923 unless defined $merge_start;
924 }
925 else {
926 408 ▶ push @to_merge, [ $merge_start, $i - 1 ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $merge_start and $i - 1 > $merge_start 5 403 408
Condition100%3/3
defined $merge_start and $i - 1 > $merge_start
!ll&&!rl&&r
303735
MC/DC100%2/2
defined $merge_start and $i - 1 > $merge_start
defined $merge_start $i - 1 > $merge_start
Truth table100%3/3
A and B
A= defined $merge_startB= $i - 1 > $merge_start
ABresult
0X0
100
111
927 if defined $merge_start && $i - 1 > $merge_start;
928  
929 408 undef $merge_start;
930 }
931 }
932  
933 # If $merge_start is still defined, then the last event was a text event
934 # which may need to be merged.
935 6 push @to_merge, [ $merge_start, $#{$events} ]
936 1100 ▶ if defined $merge_start && $#{$events} > $merge_start;
Branch: 1 branch
BranchTrueFalseTotal
if defined $merge_start and $#{$events;} > $merge_start 6 1094 1100
Condition66%2/3
defined $merge_start and $#{$events;} > $merge_start
!ll&&!rl&&r
010946
MC/DC50%1/2
defined $merge_start and $#{$events;} > $merge_start
defined $merge_start $#{$events;} > $merge_start
Truth table66%2/3
A and B
A= defined $merge_startB= $#{$events;} > $merge_start
ABresult
0X0
100
111
937  
938 1100 my $already_merged = 0;
939 1100 for my $pair (@to_merge) {
940 11 $pair->[0] -= $already_merged;
941 11 $pair->[1] -= $already_merged;
942  
943 $self->_splice_merged_text_event(
944 $events,
945 11 @{$pair},
946 );
947  
948 11 $already_merged += $pair->[1] - $pair->[0];
949 }
950 }
951  
952 11 ▶sub _splice_merged_text_event {
Subroutine _splice_merged_text_event: called CC=2 SCAR=7.0
953 11 my $self = shift;
954 11 my $events = shift;
955 11 my $start = shift;
956 11 my $end = shift;
957  
958 11 my @to_merge = map { $_->text } @{$events}[ $start .. $end ];
959  
960 $self->_print_debug(
961 "Merging consecutive text events ($start-$end) for: \n"
962 0 ▶ . ( join q{}, map {" - [$_]\n"} @to_merge ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 11 11
963 if $self->debug;
964  
965 11 my $merged_text = join q{}, @to_merge;
966  
967 11 my $event = $self->_make_event(
968 Text => (
969 text => $merged_text,
970 _merged_from => \@to_merge,
971 ),
972 );
973  
974 11 splice @{$events}, $start, ( $end - $start ) + 1, $event;
975 }
976  
977 2200 ▶sub _debug_pending_events {
Subroutine _debug_pending_events: called CC=3 SCAR=15.1
978 2200 my $self = shift;
979 2200 my $desc = shift;
980  
981 2200 ▶ return unless $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 2200 0 2200
982  
983 0 my $msg = "Pending event stream $desc:\n";
984  
985 0 for my $event ( $self->_pending_events ) {
986 0 $msg .= $event->debug_dump . "\n";
987 }
988  
989 0 $self->_print_debug($msg);
990 }
991  
992 35 __PACKAGE__->meta->make_immutable;
993  
994 35 1;
995  
996 # ABSTRACT: Span parser for standard Markdown
997  
998 __END__
« Markdent/Parser.pm ↑ Summary Markdent/Role/AnyParser.pm »