Markdown/Perl/Inlines.pm

filter: Statementstmt 99.7% 415 / 416 Branchbran 97.2% 208 / 214 Conditioncond 97.6% 84 / 86 MC/DCmcdc 95.5% 43 / 45 Subroutinesub 100% 37 / 37 Podpod 0.0% 0 / 20 totaltotal 96.2% 787 / 818 CC 140 SCAR 24.2
CC
140
Cov
99%
parse_inline_link
25.0
find_link_destination_and_title
18.0
process_whitespaces
13.0
CRAP 11.3
« Markdown/Perl/InlineTree.pm ↑ Summary Markdown/Perl/Options.pm »
1 # Package to process the inline structure of Markdown.
2  
3 package Markdown::Perl::Inlines;
4  
5 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use List::MoreUtils 'first_index', 'last_index';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 ▶use List::Util 1.45 'uniq';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Markdown::Perl::InlineTree ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 31 ▶use Markdown::Perl::Util 'normalize_label';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 31 ▶use Markdown::Perl::HTML 'remove_disallowed_tags';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 31 our $VERSION = 0.01;
20  
21 # Everywhere here, $that is a Markdown::Perl instance that we carry everywhere
22 # because it contains the options that we are using.
23 44053 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod undocumented
24 44053 my ($that, $linkrefs, @lines) = @_;
25  
26 44053 my $text = join("\n", @lines);
27 44053 my $tree = find_code_and_tag_runs($that, $text);
28  
29 # At this point, @runs contains only 'text', 'code', or 'link' elements, that
30 # can’t have any children (yet).
31  
32 44053 ▶ $tree->map(sub { process_char_escaping($that, $_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 # At this point, @runs can also contain 'literal' elements, that don’t have
35 # children.
36  
37 44053 process_links($that, $linkrefs, $tree);
38  
39 # This removes the spurious white-space at the beginning and end of lines and
40 # also inserts hard line break as required.
41 44053 process_whitespaces($that, $tree);
42  
43 # Now, there are more link elements and they can have children instead of
44 # content.
45  
46 44053 ▶ if ($that->get_use_extended_autolinks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that->get_use_extended_autolinks) 24062 19991 44053
47 24062 ▶ $tree->map(sub { create_extended_autolinks($that, $_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
48 24062 ▶ $tree->map(sub { create_extended_email_autolinks($that, $_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
49 }
50  
51 44053 process_styles($that, $tree);
52  
53 # At this point we have added the emphasis, strong emphasis, etc. in the tree.
54  
55 $tree->apply(
56 177482 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 177482 $_->escape_content($that->get_html_escaped_characters,
58 $that->get_html_escaped_code_characters);
59 44053 });
60  
61 44053 my $out = $tree->render_html();
62  
63 44053 return $out;
64 }
65  
66 # TODO: share these regex with Perl.pm (but note that we are not matching the
67 # open and close < > characters here).
68 31 my $html_tag_name_re = qr/[a-zA-Z][-a-zA-Z0-9]*/;
69 31 my $html_attribute_name_re = qr/[a-zA-Z_:][-a-zA-Z0-9_.:]*/;
70 31 my $html_space_re = qr/\n[ \t]*|[ \t][ \t]*\n?[ \t]*/; # Spaces, tabs, and up to one line ending.
71 31 my $opt_html_space_re = qr/[ \t]*\n?[ \t]*/; # Optional spaces.
72 31 my $html_attribute_value_re = qr/ [^ \t\n"'=<>`]+ | '[^']*' | "[^"]*" /x;
73 31 my $html_attribute_re =
74 qr/ ${html_space_re} ${html_attribute_name_re} (?: ${opt_html_space_re} = ${opt_html_space_re} ${html_attribute_value_re} )? /x;
75  
76 31 my $html_open_tag_re = qr/ ${html_tag_name_re} ${html_attribute_re}* ${opt_html_space_re} \/? /x;
77 31 my $html_close_tag_re = qr/ \/ ${html_tag_name_re} ${opt_html_space_re} /x;
78 31 my $html_comment_re = qr/!--|!---|!--.*?--/s;
79 31 my $html_proc_re = qr/\?.*?\?/s;
80 31 my $html_decl_re = qr/![a-zA-Z].*?/s;
81 31 my $html_cdata_re = qr/!\[CDATA\[.*?\]\]/s;
82  
83 31 my $html_tag_re =
84 qr/ ${html_open_tag_re} | ${html_close_tag_re} | ${html_comment_re} | ${html_proc_re} | ${html_decl_re} | ${html_cdata_re}/x;
85  
86 # Bug: there is a bug in that backslash escapes don’t work inside autolinks. But
87 # we can turn our autolinks into full-links later (where the escape should
88 # work). However, the spec does not test this corner case so we’re fine.
89  
90 44053 ▶sub find_code_and_tag_runs {
Subroutine find_code_and_tag_runs: called CC=12 SCAR=24.9
Pod undocumented
91 44053 my ($that, $text) = @_;
92  
93 44053 my $tree = Markdown::Perl::InlineTree->new();
94  
95 # We match code-spans and autolinks first as they bind strongest. Raw HTML
96 # should be here too, but we don’t support it yet.
97 # https://spec.commonmark.org/0.30/#code-spans
98 # TODO: https://spec.commonmark.org/0.30/#autolinks
99 # TODO: https://spec.commonmark.org/0.30/#raw-html
100 # while ($text =~ m/(?<code>\`+)|(?<html>\<)/g) {
101 # We are manually handling the backslash escaping here because they are not
102 # interpreted inside code blocks. We will then process all the others
103 # afterward.
104 44053 ▶ while ($text =~ m/(?<! \\) (?<backslashes> (?:\\\\)*) (?: (?<code>\`+) | < )/gx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(?<! \\) (?<backslashes> (?:\\\\)*) (?: (?<code>\`+) | < )/gux) 48955 44053 93008
105 my ($start_before, $start_after) =
106 48955 ($LAST_MATCH_START[0] + length($+{backslashes}), $LAST_MATCH_END[0]);
107 48955 ▶ if ($+{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'code'}) { } 18417 30538 48955
108 18417 my $fence = $+{code};
109 # We’re searching for a fence of the same length, without any backtick
110 # before or after.
111 18417 ▶ if ($text =~ m/(?<!\`)${fence}(?!\`)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(?<!\`)$fence(?!\`)/cgu) 3919 14498 18417
112 3919 my ($end_before, $end_after) = ($LAST_MATCH_START[0], $LAST_MATCH_END[0]);
113 3919 ▶ $tree->push(new_text(substr($text, 0, $start_before)))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 3235 684 3919
114 if $start_before > 0;
115 3919 $tree->push(new_code(substr($text, $start_after, ($end_before - $start_after))));
116 3919 substr $text, 0, $end_after, ''; # This resets pos($text) as we want it to.
117 } # in the else clause, pos($text) == $start_after (because of the /c modifier).
118 } else {
119 # We matched a single < character.
120 30538 my $re = $that->get_autolinks_regex;
121 30538 my $email_re = $that->get_autolinks_email_regex;
122 # We’re not using /gc in these to regex because this confuses the ProhibitUnusedCapture
123 # PerlCritic policy. Anyway, as we are always resetting pos() in case of
124 # successful match, it’s not important to update it.
125 30538 ▶ if ($text =~ m/\G(?<link>${re})>/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($text =~ /\G(?<link>$re)>/u) { } 8133 22405 30538
elsif ($text =~ /\G(?<link>$email_re)>/u) { } 4 22401 22405
elsif ($text =~ /\G(?:$html_tag_re)>/u) { } 14107 8294 22401
126 8133 ▶ $tree->push(new_text(substr($text, 0, $start_before)))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 6960 1173 8133
127 if $start_before > 0;
128 8133 $tree->push(new_link($+{link}, type => 'autolink', target => $+{link}));
129 8133 substr $text, 0, $+[0], ''; # This resets pos($text) as we want it to.
130 } elsif ($text =~ m/\G(?<link>${email_re})>/) {
131 4 ▶ $tree->push(new_text(substr($text, 0, $start_before)))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 0 4 4
132 if $start_before > 0;
133 # TODO: we have a bug in to_source_text, that will assume that the
134 # mailto: was part of the source text.
135 4 $tree->push(new_link($+{link}, type => 'autolink', target => 'mailto:'.$+{link}));
136 4 substr $text, 0, $+[0], ''; # This resets pos($text) as we want it to.
137 } elsif ($text =~ m/\G(?:${html_tag_re})>/) {
138 # This resets pos($text) as we want it to.
139 14107 ▶ $tree->push(new_text(substr($text, 0, $start_before, '')))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 13410 697 14107
140 if $start_before > 0;
141 14107 my $html = substr($text, 0, $LAST_MATCH_END[0] - $start_before, '');
142 14107 remove_disallowed_tags($html, $that->get_disallowed_html_tags);
143 14107 $tree->push(new_html($html));
144 }
145 }
146 }
147 44053 ▶ $tree->push(new_text($text)) if length($text) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 0 41671 2382 44053
148  
149 44053 return $tree;
150 }
151  
152 91439 ▶sub process_char_escaping {
Subroutine process_char_escaping: called CC=8 SCAR=20.8
Pod undocumented
153 91439 my ($that, $node) = @_;
154  
155 # This is executed after
156 91439 ▶ if ($node->{type} eq 'code' || $node->{type} eq 'link') {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->{'type'} eq 'code' or $node->{'type'} eq 'link') { } 12056 79383 91439
elsif ($node->{'type'} eq 'text') { } 65276 14107 79383
elsif ($node->{'type'} eq 'html') { } 14107 0 14107
Condition100%3/3
$node->{"type"} eq "code" or $node->{"type"} eq "link"
l!l&&r!l&&!r
3919813779383
MC/DC100%2/2
$node->{"type"} eq "code" or $node->{"type"} eq "link"
$node->{"type"} eq "code" $node->{"type"} eq "link"
Truth table100%3/3
A or B
A= $node->{"type"} eq "code"B= $node->{"type"} eq "link"
ABresult
1X1
011
000
157 # At this stage, link nodes are only autolinks, in which back-slash escaping
158 # is not processed.
159 12056 return $node;
160 } elsif ($node->{type} eq 'text') {
161 # TODO: with the current code for map, this could just be @nodes.
162 65276 my $new_tree = Markdown::Perl::InlineTree->new();
163 # TODO: make this regex configurable (the set of characters that can be
164 # escaped). Note that the regex has to be updated in Perl.pm unescape_char
165 # method too.
166 65276 ▶ while ($node->{content} =~ m/\\(\p{PosixPunct})/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"content"} =~ /\\(\p{PosixPunct})/gu) 157 65276 65433
167 # Literal parsing is OK here (even if we will later create label reference
168 # which distinguish between escaped and non-escaped literals) because we
169 # can always invert it (and it makes the rest of the processing be much
170 # simpler because we don’t need to check whether we have escaped text or
171 # not).
172 157 ▶ $new_tree->push(new_text(substr $node->{content}, 0, $LAST_MATCH_START[0]))
Branch: 1 branch
BranchTrueFalseTotal
if $Markdown::Perl::Inlines::LAST_MATCH_START[0] > 0 77 80 157
173 if $LAST_MATCH_START[0] > 0;
174 157 $new_tree->push(new_literal($1));
175 157 substr $node->{content}, 0, $LAST_MATCH_END[0], ''; # This resets pos($node->{content}) as we want it to.
176 }
177 65276 ▶ $new_tree->push($node) if length($node->{content}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $node->{"content"} > 0 65269 7 65276
178 65276 return $new_tree;
179 } elsif ($node->{type} eq 'html') {
180 14107 return $node;
181 } else {
182 0 confess 'Unexpected node type in process_char_escaping: '.$node->{type};
183 }
184 }
185  
186 # We find all the links in the tree.
187 #
188 # We are mostly implementing the recommended algorithm from
189 # https://spec.commonmark.org/0.31.2/#phase-2-inline-structure
190 # Except that we don’t do the inline parsing at this stage.
191 #
192 # Overall, this methods implement this whole section of the spec:
193 # https://spec.commonmark.org/0.30/#links
194 44053 ▶sub process_links {
Subroutine process_links: called CC=12 SCAR=24.8
Pod undocumented
195 44053 my ($that, $linkrefs, $tree) = @_;
196  
197 44053 my @open_link;
198 44053 ▶ for (my $i = 0; $i < @{$tree->{children}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$tree->{"children"};}) 93241 44053 137294
199 93241 my $n = $tree->{children}[$i];
200 93241 ▶ next if $n->{type} ne 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $n->{"type"} ne "text" 26277 66964 93241
201 66964 ▶ while ($n->{content} =~ m/(?<open>!?\[)|\]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"content"} =~ /(?<open>!?\[)|\]/gu) 88957 64807 153764
202 88957 my @pos = ($i, $LAST_MATCH_START[0], $LAST_MATCH_END[0]);
203 88957 ▶ if ($+{open}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'open'}) { } 40436 48521 88957
204 40436 ▶ my $type = $pos[2] - $pos[1] > 1 ? 'img' : 'link';
Branch: 1 branch
BranchTrueFalseTotal
$pos[2] - $pos[1] > 1 ? : 7951 32485 40436
205 40436 push @open_link, {type => $type, active => 1, pos => \@pos};
206 } else {
207 48521 ▶ next unless @open_link;
Branch: 1 branch
BranchTrueFalseTotal
unless @open_link 19413 29108 48521
208 29108 my %open = %{pop @open_link};
209 29108 ▶ next unless $open{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $open{"active"} 37 29071 29108
210 29071 my @text_span = ($open{pos}[0], $open{pos}[2], $pos[0], $pos[1]);
211 29071 my $cur_pos = pos($n->{content});
212 29071 my %target =
213 find_link_destination_and_title($that, $linkrefs, $tree, $pos[0], $pos[2], @text_span);
214 29071 pos($n->{content}) = $cur_pos;
215 29071 ▶ next unless %target;
Branch: 1 branch
BranchTrueFalseTotal
unless %target 26914 2157 29071
216 2157 my $text_tree = $tree->extract(@text_span);
217 my (undef, $dest_node_index) =
218 2157 $tree->extract($open{pos}[0], $open{pos}[1], $open{pos}[0] + 1, 1);
219 2157 my $link = new_link($text_tree, type => $open{type}, %target);
220 2157 $tree->insert($dest_node_index, $link);
221  
222 2157 ▶ if ($open{type} eq 'link') {
Branch: 1 branch
BranchTrueFalseTotal
if ($open{"type"} eq "link") 1294 863 2157
223 1294 for (@open_link) {
224 236 ▶ $_->{active} = 0 if $_->{type} eq 'link';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"type"} eq "link" 128 108 236
225 }
226 }
227 2157 $i = $dest_node_index;
228 2157 last; # same as a next OUTER, but without the need to define the OUTER label.
229 }
230 }
231 }
232 44053 return;
233 }
234  
235 # @text_span is the span of the link definition text, used in case we have a
236 # collapsed link reference call.
237 29071 ▶sub find_link_destination_and_title {
Subroutine find_link_destination_and_title: called CC=18 SCAR=28.9
Pod undocumented
238 29071 my ($that, $linkrefs, $tree, $child_start, $text_start, @text_span) = @_;
239 # We assume that the beginning of the link destination must be just after the
240 # link text and in the same child, as there can be no other constructs
241 # in-between.
242  
243 29071 my $cur_child = $child_start;
244 29071 my $n = $tree->{children}[$cur_child];
245 confess 'Unexpected link destination search in a non-text element: '.$n->{type}
246 29071 ▶ unless $n->{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"type"} eq "text" 0 29071 29071
247 29071 pos($n->{content}) = $text_start;
248 29071 $n->{content} =~ m/ \G (?<space> [ \t\n]+ )? (?: (?<inline> \( ) | (?<reference> \[\]? ) )? /x;
249 29071 my @start = ($child_start, $text_start, $child_start, $LAST_MATCH_END[0]);
250  
251 29071 my $has_space = exists $+{space};
252 29071 my $type;
253 29071 ▶ if (exists $+{inline}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $+{'inline'}) { } 3699 25372 29071
elsif (exists $+{'reference'}) { } 859 24513 25372
254 3699 $type = 'inline';
255 } elsif (exists $+{reference}) {
256 859 ▶ if ($+{reference} eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'reference'} eq '[') { } 540 319 859
257 540 $type = 'reference';
258 } else {
259 319 $type = 'collapsed';
260 }
261 } else {
262 24513 $type = 'shortcut';
263 }
264  
265 29071 my $mode = $that->get_allow_spaces_in_links;
266 29071 ▶ if ($has_space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_space) 1984 27087 29071
267 # 'reference' mode is the mode that emulates Markdown.pl which is kind of
268 # weird and probably does not intend this exact behavior (at most one space
269 # then an optional new line and, if you have it, then any number of spaces).
270 1984 ▶ if ( $mode eq 'reference'
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'reference' and $type eq 'reference' || $type eq 'collapsed' and $+{'space'} =~ /^ ?(?:\n[ \t]*)?$/u) { } 8 1976 1984
Condition100%9/9
$mode eq "reference" and $type eq "reference" || $type eq "collapsed" and $+{"space"} =~ /^ ?(?:\n[ \t]*)?$/u
!ll&&!rl&&r
1962148
$mode eq "reference" and $type eq "reference" || $type eq "collapsed"
!ll&&!rl&&r
133263022
$type eq "reference" or $type eq "collapsed"
l!l&&r!l&&!r
157630
MC/DC100%4/4
$mode eq "reference" and $type eq "reference" || $type eq "collapsed" and $+{"space"} =~ /^ ?(?:\n[ \t]*)?$/u
$mode eq "reference" $type eq "reference" $type eq "collapsed" $+{"space"} =~ /^ ?(?:\n[ \t]*)?$/u
Truth table100%6/6
A and B or C and D
A= $mode eq "reference"B= $type eq "reference"C= $type eq "collapsed"D= $+{"space"} =~ /^ ?(?:\n[ \t]*)?$/u
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
271 && ($type eq 'reference' || $type eq 'collapsed')
272 && $+{space} =~ m/^ ?(?:\n[ \t]*)?$/) {
273 # ok, do nothing
274 } else {
275 # We have forbidden spaces, so we treat this as a tentative shortcut link.
276 1976 $type = 'shortcut';
277 }
278 }
279  
280 29071 ▶ if ($type eq 'inline') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'inline') { } 3648 25423 29071
elsif ($type eq 'reference') { } 488 24935 25423
281 3648 my @target = parse_inline_link($tree, @start);
282 3648 ▶ return @target if @target;
Branch: 1 branch
BranchTrueFalseTotal
if @target 1965 1683 3648
283 # pass-through intended if we can’t parse a valid target, we will try a
284 # shortcut link.
285 } elsif ($type eq 'reference') {
286 488 my %target = parse_reference_link($that, $linkrefs, $tree, @start);
287 488 ▶ return %target if exists $target{target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target{"target"} 45 443 488
288 # no pass-through here if this was a valid reference link syntax. This is
289 # not fully specified by the spec but matches what the reference
290 # implementation does.
291 443 ▶ return if %target;
Branch: 1 branch
BranchTrueFalseTotal
if %target 356 87 443
292 # Otherwise, pass-through.
293 }
294 # This is either a collapsed or a shortcut reference link (or something that
295 # might be one).
296 26705 my $ref = $tree->span_to_source_text(@text_span, UNESCAPE_LITERAL);
297 26705 ▶ $ref = normalize_label($ref) if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 18629 8076 26705
298 26705 ▶ if (my $l = get_linkref($that, $linkrefs, $ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = get_linkref($that, $linkrefs, $ref)) 147 26558 26705
299 147 ▶ $tree->extract(@start) if $type eq 'collapsed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "collapsed" 18 129 147
300 147 return %{$l};
301 }
302 26558 return;
303 }
304  
305 3648 ▶sub parse_inline_link {
Subroutine parse_inline_link: called CC=25 SCAR=32.2
Pod undocumented
306 3648 my ($tree, @start) = @_; # ($child_start, $text_start, $child_start, $text_start + 1);
307 # @start points to before and after the '(' character opening the link.
308  
309 # $cur_child is advanced through the tree while we parse the link destination
310 # and title, it always point to the node that we are currently looking into
311 # (the one containing the end of the element that was previously found).
312 # $n is the node at index $cur_child.
313 3648 my $cur_child = $start[0];
314 3648 my $n = $tree->{children}[$cur_child];
315  
316 3648 pos($n->{content}) = $start[3];
317 3648 $n->{content} =~ m/\G[ \t]*\n?[ \t]*/;
318 3648 my $search_start = $LAST_MATCH_END[0];
319  
320 # TODO: first check if we have a destination between <>, that may have already
321 # been matched as an autolink or as a closing HTML tag :-(
322  
323 3648 my @target;
324 3648 my $ok_to_have_title = 1;
325  
326 3648 my $has_bracket =
327 $tree->find_in_text(qr/</, $cur_child, $search_start, $cur_child, $search_start + 1);
328  
329 # We have this variable early because we may be filling it soon if the link
330 # destination was already parsed as an autolink or an html element.
331 3648 my $target = undef;
332  
333 3648 ▶ if ($has_bracket) {
Branch: 3 branches
BranchTrueFalseTotal
if ($has_bracket) { } 42 3606 3648
elsif (length $n->{'content'} <= $search_start and @{$tree->{'children'};} > $cur_child + 1 and $tree->{'children'}[$cur_child + 1]{'type'} eq 'html' || $tree->{'children'}[$cur_child + 1]{'type'} eq 'link') { } 88 3518 3606
elsif (my(@end_target) = $tree->find_in_text_with_balanced_content(qr/\(/u, qr/\)/u, qr/[ [:cntrl:]]/u, $cur_child, $search_start)) { } 2917 601 3518
334 42 ▶ if (my @end_target = $tree->find_in_text(qr/>/, $cur_child, $search_start + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@end_target) = $tree->find_in_text(qr/>/u, $cur_child, $search_start + 1)) 25 17 42
335 25 @target = ($cur_child, $search_start + 1, $end_target[0], $end_target[1]);
336 25 ▶ return if $tree->find_in_text(qr/<|\n/, @target);
Branch: 1 branch
BranchTrueFalseTotal
if $tree->find_in_text(qr/<|\n/u, @target) 6 19 25
337 }
338 ▶ } elsif (
Condition100%6/6
length $n->{"content"} <= $search_start and @{$tree->{"children"};} > $cur_child + 1 and $tree->{"children"}[$cur_child + 1]{"type"} eq "html" || $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
!ll&&!rl&&r
35051388
length $n->{"content"} <= $search_start and @{$tree->{"children"};} > $cur_child + 1
!ll&&!rl&&r
3367138101
MC/DC100%3/3
length $n->{"content"} <= $search_start and @{$tree->{"children"};} > $cur_child + 1 and $tree->{"children"}[$cur_child + 1]{"type"} eq "html" || $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
length $n->{"content"} <= $search_start @{$tree->{"children"};} > $cur_child + 1 $tree->{"children"}[$cur_child + 1]{"type"} eq "html" || $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
Truth table100%4/4
A and B and C
A= length $n->{"content"} <= $search_startB= @{$tree->{"children"};} > $cur_child + 1C= $tree->{"children"}[$cur_child + 1]{"type"} eq "html" || $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
ABCresult
0XX0
10X0
1100
1111
339 length($n->{content}) <= $search_start
340 239 ▶ && @{$tree->{children}} > $cur_child + 1
Condition100%3/3
$tree->{"children"}[$cur_child + 1]{"type"} eq "html" or $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
l!l&&r!l&&!r
592913
MC/DC100%2/2
$tree->{"children"}[$cur_child + 1]{"type"} eq "html" or $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
$tree->{"children"}[$cur_child + 1]{"type"} eq "html" $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
Truth table100%3/3
A or B
A= $tree->{"children"}[$cur_child + 1]{"type"} eq "html"B= $tree->{"children"}[$cur_child + 1]{"type"} eq "link"
ABresult
1X1
011
000
341 && ( $tree->{children}[$cur_child + 1]{type} eq 'html'
342 || $tree->{children}[$cur_child + 1]{type} eq 'link')
343 ) {
344 # The element inside was already parsed as an autolink or an html element,
345 # we use it as-is for the link destination. However, we need at least one
346 # element after in the tree for this to be valid (otherwise we know that the
347 # syntax can’t be a real tree, so we return from here).
348 88 ▶ return if @{$tree->{children}} <= $cur_child + 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$tree->{"children"};} <= $cur_child + 2 2 86 88
349 86 @target = ($cur_child + 1, 0, $cur_child + 2, 0);
350 86 my $link_node = $tree->{children}[$cur_child + 1];
351 86 ▶ if ($link_node->{type} eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_node->{'type'} eq 'html') { } 57 29 86
352 57 $target = $link_node->{content};
353 57 $target =~ s/^<|>$//g;
354 } else {
355 29 $target = $link_node->{target};
356 }
357 86 ▶ return if $target =~ m/\n/; # No new lines in link targets are allowed.
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ /\n/u 2 84 86
358 } elsif (
359 my @end_target = $tree->find_in_text_with_balanced_content(
360 qr/\(/, qr/\)/, qr/[ [:cntrl:]]/,
361 $cur_child, $search_start)
362 ) {
363 2917 @target = ($cur_child, $search_start, $end_target[0], $end_target[1]);
364 }
365 3638 ▶ if (@target) {
Branch: 1 branch
BranchTrueFalseTotal
if (@target) 3020 618 3638
366 # We can’t extract the target just yet, because the parsing can still fail
367 # in which case we must not modify the tree.
368 3020 $cur_child = $target[2];
369 3020 $n = $tree->{children}[$cur_child];
370 # On the next line, [1] and not [2] because if there was a control character
371 # we will fail the whole method. So we restart the search before the end
372 # condition of the find... method above.
373 3020 ▶ pos($n->{content}) = $target[3] + ($has_bracket ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$has_bracket ? : 19 3001 3020
374 3020 $n->{content} =~ m/\G[ \t]*\n?[ \t]*/;
375 3020 $search_start = $LAST_MATCH_END[0];
376 3020 $ok_to_have_title = $LAST_MATCH_END[0] != $LAST_MATCH_START[0]; # target and title must be separated.
377 }
378  
379 # The first character of the title must be ", ', or ( and so can’t be another
380 # inline construct. As such, using a normal regex is fine (and not an
381 # InlineTree method).
382 3638 pos($n->{content}) = $search_start;
383 3638 my @end_title;
384 3638 ▶ if ($n->{content} =~ m/\G"/gc) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n->{'content'} =~ /\G"/cgu) { } 50 3588 3638
elsif ($n->{'content'} =~ /\G'/cgu) { } 5 3583 3588
elsif ($n->{'content'} =~ /\G\(/cgu) { } 55 3528 3583
385 50 @end_title = $tree->find_in_text(qr/"/, $cur_child, $search_start + 1);
386 } elsif ($n->{content} =~ m/\G'/gc) {
387 5 @end_title = $tree->find_in_text(qr/'/, $cur_child, $search_start + 1);
388 } elsif ($n->{content} =~ m/\G\(/gc) {
389 55 @end_title = $tree->find_balanced_in_text(qr/\(/, qr/\)/, $cur_child, $search_start + 1);
390 }
391 3638 my @title;
392 3638 ▶ if (@end_title) {
Branch: 1 branch
BranchTrueFalseTotal
if (@end_title) 54 3584 3638
393 54 ▶ return unless $ok_to_have_title;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok_to_have_title 0 54 54
394 54 @title = ($cur_child, $search_start + 1, $end_title[0], $end_title[1]);
395 54 $cur_child = $end_title[0];
396 54 $n = $tree->{children}[$cur_child];
397 54 pos($n->{content}) = $end_title[2]; # This time, we look after the closing character.
398 54 $n->{content} =~ m/\G[ \t]*\n?[ \t]*/;
399 54 $search_start = $LAST_MATCH_END[0];
400 }
401  
402 # TODO: call a new InlineTree method to turn (child, offset_at_end) into
403 # (child + 1, 0). This needs to be called also at the beginning of this
404 # method.
405 3638 pos($n->{content}) = $search_start;
406 3638 ▶ return unless $n->{content} =~ m/\G\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"content"} =~ /\G\)/u 1673 1965 3638
407  
408 # Now we have a valid title, we can start to rewrite the tree (beginning from
409 # the end, to not alter the node index before we touch them).
410 {
411 1965 my @last_item = (@title, @target, @start);
412 # We remove the spaces after the last item and also the closing paren.
413 1965 $tree->extract($last_item[2], $last_item[3], $cur_child, $search_start + 1);
414 }
415  
416 1965 my $title;
417 1965 ▶ if (@title) {
Branch: 1 branch
BranchTrueFalseTotal
if (@title) 29 1936 1965
418 29 my $title_tree = $tree->extract(@title);
419 29 $title = $title_tree->to_source_text();
420 29 my @last_item = (@target, @start);
421 29 $tree->extract($last_item[2], $last_item[3], $title[0], $title[1]);
422 }
423  
424 1965 ▶ if (@target) {
Branch: 1 branch
BranchTrueFalseTotal
if (@target) 1965 0 1965
425 1965 my $target_tree = $tree->extract(@target);
426 1965 ▶ $target = $target_tree->to_source_text() unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 1959 6 1965
427 1965 $tree->extract($start[2], $start[3], $target[0], $target[1]);
428 }
429  
430 1965 $tree->extract(@start);
431  
432 # A link with no destination at all, like [foo](), has an empty target.
433 1965 ▶ return (target => $target // '', (defined $title ? (title => $title) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $title ? : 29 1936 1965
Condition50%1/2
$target // ""
l!l
19650
MC/DC0%0/1
$target // ""
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
434 }
435  
436 488 ▶sub parse_reference_link {
Subroutine parse_reference_link: called CC=3 SCAR=11.0
Pod undocumented
437 488 my ($that, $linkrefs, $tree, @start) = @_; # ($child_start, $text_start, $child_start, $text_start + 1);
438  
439 488 my $cur_child = $start[0];
440 488 my $n = $tree->{children}[$cur_child];
441  
442 488 my $ref_start = $start[3];
443  
444 488 ▶ if (my @end_ref = $tree->find_in_text(qr/]/, $cur_child, $start[3])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@end_ref) = $tree->find_in_text(qr/]/u, $cur_child, $start[3])) 401 87 488
445 401 my $ref =
446 normalize_label($tree->span_to_source_text(@start[2, 3], @end_ref[0, 1], UNESCAPE_LITERAL));
447 401 ▶ if (my $l = get_linkref($that, $linkrefs, $ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = get_linkref($that, $linkrefs, $ref)) { } 45 356 401
448 45 $tree->extract(@start[0, 1], @end_ref[0, 2]);
449 45 return %{$l};
450 } else {
451 # TODO: we should only return this if the span was indeed a valid
452 # reference link label (not longer than 1000 characters mostly).
453 # This is used to notice that we had a proper reference link syntax and
454 # not fallback to trying a shortcut link.
455 356 return (ignored_valid_value => 1);
456 }
457 }
458 87 return;
459 }
460  
461 # Returns a hashref with (title and dest) or undef.
462 27106 ▶sub get_linkref {
Subroutine get_linkref: called CC=3 SCAR=11.0
Pod undocumented
463 27106 my ($that, $linkrefs, $ref) = @_;
464 27106 ▶ if (exists $linkrefs->{$ref}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $linkrefs->{$ref}) { } 186 26920 27106
elsif (exists $that->{'hooks'}{'resolve_link_ref'}) { } 7 26913 26920
465 186 return $linkrefs->{$ref};
466 } elsif (exists $that->{hooks}{resolve_link_ref}) {
467 7 return $that->{hooks}{resolve_link_ref}->($ref);
468 }
469 26913 return;
470 }
471  
472 # This methods remove line break at the beginning and end of lines (inside text
473 # nodes only), and add hard line breaks as required.
474 #
475 # $not_root is set when we recurse inside sub-tree, to indicate that the first
476 # and last node of the the tree are not, in fact, the beginning and and of the
477 # paragraph.
478 46210 ▶sub process_whitespaces {
Subroutine process_whitespaces: called CC=13 SCAR=25.6
Pod undocumented
479 46210 my ($that, $tree, $not_root) = @_;
480  
481 46210 ▶ for (my $i = 0; $i < @{$tree->{children}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$tree->{"children"};}) 96484 46210 142694
482 96484 my $n = $tree->{children}[$i];
483 96484 ▶ process_whitespaces($that, $n->{subtree}, 1) if exists $n->{subtree};
Branch: 1 branch
BranchTrueFalseTotal
if exists $n->{"subtree"} 2157 94327 96484
484 96484 ▶ next unless $n->{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"type"} eq "text" 28434 68050 96484
485 # TODO: add tests for the fact that we don’t want hard break at the end of a
486 # paragraph.
487 68050 my $re;
488 68050 ▶ if ($that->get_two_spaces_hard_line_breaks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that->get_two_spaces_hard_line_breaks) { } 47780 20270 68050
489 47780 $re = qr/(?: {2,}|\\)\n(?=.) */s;
490 } else {
491 20270 $re = qr/\\\n(?=.) */s;
492 }
493 68050 my @hard_breaks = split($re, $n->{content}, -1);
494 68050 ▶ for (my $j = 0; $j < @hard_breaks; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @hard_breaks) 78459 68050 146509
495 # $hard_breaks[$j] = '' unless defined($hard_breaks[$j]);
496 78459 ▶ $hard_breaks[$j] =~ s/^ +// if !$not_root && $i == 0 && $j == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $not_root and $i == 0 and $j == 0 41921 36538 78459
Condition100%6/6
not $not_root and $i == 0 and $j == 0
!ll&&!rl&&r
31070546841921
not $not_root and $i == 0
!ll&&!rl&&r
24962857447389
MC/DC100%3/3
not $not_root and $i == 0 and $j == 0
not $not_root $i == 0 $j == 0
Truth table100%4/4
not(A) and B and C
A= not $not_rootB= $i == 0C= $j == 0
ABCresult
0XX0
10X0
1100
1111
497 78459 $hard_breaks[$j] =~ s/(\n|\r) +/$1/g;
498 $hard_breaks[$j] =~ s/ +$//gm
499 78459 ▶ if !$not_root && $i == $#{$tree->{children}} && $j == $#hard_breaks;
Branch: 1 branch
BranchTrueFalseTotal
if not $not_root and $i == $#{$$tree{"children"};} and $j == $#hard_breaks 41267 37192 78459
Condition100%6/6
not $not_root and $i == $#{$$tree{"children"};} and $j == $#hard_breaks
!ll&&!rl&&r
30679651341267
not $not_root and $i == $#{$$tree{"children"};}
!ll&&!rl&&r
24962818347780
MC/DC100%3/3
not $not_root and $i == $#{$$tree{"children"};} and $j == $#hard_breaks
not $not_root $i == $#{$$tree{"children"};} $j == $#hard_breaks
Truth table100%4/4
not(A) and B and C
A= not $not_rootB= $i == $#{$$tree{"children"};}C= $j == $#hard_breaks
ABCresult
0XX0
10X0
1100
1111
500 78459 ▶ if ($j == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) { } 68050 10409 78459
501 68050 $n->{content} = $hard_breaks[0];
502 } else {
503 10409 $tree->insert($i + 1, new_html('<br />'), new_text("\n".$hard_breaks[$j]));
504 10409 $i += 2;
505 }
506 }
507 }
508 46210 return;
509 }
510  
511 # This methods adds "style", that is it parses the emphasis (* and _) and also
512 # strike-through (~). To do so, we process each level of the tree independently
513 # because a style-run can’t cross another HTML construct (but it can span over
514 # it).
515 #
516 # We first find all the possible delimiters and insert them in the tree instead
517 # of their text. And then decide whether they are actually opening, closing, or
518 # neither.
519 #
520 # This methods implement all of:
521 # https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis
522 46210 ▶sub process_styles {
Subroutine process_styles: called CC=6 SCAR=17.9
Pod undocumented
523 46210 my ($that, $tree) = @_;
524  
525 # We recurse first as there are less children to iterate over than after.
526 46210 for my $c (@{$tree->{children}}) {
527 118039 ▶ process_styles($that, $c->{subtree}) if exists $c->{subtree};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{"subtree"} 2157 115882 118039
528 }
529  
530 # TODO: only search for characters that are actually used by our current
531 # options.
532 46210 my $current_child = 0;
533 46210 my @delimiters;
534 46210 my $delim = delim_characters($that);
535 46210 my %max_delim_run_length = %{$that->get_inline_delimiters_max_run_length};
536 46210 ▶ while (my @match = $tree->find_in_text(qr/([${delim}])\1*/, $current_child, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@match) = $tree->find_in_text(qr/([$delim])\1*/u, $current_child, 0)) 39855 46210 86065
537 # We extract the delimiter run into a new node, that will be at $index.
538 39855 my ($delim_tree, $index) = $tree->extract($match[0], $match[1], $match[0], $match[2]);
539 # We use the type literal so that if we do nothing with the delimiter it
540 # will be rendered correctly. We keep track of which literals might be
541 # delimiters using the @delimiters array.
542 39855 $delim_tree->{children}[0]{type} = 'literal';
543 39855 $tree->insert($index, $delim_tree);
544 39855 my $d = classify_delimiter($that, $tree, $index);
545 39855 ▶ if (!exists $max_delim_run_length{$d->{delim}}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $max_delim_run_length{$d->{"delim"}} or $d->{"len"} <= $max_delim_run_length{$d->{"delim"}}) 39853 2 39855
Condition100%3/3
not exists $max_delim_run_length{$d->{"delim"}} or $d->{"len"} <= $max_delim_run_length{$d->{"delim"}}
l!l&&r!l&&!r
3984762
MC/DC100%2/2
not exists $max_delim_run_length{$d->{"delim"}} or $d->{"len"} <= $max_delim_run_length{$d->{"delim"}}
not exists $max_delim_run_length{$d->{"delim"}} $d->{"len"} <= $max_delim_run_length{$d->{"delim"}}
Truth table100%3/3
not(A) or B
A= not exists $max_delim_run_length{$d->{"delim"}}B= $d->{"len"} <= $max_delim_run_length{$d->{"delim"}}
ABresult
1X1
011
000
546 || $d->{len} <= $max_delim_run_length{$d->{delim}}) {
547 39853 push @delimiters, $d;
548 }
549 39855 $current_child = $index + 1;
550 }
551  
552 46210 match_delimiters($that, $tree, @delimiters);
553 46210 return;
554 }
555  
556 # Decides whether the delimiter run at the given index in the tree can open or
557 # close emphasis (or any other style).
558 39855 ▶sub classify_delimiter {
Subroutine classify_delimiter: called CC=10 SCAR=23.0
Pod undocumented
559 39855 my ($that, $tree, $index) = @_;
560 39855 my $pred_type = classify_flank($that, $tree, $index, 'left');
561 39855 my $succ_type = classify_flank($that, $tree, $index, 'right');
562 39855 ▶ my $is_left = $succ_type ne 'space' && ($succ_type ne 'punct' || $pred_type ne 'none');
Condition100%6/6
$succ_type ne 'space' && ($succ_type ne 'punct' || $pred_type ne 'none')
!ll&&!rl&&r
6129765926067
$succ_type ne "punct" or $pred_type ne "none"
l!l&&r!l&&!r
11372146957659
MC/DC100%3/3
$succ_type ne 'space' && ($succ_type ne 'punct' || $pred_type ne 'none')
$succ_type ne 'space' $succ_type ne "punct" $pred_type ne "none"
Truth table100%4/4
A && B or C
A= $succ_type ne 'space'B= $succ_type ne "punct"C= $pred_type ne "none"
ABCresult
0XX0
1000
11X1
1011
563 39855 ▶ my $is_right = $pred_type ne 'space' && ($pred_type ne 'punct' || $succ_type ne 'none');
Condition100%6/6
$pred_type ne 'space' && ($pred_type ne 'punct' || $succ_type ne 'none')
!ll&&!rl&&r
8219632825308
$pred_type ne "punct" or $succ_type ne "none"
l!l&&r!l&&!r
11902134066328
MC/DC100%3/3
$pred_type ne 'space' && ($pred_type ne 'punct' || $succ_type ne 'none')
$pred_type ne 'space' $pred_type ne "punct" $succ_type ne "none"
Truth table100%4/4
A && B or C
A= $pred_type ne 'space'B= $pred_type ne "punct"C= $succ_type ne "none"
ABCresult
0XX0
1000
11X1
1011
564 39855 my $len = length($tree->{children}[$index]{content});
565 39855 my $delim = substr $tree->{children}[$index]{content}, 0, 1;
566 39855 my $can_open = 0;
567 39855 my $can_close = 0;
568 # This is implementing the first 8 rules (out of 17...) of
569 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis
570 # The rules are more complex for '_' than for '*' because it is assuming that
571 # underscores can appear within word. So we apply the star rules to all other
572 # delimiters (that is, we only check for underscore here). Currently our only
573 # other delimiter is '~'.
574 # TODO: add an option to decide which rule to apply per delimiter.
575 39855 ▶ if ($delim eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim eq '_') { } 8198 31657 39855
576 8198 ▶ $can_open = $is_left && (!$is_right || $pred_type eq 'punct');
Condition100%6/6
$is_left && (!$is_right || $pred_type eq 'punct')
!ll&&!rl&&r
22831235792
not $is_right or $pred_type eq "punct"
l!l&&r!l&&!r
21833609123
MC/DC100%3/3
$is_left && (!$is_right || $pred_type eq 'punct')
$is_left not $is_right $pred_type eq "punct"
Truth table100%4/4
A && not(B) or C
A= $is_leftB= not $is_rightC= $pred_type eq "punct"
ABCresult
0XX0
1000
11X1
1011
577 8198 ▶ $can_close = $is_right && (!$is_left || $succ_type eq 'punct');
Condition100%6/6
$is_right && (!$is_left || $succ_type eq 'punct')
!ll&&!rl&&r
25601235515
not $is_left or $succ_type eq "punct"
l!l&&r!l&&!r
19063609123
MC/DC100%3/3
$is_right && (!$is_left || $succ_type eq 'punct')
$is_right not $is_left $succ_type eq "punct"
Truth table100%4/4
A && not(B) or C
A= $is_rightB= not $is_leftC= $succ_type eq "punct"
ABCresult
0XX0
1000
11X1
1011
578 } else {
579 31657 $can_open = $is_left;
580 31657 $can_close = $is_right;
581 }
582 return {
583 39855 index => $index,
584 can_open => $can_open,
585 can_close => $can_close,
586 len => $len,
587 delim => $delim,
588 orig_len => $len
589 };
590 }
591  
592 # Computes whether the type of the "flank" of the delimiter run at the given
593 # index in the tree (looking either at the "left" or "right" side). This returns
594 # one of 'none', 'punct', or 'space' following the rule given in
595 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis.
596 # The purpose is to decide whether the delimiter run is left flanking and/or
597 # right flanking (that decision happens in classify_delimiter).
598 79710 ▶sub classify_flank {
Subroutine classify_flank: called CC=12 SCAR=24.8
Pod undocumented
599 79710 my ($that, $tree, $index, $side) = @_;
600 79710 ▶ return 'space' if $index == 0 && $side eq 'left';
Branch: 1 branch
BranchTrueFalseTotal
if $index == 0 and $side eq "left" 3408 76302 79710
Condition100%3/3
$index == 0 and $side eq "left"
!ll&&!rl&&r
7289434083408
MC/DC100%2/2
$index == 0 and $side eq "left"
$index == 0 $side eq "left"
Truth table100%3/3
A and B
A= $index == 0B= $side eq "left"
ABresult
0X0
100
111
601 76302 ▶ return 'space' if $index == $#{$tree->{children}} && $side eq 'right';
Branch: 1 branch
BranchTrueFalseTotal
if $index == $#{$$tree{"children"};} and $side eq "right" 3056 73246 76302
Condition100%3/3
$index == $#{$$tree{"children"};} and $side eq "right"
!ll&&!rl&&r
7032129253056
MC/DC100%2/2
$index == $#{$$tree{"children"};} and $side eq "right"
$index == $#{$$tree{"children"};} $side eq "right"
Truth table100%3/3
A and B
A= $index == $#{$$tree{"children"};}B= $side eq "right"
ABresult
0X0
100
111
602 73246 ▶ my $node = $tree->{children}[$index + ($side eq 'left' ? -1 : 1)];
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'left' ? : 36447 36799 73246
603 # If the node before the delimiters is not text, let’s assume that we had some
604 # punctuation characters that delimited it.
605 73246 ▶ return 'punct' if $node->{type} ne 'text' && $node->{type} ne 'literal';
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"type"} ne "text" and $node->{"type"} ne "literal" 3849 69397 73246
Condition100%3/3
$node->{"type"} ne "text" and $node->{"type"} ne "literal"
!ll&&!rl&&r
687256723849
MC/DC100%2/2
$node->{"type"} ne "text" and $node->{"type"} ne "literal"
$node->{"type"} ne "text" $node->{"type"} ne "literal"
Truth table100%3/3
A and B
A= $node->{"type"} ne "text"B= $node->{"type"} ne "literal"
ABresult
0X0
100
111
606 69397 ▶ my $space_re = $side eq 'left' ? qr/\s$/u : qr/^\s/u;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'left' ? : 34916 34481 69397
607 69397 ▶ return 'space' if $node->{content} =~ m/${space_re}/;
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"content"} =~ /$space_re/u 7884 61513 69397
608 61513 ▶ my $punct_re = $side eq 'left' ? qr/[\p{Punct}\p{Symbol}]$/u : qr/^[\p{Punct}\p{Symbol}]/u;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'left' ? : 30105 31408 61513
609 61513 ▶ return 'punct' if $node->{content} =~ m/${punct_re}/;
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"content"} =~ /$punct_re/u 38239 23274 61513
610 23274 return 'none';
611 }
612  
613 # We match the pair of delimiters together as much as we can, following the
614 # rules of the CommonMark spec.
615 46210 ▶sub match_delimiters {
Subroutine match_delimiters: called CC=4 SCAR=13.9
Pod undocumented
616 46210 my ($that, $tree, @delimiters) = @_;
617  
618 46210 ▶ for (my $close_index = 1; $close_index < @delimiters; $close_index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_index < @delimiters) 20572 46210 66782
619 20572 my %c = %{$delimiters[$close_index]};
620 20572 ▶ next if !$c{can_close};
Branch: 1 branch
BranchTrueFalseTotal
unless $c{"can_close"} 4812 15760 20572
621 # We have a closing delimiter, now we backtrack and find the tighter match
622 # for this closing delimiter. This is because "*foo _bar* baz_" will only
623 # match the * (that comes first) but "*foo *bar*"" will match the second
624 # and third star, that are the tightest match. This is for rule 15 and 16 of
625 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis
626 # We also apply rules 9 and 10 here. Rules 1-8 have already been computed in
627 # classify_delimiter.
628 my $open_index =
629 15182 ▶ last_index { $_->{can_open} && $_->{delim} eq $c{delim} && valid_rules_9_10($_, \%c) }
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$_->{"can_open"} and $_->{"delim"} eq $c{"delim"} and valid_rules_9_10($_, \%c) 11262 3920 15182
Condition100%3/3
$_->{"can_open"} and $_->{"delim"} eq $c{"delim"}
!ll&&!rl&&r
1473244711262
MC/DC100%2/2
$_->{"can_open"} and $_->{"delim"} eq $c{"delim"}
$_->{"can_open"} $_->{"delim"} eq $c{"delim"}
Truth table100%3/3
A and B
A= $_->{"can_open"}B= $_->{"delim"} eq $c{"delim"}
ABresult
0X0
100
111
630 15760 @delimiters[0 .. $close_index - 1];
631 # TODO: here there are a lot of optimization that we could apply, based on
632 # the "process emphasis" method from the spec (like removing our closing
633 # delimiter if it is not an opener, and keeping track of the fact that
634 # we have no delimiter in the 0..close_index-1 range that can match a
635 # delimiter of the same type as %c).
636 # This does not seem very important for reasonable inputs. So, instead, we
637 # just check the next potential closer.
638 15760 ▶ next if $open_index == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $open_index == -1 5672 10088 15760
639  
640 10088 $close_index = apply_delimiters($that, $tree, \@delimiters, $open_index, $close_index);
641 }
642  
643 46210 return;
644 }
645  
646 # Given a tree, its delimiters and the index of two delimiters, rewrite the
647 # tree with the style applied by these delimiters (we’re assuming here that they
648 # are of a matching type).
649 #
650 # The delimiter may not be consumed entirely (but we will consume as much as
651 # possible).
652 10088 ▶sub apply_delimiters {
Subroutine apply_delimiters: called CC=5 SCAR=16.1
Pod undocumented
653 10088 my ($that, $tree, $delimiters, $open_index, $close_index) = @_;
654 10088 my %o = %{$delimiters->[$open_index]};
655 10088 my %c = %{$delimiters->[$close_index]};
656  
657 # We rewrite the tree in between our two delimiters.
658 # TODO: maybe we need a splice method in InlineTree.
659 10088 my @styled_subnodes = splice @{$tree->{children}}, $o{index} + 1, $c{index} - $o{index} - 1;
660 10088 my $styled_tree = Markdown::Perl::InlineTree->new();
661 10088 $styled_tree->push(@styled_subnodes);
662 # With our current algorithm in match_delimiters we know that there is no
663 # reasons to recurse (because the closing delimiter here was the first
664 # closing delimiter with a matching opener.)
665 # my @styled_delimiters = map { $_->{index} -= $o{index} + 1; $_ } splice @{$delimiters},
666 # $open_index + 1, $close_index - $open_index - 1;
667 # match_delimiters($that, $styled_tree, @styled_delimiters);
668 10088 splice @{$delimiters}, $open_index + 1, $close_index - $open_index - 1;
669  
670 # And now we rebuild our own tree around the new one.
671 10088 my $len = min($o{len}, $c{len}, max_delim_length($that, $o{delim}));
672 10088 my $styled_node = new_style($styled_tree, tag => delim_to_html_tag($that, $o{delim} x $len));
673 10088 my $style_start = $o{index};
674 10088 my $style_length = 2;
675 10088 $close_index = $open_index + 1;
676 10088 ▶ if ($len < $o{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $o{'len'}) { } 375 9713 10088
677 375 substr($tree->{children}[$o{index}]{content}, $o{len} - $len) = ''; ## no critic (ProhibitLvalueSubstr)
678 375 $delimiters->[$open_index]{len} -= $len;
679 375 $style_start++;
680 375 $style_length--;
681 } else {
682 9713 splice @{$delimiters}, $open_index, 1;
683 9713 $close_index--;
684 }
685 10088 ▶ if ($len < $c{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $c{'len'}) { } 431 9657 10088
686 # The closing node is now just after the opening one.
687 431 substr($tree->{children}[$o{index} + 1]{content}, $c{len} - $len) = ''; ## no critic (ProhibitLvalueSubstr)
688 431 $delimiters->[$close_index]{len} -= $len;
689 431 $style_length--;
690 } else {
691 9657 splice @{$delimiters}, $close_index, 1; # We remove our closing delimiter.
692 }
693 10088 splice @{$tree->{children}}, $style_start, $style_length, $styled_node;
694 10088 for my $i ($close_index .. $#{$delimiters}) {
695 7250 $delimiters->[$i]{index} -= $c{index} - $o{index} - 2 + $style_length;
696 }
697 10088 ▶ return $open_index - ($len < $o{len} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$len < $o{'len'} ? : 375 9713 10088
698 }
699  
700 # Returns true if the given delimiters can be an open/close pair without
701 # breaking rules 9 and 10 of
702 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis.
703 11262 ▶sub valid_rules_9_10 {
Subroutine valid_rules_9_10: called CC=5 SCAR=16.1
Pod undocumented
704 11262 my ($o, $c) = @_;
705 # TODO: BUG: there is a probable bug here in that the length of the delimiter
706 # to consider is not its current length but the length of the original span
707 # of which it was a part.
708 return
709 (!$o->{can_close} && !$c->{can_open})
710 || (($o->{orig_len} + $c->{orig_len}) % 3 != 0)
711 11262 ▶ || ($o->{orig_len} % 3 == 0 && $c->{orig_len} % 3 == 0);
Condition91%11/12
!($o->{'can_close'} || $c->{'can_open'}) || ($o->{'orig_len'} + $c->{'orig_len'}) % 3 != 0 || $o->{'orig_len'} % 3 == 0 && $c->{'orig_len'} % 3 == 0
l!l&&r!l&&!r
10078101174
not $o->{"can_close"} || $c->{"can_open"} or ($o->{"orig_len"} + $c->{"orig_len"}) % 3 != 0
l!l&&r!l&&!r
709229861184
$o->{'can_close'} || $c->{'can_open'}
l!l&&r!l&&!r
263815327092
$o->{"orig_len"} % 3 == 0 and $c->{"orig_len"} % 3 == 0
!ll&&!rl&&r
1174010
MC/DC80%4/5
!($o->{'can_close'} || $c->{'can_open'}) || ($o->{'orig_len'} + $c->{'orig_len'}) % 3 != 0 || $o->{'orig_len'} % 3 == 0 && $c->{'orig_len'} % 3 == 0
$o->{'can_close'} $c->{'can_open'} ($o->{"orig_len"} + $c->{"orig_len"}) % 3 != 0 $o->{"orig_len"} % 3 == 0 $c->{"orig_len"} % 3 == 0
Truth table66%6/9
not(A || B) or C || D and E
A= $o->{'can_close'}B= $c->{'can_open'}C= ($o->{"orig_len"} + $c->{"orig_len"}) % 3 != 0D= $o->{"orig_len"} % 3 == 0E= $c->{"orig_len"} % 3 == 0
ABCDEresult
00XXX1
1X1XX1
011XX1
1X0111
010111
1X00X0
1X0100
0100X0
010100
712 }
713  
714 # TODO: use ^ and ˇ to represent sup and sub
715 # TODO: add support for MathML in some way.
716 10088 ▶sub delim_to_html_tag {
Subroutine delim_to_html_tag: called CC=1 SCAR=0.0
Pod undocumented
717 10088 my ($that, $delim) = @_;
718 # TODO: sort what to do if a given delimiter does not have a variant with
719 # two characters (we must backtrack somewhere in match_delimiters probably).
720 # TODO: add support for <span class="foo"> when the value in the map is ".foo"
721 # instead of just "foo".
722 10088 return $that->get_inline_delimiters()->{$delim};
723 }
724  
725 # Return the list of characters that can be delimiters (using the regex
726 # character class syntax).
727 46210 ▶sub delim_characters {
Subroutine delim_characters: called CC=1 SCAR=0.0
Pod undocumented
728 46210 my ($that) = @_;
729 # TODO: memo-ize this function inside $that (but clear it when the options
730 # change).
731 46210 my @c = map { substr $_, 0, 1 } keys %{$that->get_inline_delimiters()};
732 46210 return join('', uniq @c);
733 }
734  
735 # Returns the max defined delim
736 10088 ▶sub max_delim_length {
Subroutine max_delim_length: called CC=2 SCAR=6.9
Pod undocumented
737 10088 my ($that, $delim) = @_;
738 # TODO: memo-ize this function
739 # We assume that the $delim is in the map because it reached this point and
740 # also that the map can contains only delimiters not repeated or repeated
741 # once.
742 10088 ▶ return exists $that->get_inline_delimiters()->{$delim x 2} ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $that->get_inline_delimiters->{$delim x 2} ? : 10083 5 10088
743 }
744  
745 58296 ▶sub create_extended_autolinks {
Subroutine create_extended_autolinks: called CC=9 SCAR=22.0
Pod undocumented
746 58296 my ($that, $n) = @_;
747 58296 ▶ if ($n->{type} ne 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"type"} ne "text") 18305 39991 58296
748 18305 return $n;
749 }
750  
751 39991 my @nodes;
752  
753 # TODO: technically we should forbid the presence of _ in the last two parts
754 # of the domain, according to the gfm spec.
755 ## no critic (ProhibitComplexRegexes)
756 39991 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"content"} =~ m[ (?<prefix> ^ | [ \t\n*_~\(] ) # The link must start after a whitespace or some specific delimiters. (?<url> (?: (?<scheme>https?://) | www\. ) # It must start by a scheme or the string wwww. [-_a-zA-Z0-9]+ (?: \. [-_a-zA-Z0-9]+ )* # Then there must be something that looks like a domain (?: / [^ \t\n<]*? )? # Some characters are forbidden in the link. ) [?!.,:*_~]* (?: [ \t\n<] | $) # We remove some punctuation from the end of the link. ]ux) 1165 39991 41156
757 $n->{content} =~ m/
758 (?<prefix> ^ | [ \t\n*_~\(] ) # The link must start after a whitespace or some specific delimiters.
759 (?<url>
760 (?: (?<scheme>https?:\/\/) | www\. ) # It must start by a scheme or the string wwww.
761 [-_a-zA-Z0-9]+ (?: \. [-_a-zA-Z0-9]+ )* # Then there must be something that looks like a domain
762 (?: \/ [^ \t\n<]*? )? # Some characters are forbidden in the link.
763 )
764 [?!.,:*_~]* (?: [ \t\n<] | $) # We remove some punctuation from the end of the link.
765 /x
766 ## use critic
767 ) {
768 1165 my $url = $+{url};
769 1165 my $match_start = $LAST_MATCH_START[0] + length($LAST_PAREN_MATCH{prefix});
770 1165 my $match_end = $match_start + length($url);
771 1165 my $has_scheme = exists $LAST_PAREN_MATCH{scheme};
772 1165 ▶ if ($url =~ m/\)+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\)+$/u) 6 1159 1165
773 6 my $nb_final_closing_parens = $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
774 6 my $open = 0;
775 6 () = $url =~ m/ \( (?{$open++}) | \) (?{$open--}) /gx;
776 6 my $remove = min($nb_final_closing_parens, -$open);
777 6 ▶ if ($remove > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove > 0) 3 3 6
778 3 $match_end -= $remove;
779 3 substr $url, -$remove, $remove, '';
780 }
781 }
782 1165 ▶ if ($url =~ m/\&[a-zA-Z0-9]+;$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\&[a-zA-Z0-9]+;$/u) 3 1162 1165
783 3 my $len = $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
784 3 $match_end -= $len;
785 3 substr $url, -$len, $len, '';
786 }
787 1165 ▶ if ($match_start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_start > 0) 718 447 1165
788 718 push @nodes, new_text(substr $n->{content}, 0, $match_start);
789 }
790 1165 ▶ my $scheme = $has_scheme ? '' : $that->get_default_extended_autolinks_scheme.'://';
Branch: 1 branch
BranchTrueFalseTotal
$has_scheme ? : 623 542 1165
791 1165 push @nodes,
792 new_link($url, type => 'autolink', target => $scheme.$url, debug => 'extended autolink');
793 1165 $n = new_text(substr $n->{content}, $match_end);
794 }
795 39991 ▶ push @nodes, $n if length($n->{content}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $n->{"content"} > 0 38842 1149 39991
796 39991 return @nodes;
797 }
798  
799 59030 ▶sub create_extended_email_autolinks {
Subroutine create_extended_email_autolinks: called CC=6 SCAR=17.9
Pod undocumented
800 59030 my ($that, $n) = @_;
801 59030 ▶ if ($n->{type} ne 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"type"} ne "text") 19470 39560 59030
802 19470 return $n;
803 }
804  
805 39560 my @nodes;
806  
807 # TODO: We’re not handling links with prefix protocol (mailto: or xmpp:) but
808 # these are not tested by the spec present in the current repo (although they
809 # are documented online).
810 ## no critic (ProhibitComplexRegexes)
811 39560 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"content"} =~ m[ (?<prefix> ^ | [ \t\n*_~\(] ) # The link must start after a whitespace or some specific delimiters. (?<email> (?<scheme> mailto:// )? [-_.+a-zA-Z0-9]+ @ [-_a-zA-Z0-9]+ (?: \. [-_a-zA-Z0-9]+ )+ (?<= [a-zA-Z0-9] ) ) (?: [ \t\n.<] | $ ) # We remove some punctuation from the end of the link. ]ux) 5 39560 39565
812 $n->{content} =~ m/
813 (?<prefix> ^ | [ \t\n*_~\(] ) # The link must start after a whitespace or some specific delimiters.
814 (?<email>
815 (?<scheme> mailto:\/\/ )?
816 [-_.+a-zA-Z0-9]+ @ [-_a-zA-Z0-9]+ (?: \. [-_a-zA-Z0-9]+ )+ (?<= [a-zA-Z0-9] )
817 )
818 (?: [ \t\n.<] | $ ) # We remove some punctuation from the end of the link.
819 /x
820 ## use critic
821 ) {
822 5 my $email = $+{email};
823 5 my $match_start = $LAST_MATCH_START[0] + length($LAST_PAREN_MATCH{prefix});
824 5 my $match_end = $match_start + length($email);
825 5 my $has_scheme = exists $LAST_PAREN_MATCH{scheme};
826 5 ▶ if ($match_start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_start > 0) 1 4 5
827 1 push @nodes, new_text(substr $n->{content}, 0, $match_start);
828 }
829 5 ▶ my $scheme = $has_scheme ? '' : 'mailto:';
Branch: 1 branch
BranchTrueFalseTotal
$has_scheme ? : 0 5 5
830 5 push @nodes,
831 new_link(
832 $email,
833 type => 'autolink',
834 target => $scheme.$email,
835 debug => 'extended autolink');
836 5 $n = new_text(substr $n->{content}, $match_end);
837 }
838 39560 ▶ push @nodes, $n if length($n->{content}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $n->{"content"} > 0 39557 3 39560
839 39560 return @nodes;
840 }
841  
842 31 1;
« Markdown/Perl/InlineTree.pm ↑ Summary Markdown/Perl/Options.pm »