Markdent/Parser/SpanParser.pm

filter: Statementstmt 95.4% 444 / 465 Branchbran 85.8% 115 / 134 Conditioncond 84.6% 22 / 26 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 69 / 69 Podpod 100% 2 / 2 totaltotal 93.2% 664 / 712 CC 88 SCAR 14.5
CC
88
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/Role/BalancedEvent.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.39';
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 184 ▶ 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 184 ▶ 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 sub extract_link_ids {
108 178 ▶ my $self = shift;
Subroutine extract_link_ids: called CC=1 SCAR=0.0
Pod documented
109 178 my $text = shift;
110  
111 178 ${$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 sub _process_id_for_link {
126 25 ▶ my $self = shift;
Subroutine _process_id_for_link: called CC=1 SCAR=0.0
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 sub _parse_uri_and_title {
140 39 ▶ my $self = shift;
Subroutine _parse_uri_and_title: called CC=3 SCAR=11.0
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 sub parse_block {
158 1098 ▶ my $self = shift;
Subroutine parse_block: called CC=3 SCAR=11.0
Pod documented
159 1098 my $text = shift;
160  
161 1098 ▶ $self->_print_debug("Parsing text for span-level markup\n\n$text\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1098 1098
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 1098 $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 1098 $self->_convert_invalid_start_events_to_text('is done');
171  
172 1098 $self->_debug_pending_events('before text merging');
173  
174 1098 $self->_merge_consecutive_text_events;
175  
176 1098 $self->_debug_pending_events('after text merging');
177  
178 1098 $self->handler->handle_event($_) for $self->_pending_events;
179  
180 1098 $self->_clear_pending_events;
181  
182 1098 return;
183 }
184  
185 sub _parse_text {
186 1130 ▶ my $self = shift;
Subroutine _parse_text: called CC=7 SCAR=20.2
187 1130 my $text = shift;
188  
189 PARSE:
190 1130 while (1) {
191 3089 ▶ if ( $self->debug && pos ${$text} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and pos ${$text;}) 0 3089 3089
Condition33%1/3
$self->debug and pos ${$text;}
!ll&&!rl&&r
308900
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 3089 ▶ if ( ${$text} =~ /\G\z/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$text;} =~ /\G\z/cg) 1130 1959 3089
198 1130 $self->_event_for_text_buffer;
199 1130 last;
200 }
201  
202 1959 my @look_for = $self->_possible_span_matches;
203  
204 1959 $self->_debug_look_for(@look_for);
205  
206 1959 for my $span (@look_for) {
207 17500 ▶ my ( $markup, @args ) = ref $span ? @{$span} : $span;
Branch: 1 branch
BranchTrueFalseTotal
ref $span ? : 358 17142 17500
208  
209 17500 my $meth = '_match_' . $markup;
210  
211 17500 ▶ $self->$meth( $text, @args )
Branch: 1 branch
BranchTrueFalseTotal
$self->$meth($text, @args) and next PARSE 399 17101 17500
212 and next PARSE;
213 }
214  
215 1560 $self->_match_plain_text($text);
216 }
217 }
218  
219 sub _possible_span_matches {
220 1959 ▶ my $self = shift;
Subroutine _possible_span_matches: called CC=3 SCAR=11.0
221  
222 1959 my %open = $self->_open_start_events_for_span( 'code', 'link' );
223 1959 ▶ if ( my $event = $open{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $event = $open{"code"}) 199 1760 1959
224 199 return [ 'code_end', $event->delimiter ];
225 }
226  
227 1760 my @look_for = 'escape';
228  
229 1760 push @look_for, $self->_look_for_strong_and_emphasis;
230  
231 1760 push @look_for, 'code_start';
232  
233 1760 ▶ unless ( $open{link} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($open{"link"}) 1717 43 1760
234 1717 push @look_for, qw( auto_link link image );
235 }
236  
237 1760 push @look_for, 'html_comment', 'html_tag', 'html_entity', 'line_break';
238  
239 1760 return @look_for;
240 }
241  
242 sub _look_for_strong_and_emphasis {
243 1760 ▶ my $self = shift;
Subroutine _look_for_strong_and_emphasis: called CC=9 SCAR=22.0
244  
245 1760 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 1760 ▶ if ( $open{strong} && $open{emphasis} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($open{'strong'} and $open{'emphasis'}) { } 18 1742 1760
elsif ($open{'emphasis'}) { } 82 1660 1742
elsif ($open{'strong'}) { } 45 1615 1660
Condition100%3/3
$open{"strong"} and $open{"emphasis"}
!ll&&!rl&&r
16974518
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 1615 return ( 'strong_start', 'emphasis_start' );
283 }
284  
285 sub _open_start_events_for_span {
286 3857 ▶ my $self = shift;
Subroutine _open_start_events_for_span: called CC=4 SCAR=13.9
287 3857 my %wanted_start = map { 'start_' . $_ => $_ } @_;
288 3857 my %wanted_end = map { 'end_' . $_ => $_ } @_;
289  
290 3857 my %open;
291 3857 for my $event ( $self->_pending_events ) {
292 7570 my $event_name = $event->event_name;
293 $open{ $wanted_start{$event_name} } = $event
294 7570 ▶ if $wanted_start{$event_name};
Branch: 1 branch
BranchTrueFalseTotal
if $wanted_start{$event_name} 1216 6354 7570
295  
296 delete $open{ $wanted_end{$event_name} }
297 7570 ▶ if $wanted_end{$event_name};
Branch: 1 branch
BranchTrueFalseTotal
if $wanted_end{$event_name} 809 6761 7570
298 }
299  
300 3857 return %open;
301 }
302  
303 sub _build_emphasis_start_delimiter_re {
304 144 ▶ my $self = shift;
Subroutine _build_emphasis_start_delimiter_re: called CC=1 SCAR=0.0
305  
306 144 return qr/(?:\*|_)/;
307 }
308  
309 sub _build_escapable_chars {
310 163 ▶ return [ qw( \ ` * _ { } [ ] ( ) + - . ! < > ~ ), '#' ];
Subroutine _build_escapable_chars: called CC=1 SCAR=0.0
311 }
312  
313 sub _build_escape_re {
314 163 ▶ my $self = shift;
Subroutine _build_escape_re: called CC=1 SCAR=0.0
315  
316 163 my $chars = join q{}, uniq( @{ $self->_escapable_chars } );
317  
318 163 return qr/\\([\Q$chars\E])/;
319 }
320  
321 sub _build_line_break_re {
322 163 ▶ my $self = shift;
Subroutine _build_line_break_re: called CC=1 SCAR=0.0
323  
324 163 return qr/\p{SpaceSeparator}{2}\n/;
325 }
326  
327 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
328 sub _match_escape {
329 1760 ▶ my $self = shift;
Subroutine _match_escape: called CC=3 SCAR=11.0
330 1760 my $text = shift;
331  
332 1760 my $escape_re = $self->_escape_re;
333  
334 1760 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G ($escape_re) /cgx 1747 13 1760
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 sub _match_strong_start {
347 1684 ▶ my $self = shift;
Subroutine _match_strong_start: called CC=2 SCAR=6.9
348 1684 my $text = shift;
349  
350 1684 ▶ my ($delim) = $self->_match_delimiter_start( $text, qr/(?:\*\*|__)/ )
Branch: 1 branch
BranchTrueFalseTotal
my($delim) = $self->_match_delimiter_start($text, qr/(?:\*\*|__)/) or return 1666 18 1684
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 sub _match_strong_end {
361 57 ▶ my $self = shift;
Subroutine _match_strong_end: called CC=2 SCAR=6.9
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 45 12 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 sub _match_emphasis_start {
376 1617 ▶ my $self = shift;
Subroutine _match_emphasis_start: called CC=2 SCAR=6.9
377 1617 my $text = shift;
378  
379 1617 ▶ my ($delim) = $self->_match_delimiter_start(
Branch: 1 branch
BranchTrueFalseTotal
my($delim) = $self->_match_delimiter_start($text, $self->_emphasis_start_delimiter_re) or return 1567 50 1617
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 sub _match_emphasis_end {
392 100 ▶ my $self = shift;
Subroutine _match_emphasis_end: called CC=2 SCAR=6.9
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 54 46 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 sub _emphasis_end_delimiter_re {
410 90 ▶ my $self = shift;
Subroutine _emphasis_end_delimiter_re: called CC=1 SCAR=0.0
411 90 my $delim = shift;
412  
413 90 return qr/\Q$delim\E/;
414 }
415  
416 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
417 sub _match_code_start {
418 1621 ▶ my $self = shift;
Subroutine _match_code_start: called CC=2 SCAR=6.9
419 1621 my $text = shift;
420  
421 1621 ▶ my ($delim)
Branch: 1 branch
BranchTrueFalseTotal
my($delim) = $self->_match_delimiter_start($text, qr/\`+\p{SpaceSeparator}*/) or return 1529 92 1621
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 sub _match_code_end {
435 199 ▶ my $self = shift;
Subroutine _match_code_end: called CC=2 SCAR=6.9
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 109 90 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 sub _match_delimiter_start {
450 4979 ▶ my $self = shift;
Subroutine _match_delimiter_start: called CC=2 SCAR=6.9
451 4979 my $text = shift;
452 4979 my $delim = shift;
453  
454 4979 ▶ return unless ${$text} =~ / \G ($delim)/xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G ($delim)/cgx 4818 161 4979
455  
456 161 return $1;
457 }
458  
459 sub _match_delimiter_end {
460 358 ▶ my $self = shift;
Subroutine _match_delimiter_end: called CC=2 SCAR=6.9
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 sub _match_auto_link {
470 1492 ▶ my $self = shift;
Subroutine _match_auto_link: called CC=2 SCAR=6.9
471 1492 my $text = shift;
472  
473 1492 ▶ return unless ${$text} =~ /\G <( (?:https?|mailto|ftp): [^>]+ ) >/xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G <( (?:https?|mailto|ftp): [^>]+ ) >/cgx 1491 1 1492
474  
475 1 my $link = $self->_make_event( AutoLink => uri => $1 );
476  
477 1 $self->_markup_event($link);
478  
479 1 return 1;
480 }
481  
482 # Stolen from Text::Markdown
483 35 my $nested_brackets;
484 $nested_brackets = qr{
485 (?> # Atomic matching
486 [^\[\]]+ # Anything other than brackets
487 |
488 \[
489 (??{ $nested_brackets }) # Recursive set of nested brackets
490 \]
491 )*
492 35 }x;
493  
494 # Also stolen from Text::Markdown
495 35 my $nested_parens;
496 $nested_parens = qr{
497 (?> # Atomic matching
498 [^()]+ # Anything other than parens
499 |
500 \(
501 (??{ $nested_parens }) # Recursive set of nested parens
502 \)
503 )*
504 35 }x;
505  
506 sub _match_link {
507 1491 ▶ my $self = shift;
Subroutine _match_link: called CC=5 SCAR=16.1
508 1491 my $text = shift;
509  
510 1491 ▶ my $pos = pos ${$text} || 0;
Condition100%2/2
pos ${$text;} || 0
l!l
4101081
MC/DC100%1/1
pos ${$text;} || 0
pos ${$text;}
Truth table100%2/2
A
A= pos ${$text;}
Aresult
11
00
511  
512 # For some inexplicable reason, this regex needs to be recreated each time
513 # the method is called or $nested_brackets && $nested_parens are
514 # undef. Presumably this has something to do with using it in a
515 # subroutine's lexical scope (resetting the stack on each invocation?)
516 return
517 1491 ▶ 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 1453 38 1491
518 \[ ($nested_brackets) \] # link or alt text
519 (?:
520 \( ($nested_parens) \)
521 |
522 \s*
523 \[ ( [^]]* ) \] # an id (can be empty)
524 )? # with no id or explicit uri, use text as id
525 /xgc;
526  
527 38 my ( $link_text, $attr ) = $self->_link_match_results( $1, $2, $3 );
528  
529 38 ▶ unless ( defined $attr->{uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr->{"uri"}) 6 32 38
530 6 ▶ pos ${$text} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 6 0 6
531 if defined $pos;
532  
533 6 return;
534 }
535  
536 32 my $start = $self->_make_event( StartLink => %{$attr} );
537  
538 32 $self->_markup_event($start);
539  
540 32 $self->_parse_text( \$link_text );
541  
542 32 my $end = $self->_make_event('EndLink');
543  
544 32 $self->_markup_event($end);
545  
546 32 return 1;
547 }
548  
549 sub _match_image {
550 1459 ▶ my $self = shift;
Subroutine _match_image: called CC=5 SCAR=16.1
551 1459 my $text = shift;
552  
553 1459 ▶ my $pos = pos ${$text} || 0;
Condition100%2/2
pos ${$text;} || 0
l!l
3801079
MC/DC100%1/1
pos ${$text;} || 0
pos ${$text;}
Truth table100%2/2
A
A= pos ${$text;}
Aresult
11
00
554  
555 return
556 1459 ▶ 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 1450 9 1459
557 !
558 \[ ($nested_brackets) \] # link or alt text
559 (?:
560 \( ($nested_parens) \)
561 |
562 \s*
563 \[ ( [^]]* ) \] # an id (can be empty)
564 )? # with no id or explicit uri, use text as id
565 /xgc;
566  
567 9 my ( $alt_text, $attr ) = $self->_link_match_results( $1, $2, $3 );
568  
569 9 ▶ unless ( defined $attr->{uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr->{"uri"}) 1 8 9
570 1 ▶ pos ${$text} = $pos
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 1 0 1
571 if defined $pos;
572  
573 1 return;
574 }
575  
576 8 $attr->{alt_text} = $alt_text;
577  
578 8 my $image = $self->_make_event( Image => %{$attr} );
579  
580 8 $self->_markup_event($image);
581  
582 8 return 1;
583 }
584 ## use critic
585  
586 sub _link_match_results {
587 47 ▶ my $self = shift;
Subroutine _link_match_results: called CC=7 SCAR=19.5
588 47 my $text = shift;
589 47 my $uri_and_title = shift;
590 47 my $id = shift;
591  
592 47 my %attr;
593 47 ▶ if ( defined $uri_and_title ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri_and_title) { } 14 33 47
594 14 my ( $uri, $title ) = $self->_parse_uri_and_title($uri_and_title);
595  
596 14 $attr{uri} = $uri;
597 14 ▶ $attr{title} = $title
Branch: 1 branch
BranchTrueFalseTotal
if defined $title 4 10 14
598 if defined $title;
599 }
600 else {
601 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
602 11 $id = $text;
603 11 $attr{is_implicit_id} = 1;
604 }
605  
606 33 $id =~ s/\s+/ /g;
607  
608 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
609  
610 33 $attr{uri} = $link->[0];
611 33 ▶ $attr{title} = $link->[1]
Branch: 1 branch
BranchTrueFalseTotal
if defined $link->[1] 12 21 33
612 if defined $link->[1];
613 33 $attr{id} = $id;
614 }
615  
616 47 return ( $text, \%attr );
617 }
618  
619 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
620 sub _match_html_comment {
621 1488 ▶ my $self = shift;
Subroutine _match_html_comment: called CC=2 SCAR=6.9
622 1488 my $text = shift;
623  
624 1488 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G $Markdent::Parser::SpanParser::HTMLComment /cgsx 1487 1 1488
625 $HTMLComment
626 /xgcs;
627  
628 1 my $comment = $1;
629  
630 1 $self->_detab_text( \$comment );
631  
632 1 my $event = $self->_make_event( HTMLComment => text => $comment );
633  
634 1 $self->_markup_event($event);
635  
636 1 return 1;
637 }
638  
639 35 my %InlineTags = map { $_ => 1 }
640 qw( area base basefont br col frame hr iframe img input link meta param );
641  
642 sub _match_html_tag {
643 1487 ▶ my $self = shift;
Subroutine _match_html_tag: called CC=4 SCAR=13.9
644 1487 my $text = shift;
645  
646 1487 ▶ return unless ${$text} =~ m{\G (< [^>]+ >)}xgc;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G (< [^>]+ >)/cgx 1468 19 1487
647  
648 19 my $tag = $1;
649  
650 19 my $event;
651 19 ▶ if ( $tag =~ m{^</(\w+)>$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ m[^</(\w+)>$]) { } 8 11 19
652 8 $event = $self->_make_event( EndHTMLTag => tag => $1 );
653 }
654 else {
655 11 $tag =~ s{^<|/?>$}{}g;
656  
657 11 my ( $tag_name, $attr_text ) = split /\s+/, $tag, 2;
658  
659 11 my $attr = $self->_parse_attributes($attr_text);
660  
661 11 ▶ if ( $InlineTags{$tag_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($InlineTags{$tag_name}) { } 3 8 11
662 3 $event = $self->_make_event(
663 HTMLTag => (
664 tag => $tag_name,
665 attributes => $attr,
666 ),
667 );
668 }
669 else {
670 8 $event = $self->_make_event(
671 StartHTMLTag => (
672 tag => $tag_name,
673 attributes => $attr,
674 ),
675 );
676 }
677 }
678  
679 19 $self->_markup_event($event);
680  
681 19 return 1;
682 }
683  
684 # This parsing code is copied from HTML::Parser::Simple::Attributes by Ron
685 # Savage with some modifications & additions.
686 35 my @quote = (
687 qr{\G([a-zA-Z0-9_-]+)\s*=\s*["]([^"]+)["](?:\s+|\z)}s, # Double quotes.
688 qr{\G([a-zA-Z0-9_-]+)\s*=\s*[']([^']+)['](?:\s+|\z)}s, # Single quotes.
689 qr{\G([a-zA-Z0-9_-]+)\s*=\s*([^\s'"]+)(?:\s+|\z)}s, # Unquoted.
690 qr{\G([a-zA-Z0-9_-]+)(?:\s+|\z)}, # Empty attribute (name w/ no value).
691 );
692  
693 sub _parse_attributes {
694 11 ▶ my $self = shift;
Subroutine _parse_attributes: called CC=7 SCAR=19.5
695 11 my $text = shift;
696  
697 # If the tag had no attributes there's nothing to parse.
698 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
699  
700 7 my %attrs;
701 OUTER:
702 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
703 12 for my $q (@quote) {
704 18 ▶ if ( $text =~ /$q/cg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$q/cg) 12 6 18
705 12 $attrs{$1} = $2;
706 12 next OUTER;
707 }
708 }
709  
710 0 die "Can't parse $text - not a properly formed attribute string\n";
711 }
712  
713 7 return \%attrs;
714 }
715  
716 sub _match_html_entity {
717 1468 ▶ my $self = shift;
Subroutine _match_html_entity: called CC=2 SCAR=6.9
718 1468 my $text = shift;
719  
720 1468 ▶ return unless ${$text} =~ / \G
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ / \G &(\S+?); /cgsx 1461 7 1468
721 &(\S+?);
722 /xgcs;
723  
724 7 my $event = $self->_make_event( HTMLEntity => entity => $1 );
725  
726 7 $self->_markup_event($event);
727  
728 7 return 1;
729 }
730  
731 sub _match_line_break {
732 1461 ▶ my $self = shift;
Subroutine _match_line_break: called CC=2 SCAR=6.9
733 1461 my $text = shift;
734  
735 1461 my $line_break_re = $self->_line_break_re;
736  
737 1461 ▶ return unless ${$text} =~ /\G$line_break_re/gcs;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$text;} =~ /\G$line_break_re/cgs 1458 3 1461
738  
739 3 my $event = $self->_make_event('LineBreak');
740  
741 3 $self->_markup_event($event);
742  
743 3 return 1;
744 }
745  
746 sub _match_plain_text {
747 1560 ▶ my $self = shift;
Subroutine _match_plain_text: called CC=3 SCAR=11.1
748 1560 my $text = shift;
749  
750 my $end_of_text_re = join '|',
751 1560 grep {defined} (
752 $self->_text_end_res,
753 );
754  
755 # Note that we're careful not to consume any of the characters marking the
756 # (possible) end of the plain text. If those things turn out to _not_ be
757 # markup, we'll get them on the next pass, because we always match at
758 # least one character, so we should never get stuck in a loop.
759 return
760 1560 ▶ 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 1560 1560
761 ( .+? ) # at least one character followed by ...
762 (?=
763 $end_of_text_re
764 |
765 \* # possible span markup - bold or italics
766 |
767 _ # possible span markup - bold or italics
768 |
769 \p{SpaceSeparator}* \`
770 |
771 !?\[ # possible image or link
772 |
773 < [^>]+ > # an HTML tag
774 |
775 &\S+; # an HTML entity
776 |
777 \z # or the end of the string
778 )
779 /xgcs;
780  
781 1560 ▶ $self->_print_debug("Interpreting as plain text\n\n[$1]\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1560 1560
782 if $self->debug;
783  
784 1560 $self->_save_span_text($1);
785  
786 1560 return 1;
787 }
788 ## use critic
789  
790 sub _text_end_res {
791 1560 ▶ my $self = shift;
Subroutine _text_end_res: called CC=1 SCAR=0.0
792  
793 return (
794 1560 $self->_escape_re,
795 $self->_line_break_re,
796 );
797 }
798  
799 sub _markup_event {
800 418 ▶ my $self = shift;
Subroutine _markup_event: called CC=4 SCAR=17.3
801 418 my $event = shift;
802  
803 418 $self->_event_for_text_buffer;
804  
805 418 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 418 418
806 0 my $msg = 'Found markup: ' . $event->event_name;
807  
808 0 ▶ if ( $event->can('delimiter') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->can("delimiter")) 0 0 0
809 0 $msg .= ' - delimiter: [' . $event->delimiter . ']';
810 }
811  
812 0 $msg .= "\n";
813  
814 0 $self->_print_debug($msg);
815 }
816  
817 418 $self->_add_pending_event($event);
818  
819 418 ▶ $self->_convert_invalid_start_events_to_text
Branch: 1 branch
BranchTrueFalseTotal
if $event->is_end 189 229 418
820 if $event->is_end;
821 }
822  
823 sub _event_for_text_buffer {
824 1548 ▶ my $self = shift;
Subroutine _event_for_text_buffer: called CC=2 SCAR=6.9
825  
826 1548 ▶ return unless $self->_has_span_text_buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_span_text_buffer 70 1478 1548
827  
828 1478 my $text = $self->_span_text_buffer;
829  
830 1478 $self->_detab_text( \$text );
831  
832 1478 my $event = $self->_make_event( Text => text => $text );
833  
834 1478 $self->_add_pending_event($event);
835  
836 1478 $self->_clear_span_text_buffer;
837 }
838  
839 sub _convert_invalid_start_events_to_text {
840 1287 ▶ my $self = shift;
Subroutine _convert_invalid_start_events_to_text: called CC=9 SCAR=22.2
841 1287 my $is_done = shift;
842  
843 # We want to operate directly on the reference so we can convert
844 # individual events in place
845 1287 my $events = $self->__pending_events;
846  
847 1287 my @starts;
848 EVENT:
849 1287 for my $i ( 0 .. $#{$events} ) {
850 3345 my $event = $events->[$i];
851  
852 3345 ▶ next unless $event->does('Markdent::Role::BalancedEvent');
Branch: 1 branch
BranchTrueFalseTotal
unless $event->does("Markdent::Role::BalancedEvent") 2246 1099 3345
853  
854 1099 ▶ if ( $event->is_start ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->is_start) { } 562 537 1099
elsif ($event->is_end) { } 537 0 537
855 562 push @starts, [ $i, $event ];
856 }
857 elsif ( $event->is_end ) {
858 537 ▶ while ( my $start = pop @starts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $start = pop @starts) 537 0 537
859 next EVENT
860 537 ▶ if $event->balances_event( $start->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $event->balances_event($start->[1]) 537 0 537
861  
862 0 $events->[ $start->[0] ]
863 = $self->_convert_start_event_to_text( $start->[1] );
864 }
865 }
866 }
867  
868 1287 ▶ return unless $is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_done 189 1098 1287
869  
870 1098 for my $start (@starts) {
871 12 $events->[ $start->[0] ]
872 = $self->_convert_start_event_to_text( $start->[1] );
873 }
874 }
875  
876 sub _convert_start_event_to_text {
877 12 ▶ my $self = shift;
Subroutine _convert_start_event_to_text: called CC=3 SCAR=16.3
878 12 my $event = shift;
879  
880 12 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 12 12
881 0 my $msg = 'Found bad start event for ' . $event->name;
882  
883 0 ▶ if ( $event->can('delimiter') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->can("delimiter")) 0 0 0
884 0 $msg .= q{ with "} . $event->delimiter . q{" as the delimiter};
885 }
886  
887 0 $msg .= "\n";
888  
889 0 $self->_print_debug($msg);
890 }
891  
892 12 return $self->_make_event(
893 Text => (
894 text => $event->as_text,
895 _converted_from => $event->event_name,
896 )
897 );
898 }
899  
900 sub _merge_consecutive_text_events {
901 1098 ▶ my $self = shift;
Subroutine _merge_consecutive_text_events: called CC=9 SCAR=22.0
902  
903 1098 my $events = $self->__pending_events;
904  
905 1098 my $merge_start;
906  
907 my @to_merge;
908 1098 for my $i ( 0 .. $#{$events} ) {
909 1896 my $event = $events->[$i];
910  
911 1896 ▶ if ( $event->event_name eq 'text' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->event_name eq 'text') { } 1490 406 1896
912 1490 ▶ $merge_start = $i
Branch: 1 branch
BranchTrueFalseTotal
unless defined $merge_start 1474 16 1490
913 unless defined $merge_start;
914 }
915 else {
916 406 ▶ push @to_merge, [ $merge_start, $i - 1 ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $merge_start and $i - 1 > $merge_start 5 401 406
Condition100%3/3
defined $merge_start and $i - 1 > $merge_start
!ll&&!rl&&r
303715
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
917 if defined $merge_start && $i - 1 > $merge_start;
918  
919 406 undef $merge_start;
920 }
921 }
922  
923 # If $merge_start is still defined, then the last event was a text event
924 # which may need to be merged.
925 6 push @to_merge, [ $merge_start, $#{$events} ]
926 1098 ▶ if defined $merge_start && $#{$events} > $merge_start;
Branch: 1 branch
BranchTrueFalseTotal
if defined $merge_start and $#{$events;} > $merge_start 6 1092 1098
Condition66%2/3
defined $merge_start and $#{$events;} > $merge_start
!ll&&!rl&&r
010926
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
927  
928 1098 my $already_merged = 0;
929 1098 for my $pair (@to_merge) {
930 11 $pair->[0] -= $already_merged;
931 11 $pair->[1] -= $already_merged;
932  
933 $self->_splice_merged_text_event(
934 $events,
935 11 @{$pair},
936 );
937  
938 11 $already_merged += $pair->[1] - $pair->[0];
939 }
940 }
941  
942 sub _splice_merged_text_event {
943 11 ▶ my $self = shift;
Subroutine _splice_merged_text_event: called CC=2 SCAR=7.0
944 11 my $events = shift;
945 11 my $start = shift;
946 11 my $end = shift;
947  
948 11 my @to_merge = map { $_->text } @{$events}[ $start .. $end ];
949  
950 $self->_print_debug(
951 "Merging consecutive text events ($start-$end) for: \n"
952 11 ▶ . ( join q{}, map {" - [$_]\n"} @to_merge ) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 11 11
953 if $self->debug;
954  
955 11 my $merged_text = join q{}, @to_merge;
956  
957 11 my $event = $self->_make_event(
958 Text => (
959 text => $merged_text,
960 _merged_from => \@to_merge,
961 ),
962 );
963  
964 11 splice @{$events}, $start, ( $end - $start ) + 1, $event;
965 }
966  
967 sub _debug_pending_events {
968 2196 ▶ my $self = shift;
Subroutine _debug_pending_events: called CC=3 SCAR=15.1
969 2196 my $desc = shift;
970  
971 2196 ▶ return unless $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 2196 0 2196
972  
973 0 my $msg = "Pending event stream $desc:\n";
974  
975 0 for my $event ( $self->_pending_events ) {
976 0 $msg .= $event->debug_dump . "\n";
977 }
978  
979 0 $self->_print_debug($msg);
980 }
981  
982 35 __PACKAGE__->meta->make_immutable;
983  
984 35 1;
985  
986 # ABSTRACT: Span parser for standard Markdown
987  
988 __END__
989  
990 =pod
991  
992 =encoding UTF-8
993  
994 =head1 NAME
995  
996 Markdent::Parser::SpanParser - Span parser for standard Markdown
997  
998 =head1 VERSION
999  
1000 version 0.39
1001  
1002 =head1 DESCRIPTION
1003  
1004 This class parses spans for the standard Markdown dialect (as defined by
1005 Daring Fireball and mdtest).
1006  
1007 =head1 METHODS
1008  
1009 This class provides the following methods:
1010  
1011 =head2 Markdent::Parser::SpanParser->new( handler => $handler )
1012  
1013 Creates a new span parser object. You must provide a span parser object.
1014  
1015 =head2 $span_parser->extract_link_ids(\$markdown)
1016  
1017 This method takes a reference to a markdown string and parses it for link
1018 ids. These are removed from the document and stored in the span parser for
1019 later use.
1020  
1021 =head2 $span_parser->parse_block(\$block)
1022  
1023 Parses a block for span-level markup.
1024  
1025 =head1 ROLES
1026  
1027 This class does the L<Markdent::Role::SpanParser>,
1028 L<Markdent::Role::AnyParser>, and L<Markdent::Role::DebugPrinter> roles.
1029  
1030 =head1 BUGS
1031  
1032 See L<Markdent> for bug reporting details.
1033  
1034 Bugs may be submitted at L<https://github.com/houseabsolute/Markdent/issues>.
1035  
1036 I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
1037  
1038 =head1 SOURCE
1039  
1040 The source code repository for Markdent can be found at L<https://github.com/houseabsolute/Markdent>.
1041  
1042 =head1 AUTHOR
1043  
1044 Dave Rolsky <autarch@urth.org>
1045  
1046 =head1 COPYRIGHT AND LICENSE
1047  
1048 This software is copyright (c) 2021 by Dave Rolsky.
1049  
1050 This is free software; you can redistribute it and/or modify it under
1051 the same terms as the Perl 5 programming language system itself.
1052  
1053 The full text of the license can be found in the
1054 F<LICENSE> file included with this distribution.
1055  
1056 =cut
« Markdent/Role/BalancedEvent.pm ↑ Summary Markdent/Role/AnyParser.pm »