Markdown/Perl/Inlines.pm

filter: Statementstmt 99.7% 415 / 416 Branchbran 97.6% 209 / 214 Conditioncond 98.8% 83 / 84 MC/DCmcdc 97.7% 43 / 44 Subroutinesub 100% 37 / 37 Podpod 0.0% 0 / 20 totaltotal 96.5% 787 / 815 CC 139 SCAR 24.0
CC
139
Cov
99%
parse_inline_link
24.0
find_link_destination_and_title
18.0
process_whitespaces
13.0
CRAP 11.1
« 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 sub render {
24 44011 ▶ my ($that, $linkrefs, @lines) = @_;
Subroutine render: called CC=2 SCAR=6.9
Pod undocumented
25  
26 44011 my $text = join("\n", @lines);
27 44011 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 44011 ▶ $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 44011 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 44011 process_whitespaces($that, $tree);
42  
43 # Now, there are more link elements and they can have children instead of
44 # content.
45  
46 44011 ▶ if ($that->get_use_extended_autolinks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that->get_use_extended_autolinks) 24054 19957 44011
47 24054 ▶ $tree->map(sub { create_extended_autolinks($that, $_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
48 24054 ▶ $tree->map(sub { create_extended_email_autolinks($that, $_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
49 }
50  
51 44011 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 sub {
57 179473 ▶ $_->escape_content($that->get_html_escaped_characters,
Subroutine __ANON__: called CC=1 SCAR=0.0
58 $that->get_html_escaped_code_characters);
59 44011 });
60  
61 44011 my $out = $tree->render_html();
62  
63 44011 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 sub find_code_and_tag_runs {
91 44011 ▶ my ($that, $text) = @_;
Subroutine find_code_and_tag_runs: called CC=12 SCAR=24.9
Pod undocumented
92  
93 44011 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 44011 ▶ while ($text =~ m/(?<! \\) (?<backslashes> (?:\\\\)*) (?: (?<code>\`+) | < )/gx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(?<! \\) (?<backslashes> (?:\\\\)*) (?: (?<code>\`+) | < )/gux) 49633 44011 93644
105 my ($start_before, $start_after) =
106 49633 ($LAST_MATCH_START[0] + length($+{backslashes}), $LAST_MATCH_END[0]);
107 49633 ▶ if ($+{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'code'}) { } 18479 31154 49633
108 18479 my $fence = $+{code};
109 # We’re searching for a fence of the same length, without any backtick
110 # before or after.
111 18479 ▶ if ($text =~ m/(?<!\`)${fence}(?!\`)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(?<!\`)$fence(?!\`)/cgu) 3779 14700 18479
112 3779 my ($end_before, $end_after) = ($LAST_MATCH_START[0], $LAST_MATCH_END[0]);
113 3779 ▶ $tree->push(new_text(substr($text, 0, $start_before)))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 3136 643 3779
114 if $start_before > 0;
115 3779 $tree->push(new_code(substr($text, $start_after, ($end_before - $start_after))));
116 3779 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 31154 my $re = $that->get_autolinks_regex;
121 31154 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 31154 ▶ if ($text =~ m/\G(?<link>${re})>/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($text =~ /\G(?<link>$re)>/u) { } 8537 22617 31154
elsif ($text =~ /\G(?<link>$email_re)>/u) { } 4 22613 22617
elsif ($text =~ /\G(?:$html_tag_re)>/u) { } 14411 8202 22613
126 8537 ▶ $tree->push(new_text(substr($text, 0, $start_before)))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 7384 1153 8537
127 if $start_before > 0;
128 8537 $tree->push(new_link($+{link}, type => 'autolink', target => $+{link}));
129 8537 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 14411 ▶ $tree->push(new_text(substr($text, 0, $start_before, '')))
Branch: 1 branch
BranchTrueFalseTotal
if $start_before > 0 13628 783 14411
140 if $start_before > 0;
141 14411 my $html = substr($text, 0, $LAST_MATCH_END[0] - $start_before, '');
142 14411 remove_disallowed_tags($html, $that->get_disallowed_html_tags);
143 14411 $tree->push(new_html($html));
144 }
145 }
146 }
147 44011 ▶ $tree->push(new_text($text)) if $text;
Branch: 1 branch
BranchTrueFalseTotal
if $text 41586 2425 44011
148  
149 44011 return $tree;
150 }
151  
152 sub process_char_escaping {
153 92465 ▶ my ($that, $node) = @_;
Subroutine process_char_escaping: called CC=8 SCAR=20.8
Pod undocumented
154  
155 # This is executed after
156 92465 ▶ if ($node->{type} eq 'code' || $node->{type} eq 'link') {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->{'type'} eq 'code' or $node->{'type'} eq 'link') { } 12320 80145 92465
elsif ($node->{'type'} eq 'text') { } 65734 14411 80145
elsif ($node->{'type'} eq 'html') { } 14411 0 14411
Condition100%3/3
$node->{"type"} eq "code" or $node->{"type"} eq "link"
l!l&&r!l&&!r
3779854180145
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 12320 return $node;
160 } elsif ($node->{type} eq 'text') {
161 # TODO: with the current code for map, this could just be @nodes.
162 65734 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 65734 ▶ while ($node->{content} =~ m/\\(\p{PosixPunct})/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"content"} =~ /\\(\p{PosixPunct})/gu) 157 65734 65891
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 65734 ▶ $new_tree->push($node) if $node->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"content"} 65727 7 65734
178 65734 return $new_tree;
179 } elsif ($node->{type} eq 'html') {
180 14411 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 sub process_links {
195 44011 ▶ my ($that, $linkrefs, $tree) = @_;
Subroutine process_links: called CC=12 SCAR=24.8
Pod undocumented
196  
197 44011 my @open_link;
198 44011 ▶ for (my $i = 0; $i < @{$tree->{children}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$tree->{"children"};}) 94408 44011 138419
199 94408 my $n = $tree->{children}[$i];
200 94408 ▶ next if $n->{type} ne 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $n->{"type"} ne "text" 26860 67548 94408
201 67548 ▶ while ($n->{content} =~ m/(?<open>!?\[)|\]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"content"} =~ /(?<open>!?\[)|\]/gu) 89396 65321 154717
202 89396 my @pos = ($i, $LAST_MATCH_START[0], $LAST_MATCH_END[0]);
203 89396 ▶ if ($+{open}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'open'}) { } 40797 48599 89396
204 40797 ▶ my $type = $pos[2] - $pos[1] > 1 ? 'img' : 'link';
Branch: 1 branch
BranchTrueFalseTotal
$pos[2] - $pos[1] > 1 ? : 8434 32363 40797
205 40797 push @open_link, {type => $type, active => 1, pos => \@pos};
206 } else {
207 48599 ▶ next unless @open_link;
Branch: 1 branch
BranchTrueFalseTotal
unless @open_link 19538 29061 48599
208 29061 my %open = %{pop @open_link};
209 29061 ▶ next unless $open{active};
Branch: 1 branch
BranchTrueFalseTotal
unless $open{"active"} 46 29015 29061
210 29015 my @text_span = ($open{pos}[0], $open{pos}[2], $pos[0], $pos[1]);
211 29015 my $cur_pos = pos($n->{content});
212 29015 my %target =
213 find_link_destination_and_title($that, $linkrefs, $tree, $pos[0], $pos[2], @text_span);
214 29015 pos($n->{content}) = $cur_pos;
215 29015 ▶ next unless %target;
Branch: 1 branch
BranchTrueFalseTotal
unless %target 26788 2227 29015
216 2227 my $text_tree = $tree->extract(@text_span);
217 my (undef, $dest_node_index) =
218 2227 $tree->extract($open{pos}[0], $open{pos}[1], $open{pos}[0] + 1, 1);
219 2227 my $link = new_link($text_tree, type => $open{type}, %target);
220 2227 $tree->insert($dest_node_index, $link);
221  
222 2227 ▶ if ($open{type} eq 'link') {
Branch: 1 branch
BranchTrueFalseTotal
if ($open{"type"} eq "link") 1260 967 2227
223 1260 for (@open_link) {
224 275 ▶ $_->{active} = 0 if $_->{type} eq 'link';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"type"} eq "link" 156 119 275
225 }
226 }
227 2227 $i = $dest_node_index;
228 2227 last; # same as a next OUTER, but without the need to define the OUTER label.
229 }
230 }
231 }
232 44011 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 sub find_link_destination_and_title {
238 29015 ▶ my ($that, $linkrefs, $tree, $child_start, $text_start, @text_span) = @_;
Subroutine find_link_destination_and_title: called CC=18 SCAR=28.9
Pod undocumented
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 29015 my $cur_child = $child_start;
244 29015 my $n = $tree->{children}[$cur_child];
245 confess 'Unexpected link destination search in a non-text element: '.$n->{type}
246 29015 ▶ unless $n->{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"type"} eq "text" 0 29015 29015
247 29015 pos($n->{content}) = $text_start;
248 29015 $n->{content} =~ m/ \G (?<space> [ \t\n]+ )? (?: (?<inline> \( ) | (?<reference> \[\]? ) )? /x;
249 29015 my @start = ($child_start, $text_start, $child_start, $LAST_MATCH_END[0]);
250  
251 29015 my $has_space = exists $+{space};
252 29015 my $type;
253 29015 ▶ if (exists $+{inline}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $+{'inline'}) { } 3833 25182 29015
elsif (exists $+{'reference'}) { } 940 24242 25182
254 3833 $type = 'inline';
255 } elsif (exists $+{reference}) {
256 940 ▶ if ($+{reference} eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'reference'} eq '[') { } 606 334 940
257 606 $type = 'reference';
258 } else {
259 334 $type = 'collapsed';
260 }
261 } else {
262 24242 $type = 'shortcut';
263 }
264  
265 29015 my $mode = $that->get_allow_spaces_in_links;
266 29015 ▶ if ($has_space) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_space) 1898 27117 29015
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 1898 ▶ 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 1890 1898
Condition100%9/9
$mode eq "reference" and $type eq "reference" || $type eq "collapsed" and $+{"space"} =~ /^ ?(?:\n[ \t]*)?$/u
!ll&&!rl&&r
188198
$mode eq "reference" and $type eq "reference" || $type eq "collapsed"
!ll&&!rl&&r
124064117
$type eq "reference" or $type eq "collapsed"
l!l&&r!l&&!r
116641
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 1890 $type = 'shortcut';
277 }
278 }
279  
280 29015 ▶ if ($type eq 'inline') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'inline') { } 3784 25231 29015
elsif ($type eq 'reference') { } 572 24659 25231
281 3784 my @target = parse_inline_link($tree, @start);
282 3784 ▶ return @target if @target;
Branch: 1 branch
BranchTrueFalseTotal
if @target 2061 1723 3784
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 572 my %target = parse_reference_link($that, $linkrefs, $tree, @start);
287 572 ▶ return %target if exists $target{target};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target{"target"} 45 527 572
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 527 ▶ return if %target;
Branch: 1 branch
BranchTrueFalseTotal
if %target 389 138 527
292 # Otherwise, pass-through.
293 }
294 # This is either a collapsed or a shortcut reference link (or something that
295 # might be one).
296 26520 my $ref = $tree->span_to_source_text(@text_span, UNESCAPE_LITERAL);
297 26520 ▶ $ref = normalize_label($ref) if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 18356 8164 26520
298 26520 ▶ if (my $l = get_linkref($that, $linkrefs, $ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = get_linkref($that, $linkrefs, $ref)) 121 26399 26520
299 121 ▶ $tree->extract(@start) if $type eq 'collapsed';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "collapsed" 18 103 121
300 121 return %{$l};
301 }
302 26399 return;
303 }
304  
305 sub parse_inline_link {
306 3784 ▶ my ($tree, @start) = @_; # ($child_start, $text_start, $child_start, $text_start + 1);
Subroutine parse_inline_link: called CC=24 SCAR=31.8
Pod undocumented
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 3784 my $cur_child = $start[0];
314 3784 my $n = $tree->{children}[$cur_child];
315  
316 3784 pos($n->{content}) = $start[3];
317 3784 $n->{content} =~ m/\G[ \t]*\n?[ \t]*/;
318 3784 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 3784 my @target;
324 3784 my $ok_to_have_title = 1;
325  
326 3784 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 3784 my $target = '';
332  
333 3784 ▶ if ($has_bracket) {
Branch: 3 branches
BranchTrueFalseTotal
if ($has_bracket) { } 40 3744 3784
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') { } 94 3650 3744
elsif (my(@end_target) = $tree->find_in_text_with_balanced_content(qr/\(/u, qr/\)/u, qr/[ [:cntrl:]]/u, $cur_child, $search_start)) { } 3067 583 3650
334 40 ▶ 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 15 40
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) 2 23 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
36361494
length $n->{"content"} <= $search_start and @{$tree->{"children"};} > $cur_child + 1
!ll&&!rl&&r
3490146108
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 254 ▶ && @{$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
464814
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 94 ▶ return if @{$tree->{children}} <= $cur_child + 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$tree->{"children"};} <= $cur_child + 2 13 81 94
349 81 @target = ($cur_child + 1, 0, $cur_child + 2, 0);
350 81 my $link_node = $tree->{children}[$cur_child + 1];
351 81 ▶ if ($link_node->{type} eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_node->{'type'} eq 'html') { } 41 40 81
352 41 $target = $link_node->{content};
353 41 $target =~ s/^<|>$//g;
354 } else {
355 40 $target = $link_node->{target};
356 }
357 81 ▶ return if $target =~ m/\n/; # No new lines in link targets are allowed.
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ /\n/u 2 79 81
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 3067 @target = ($cur_child, $search_start, $end_target[0], $end_target[1]);
364 }
365 3767 ▶ if (@target) {
Branch: 1 branch
BranchTrueFalseTotal
if (@target) 3169 598 3767
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 3169 $cur_child = $target[2];
369 3169 $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 3169 ▶ pos($n->{content}) = $target[3] + ($has_bracket ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$has_bracket ? : 23 3146 3169
374 3169 $n->{content} =~ m/\G[ \t]*\n?[ \t]*/;
375 3169 $search_start = $LAST_MATCH_END[0];
376 3169 $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 3767 pos($n->{content}) = $search_start;
383 3767 my @end_title;
384 3767 ▶ if ($n->{content} =~ m/\G"/gc) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n->{'content'} =~ /\G"/cgu) { } 48 3719 3767
elsif ($n->{'content'} =~ /\G'/cgu) { } 4 3715 3719
elsif ($n->{'content'} =~ /\G\(/cgu) { } 48 3667 3715
385 48 @end_title = $tree->find_in_text(qr/"/, $cur_child, $search_start + 1);
386 } elsif ($n->{content} =~ m/\G'/gc) {
387 4 @end_title = $tree->find_in_text(qr/'/, $cur_child, $search_start + 1);
388 } elsif ($n->{content} =~ m/\G\(/gc) {
389 48 @end_title = $tree->find_balanced_in_text(qr/\(/, qr/\)/, $cur_child, $search_start + 1);
390 }
391 3767 my @title;
392 3767 ▶ if (@end_title) {
Branch: 1 branch
BranchTrueFalseTotal
if (@end_title) 46 3721 3767
393 46 ▶ return unless $ok_to_have_title;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok_to_have_title 2 44 46
394 44 @title = ($cur_child, $search_start + 1, $end_title[0], $end_title[1]);
395 44 $cur_child = $end_title[0];
396 44 $n = $tree->{children}[$cur_child];
397 44 pos($n->{content}) = $end_title[2]; # This time, we look after the closing character.
398 44 $n->{content} =~ m/\G[ \t]*\n?[ \t]*/;
399 44 $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 3765 pos($n->{content}) = $search_start;
406 3765 ▶ return unless $n->{content} =~ m/\G\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"content"} =~ /\G\)/u 1704 2061 3765
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 2061 my @last_item = (@title, @target, @start);
412 # We remove the spaces after the last item and also the closing paren.
413 2061 $tree->extract($last_item[2], $last_item[3], $cur_child, $search_start + 1);
414 }
415  
416 2061 my $title;
417 2061 ▶ if (@title) {
Branch: 1 branch
BranchTrueFalseTotal
if (@title) 24 2037 2061
418 24 my $title_tree = $tree->extract(@title);
419 24 $title = $title_tree->to_source_text();
420 24 my @last_item = (@target, @start);
421 24 $tree->extract($last_item[2], $last_item[3], $title[0], $title[1]);
422 }
423  
424 2061 ▶ if (@target) {
Branch: 1 branch
BranchTrueFalseTotal
if (@target) 2061 0 2061
425 2061 my $target_tree = $tree->extract(@target);
426 2061 ▶ $target = $target_tree->to_source_text() unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 2059 2 2061
427 2061 $tree->extract($start[2], $start[3], $target[0], $target[1]);
428 }
429  
430 2061 $tree->extract(@start);
431  
432 2061 ▶ return (target => $target, ($title ? (title => $title) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$title ? : 24 2037 2061
433 }
434  
435 sub parse_reference_link {
436 572 ▶ my ($that, $linkrefs, $tree, @start) = @_; # ($child_start, $text_start, $child_start, $text_start + 1);
Subroutine parse_reference_link: called CC=3 SCAR=11.0
Pod undocumented
437  
438 572 my $cur_child = $start[0];
439 572 my $n = $tree->{children}[$cur_child];
440  
441 572 my $ref_start = $start[3];
442  
443 572 ▶ 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])) 434 138 572
444 434 my $ref =
445 normalize_label($tree->span_to_source_text(@start[2, 3], @end_ref[0, 1], UNESCAPE_LITERAL));
446 434 ▶ if (my $l = get_linkref($that, $linkrefs, $ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = get_linkref($that, $linkrefs, $ref)) { } 45 389 434
447 45 $tree->extract(@start[0, 1], @end_ref[0, 2]);
448 45 return %{$l};
449 } else {
450 # TODO: we should only return this if the span was indeed a valid
451 # reference link label (not longer than 1000 characters mostly).
452 # This is used to notice that we had a proper reference link syntax and
453 # not fallback to trying a shortcut link.
454 389 return (ignored_valid_value => 1);
455 }
456 }
457 138 return;
458 }
459  
460 # Returns a hashref with (title and dest) or undef.
461 sub get_linkref {
462 26954 ▶ my ($that, $linkrefs, $ref) = @_;
Subroutine get_linkref: called CC=3 SCAR=11.0
Pod undocumented
463 26954 ▶ if (exists $linkrefs->{$ref}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $linkrefs->{$ref}) { } 160 26794 26954
elsif (exists $that->{'hooks'}{'resolve_link_ref'}) { } 7 26787 26794
464 160 return $linkrefs->{$ref};
465 } elsif (exists $that->{hooks}{resolve_link_ref}) {
466 7 return $that->{hooks}{resolve_link_ref}->($ref);
467 }
468 26787 return;
469 }
470  
471 # This methods remove line break at the beginning and end of lines (inside text
472 # nodes only), and add hard line breaks as required.
473 #
474 # $not_root is set when we recurse inside sub-tree, to indicate that the first
475 # and last node of the the tree are not, in fact, the beginning and and of the
476 # paragraph.
477 sub process_whitespaces {
478 46238 ▶ my ($that, $tree, $not_root) = @_;
Subroutine process_whitespaces: called CC=13 SCAR=25.6
Pod undocumented
479  
480 46238 ▶ for (my $i = 0; $i < @{$tree->{children}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$tree->{"children"};}) 97857 46238 144095
481 97857 my $n = $tree->{children}[$i];
482 97857 ▶ process_whitespaces($that, $n->{subtree}, 1) if exists $n->{subtree};
Branch: 1 branch
BranchTrueFalseTotal
if exists $n->{"subtree"} 2227 95630 97857
483 97857 ▶ next unless $n->{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"type"} eq "text" 29087 68770 97857
484 # TODO: add tests for the fact that we don’t want hard break at the end of a
485 # paragraph.
486 68770 my $re;
487 68770 ▶ if ($that->get_two_spaces_hard_line_breaks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that->get_two_spaces_hard_line_breaks) { } 48276 20494 68770
488 48276 $re = qr/(?: {2,}|\\)\n(?=.) */s;
489 } else {
490 20494 $re = qr/\\\n(?=.) */s;
491 }
492 68770 my @hard_breaks = split($re, $n->{content}, -1);
493 68770 ▶ for (my $j = 0; $j < @hard_breaks; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @hard_breaks) 79140 68770 147910
494 # $hard_breaks[$j] = '' unless defined($hard_breaks[$j]);
495 79140 ▶ $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 37219 79140
Condition100%6/6
not $not_root and $i == 0 and $j == 0
!ll&&!rl&&r
31629559041921
not $not_root and $i == 0
!ll&&!rl&&r
24882914147511
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
496 79140 $hard_breaks[$j] =~ s/(\n|\r) +/$1/g;
497 $hard_breaks[$j] =~ s/ +$//gm
498 79140 ▶ 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 41216 37924 79140
Condition100%6/6
not $not_root and $i == $#{$$tree{"children"};} and $j == $#hard_breaks
!ll&&!rl&&r
31808611641216
not $not_root and $i == $#{$$tree{"children"};}
!ll&&!rl&&r
24882932047332
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
499 79140 ▶ if ($j == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j == 0) { } 68770 10370 79140
500 68770 $n->{content} = $hard_breaks[0];
501 } else {
502 10370 $tree->insert($i + 1, new_html('<br />'), new_text("\n".$hard_breaks[$j]));
503 10370 $i += 2;
504 }
505 }
506 }
507 46238 return;
508 }
509  
510 # This methods adds "style", that is it parses the emphasis (* and _) and also
511 # strike-through (~). To do so, we process each level of the tree independently
512 # because a style-run can’t cross another HTML construct (but it can span over
513 # it).
514 #
515 # We first find all the possible delimiters and insert them in the tree instead
516 # of their text. And then decide whether they are actually opening, closing, or
517 # neither.
518 #
519 # This methods implement all of:
520 # https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis
521 sub process_styles {
522 46238 ▶ my ($that, $tree) = @_;
Subroutine process_styles: called CC=6 SCAR=17.9
Pod undocumented
523  
524 # We recurse first as there are less children to iterate over than after.
525 46238 for my $c (@{$tree->{children}}) {
526 119229 ▶ process_styles($that, $c->{subtree}) if exists $c->{subtree};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{"subtree"} 2227 117002 119229
527 }
528  
529 # TODO: only search for characters that are actually used by our current
530 # options.
531 46238 my $current_child = 0;
532 46238 my @delimiters;
533 46238 my $delim = delim_characters($that);
534 46238 my %max_delim_run_length = %{$that->get_inline_delimiters_max_run_length};
535 46238 ▶ 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)) 40447 46238 86685
536 # We extract the delimiter run into a new node, that will be at $index.
537 40447 my ($delim_tree, $index) = $tree->extract($match[0], $match[1], $match[0], $match[2]);
538 # We use the type literal so that if we do nothing with the delimiter it
539 # will be rendered correctly. We keep track of which literals might be
540 # delimiters using the @delimiters array.
541 40447 $delim_tree->{children}[0]{type} = 'literal';
542 40447 $tree->insert($index, $delim_tree);
543 40447 my $d = classify_delimiter($that, $tree, $index);
544 40447 ▶ 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"}}) 40445 2 40447
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
4043962
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
545 || $d->{len} <= $max_delim_run_length{$d->{delim}}) {
546 40445 push @delimiters, $d;
547 }
548 40447 $current_child = $index + 1;
549 }
550  
551 46238 match_delimiters($that, $tree, @delimiters);
552 46238 return;
553 }
554  
555 # Decides whether the delimiter run at the given index in the tree can open or
556 # close emphasis (or any other style).
557 sub classify_delimiter {
558 40447 ▶ my ($that, $tree, $index) = @_;
Subroutine classify_delimiter: called CC=10 SCAR=23.0
Pod undocumented
559 40447 my $pred_type = classify_flank($that, $tree, $index, 'left');
560 40447 my $succ_type = classify_flank($that, $tree, $index, 'right');
561 40447 ▶ 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
6052764126754
$succ_type ne "punct" or $pred_type ne "none"
l!l&&r!l&&!r
11469152857641
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
562 40447 ▶ 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
8089662225736
$pred_type ne "punct" or $succ_type ne "none"
l!l&&r!l&&!r
11855138816622
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
563 40447 my $len = length($tree->{children}[$index]{content});
564 40447 my $delim = substr $tree->{children}[$index]{content}, 0, 1;
565 40447 my $can_open = 0;
566 40447 my $can_close = 0;
567 # This is implementing the first 8 rules (out of 17...) of
568 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis
569 # The rules are more complex for '_' than for '*' because it is assuming that
570 # underscores can appear within word. So we apply the star rules to all other
571 # delimiters (that is, we only check for underscore here). Currently our only
572 # other delimiter is '~'.
573 # TODO: add an option to decide which rule to apply per delimiter.
574 40447 ▶ if ($delim eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim eq '_') { } 8583 31864 40447
575 8583 ▶ $can_open = $is_left && (!$is_right || $pred_type eq 'punct');
Condition100%6/6
$is_left && (!$is_right || $pred_type eq 'punct')
!ll&&!rl&&r
22361116236
not $is_right or $pred_type eq "punct"
l!l&&r!l&&!r
23363900111
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
576 8583 ▶ $can_close = $is_right && (!$is_left || $succ_type eq 'punct');
Condition100%6/6
$is_right && (!$is_left || $succ_type eq 'punct')
!ll&&!rl&&r
26941115778
not $is_left or $succ_type eq "punct"
l!l&&r!l&&!r
18783900111
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
577 } else {
578 31864 $can_open = $is_left;
579 31864 $can_close = $is_right;
580 }
581 return {
582 40447 index => $index,
583 can_open => $can_open,
584 can_close => $can_close,
585 len => $len,
586 delim => $delim,
587 orig_len => $len
588 };
589 }
590  
591 # Computes whether the type of the "flank" of the delimiter run at the given
592 # index in the tree (looking either at the "left" or "right" side). This returns
593 # one of 'none', 'punct', or 'space' following the rule given in
594 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis.
595 # The purpose is to decide whether the delimiter run is left flanking and/or
596 # right flanking (that decision happens in classify_delimiter).
597 sub classify_flank {
598 80894 ▶ my ($that, $tree, $index, $side) = @_;
Subroutine classify_flank: called CC=12 SCAR=24.8
Pod undocumented
599 80894 ▶ return 'space' if $index == 0 && $side eq 'left';
Branch: 1 branch
BranchTrueFalseTotal
if $index == 0 and $side eq "left" 3330 77564 80894
Condition100%3/3
$index == 0 and $side eq "left"
!ll&&!rl&&r
7423433303330
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
600 77564 ▶ return 'space' if $index == $#{$tree->{children}} && $side eq 'right';
Branch: 1 branch
BranchTrueFalseTotal
if $index == $#{$$tree{"children"};} and $side eq "right" 3060 74504 77564
Condition100%3/3
$index == $#{$$tree{"children"};} and $side eq "right"
!ll&&!rl&&r
7160828963060
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
601 74504 ▶ my $node = $tree->{children}[$index + ($side eq 'left' ? -1 : 1)];
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'left' ? : 37117 37387 74504
602 # If the node before the delimiters is not text, let’s assume that we had some
603 # punctuation characters that delimited it.
604 74504 ▶ 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" 4108 70396 74504
Condition100%3/3
$node->{"type"} ne "text" and $node->{"type"} ne "literal"
!ll&&!rl&&r
696996974108
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
605 70396 ▶ my $space_re = $side eq 'left' ? qr/\s$/u : qr/^\s/u;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'left' ? : 35436 34960 70396
606 70396 ▶ return 'space' if $node->{content} =~ m/${space_re}/;
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"content"} =~ /$space_re/u 7751 62645 70396
607 62645 ▶ 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' ? : 30677 31968 62645
608 62645 ▶ return 'punct' if $node->{content} =~ m/${punct_re}/;
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"content"} =~ /$punct_re/u 39321 23324 62645
609 23324 return 'none';
610 }
611  
612 # We match the pair of delimiters together as much as we can, following the
613 # rules of the CommonMark spec.
614 sub match_delimiters {
615 46238 ▶ my ($that, $tree, @delimiters) = @_;
Subroutine match_delimiters: called CC=4 SCAR=13.9
Pod undocumented
616  
617 46238 ▶ for (my $close_index = 1; $close_index < @delimiters; $close_index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_index < @delimiters) 20676 46238 66914
618 20676 my %c = %{$delimiters[$close_index]};
619 20676 ▶ next if !$c{can_close};
Branch: 1 branch
BranchTrueFalseTotal
unless $c{"can_close"} 5020 15656 20676
620 # We have a closing delimiter, now we backtrack and find the tighter match
621 # for this closing delimiter. This is because "*foo _bar* baz_" will only
622 # match the * (that comes first) but "*foo *bar*"" will match the second
623 # and third star, that are the tightest match. This is for rule 15 and 16 of
624 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis
625 # We also apply rules 9 and 10 here. Rules 1-8 have already been computed in
626 # classify_delimiter.
627 my $open_index =
628 15333 ▶ 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) 11517 3816 15333
Condition100%3/3
$_->{"can_open"} and $_->{"delim"} eq $c{"delim"}
!ll&&!rl&&r
1296252011517
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
629 15656 @delimiters[0 .. $close_index - 1];
630 # TODO: here there are a lot of optimization that we could apply, based on
631 # the "process emphasis" method from the spec (like removing our closing
632 # delimiter if it is not an opener, and keeping track of the fact that
633 # we have no delimiter in the 0..close_index-1 range that can match a
634 # delimiter of the same type as %c).
635 # This does not seem very important for reasonable inputs. So, instead, we
636 # just check the next potential closer.
637 15656 ▶ next if $open_index == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $open_index == -1 5450 10206 15656
638  
639 10206 $close_index = apply_delimiters($that, $tree, \@delimiters, $open_index, $close_index);
640 }
641  
642 46238 return;
643 }
644  
645 # Given a tree, its delimiters and the index of two delimiters, rewrite the
646 # tree with the style applied by these delimiters (we’re assuming here that they
647 # are of a matching type).
648 #
649 # The delimiter may not be consumed entirely (but we will consume as much as
650 # possible).
651 sub apply_delimiters {
652 10206 ▶ my ($that, $tree, $delimiters, $open_index, $close_index) = @_;
Subroutine apply_delimiters: called CC=5 SCAR=16.1
Pod undocumented
653 10206 my %o = %{$delimiters->[$open_index]};
654 10206 my %c = %{$delimiters->[$close_index]};
655  
656 # We rewrite the tree in between our two delimiters.
657 # TODO: maybe we need a splice method in InlineTree.
658 10206 my @styled_subnodes = splice @{$tree->{children}}, $o{index} + 1, $c{index} - $o{index} - 1;
659 10206 my $styled_tree = Markdown::Perl::InlineTree->new();
660 10206 $styled_tree->push(@styled_subnodes);
661 # With our current algorithm in match_delimiters we know that there is no
662 # reasons to recurse (because the closing delimiter here was the first
663 # closing delimiter with a matching opener.)
664 # my @styled_delimiters = map { $_->{index} -= $o{index} + 1; $_ } splice @{$delimiters},
665 # $open_index + 1, $close_index - $open_index - 1;
666 # match_delimiters($that, $styled_tree, @styled_delimiters);
667 10206 splice @{$delimiters}, $open_index + 1, $close_index - $open_index - 1;
668  
669 # And now we rebuild our own tree around the new one.
670 10206 my $len = min($o{len}, $c{len}, max_delim_length($that, $o{delim}));
671 10206 my $styled_node = new_style($styled_tree, tag => delim_to_html_tag($that, $o{delim} x $len));
672 10206 my $style_start = $o{index};
673 10206 my $style_length = 2;
674 10206 $close_index = $open_index + 1;
675 10206 ▶ if ($len < $o{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $o{'len'}) { } 381 9825 10206
676 381 substr($tree->{children}[$o{index}]{content}, $o{len} - $len) = ''; ## no critic (ProhibitLvalueSubstr)
677 381 $delimiters->[$open_index]{len} -= $len;
678 381 $style_start++;
679 381 $style_length--;
680 } else {
681 9825 splice @{$delimiters}, $open_index, 1;
682 9825 $close_index--;
683 }
684 10206 ▶ if ($len < $c{len}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $c{'len'}) { } 370 9836 10206
685 # The closing node is now just after the opening one.
686 370 substr($tree->{children}[$o{index} + 1]{content}, $c{len} - $len) = ''; ## no critic (ProhibitLvalueSubstr)
687 370 $delimiters->[$close_index]{len} -= $len;
688 370 $style_length--;
689 } else {
690 9836 splice @{$delimiters}, $close_index, 1; # We remove our closing delimiter.
691 }
692 10206 splice @{$tree->{children}}, $style_start, $style_length, $styled_node;
693 10206 for my $i ($close_index .. $#{$delimiters}) {
694 7606 $delimiters->[$i]{index} -= $c{index} - $o{index} - 2 + $style_length;
695 }
696 10206 ▶ return $open_index - ($len < $o{len} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$len < $o{'len'} ? : 381 9825 10206
697 }
698  
699 # Returns true if the given delimiters can be an open/close pair without
700 # breaking rules 9 and 10 of
701 # https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis.
702 sub valid_rules_9_10 {
703 11517 ▶ my ($o, $c) = @_;
Subroutine valid_rules_9_10: called CC=5 SCAR=16.1
Pod undocumented
704 # TODO: BUG: there is a probable bug here in that the length of the delimiter
705 # to consider is not its current length but the length of the original span
706 # of which it was a part.
707 return
708 (!$o->{can_close} && !$c->{can_open})
709 || (($o->{orig_len} + $c->{orig_len}) % 3 != 0)
710 11517 ▶ || ($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
10194121311
not $o->{"can_close"} || $c->{"can_open"} or ($o->{"orig_len"} + $c->{"orig_len"}) % 3 != 0
l!l&&r!l&&!r
711530791323
$o->{'can_close'} || $c->{'can_open'}
l!l&&r!l&&!r
277516277115
$o->{"orig_len"} % 3 == 0 and $c->{"orig_len"} % 3 == 0
!ll&&!rl&&r
1311012
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 table77%7/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
711 }
712  
713 # TODO: use ^ and ˇ to represent sup and sub
714 # TODO: add support for MathML in some way.
715 sub delim_to_html_tag {
716 10206 ▶ my ($that, $delim) = @_;
Subroutine delim_to_html_tag: called CC=1 SCAR=0.0
Pod undocumented
717 # TODO: sort what to do if a given delimiter does not have a variant with
718 # two characters (we must backtrack somewhere in match_delimiters probably).
719 # TODO: add support for <span class="foo"> when the value in the map is ".foo"
720 # instead of just "foo".
721 10206 return $that->get_inline_delimiters()->{$delim};
722 }
723  
724 # Return the list of characters that can be delimiters (using the regex
725 # character class syntax).
726 sub delim_characters {
727 46238 ▶ my ($that) = @_;
Subroutine delim_characters: called CC=1 SCAR=0.0
Pod undocumented
728 # TODO: memo-ize this function inside $that (but clear it when the options
729 # change).
730 46238 my @c = map { substr $_, 0, 1 } keys %{$that->get_inline_delimiters()};
731 46238 return join('', uniq @c);
732 }
733  
734 # Returns the max defined delim
735 sub max_delim_length {
736 10206 ▶ my ($that, $delim) = @_;
Subroutine max_delim_length: called CC=2 SCAR=6.9
Pod undocumented
737 # TODO: memo-ize this function
738 # We assume that the $delim is in the map because it reached this point and
739 # also that the map can contains only delimiters not repeated or repeated
740 # once.
741 10206 ▶ return exists $that->get_inline_delimiters()->{$delim x 2} ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $that->get_inline_delimiters->{$delim x 2} ? : 10201 5 10206
742 }
743  
744 sub create_extended_autolinks {
745 58883 ▶ my ($that, $n) = @_;
Subroutine create_extended_autolinks: called CC=9 SCAR=22.0
Pod undocumented
746 58883 ▶ if ($n->{type} ne 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"type"} ne "text") 18580 40303 58883
747 18580 return $n;
748 }
749  
750 40303 my @nodes;
751  
752 # TODO: technically we should forbid the presence of _ in the last two parts
753 # of the domain, according to the gfm spec.
754 ## no critic (ProhibitComplexRegexes)
755 40303 ▶ 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) 1182 40303 41485
756 $n->{content} =~ m/
757 (?<prefix> ^ | [ \t\n*_~\(] ) # The link must start after a whitespace or some specific delimiters.
758 (?<url>
759 (?: (?<scheme>https?:\/\/) | www\. ) # It must start by a scheme or the string wwww.
760 [-_a-zA-Z0-9]+ (?: \. [-_a-zA-Z0-9]+ )* # Then there must be something that looks like a domain
761 (?: \/ [^ \t\n<]*? )? # Some characters are forbidden in the link.
762 )
763 [?!.,:*_~]* (?: [ \t\n<] | $) # We remove some punctuation from the end of the link.
764 /x
765 ## use critic
766 ) {
767 1182 my $url = $+{url};
768 1182 my $match_start = $LAST_MATCH_START[0] + length($LAST_PAREN_MATCH{prefix});
769 1182 my $match_end = $match_start + length($url);
770 1182 my $has_scheme = exists $LAST_PAREN_MATCH{scheme};
771 1182 ▶ if ($url =~ m/\)+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\)+$/u) 10 1172 1182
772 10 my $nb_final_closing_parens = $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
773 10 my $open = 0;
774 10 () = $url =~ m/ \( (?{$open++}) | \) (?{$open--}) /gx;
775 10 my $remove = min($nb_final_closing_parens, -$open);
776 10 ▶ if ($remove > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove > 0) 4 6 10
777 4 $match_end -= $remove;
778 4 substr $url, -$remove, $remove, '';
779 }
780 }
781 1182 ▶ if ($url =~ m/\&[a-zA-Z0-9]+;$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\&[a-zA-Z0-9]+;$/u) 1 1181 1182
782 1 my $len = $LAST_MATCH_END[0] - $LAST_MATCH_START[0];
783 1 $match_end -= $len;
784 1 substr $url, -$len, $len, '';
785 }
786 1182 ▶ if ($match_start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_start > 0) 679 503 1182
787 679 push @nodes, new_text(substr $n->{content}, 0, $match_start);
788 }
789 1182 ▶ my $scheme = $has_scheme ? '' : $that->get_default_extended_autolinks_scheme.'://';
Branch: 1 branch
BranchTrueFalseTotal
$has_scheme ? : 605 577 1182
790 1182 push @nodes,
791 new_link($url, type => 'autolink', target => $scheme.$url, debug => 'extended autolink');
792 1182 $n = new_text(substr $n->{content}, $match_end);
793 }
794 40303 ▶ push @nodes, $n if length($n->{content}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $n->{"content"} > 0 39071 1232 40303
795 40303 return @nodes;
796 }
797  
798 sub create_extended_email_autolinks {
799 59512 ▶ my ($that, $n) = @_;
Subroutine create_extended_email_autolinks: called CC=6 SCAR=17.9
Pod undocumented
800 59512 ▶ if ($n->{type} ne 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{"type"} ne "text") 19762 39750 59512
801 19762 return $n;
802 }
803  
804 39750 my @nodes;
805  
806 # TODO: We’re not handling links with prefix protocol (mailto: or xmpp:) but
807 # these are not tested by the spec present in the current repo (although they
808 # are documented online).
809 ## no critic (ProhibitComplexRegexes)
810 39750 ▶ 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 39750 39755
811 $n->{content} =~ m/
812 (?<prefix> ^ | [ \t\n*_~\(] ) # The link must start after a whitespace or some specific delimiters.
813 (?<email>
814 (?<scheme> mailto:\/\/ )?
815 [-_.+a-zA-Z0-9]+ @ [-_a-zA-Z0-9]+ (?: \. [-_a-zA-Z0-9]+ )+ (?<= [a-zA-Z0-9] )
816 )
817 (?: [ \t\n.<] | $ ) # We remove some punctuation from the end of the link.
818 /x
819 ## use critic
820 ) {
821 5 my $email = $+{email};
822 5 my $match_start = $LAST_MATCH_START[0] + length($LAST_PAREN_MATCH{prefix});
823 5 my $match_end = $match_start + length($email);
824 5 my $has_scheme = exists $LAST_PAREN_MATCH{scheme};
825 5 ▶ if ($match_start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_start > 0) 1 4 5
826 1 push @nodes, new_text(substr $n->{content}, 0, $match_start);
827 }
828 5 ▶ my $scheme = $has_scheme ? '' : 'mailto:';
Branch: 1 branch
BranchTrueFalseTotal
$has_scheme ? : 0 5 5
829 5 push @nodes,
830 new_link(
831 $email,
832 type => 'autolink',
833 target => $scheme.$email,
834 debug => 'extended autolink');
835 5 $n = new_text(substr $n->{content}, $match_end);
836 }
837 39750 ▶ push @nodes, $n if length($n->{content}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $n->{"content"} > 0 39747 3 39750
838 39750 return @nodes;
839 }
840  
841 31 1;
« Markdown/Perl/InlineTree.pm ↑ Summary Markdown/Perl/Options.pm »