Markdown/Perl/InlineTree.pm

filter: Statementstmt 93.6% 296 / 316 Branchbran 82.9% 141 / 170 Conditioncond 83.8% 83 / 99 MC/DCmcdc 61.6% 37 / 60 Subroutinesub 97.8% 46 / 47 Podpod 82.1% 23 / 28 totaltotal 86.9% 626 / 720 CC 127 SCAR 24.8
CC
127
Cov
89%
new
23.5
extract
23.5
render_node_html
14.2
CRAP 11.9
« Markdown/Perl/BlockParser.pm ↑ Summary Markdown/Perl/Inlines.pm »
1 # A tree data structure to represent the content of an inline text of a block
2 # element.
3  
4 package Markdown::Perl::InlineTree;
5  
6 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use feature ':5.24';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use Hash::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 32 our $VERSION = 0.01;
18  
19 32 our @EXPORT_OK =
20 qw(new_text new_code new_link new_html new_style new_literal is_node is_tree UNESCAPE_LITERAL);
21 32 our %EXPORT_TAGS = (all => \@EXPORT_OK);
22  
23 =pod
24  
25 =encoding utf8
26  
27 =head1 NAME
28  
29 Markdown::Perl::InlineTree
30  
31 =head1 SYNOPSIS
32  
33 A tree structure meant to represent the inline elements of a Markdown paragraph.
34  
35 This package is internal to the implementation of L<Markdown::Perl> and its
36 documentation should be useful only if you are trying to modify the library.
37  
38 Otherwise please refer to the L<Markdown::Perl> and L<pmarkdown> documentation.
39  
40 =head1 DESCRIPTION
41  
42 =head2 new
43  
44 my $tree = Markdown::Perl::InlineTree->new();
45  
46 The constructor currently does not support any options.
47  
48 =cut
49  
50 sub new {
51 321472 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52  
53 321472 return bless {children => []}, $class;
54 }
55  
56 32 package Markdown::Perl::InlineNode { ## no critic (ProhibitMultiplePackages)
57 32 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 32 ▶ use Markdown::Perl::HTML 'decode_entities', 'html_escape', 'http_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 sub hashpush (\%%) {
61 298845 ▶ my ($hash, %args) = @_;
Subroutine hashpush: called CC=2 SCAR=6.9
62 298845 ▶ while (my ($k, $v) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 298845 298845 597690
63 298845 $hash->{$k} = $v;
64 }
65 298845 return;
66 }
67  
68 sub new {
69 264636 ▶ my ($class, $type, $content, %options) = @_;
Subroutine new: called CC=21 SCAR=31.6
70  
71 264636 my $this = {type => $type, escaped => 0};
72 264636 ▶ $this->{debug} = delete $options{debug} if exists $options{debug};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"debug"} 1187 263449 264636
73 264636 my $content_ref = ref $content;
74 264636 ▶ if (Scalar::Util::blessed($content)
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($content) and $content->isa('Markdown::Perl::InlineTree')) { } 12433 252203 264636
elsif (not ref $content) { } 252203 0 252203
Condition66%2/3
builtin::blessed($content) and $content->isa("Markdown::Perl::InlineTree")
!ll&&!rl&&r
252203012433
MC/DC50%1/2
builtin::blessed($content) and $content->isa("Markdown::Perl::InlineTree")
builtin::blessed($content) $content->isa("Markdown::Perl::InlineTree")
Truth table66%2/3
A and B
A= builtin::blessed($content)B= $content->isa("Markdown::Perl::InlineTree")
ABresult
0X0
100
111
75 && $content->isa('Markdown::Perl::InlineTree')) {
76 12433 hashpush %{$this}, subtree => $content;
77 } elsif (!ref($content)) {
78 252203 hashpush %{$this}, content => $content;
79 } else {
80 0 confess "Unexpected content for inline ${type} node: ".ref($content);
81 }
82 # There is one more node type, not created here, that looks like a text
83 # node but that is a 'delimiter' node. These nodes are created manually
84 # inside the Inlines module.
85 264636 ▶ if ($type eq 'text' || $type eq 'code' || $type eq 'literal' || $type eq 'html') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'text' or $type eq 'code' or $type eq 'literal' or $type eq 'html') { } 242475 22161 264636
elsif ($type eq 'link') { } 11955 10206 22161
elsif ($type eq 'style') { } 10206 0 10206
Condition100%9/9
$type eq "text" or $type eq "code" or $type eq "literal" or $type eq "html"
l!l&&r!l&&!r
2176942478122161
$type eq "text" or $type eq "code" or $type eq "literal"
l!l&&r!l&&!r
21753715746942
$type eq "text" or $type eq "code"
l!l&&r!l&&!r
213757378047099
MC/DC100%4/4
$type eq "text" or $type eq "code" or $type eq "literal" or $type eq "html"
$type eq "text" $type eq "code" $type eq "literal" $type eq "html"
Truth table100%5/5
A or B or C or D
A= $type eq "text"B= $type eq "code"C= $type eq "literal"D= $type eq "html"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
86 242475 ▶ confess "Unexpected content for inline ${type} node: ${content_ref}" if $content_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $content_ref 0 242475 242475
87 242475 ▶ confess "Unexpected parameters for inline ${type} node: ".join(', ', %options)
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 242475 242475
88 if %options;
89 } elsif ($type eq 'link') {
90 11955 ▶ confess 'Missing required option "type" for inline link node' unless exists $options{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"type"} 0 11955 11955
91 11955 hashpush %{$this}, linktype => delete $options{type};
92 confess 'Missing required option "target" for inline link node'
93 11955 ▶ unless exists $options{target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"target"} 0 11955 11955
94 11955 hashpush %{$this}, target => delete $options{target};
95 11955 ▶ hashpush %{$this}, title => delete $options{title} if exists $options{title};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"title"} 92 11863 11955
96 11955 ▶ hashpush %{$this}, content => delete $options{content} if exists $options{content};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"content"} 1 11954 11955
97 11955 ▶ confess 'Unexpected parameters for inline link node: '.join(', ', %options) if keys %options;
Branch: 1 branch
BranchTrueFalseTotal
if keys %options 0 11955 11955
98 } elsif ($type eq 'style') {
99 confess 'Unexpected parameters for inline style node: '.join(', ', %options)
100 10206 ▶ if keys %options > 1 || !exists $options{tag};
Branch: 1 branch
BranchTrueFalseTotal
if keys %options > 1 or not exists $options{"tag"} 0 10206 10206
Condition33%1/3
keys %options > 1 or not exists $options{"tag"}
l!l&&r!l&&!r
0010206
MC/DC0%0/2
keys %options > 1 or not exists $options{"tag"}
keys %options > 1 not exists $options{"tag"}
Truth table33%1/3
A or not(B)
A= keys %options > 1B= not exists $options{"tag"}
ABresult
1X1
011
000
101 10206 ▶ confess 'The content of a style node must be an InlineTree' unless $content_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $content_ref 0 10206 10206
102 10206 hashpush %{$this}, tag => $options{tag};
103 } else {
104 0 confess "Unexpected type for an InlineNode: ${type}";
105 }
106 264636 bless $this, $class;
107  
108 264636 Hash::Util::lock_keys %{$this};
109 264636 return $this;
110 }
111  
112 sub clone {
113 114155 ▶ my ($this) = @_;
Subroutine clone: called CC=1 SCAR=0.0
114  
115 114155 return bless {%{$this}}, ref($this);
116 }
117  
118 sub has_subtree {
119 419220 ▶ my ($this) = @_;
Subroutine has_subtree: called CC=1 SCAR=0.0
120  
121 419220 return exists $this->{subtree};
122 }
123  
124 sub escape_content {
125 179473 ▶ my ($this, $char_class_to_escape, $char_class_to_escape_in_code) = @_;
Subroutine escape_content: called CC=12 SCAR=25.2
126  
127 179473 ▶ confess 'Node should not already be escaped when calling to_text' if $this->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"escaped"} 0 179473 179473
128 179473 $this->{escaped} = 1;
129  
130 179473 ▶ if ($this->{type} eq 'text') {
Branch: 5 branches
BranchTrueFalseTotal
if ($this->{'type'} eq 'text') { } 107837 71636 179473
elsif ($this->{'type'} eq 'literal') { } 20921 50715 71636
elsif ($this->{'type'} eq 'code') { } 3779 46936 50715
elsif ($this->{'type'} eq 'link') { } 11954 34982 46936
elsif ($this->{'type'} eq 'html' or $this->{'type'} eq 'style') { } 34982 0 34982
131 107837 decode_entities($this->{content});
132 107837 html_escape($this->{content}, $char_class_to_escape);
133 } elsif ($this->{type} eq 'literal') {
134 20921 html_escape($this->{content}, $char_class_to_escape);
135 } elsif ($this->{type} eq 'code') {
136 # New lines are treated like spaces in code.
137 3779 $this->{content} =~ s/\n/ /g;
138 # If the content is not just whitespace and it has one space at the
139 # beginning and one at the end, then we remove them.
140 3779 $this->{content} =~ s/^ (.*[^ ].*) $/$1/g;
141 3779 html_escape($this->{content}, $char_class_to_escape_in_code);
142 } elsif ($this->{type} eq 'link') {
143 11954 ▶ if ($this->{linktype} eq 'autolink') {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'linktype'} eq 'autolink') { } 9727 2227 11954
elsif ($this->{'linktype'} eq 'link' or $this->{'linktype'} eq 'img') { } 2227 0 2227
144 # For autolinks we don’t decode entities as these are treated like html
145 # construct.
146 9727 html_escape($this->{content}, $char_class_to_escape);
147 9727 http_escape($this->{target});
148 9727 html_escape($this->{target}, $char_class_to_escape);
149 ▶ } elsif ($this->{linktype} eq 'link' || $this->{linktype} eq 'img') {
Condition66%2/3
$this->{"linktype"} eq "link" or $this->{"linktype"} eq "img"
l!l&&r!l&&!r
12609670
MC/DC0%0/2
$this->{"linktype"} eq "link" or $this->{"linktype"} eq "img"
$this->{"linktype"} eq "link" $this->{"linktype"} eq "img"
Truth table66%2/3
A or B
A= $this->{"linktype"} eq "link"B= $this->{"linktype"} eq "img"
ABresult
1X1
011
000
150 # This is a real MD link definition (or image). The target and title
151 # have been generated through the to_source_text() method, so they need
152 # to be decoded and html_escaped
153 2227 ▶ if (exists $this->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"title"}) 92 2135 2227
154 92 decode_entities($this->{title});
155 92 html_escape($this->{title}, $char_class_to_escape);
156 }
157 2227 decode_entities($this->{target});
158 2227 http_escape($this->{target});
159 2227 html_escape($this->{target}, $char_class_to_escape);
160 } else {
161 0 confess 'Unexpected link type in render_node_html: '.$this->{linktype};
162 }
163 ▶ } elsif ($this->{type} eq 'html' || $this->{type} eq 'style') {
Condition66%2/3
$this->{"type"} eq "html" or $this->{"type"} eq "style"
l!l&&r!l&&!r
24776102060
MC/DC0%0/2
$this->{"type"} eq "html" or $this->{"type"} eq "style"
$this->{"type"} eq "html" $this->{"type"} eq "style"
Truth table66%2/3
A or B
A= $this->{"type"} eq "html"B= $this->{"type"} eq "style"
ABresult
1X1
011
000
164 # Nothing here on purpose
165 } else {
166 0 confess 'Unexpected node type in render_node_html: '.$this->{type};
167 }
168 179473 return;
169 }
170 } # package Markdown::Perl::InlineNode
171  
172 =pod
173  
174 =head2 new_text, new_code, new_link, new_literal
175  
176 my $text_node = new_text('text content');
177 my $code_node = new_code('code content');
178 my $link_node = new_link('text content', type=> 'type', target => 'the target'[, title => 'the title']);
179 my $link_node = new_link($subtree_content, type=> 'type', target => 'the target'[, title => 'the title'][, content => 'override content']);
180 my $html_node = new_html('<raw html content>');
181 my $style_node = new_literal($subtree_content, 'html_tag');
182 my $literal_node = new_literal('literal content');
183  
184 These methods return a text node that can be inserted in an C<InlineTree>.
185  
186 =cut
187  
188 213757 ▶sub new_text { return Markdown::Perl::InlineNode->new(text => @_) }
Subroutine new_text: called CC=1 SCAR=0.0
Pod documented
189 3780 ▶sub new_code { return Markdown::Perl::InlineNode->new(code => @_) }
Subroutine new_code: called CC=1 SCAR=0.0
Pod documented
190 11955 ▶sub new_link { return Markdown::Perl::InlineNode->new(link => @_) }
Subroutine new_link: called CC=1 SCAR=0.0
Pod documented
191 24781 ▶sub new_html { return Markdown::Perl::InlineNode->new(html => @_) }
Subroutine new_html: called CC=1 SCAR=0.0
Pod undocumented
192 10206 ▶sub new_style { return Markdown::Perl::InlineNode->new(style => @_) }
Subroutine new_style: called CC=1 SCAR=0.0
Pod undocumented
193 157 ▶sub new_literal { return Markdown::Perl::InlineNode->new(literal => @_) }
Subroutine new_literal: called CC=1 SCAR=0.0
Pod documented
194  
195 =pod
196  
197 =head2 is_node, is_tree
198  
199 These two methods returns whether a given object is a node that can be inserted
200 in an C<InlineTree> and whether it’s an C<InlineTree> object.
201  
202 =cut
203  
204 sub is_node {
205 686863 ▶ my ($obj) = @_;
Subroutine is_node: called CC=2 SCAR=7.1
Pod documented
206 686863 ▶ return blessed($obj) && $obj->isa('Markdown::Perl::InlineNode');
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Markdown::Perl::InlineNode')
!ll&&!rl&&r
0106181580682
MC/DC50%1/2
builtin::blessed($obj) && $obj->isa('Markdown::Perl::InlineNode')
builtin::blessed($obj) $obj->isa('Markdown::Perl::InlineNode')
Truth table66%2/3
A && B
A= builtin::blessed($obj)B= $obj->isa('Markdown::Perl::InlineNode')
ABresult
0X0
100
111
207 }
208  
209 sub is_tree {
210 65734 ▶ my ($obj) = @_;
Subroutine is_tree: called CC=2 SCAR=8.1
Pod documented
211 65734 ▶ return blessed($obj) && $obj->isa('Markdown::Perl::InlineTree');
Condition33%1/3
builtin::blessed($obj) && $obj->isa('Markdown::Perl::InlineTree')
!ll&&!rl&&r
0065734
MC/DC0%0/2
builtin::blessed($obj) && $obj->isa('Markdown::Perl::InlineTree')
builtin::blessed($obj) $obj->isa('Markdown::Perl::InlineTree')
Truth table33%1/3
A && B
A= builtin::blessed($obj)B= $obj->isa('Markdown::Perl::InlineTree')
ABresult
0X0
100
111
212 }
213  
214 =pod
215  
216 =head2 push
217  
218 $tree->push(@nodes_or_trees);
219  
220 Push a list of nodes at the end of the top-level nodes of the current tree.
221  
222 If passed C<InlineTree> objects, then the nodes of these trees are pushed (not
223 the tree itself).
224  
225 =cut
226  
227 sub push { ## no critic (ProhibitBuiltinHomonyms)
228 488600 ▶ my ($this, @nodes_or_trees) = @_;
Subroutine push: called CC=4 SCAR=14.0
Pod documented
229  
230 488600 for my $node_or_tree (@nodes_or_trees) {
231 487745 ▶ if (is_node($node_or_tree)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_node($node_or_tree)) { } 422011 65734 487745
elsif (is_tree($node_or_tree)) { } 65734 0 65734
232 422011 push @{$this->{children}}, $node_or_tree;
233 } elsif (is_tree($node_or_tree)) {
234 65734 push @{$this->{children}}, @{$node_or_tree->{children}};
235 } else {
236 0 confess 'Invalid argument type for InlineTree::push: '.ref($node_or_tree);
237 }
238 }
239  
240 488600 return;
241 }
242  
243 =pod
244  
245 =head2 replace
246  
247 $tree->replace($index, @nodes);
248  
249 Remove the existing node at the given index and replace it by the given list of
250 nodes (or, if passed C<InlineTree> objects, their own nodes).
251  
252 =cut
253  
254 sub replace {
255 76753 ▶ my ($this, $child_index, @new_nodes) = @_;
Subroutine replace: called CC=2 SCAR=7.2
Pod documented
256 76753 splice @{$this->{children}}, $child_index, 1,
257 76753 ▶ map { is_node($_) ? $_ : @{$_->{children}} } @new_nodes;
Branch: 1 branch
BranchTrueFalseTotal
is_node($_) ? : 135703 0 135703
258 76753 return;
259 }
260  
261 =pod
262  
263 =head2 insert
264  
265 $tree->insert($index, @new_nodes);
266  
267 Inserts the given nodes (or, if passed C<InlineTree> objects, their own nodes)
268 at the given index. After the operation, the first inserted node will have that
269 index.
270  
271 =cut
272  
273 sub insert {
274 53045 ▶ my ($this, $index, @new_nodes) = @_;
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
275 53045 ▶ splice @{$this->{children}}, $index, 0, map { is_node($_) ? $_ : @{$_->{children}} } @new_nodes;
Branch: 1 branch
BranchTrueFalseTotal
is_node($_) ? : 22968 40447 63415
276 53045 return;
277 }
278  
279 =pod
280  
281 =head2 extract
282  
283 $tree->extract($start_child, $start_offset, $end_child, $end_offset);
284  
285 Extract the content of the given tree, starting at the child with the given
286 index (which must be a B<text> node) and at the given offset in the child’s
287 text, and ending at the given node and offset (which must also be a B<text>
288 node).
289  
290 That content is removed from the input tree and returned as a new C<InlineTree>
291 object. Returns a pair with the new tree and the index of the first child after
292 the removed content in the input tree. Usually it will be C<$start_child + 1>,
293 but it can be C<$start_child> if C<$start_offset> was 0.
294  
295 In scalar context, returns only the extracted tree.
296  
297 =cut
298  
299 sub extract {
300 80215 ▶ my ($this, $child_start, $text_start, $child_end, $text_end) = @_;
Subroutine extract: called CC=23 SCAR=31.6
Pod documented
301  
302 # In this method, we should not read $sn and $en when they are not split (that
303 # is if text_start or text_end are 0), so that the method works at the
304 # boundary of non-text nodes.
305  
306 80215 my $sn = $this->{children}[$child_start];
307 confess 'Start node in an extract operation is not of type text: '.$sn->{type}
308 80215 ▶ unless $sn->{type} eq 'text' || $text_start == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $sn->{"type"} eq "text" or $text_start == 0 0 80215 80215
Condition66%2/3
$sn->{"type"} eq "text" or $text_start == 0
l!l&&r!l&&!r
8021320
MC/DC0%0/2
$sn->{"type"} eq "text" or $text_start == 0
$sn->{"type"} eq "text" $text_start == 0
Truth table66%2/3
A or B
A= $sn->{"type"} eq "text"B= $text_start == 0
ABresult
1X1
011
000
309  
310 ## I don’t think that this block is useful (I should add tests for this case
311 ## to check if this is needed).
312 ## The code after this block will be invalid if we extract an empty span, but
313 ## I don’t think that this can happen in practice.
314 # if ($child_start == $child_end && $text_start == $text_end) {
315 # my $offset = 0;
316 # if ($text_start != 0) {
317 # if ($text_start != length($sn->{content})) {
318 # my $nn = new_text(substr $sn->{content}, $text_start, length($sn->{content}), '');
319 # $this->insert($child_start + 1, $nn);
320 # }
321 # $offset = 1;
322 # }
323 # return (Markdown::Perl::InlineTree->new(), $child_start + $offset) if wantarray;
324 # return Markdown::Perl::InlineTree->new();
325 # }
326  
327 80215 my $en = $this->{children}[$child_end];
328 confess 'End node in an extract operation is not of type text: '.$en->{type}
329 80215 ▶ unless $text_end == 0 || $en->{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $text_end == 0 or $en->{"type"} eq "text" 0 80215 80215
Condition66%2/3
$text_end == 0 or $en->{"type"} eq "text"
l!l&&r!l&&!r
237799780
MC/DC0%0/2
$text_end == 0 or $en->{"type"} eq "text"
$text_end == 0 $en->{"type"} eq "text"
Truth table66%2/3
A or B
A= $text_end == 0B= $en->{"type"} eq "text"
ABresult
1X1
011
000
330 80215 ▶ confess 'Start offset is less than 0 in an extract operation' if $text_start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $text_start < 0 0 80215 80215
331 confess 'End offset is past the end of the text in an extract operation'
332 80215 ▶ if $text_end != 0 && $text_end > length($en->{content});
Branch: 1 branch
BranchTrueFalseTotal
if $text_end != 0 and $text_end > length $en->{"content"} 0 80215 80215
Condition66%2/3
$text_end != 0 and $text_end > length $en->{"content"}
!ll&&!rl&&r
237799780
MC/DC0%0/2
$text_end != 0 and $text_end > length $en->{"content"}
$text_end != 0 $text_end > length $en->{"content"}
Truth table66%2/3
A and B
A= $text_end != 0B= $text_end > length $en->{"content"}
ABresult
0X0
100
111
333  
334 80215 my $empty_last = 0;
335 80215 ▶ if ($text_end == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_end == 0) 237 79978 80215
336 237 $empty_last = 1;
337 237 $child_end--;
338 }
339  
340 # Clone will not recurse into sub-trees. But the start and end nodes can’t
341 # have sub-trees, and the middle ones don’t matter because they are not shared
342 # with the initial tree.
343 my @nodes =
344 80215 map { $_->clone() } @{$this->{children}}[$child_start .. $child_end];
345 ## no critic (ProhibitLvalueSubstr)
346 80215 ▶ substr($nodes[-1]{content}, $text_end) = '' unless $empty_last; ## We have already removed the empty last node.
Branch: 1 branch
BranchTrueFalseTotal
unless $empty_last 79978 237 80215
347 80215 substr($nodes[0]{content}, 0, $text_start) = ''; # We must do this after text_end in case they are the same node.
348 80215 ▶ shift @nodes if length($nodes[0]{content}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $nodes[0]{"content"} == 0 10796 69419 80215
349 80215 ▶ pop @nodes if !$empty_last && @nodes && length($nodes[-1]{content}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $empty_last and @nodes and length $nodes[-1]{"content"} == 0 0 80215 80215
Condition83%5/6
not $empty_last and @nodes and length $nodes[-1]{"content"} == 0
!ll&&!rl&&r
10821693940
not $empty_last and @nodes
!ll&&!rl&&r
2371058469394
MC/DC0%0/3
not $empty_last and @nodes and length $nodes[-1]{"content"} == 0
not $empty_last @nodes length $nodes[-1]{"content"} == 0
Truth table75%3/4
not(A) and B and C
A= not $empty_lastB= @nodesC= length $nodes[-1]{"content"} == 0
ABCresult
0XX0
10X0
1100
1111
350 80215 my $new_tree = Markdown::Perl::InlineTree->new();
351 80215 $new_tree->push(@nodes);
352  
353 80215 ▶ if ($child_start != $child_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_start != $child_end) { } 3462 76753 80215
354 3462 ▶ if ($text_start == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_start == 0) { } 495 2967 3462
355 495 $child_start--;
356 } else {
357 2967 substr($sn->{content}, $text_start) = '';
358 }
359 3462 ▶ if ($empty_last || $text_end == length($en->{content})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty_last or $text_end == length $en->{'content'}) { } 2444 1018 3462
Condition100%3/3
$empty_last or $text_end == length $en->{"content"}
l!l&&r!l&&!r
23322111018
MC/DC100%2/2
$empty_last or $text_end == length $en->{"content"}
$empty_last $text_end == length $en->{"content"}
Truth table100%3/3
A or B
A= $empty_lastB= $text_end == length $en->{"content"}
ABresult
1X1
011
000
360 2444 $child_end++;
361 } else {
362 1018 substr($en->{content}, 0, $text_end) = '';
363 }
364 3462 splice @{$this->{children}}, $child_start + 1, $child_end - $child_start - 1;
365 } else {
366 76753 my @new_nodes;
367 76753 ▶ if ($text_start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_start > 0) 71037 5716 76753
368 71037 CORE::push @new_nodes, new_text(substr $sn->{content}, 0, $text_start);
369 }
370 76753 ▶ if (!$empty_last && $text_end < length($sn->{content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $empty_last and $text_end < length $sn->{"content"}) 64666 12087 76753
Condition100%3/3
not $empty_last and $text_end < length $sn->{"content"}
!ll&&!rl&&r
41208364666
MC/DC100%2/2
not $empty_last and $text_end < length $sn->{"content"}
not $empty_last $text_end < length $sn->{"content"}
Truth table100%3/3
not(A) and B
A= not $empty_lastB= $text_end < length $sn->{"content"}
ABresult
0X0
100
111
371 64666 CORE::push @new_nodes, new_text(substr $sn->{content}, $text_end);
372 }
373 76753 $this->replace($child_start, @new_nodes);
374 76753 ▶ $child_start-- if $text_start == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $text_start == 0 5716 71037 76753
375 }
376 ## use critic (ProhibitLvalueSubstr)
377  
378 80215 ▶ return ($new_tree, $child_start + 1) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 42674 37541 80215
379 37541 return $new_tree;
380 }
381  
382 =pod
383  
384 =head2 map_shallow
385  
386 $tree->map_shallow($sub);
387  
388 Apply the given sub to each direct child of the tree. The sub can return a node
389 or a tree and that returned content is concatenated to form a new tree.
390  
391 Only the top-level nodes of the tree are visited.
392  
393 In void context, update the tree in-place. Otherwise, the new tree is returned.
394  
395 In all cases, C<$sub> must return new nodes or trees, it can’t modify the input
396 object. The argument to C<$sub> are passed in the usual way in C<@_>, not in
397 C<$_>.
398  
399 =cut
400  
401 sub map_shallow {
402 0 ▶ my ($this, $sub) = @_;
Subroutine map_shallow: not called CC=3 SCAR=24.8
Pod documented
403  
404 0 my $new_tree = Markdown::Perl::InlineTree->new();
405  
406 0 for (@{$this->{children}}) {
407 0 $new_tree->push($sub->());
408 }
409  
410 0 ▶ return $new_tree if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
411 0 %{$this} = %{$new_tree};
412 0 return;
413 }
414  
415 =pod
416  
417 =head2 map
418  
419 $tree->map($sub);
420  
421 Same as C<map_shallow>, but the tree is visited recursively. The subtree of
422 individual nodes are visited and their content replaced before the node itself
423 are visited.
424  
425 =cut
426  
427 sub map { ## no critic (ProhibitBuiltinHomonyms)
428 121303 ▶ my ($this, $sub, $start, $stop) = @_;
Subroutine map: called CC=8 SCAR=20.8
Pod documented
429 # $start and $stop are not documented for this function, they are used by
430 # clone().
431  
432 121303 my $new_tree = Markdown::Perl::InlineTree->new();
433  
434 121303 ▶ for (@{$this->{children}}[$start // 0 .. $stop // $#{$this->{children}}]) {
Condition100%5/5
$start // 0
l!l
2695594348
$stop // $#{$$this{"children"};}
l!l&&r!l&&!r
269553994654402
MC/DC100%1/1
$start // 0
$start
MC/DC100%2/2
$stop // $#{$$this{"children"};}
$stop $#{$$this{"children"};}
Truth table100%2/2
A
A= $start
Aresult
11
00
Truth table100%3/3
A // B
A= $stopB= $#{$$this{"children"};}
ABresult
1X1
011
000
435 239747 ▶ if ($_->has_subtree()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->has_subtree) { } 2229 237518 239747
436 2229 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 47 2182 2229
437 47 my $new_node = $_->clone();
438 47 $new_node->{subtree}->map($sub);
439 47 local *_ = \$new_node;
440 47 $new_tree->push($sub->());
441 } else {
442 # Is there a risk that this modifies $_ before the call to $sub?
443 2182 $_->{subtree}->map($sub);
444 2182 $new_tree->push($sub->());
445 }
446 } else {
447 237518 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 28783 208735 237518
448 28783 my $new_node = $_->clone();
449 28783 local *_ = \$new_node;
450 28783 $new_tree->push($sub->());
451 } else {
452 208735 $new_tree->push($sub->());
453 }
454 }
455 }
456  
457 121303 ▶ return $new_tree if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 26955 94348 121303
458 94348 %{$this} = %{$new_tree};
459 94348 return;
460 }
461  
462 =pod
463  
464 =head2 apply
465  
466 $tree->apply($sub);
467  
468 Apply the given C<$sub> to all nodes of the tree. The sub receives the current
469 node in C<$_> and can modify it. The return value of the sub is ignored.
470  
471 =cut
472  
473 sub apply {
474 56444 ▶ my ($this, $sub) = @_;
Subroutine apply: called CC=3 SCAR=11.0
Pod documented
475  
476 56444 for (@{$this->{children}}) {
477 179473 $sub->();
478 179473 ▶ $_->{subtree}->apply($sub) if $_->has_subtree();
Branch: 1 branch
BranchTrueFalseTotal
if $_->has_subtree 12433 167040 179473
479 }
480  
481 56444 return;
482 }
483  
484 =pod
485  
486 =head2 clone
487  
488 my $new_tree = $tree->clone([$child_start, $child_end]);
489  
490 Clone (deep copy) the entire tree or a portion of it.
491  
492 =cut
493  
494 sub clone {
495 26955 ▶ my ($this, $start, $stop) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
496 26955 ▶ return $this->map(sub { $_ }, $start, $stop);
Subroutine __ANON__: called CC=1 SCAR=0.0
497 }
498  
499 =head2 fold
500  
501 $tree->fold($sub, $init);
502  
503 Iterates over the top-level nodes of the tree, calling C<$sub> for each of them.
504 It receives two arguments, the current node and the result of the previous call.
505 The first call receives C<$init> as its second argument.
506  
507 Returns the result of the last call of C<$sub>.
508  
509 =cut
510  
511 # TODO: maybe have a "cat" methods that expects each node to return a string and
512 # concatenate them, so that we can concatenate them all together at once, which
513 # might be more efficient.
514 sub fold {
515 85485 ▶ my ($this, $sub, $init) = @_;
Subroutine fold: called CC=2 SCAR=6.9
Pod documented
516  
517 85485 my $out = $init;
518  
519 85485 for (@{$this->{children}}) {
520 201966 $out = $sub->($_, $out);
521 }
522  
523 85485 return $out;
524 }
525  
526 =pod
527  
528 =head2 find_in_text
529  
530 $tree->find_in_text($regex, $start_child, $start_offset, [$end_child, $end_offset]);
531  
532 Find the first match of the given regex in the tree, starting at the given
533 offset in the node. This only considers top-level nodes of the tree and skip
534 over non B<text> node (including the first one).
535  
536 If C<$end_child> and C<$end_offset> are given, then does not look for anything
537 starting at or after that bound.
538  
539 Does not match the regex across multiple nodes.
540  
541 Returns C<$child_number, $match_start_offset, $match_end_offset> (or just a
542 I<true> value in scalar context) or C<undef>.
543  
544 =cut
545  
546 sub find_in_text {
547 91164 ▶ my ($this, $re, $child_start, $text_start, $child_bound, $text_bound) = @_;
Subroutine find_in_text: called CC=1 SCAR=0.0
Pod documented
548 # qr/^\b$/ is a regex that can’t match anything.
549 91164 return $this->find_balanced_in_text(qr/^\b$/, $re, $child_start, $text_start, $child_bound,
550 $text_bound);
551 }
552  
553 =pod
554  
555 =head2 find_balanced_in_text
556  
557 $tree->find_balanced_in_text(
558 $open_re, $close_re, $start_child, $start_offset, $child_bound, $text_bound);
559  
560 Same as C<find_in_text> except that this method searches for both C<$open_re> and
561 C<$close_re> and, each time C<$open_re> is found, it needs to find C<$close_re>
562 one more time before we it returns. The method assumes that C<$open_re> has
563 already been seen once before the given C<$start_child> and C<$start_offset>.
564  
565 =cut
566  
567 sub find_balanced_in_text {
568 91213 ▶ my ($this, $open_re, $close_re, $child_start, $text_start, $child_bound, $text_bound) = @_;
Subroutine find_balanced_in_text: called CC=12 SCAR=24.8
Pod documented
569  
570 91213 my $open = 1;
571  
572 91213 ▶ for my $i ($child_start .. ($child_bound // $#{$this->{children}})) {
Condition100%3/3
$child_bound // $#{$$this{"children"};}
l!l&&r!l&&!r
38116313024272
MC/DC100%2/2
$child_bound // $#{$$this{"children"};}
$child_bound $#{$$this{"children"};}
Truth table100%3/3
A // B
A= $child_boundB= $#{$$this{"children"};}
ABresult
1X1
011
000
573 158959 ▶ next unless $this->{children}[$i]{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"children"}[$i]{"type"} eq "text" 40773 118186 158959
574 118186 ▶ if ($i == $child_start && $text_start != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $child_start and $text_start != 0) { } 4526 113660 118186
Condition100%3/3
$i == $child_start and $text_start != 0
!ll&&!rl&&r
35726779344526
MC/DC100%2/2
$i == $child_start and $text_start != 0
$i == $child_start $text_start != 0
Truth table100%3/3
A and B
A= $i == $child_startB= $text_start != 0
ABresult
0X0
100
111
575 4526 pos($this->{children}[$i]{content}) = $text_start;
576 } else {
577 113660 pos($this->{children}[$i]{content}) = 0;
578 }
579  
580 # When the code in this regex is executed, we are sure that the engine
581 # won’t backtrack (as we are at the end of the regex).
582 118186 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"children"}[$i]{"content"} =~ / $open_re(?{ $open++; }) | $close_re(?{ $open--; }) /gux) 41426 76820 118246
583 38 $this->{children}[$i]{content} =~ m/ ${open_re}(?{$open++}) | ${close_re}(?{$open--}) /gx)
584 {
585 41426 ▶ return if $i == ($child_bound // -1) && $LAST_MATCH_START[0] >= $text_bound;
Branch: 1 branch
BranchTrueFalseTotal
if $i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound 366 41060 41426
Condition100%5/5
$i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
!ll&&!rl&&r
4101842366
$child_bound // -1
l!l
40941017
MC/DC100%2/2
$i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
$i == ($child_bound // -1) $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
MC/DC100%1/1
$child_bound // -1
$child_bound
Truth table100%3/3
A and B
A= $i == ($child_bound // -1)B= $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
ABresult
0X0
100
111
Truth table100%2/2
A
A= $child_bound
Aresult
11
00
586 41060 ▶ if ($open == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open == 0) 41000 60 41060
587 41000 ▶ return ($i, $LAST_MATCH_START[0], $LAST_MATCH_END[0]) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 40958 42 41000
588 42 return 1;
589 }
590 }
591 }
592  
593 49847 return;
594 }
595  
596 =pod
597  
598 =head2 find_in_text_with_balanced_content
599  
600 $tree->find_in_text_with_balanced_content(
601 $open_re, $close_re, $end_re, $start_child, $start_offset,
602 $child_bound, $text_bound);
603  
604 Similar to C<find_balanced_in_text> except that this method ends when C<$end_re>
605 is seen, after the C<$open_re> and C<$close_re> regex have been seen a balanced
606 number of time. If the closing one is seen more than the opening one, the search
607 succeeds too. The method does B<not> assumes that C<$open_re> has already been
608 seen before the given C<$start_child> and C<$start_offset> (as opposed to
609 C<find_balanced_in_text>).
610  
611 =cut
612  
613 sub find_in_text_with_balanced_content {
614 3653 ▶ my ($this, $open_re, $close_re, $end_re, $child_start, $text_start, $child_bound, $text_bound) =
Subroutine find_in_text_with_balanced_content: called CC=13 SCAR=25.8
Pod documented
615 @_;
616  
617 3653 my $open = 0;
618  
619 3653 ▶ for my $i ($child_start .. ($child_bound // $#{$this->{children}})) {
Condition66%2/3
$child_bound // $#{$$this{"children"};}
l!l&&r!l&&!r
024591194
MC/DC50%1/2
$child_bound // $#{$$this{"children"};}
$child_bound $#{$$this{"children"};}
Truth table66%2/3
A // B
A= $child_boundB= $#{$$this{"children"};}
ABresult
1X1
011
000
620 4280 ▶ next unless $this->{children}[$i]{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"children"}[$i]{"type"} eq "text" 333 3947 4280
621 3947 ▶ if ($i == $child_start && $text_start != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $child_start and $text_start != 0) { } 3651 296 3947
Condition100%3/3
$i == $child_start and $text_start != 0
!ll&&!rl&&r
29423651
MC/DC100%2/2
$i == $child_start and $text_start != 0
$i == $child_start $text_start != 0
Truth table100%3/3
A and B
A= $i == $child_startB= $text_start != 0
ABresult
0X0
100
111
622 3651 pos($this->{children}[$i]{content}) = $text_start;
623 } else {
624 296 pos($this->{children}[$i]{content}) = 0;
625 }
626  
627 # When the code in this regex is executed, we are sure that the engine
628 # won’t backtrack (as we are at the end of the regex).
629  
630 3947 my $done = 0;
631 3947 ▶ while ($this->{children}[$i]{content} =~
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"children"}[$i]{"content"} =~ / $end_re(?{ $done = 1; }) | $open_re(?{ $open++; }) | $close_re(?{ $open--; }) /gux) 4362 877 5239
632 1618 m/ ${end_re}(?{$done = 1}) | ${open_re}(?{$open++}) | ${close_re}(?{$open--}) /gx) {
633 4362 ▶ return if $i == ($child_bound // -1) && $LAST_MATCH_START[0] >= $text_bound;
Branch: 1 branch
BranchTrueFalseTotal
if $i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound 0 4362 4362
Condition40%2/5
$i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
!ll&&!rl&&r
436200
$child_bound // -1
l!l
04362
MC/DC0%0/2
$i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
$i == ($child_bound // -1) $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
MC/DC0%0/1
$child_bound // -1
$child_bound
Truth table33%1/3
A and B
A= $i == ($child_bound // -1)B= $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
ABresult
0X0
100
111
Truth table50%1/2
A
A= $child_bound
Aresult
11
00
634 4362 ▶ return ($i, $LAST_MATCH_START[0], $LAST_MATCH_END[0]) if ($open == 0 && $done) || $open < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $open == 0 and $done or $open < 0 3070 1292 4362
Condition100%6/6
$open == 0 and $done or $open < 0
l!l&&r!l&&!r
105120191292
$open == 0 and $done
!ll&&!rl&&r
31121991051
MC/DC100%3/3
$open == 0 and $done or $open < 0
$open == 0 $done $open < 0
Truth table80%4/5
A and B or C
A= $open == 0B= $doneC= $open < 0
ABCresult
11X1
0X11
1011
0X00
1000
635 1292 $done = 0;
636 }
637 }
638  
639 583 return;
640 }
641  
642 =pod
643  
644 =head2 render_html
645  
646 $tree->render_html();
647  
648 Returns the HTML representation of that C<InlineTree>.
649  
650 =cut
651  
652 sub render_html {
653 55360 ▶ my ($tree) = @_;
Subroutine render_html: called CC=1 SCAR=0.0
Pod documented
654 55360 return $tree->fold(\&render_node_html, '');
655 }
656  
657 sub render_node_html {
658 177295 ▶ my ($n, $acc) = @_;
Subroutine render_node_html: called CC=14 SCAR=26.5
Pod undocumented
659  
660 177295 ▶ confess 'Node should already be escaped when calling render_html' unless $n->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"escaped"} 0 177295 177295
661  
662 177295 ▶ if ($n->{type} eq 'text' || $n->{type} eq 'literal' || $n->{type} eq 'html') {
Branch: 4 branches
BranchTrueFalseTotal
if ($n->{'type'} eq 'text' or $n->{'type'} eq 'literal' or $n->{'type'} eq 'html') { } 151640 25655 177295
elsif ($n->{'type'} eq 'code') { } 3750 21905 25655
elsif ($n->{'type'} eq 'link') { } 11806 10099 21905
elsif ($n->{'type'} eq 'style') { } 10099 0 10099
Condition100%6/6
$n->{"type"} eq "text" or $n->{"type"} eq "literal" or $n->{"type"} eq "html"
l!l&&r!l&&!r
1271132452725655
$n->{"type"} eq "text" or $n->{"type"} eq "literal"
l!l&&r!l&&!r
1064022071150182
MC/DC100%3/3
$n->{"type"} eq "text" or $n->{"type"} eq "literal" or $n->{"type"} eq "html"
$n->{"type"} eq "text" $n->{"type"} eq "literal" $n->{"type"} eq "html"
Truth table100%4/4
A or B or C
A= $n->{"type"} eq "text"B= $n->{"type"} eq "literal"C= $n->{"type"} eq "html"
ABCresult
1XX1
01X1
0011
0000
663 151640 return $acc.$n->{content};
664 } elsif ($n->{type} eq 'code') {
665 3750 return $acc.'<code>'.$n->{content}.'</code>';
666 } elsif ($n->{type} eq 'link') {
667 11806 my $title = '';
668 11806 ▶ if (exists $n->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $n->{"title"}) 92 11714 11806
669 92 $title = " title=\"$n->{title}\"";
670 }
671 11806 ▶ if ($n->{linktype} eq 'link' || $n->{linktype} eq 'autolink') {
Branch: 2 branches
BranchTrueFalseTotal
if ($n->{'linktype'} eq 'link' or $n->{'linktype'} eq 'autolink') { } 10854 952 11806
elsif ($n->{'linktype'} eq 'img') { } 952 0 952
Condition100%3/3
$n->{"linktype"} eq "link" or $n->{"linktype"} eq "autolink"
l!l&&r!l&&!r
12519603952
MC/DC100%2/2
$n->{"linktype"} eq "link" or $n->{"linktype"} eq "autolink"
$n->{"linktype"} eq "link" $n->{"linktype"} eq "autolink"
Truth table100%3/3
A or B
A= $n->{"linktype"} eq "link"B= $n->{"linktype"} eq "autolink"
ABresult
1X1
011
000
672 # $n->{content} can only be set in the case of autolink or through the
673 # resolve_link_ref hook (in which case it takes precedence over whatever
674 # was in the link definition).
675 10854 ▶ my $content = exists $n->{content} ? $n->{content} : $n->{subtree}->render_html();
Branch: 1 branch
BranchTrueFalseTotal
exists $n->{'content'} ? : 9604 1250 10854
676 10854 return $acc."<a href=\"$n->{target}\"${title}>${content}</a>";
677 } elsif ($n->{linktype} eq 'img') {
678 952 my $content = $n->{subtree}->to_text();
679 952 return $acc."<img src=\"$n->{target}\" alt=\"${content}\"${title} />";
680 } else {
681 0 confess 'Unexpected link type in render_node_html: '.$n->{linktype};
682 }
683 } elsif ($n->{type} eq 'style') {
684 10099 my $content = $n->{subtree}->render_html();
685 10099 ▶ if ($n->{tag} =~ m/^\.(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{'tag'} =~ /^\.(.*)$/u) { } 1 10098 10099
686 1 my $class = $1;
687 1 return $acc."<span class=\"${class}\">${content}</span>";
688 } else {
689 10098 my $tag = $n->{tag};
690 10098 return $acc."<${tag}>${content}</${tag}>";
691 }
692 } else {
693 0 confess 'Unexpected node type in render_node_html: '.$n->{type};
694 }
695 }
696  
697 =pod
698  
699 =head2 to_text
700  
701 $tree->to_text();
702  
703 Returns the text content of this C<InlineTree> discarding all HTML formatting.
704 This is used mainly to produce the C<alt> text of image nodes (which can contain
705 any Markdown construct in the source).
706  
707 =cut
708  
709 sub to_text {
710 1083 ▶ my ($tree) = @_;
Subroutine to_text: called CC=1 SCAR=0.0
Pod documented
711 1083 return $tree->fold(\&node_to_text, '');
712 }
713  
714 sub node_to_text {
715 2177 ▶ my ($n, $acc) = @_;
Subroutine node_to_text: called CC=9 SCAR=22.1
Pod undocumented
716 2177 ▶ confess 'Node should already be escaped when calling to_text' unless $n->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"escaped"} 0 2177 2177
717 2177 ▶ if ($n->{type} eq 'text') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n->{'type'} eq 'text') { } 1434 743 2177
elsif ($n->{'type'} eq 'style') { } 107 636 743
elsif ($n->{'type'} eq 'literal' or $n->{'type'} eq 'html') { } 459 177 636
elsif ($n->{'type'} eq 'link') { } 148 29 177
elsif ($n->{'type'} eq 'code') { } 29 0 29
718 1434 return $acc.$n->{content};
719 } elsif ($n->{type} eq 'style') {
720 107 return $acc.$n->{subtree}->to_text();
721 ▶ } elsif ($n->{type} eq 'literal' || $n->{type} eq 'html') {
Condition100%3/3
$n->{"type"} eq "literal" or $n->{"type"} eq "html"
l!l&&r!l&&!r
210249177
MC/DC100%2/2
$n->{"type"} eq "literal" or $n->{"type"} eq "html"
$n->{"type"} eq "literal" $n->{"type"} eq "html"
Truth table100%3/3
A or B
A= $n->{"type"} eq "literal"B= $n->{"type"} eq "html"
ABresult
1X1
011
000
722 # This is not the exact same behavior as cmark because we will escape
723 # literals here, while cmark would not escape them. The cmark behavior is
724 # probably faulty here (and is not tested by the test suite).
725 459 return $acc.$n->{content};
726 } elsif ($n->{type} eq 'link') {
727 148 ▶ if ($n->{linktype} ne 'autolink') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{'linktype'} ne 'autolink') { } 24 124 148
728 24 return $acc.$n->{subtree}->to_text();
729 } else {
730 124 return $acc.$n->{content};
731 }
732 } elsif ($n->{type} eq 'code') {
733 29 return $acc.'<code>'.$n->{content}.'</code>';
734 } else {
735 0 confess 'Unsupported node type for to_text: '.$n->{type};
736 }
737 }
738  
739 =pod
740  
741 =head2 to_source_text
742  
743 $tree->to_source_text($unescape_literal);
744  
745 Returns the original Markdown source corresponding to this C<InlineTree>. This
746 is used to produce the reference label, target and title of link elements and so
747 can support only node types that have a higher priority than links (nodes that
748 may have been built already when this is called).
749  
750 The source is returned as-is, the HTML entities are neither decoded nor escaped.
751  
752 If C<$unescape_literal> is true, then literal values that were escaped in the
753 source are unescaped (e.g. C<\;> will appear again as C<\;>). Otherwise they
754 will just appear as their literal value (e.g. C<;>).
755  
756 As a readability facility, the C<UNESCAPE_LITERAL> symbol can be used to pass
757 this option (with a value of C<1>).
758  
759 =cut
760  
761 # It’s a feature that this does not interpolate.
762 32 ▶use constant UNESCAPE_LITERAL => 1; ## no critic (ProhibitConstantPragma)
Subroutine BEGIN: called CC=1 SCAR=0.0
763  
764 sub to_source_text {
765 29038 ▶ my ($tree, $unescape_literal) = @_;
Subroutine to_source_text: called CC=1 SCAR=0.0
Pod documented
766 29038 return $tree->fold(node_to_source_text($unescape_literal), '');
767 }
768  
769 sub node_to_source_text {
770 29038 ▶ my ($unescape_literal) = @_;
Subroutine node_to_source_text: called CC=1 SCAR=0.0
Pod undocumented
771 # TODO: ideally all this should be replaced by the fact that the nodes should
772 # store the span of text that they represent, to be able to extract the actual
773 # source text.
774 # This probably requires that the inlines processing should be rewritten to do
775 # the link at the same time as the auto-links and inline HTML so that this
776 # operates on text.
777 return sub {
778 22485 ▶ my ($n, $acc) = @_;
Subroutine __ANON__: called CC=10 SCAR=23.1
779 22485 ▶ confess 'Node should not already be escaped when calling to_source_text' if $n->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
if $n->{"escaped"} 0 22485 22485
780 22485 ▶ if ($n->{type} eq 'text') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n->{'type'} eq 'text') { } 21499 986 22485
elsif ($n->{'type'} eq 'literal' and $unescape_literal) { } 8 978 986
elsif ($n->{'type'} eq 'literal' or $n->{'type'} eq 'html') { } 557 421 978
elsif ($n->{'type'} eq 'code') { } 115 306 421
elsif ($n->{'type'} eq 'link') { } 306 0 306
781 21499 return $acc.$n->{content};
782 ▶ } elsif ($n->{type} eq 'literal' && $unescape_literal) {
Condition100%3/3
$n->{"type"} eq "literal" and $unescape_literal
!ll&&!rl&&r
958208
MC/DC100%2/2
$n->{"type"} eq "literal" and $unescape_literal
$n->{"type"} eq "literal" $unescape_literal
Truth table100%3/3
A and B
A= $n->{"type"} eq "literal"B= $unescape_literal
ABresult
0X0
100
111
783 8 return $acc.'\\'.$n->{content};
784 ▶ } elsif ($n->{type} eq 'literal' || $n->{type} eq 'html') {
Condition100%3/3
$n->{"type"} eq "literal" or $n->{"type"} eq "html"
l!l&&r!l&&!r
20537421
MC/DC100%2/2
$n->{"type"} eq "literal" or $n->{"type"} eq "html"
$n->{"type"} eq "literal" $n->{"type"} eq "html"
Truth table100%3/3
A or B
A= $n->{"type"} eq "literal"B= $n->{"type"} eq "html"
ABresult
1X1
011
000
785 557 return $acc.$n->{content};
786 } elsif ($n->{type} eq 'code') {
787 # TODO: This also need to be the source string with the right delimiters.
788 115 return $acc.'<code>'.$n->{content}.'</code>';
789 } elsif ($n->{type} eq 'link') {
790 306 ▶ if ($n->{linktype} eq 'autolink') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{'linktype'} eq 'autolink') { } 259 47 306
791 259 return $acc.'<'.$n->{content}.'>';
792 } else {
793 # 'img' can appear inside other links and links can appear inside images
794 # and, as-such, we may try to treat them as link reference label, so we
795 # need this case.
796 # Because their structure is complex, we return a dummy value.
797 # BUG: we can’t have a link reference using a label that looks like an
798 # image.
799 47 return $acc.'dummy_text_hopefully_this_does_not_collide_with_anything';
800 }
801 } else {
802 0 confess 'Unsupported node type for to_source_text: '.$n->{type};
803 }
804 29038 };
805 }
806  
807 =head2 span_to_source_text
808  
809 $tree->span_to_source_text($child_start, $text_start, $child_end, $text_end[, $unescape_literal]);
810  
811 Same as C<to_source_text()> but only renders the specified span of the
812 C<InlineTree>.
813  
814 =cut
815  
816 sub span_to_source_text {
817 26955 ▶ my ($tree, $child_start, $text_start, $child_end, $text_end, $unescape_literal) = @_;
Subroutine span_to_source_text: called CC=1 SCAR=0.0
Pod documented
818 26955 my $copy = $tree->clone($child_start, $child_end);
819 26955 my $extract = $copy->extract(0, $text_start, $child_end - $child_start, $text_end);
820 26955 return $extract->to_source_text($unescape_literal);
821 }
822  
823 32 1;
« Markdown/Perl/BlockParser.pm ↑ Summary Markdown/Perl/Inlines.pm »