Markdown/Perl/InlineTree.pm

filter: Statementstmt 93.6% 296 / 316 Branchbran 82.9% 141 / 170 Conditioncond 83.3% 80 / 96 MC/DCmcdc 59.6% 34 / 57 Subroutinesub 97.8% 46 / 47 Podpod 82.1% 23 / 28 totaltotal 86.8% 620 / 714 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 320095 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
51 320095 my ($class) = @_;
52  
53 320095 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 295180 ▶ sub hashpush (\%%) {
Subroutine hashpush: called CC=2 SCAR=6.9
61 295180 my ($hash, %args) = @_;
62 295180 ▶ while (my ($k, $v) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 295180 295180 590360
63 295180 $hash->{$k} = $v;
64 }
65 295180 return;
66 }
67  
68 262066 ▶ sub new {
Subroutine new: called CC=21 SCAR=31.6
69 262066 my ($class, $type, $content, %options) = @_;
70  
71 262066 my $this = {type => $type, escaped => 0};
72 262066 ▶ $this->{debug} = delete $options{debug} if exists $options{debug};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"debug"} 1170 260896 262066
73 262066 my $content_ref = ref $content;
74 262066 ▶ if (Scalar::Util::blessed($content)
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($content) and $content->isa('Markdown::Perl::InlineTree')) { } 12245 249821 262066
elsif (not ref $content) { } 249821 0 249821
Condition66%2/3
builtin::blessed($content) and $content->isa("Markdown::Perl::InlineTree")
!ll&&!rl&&r
249821012245
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 12245 hashpush %{$this}, subtree => $content;
77 } elsif (!ref($content)) {
78 249821 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 262066 ▶ 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') { } 240514 21552 262066
elsif ($type eq 'link') { } 11464 10088 21552
elsif ($type eq 'style') { } 10088 0 10088
Condition100%9/9
$type eq "text" or $type eq "code" or $type eq "literal" or $type eq "html"
l!l&&r!l&&!r
2159982451621552
$type eq "text" or $type eq "code" or $type eq "literal"
l!l&&r!l&&!r
21584115746068
$type eq "text" or $type eq "code"
l!l&&r!l&&!r
211921392046225
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 240514 ▶ confess "Unexpected content for inline ${type} node: ${content_ref}" if $content_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $content_ref 0 240514 240514
87 240514 ▶ confess "Unexpected parameters for inline ${type} node: ".join(', ', %options)
Branch: 1 branch
BranchTrueFalseTotal
if %options 0 240514 240514
88 if %options;
89 } elsif ($type eq 'link') {
90 11464 ▶ confess 'Missing required option "type" for inline link node' unless exists $options{type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"type"} 0 11464 11464
91 11464 hashpush %{$this}, linktype => delete $options{type};
92 confess 'Missing required option "target" for inline link node'
93 11464 ▶ unless exists $options{target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"target"} 0 11464 11464
94 11464 hashpush %{$this}, target => delete $options{target};
95 11464 ▶ hashpush %{$this}, title => delete $options{title} if exists $options{title};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"title"} 97 11367 11464
96 11464 ▶ hashpush %{$this}, content => delete $options{content} if exists $options{content};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"content"} 1 11463 11464
97 11464 ▶ confess 'Unexpected parameters for inline link node: '.join(', ', %options) if keys %options;
Branch: 1 branch
BranchTrueFalseTotal
if keys %options 0 11464 11464
98 } elsif ($type eq 'style') {
99 confess 'Unexpected parameters for inline style node: '.join(', ', %options)
100 10088 ▶ if keys %options > 1 || !exists $options{tag};
Branch: 1 branch
BranchTrueFalseTotal
if keys %options > 1 or not exists $options{"tag"} 0 10088 10088
Condition33%1/3
keys %options > 1 or not exists $options{"tag"}
l!l&&r!l&&!r
0010088
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 10088 ▶ confess 'The content of a style node must be an InlineTree' unless $content_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $content_ref 0 10088 10088
102 10088 hashpush %{$this}, tag => $options{tag};
103 } else {
104 0 confess "Unexpected type for an InlineNode: ${type}";
105 }
106 262066 bless $this, $class;
107  
108 262066 Hash::Util::lock_keys %{$this};
109 262066 return $this;
110 }
111  
112 113571 ▶ sub clone {
Subroutine clone: called CC=1 SCAR=0.0
113 113571 my ($this) = @_;
114  
115 113571 return bless {%{$this}}, ref($this);
116 }
117  
118 415340 ▶ sub has_subtree {
Subroutine has_subtree: called CC=1 SCAR=0.0
119 415340 my ($this) = @_;
120  
121 415340 return exists $this->{subtree};
122 }
123  
124 177482 ▶ sub escape_content {
Subroutine escape_content: called CC=12 SCAR=25.2
125 177482 my ($this, $char_class_to_escape, $char_class_to_escape_in_code) = @_;
126  
127 177482 ▶ confess 'Node should not already be escaped when calling to_text' if $this->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"escaped"} 0 177482 177482
128 177482 $this->{escaped} = 1;
129  
130 177482 ▶ if ($this->{type} eq 'text') {
Branch: 5 branches
BranchTrueFalseTotal
if ($this->{'type'} eq 'text') { } 106896 70586 177482
elsif ($this->{'type'} eq 'literal') { } 20620 49966 70586
elsif ($this->{'type'} eq 'code') { } 3915 46051 49966
elsif ($this->{'type'} eq 'link') { } 11459 34592 46051
elsif ($this->{'type'} eq 'html' or $this->{'type'} eq 'style') { } 34592 0 34592
131 106896 decode_entities($this->{content});
132 106896 html_escape($this->{content}, $char_class_to_escape);
133 } elsif ($this->{type} eq 'literal') {
134 20620 html_escape($this->{content}, $char_class_to_escape);
135 } elsif ($this->{type} eq 'code') {
136 # New lines are treated like spaces in code.
137 3915 $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 3915 $this->{content} =~ s/^ (.*[^ ].*) $/$1/g;
141 3915 html_escape($this->{content}, $char_class_to_escape_in_code);
142 } elsif ($this->{type} eq 'link') {
143 11459 ▶ if ($this->{linktype} eq 'autolink') {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'linktype'} eq 'autolink') { } 9302 2157 11459
elsif ($this->{'linktype'} eq 'link' or $this->{'linktype'} eq 'img') { } 2157 0 2157
144 # For autolinks we don’t decode entities as these are treated like html
145 # construct.
146 9302 html_escape($this->{content}, $char_class_to_escape);
147 9302 http_escape($this->{target});
148 9302 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
12948630
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 2157 ▶ if (exists $this->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"title"}) 97 2060 2157
154 97 decode_entities($this->{title});
155 97 html_escape($this->{title}, $char_class_to_escape);
156 }
157 2157 decode_entities($this->{target});
158 2157 http_escape($this->{target});
159 2157 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
24504100880
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 177482 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 211921 ▶sub new_text { return Markdown::Perl::InlineNode->new(text => @_) }
Subroutine new_text: called CC=1 SCAR=0.0
Pod documented
189 3920 ▶sub new_code { return Markdown::Perl::InlineNode->new(code => @_) }
Subroutine new_code: called CC=1 SCAR=0.0
Pod documented
190 11464 ▶sub new_link { return Markdown::Perl::InlineNode->new(link => @_) }
Subroutine new_link: called CC=1 SCAR=0.0
Pod documented
191 24516 ▶sub new_html { return Markdown::Perl::InlineNode->new(html => @_) }
Subroutine new_html: called CC=1 SCAR=0.0
Pod undocumented
192 10088 ▶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 680174 ▶sub is_node {
Subroutine is_node: called CC=2 SCAR=7.1
Pod documented
205 680174 my ($obj) = @_;
206 680174 ▶ return blessed($obj) && $obj->isa('Markdown::Perl::InlineNode');
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Markdown::Perl::InlineNode')
!ll&&!rl&&r
0105131575043
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 65276 ▶sub is_tree {
Subroutine is_tree: called CC=2 SCAR=8.1
Pod documented
210 65276 my ($obj) = @_;
211 65276 ▶ return blessed($obj) && $obj->isa('Markdown::Perl::InlineTree');
Condition33%1/3
builtin::blessed($obj) && $obj->isa('Markdown::Perl::InlineTree')
!ll&&!rl&&r
0065276
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 484155 ▶sub push { ## no critic (ProhibitBuiltinHomonyms)
Subroutine push: called CC=4 SCAR=14.0
Pod documented
228 484155 my ($this, @nodes_or_trees) = @_;
229  
230 484155 for my $node_or_tree (@nodes_or_trees) {
231 483079 ▶ if (is_node($node_or_tree)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_node($node_or_tree)) { } 417803 65276 483079
elsif (is_tree($node_or_tree)) { } 65276 0 65276
232 417803 push @{$this->{children}}, $node_or_tree;
233 } elsif (is_tree($node_or_tree)) {
234 65276 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 484155 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 75805 ▶sub replace {
Subroutine replace: called CC=2 SCAR=7.2
Pod documented
255 75805 my ($this, $child_index, @new_nodes) = @_;
256 75805 splice @{$this->{children}}, $child_index, 1,
257 0 ▶ map { is_node($_) ? $_ : @{$_->{children}} } @new_nodes;
Branch: 1 branch
BranchTrueFalseTotal
is_node($_) ? : 134264 0 134264
258 75805 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 52422 ▶sub insert {
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
274 52422 my ($this, $index, @new_nodes) = @_;
275 52422 ▶ splice @{$this->{children}}, $index, 0, map { is_node($_) ? $_ : @{$_->{children}} } @new_nodes;
Branch: 1 branch
BranchTrueFalseTotal
is_node($_) ? : 22976 39855 62831
276 52422 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 79261 ▶sub extract {
Subroutine extract: called CC=23 SCAR=31.6
Pod documented
300 79261 my ($this, $child_start, $text_start, $child_end, $text_end) = @_;
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 79261 my $sn = $this->{children}[$child_start];
307 confess 'Start node in an extract operation is not of type text: '.$sn->{type}
308 79261 ▶ unless $sn->{type} eq 'text' || $text_start == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $sn->{"type"} eq "text" or $text_start == 0 0 79261 79261
Condition66%2/3
$sn->{"type"} eq "text" or $text_start == 0
l!l&&r!l&&!r
7925560
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 79261 my $en = $this->{children}[$child_end];
328 confess 'End node in an extract operation is not of type text: '.$en->{type}
329 79261 ▶ unless $text_end == 0 || $en->{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $text_end == 0 or $en->{"type"} eq "text" 0 79261 79261
Condition66%2/3
$text_end == 0 or $en->{"type"} eq "text"
l!l&&r!l&&!r
258790030
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 79261 ▶ confess 'Start offset is less than 0 in an extract operation' if $text_start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $text_start < 0 0 79261 79261
331 confess 'End offset is past the end of the text in an extract operation'
332 79261 ▶ if $text_end != 0 && $text_end > length($en->{content});
Branch: 1 branch
BranchTrueFalseTotal
if $text_end != 0 and $text_end > length $en->{"content"} 0 79261 79261
Condition66%2/3
$text_end != 0 and $text_end > length $en->{"content"}
!ll&&!rl&&r
258790030
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 79261 my $empty_last = 0;
335 79261 ▶ if ($text_end == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_end == 0) 258 79003 79261
336 258 $empty_last = 1;
337 258 $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 79261 map { $_->clone() } @{$this->{children}}[$child_start .. $child_end];
345 ## no critic (ProhibitLvalueSubstr)
346 79261 ▶ substr($nodes[-1]{content}, $text_end) = '' unless $empty_last; ## We have already removed the empty last node.
Branch: 1 branch
BranchTrueFalseTotal
unless $empty_last 79003 258 79261
347 79261 substr($nodes[0]{content}, 0, $text_start) = ''; # We must do this after text_end in case they are the same node.
348 79261 ▶ shift @nodes if length($nodes[0]{content}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $nodes[0]{"content"} == 0 10659 68602 79261
349 79261 ▶ 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 79261 79261
Condition83%5/6
not $empty_last and @nodes and length $nodes[-1]{"content"} == 0
!ll&&!rl&&r
10673685880
not $empty_last and @nodes
!ll&&!rl&&r
2581041568588
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 79261 my $new_tree = Markdown::Perl::InlineTree->new();
351 79261 $new_tree->push(@nodes);
352  
353 79261 ▶ if ($child_start != $child_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_start != $child_end) { } 3456 75805 79261
354 3456 ▶ if ($text_start == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_start == 0) { } 510 2946 3456
355 510 $child_start--;
356 } else {
357 2946 substr($sn->{content}, $text_start) = '';
358 }
359 3456 ▶ if ($empty_last || $text_end == length($en->{content})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty_last or $text_end == length $en->{'content'}) { } 2379 1077 3456
Condition100%3/3
$empty_last or $text_end == length $en->{"content"}
l!l&&r!l&&!r
24621331077
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 2379 $child_end++;
361 } else {
362 1077 substr($en->{content}, 0, $text_end) = '';
363 }
364 3456 splice @{$this->{children}}, $child_start + 1, $child_end - $child_start - 1;
365 } else {
366 75805 my @new_nodes;
367 75805 ▶ if ($text_start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_start > 0) 70176 5629 75805
368 70176 CORE::push @new_nodes, new_text(substr $sn->{content}, 0, $text_start);
369 }
370 75805 ▶ if (!$empty_last && $text_end < length($sn->{content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $empty_last and $text_end < length $sn->{"content"}) 64088 11717 75805
Condition100%3/3
not $empty_last and $text_end < length $sn->{"content"}
!ll&&!rl&&r
121170564088
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 64088 CORE::push @new_nodes, new_text(substr $sn->{content}, $text_end);
372 }
373 75805 $this->replace($child_start, @new_nodes);
374 75805 ▶ $child_start-- if $text_start == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $text_start == 0 5629 70176 75805
375 }
376 ## use critic (ProhibitLvalueSubstr)
377  
378 79261 ▶ return ($new_tree, $child_start + 1) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 42012 37249 79261
379 37249 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 0 ▶sub map_shallow {
Subroutine map_shallow: not called CC=3 SCAR=24.8
Pod documented
402 0 my ($this, $sub) = @_;
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 121414 ▶sub map { ## no critic (ProhibitBuiltinHomonyms)
Subroutine map: called CC=8 SCAR=20.8
Pod documented
428 121414 my ($this, $sub, $start, $stop) = @_;
429 # $start and $stop are not documented for this function, they are used by
430 # clone().
431  
432 121414 my $new_tree = Markdown::Perl::InlineTree->new();
433  
434 121414 ▶ for (@{$this->{children}}[$start // 0 .. $stop // $#{$this->{children}}]) {
Condition100%4/4
$start // 0
l!l
2710794307
$stop // $#{$$this{"children"};}
l!l
2710794307
MC/DC100%1/1
$start // 0
$start
MC/DC100%1/1
$stop // $#{$$this{"children"};}
$stop
Truth table100%2/2
A
A= $start
Aresult
11
00
Truth table100%2/2
A
A= $stop
Aresult
11
00
435 237858 ▶ if ($_->has_subtree()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->has_subtree) { } 2130 235728 237858
436 2130 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 38 2092 2130
437 38 my $new_node = $_->clone();
438 38 $new_node->{subtree}->map($sub);
439 38 local *_ = \$new_node;
440 38 $new_tree->push($sub->());
441 } else {
442 # Is there a risk that this modifies $_ before the call to $sub?
443 2092 $_->{subtree}->map($sub);
444 2092 $new_tree->push($sub->());
445 }
446 } else {
447 235728 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 29007 206721 235728
448 29007 my $new_node = $_->clone();
449 29007 local *_ = \$new_node;
450 29007 $new_tree->push($sub->());
451 } else {
452 206721 $new_tree->push($sub->());
453 }
454 }
455 }
456  
457 121414 ▶ return $new_tree if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 27107 94307 121414
458 94307 %{$this} = %{$new_tree};
459 94307 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 56298 ▶sub apply {
Subroutine apply: called CC=3 SCAR=11.0
Pod documented
474 56298 my ($this, $sub) = @_;
475  
476 56298 for (@{$this->{children}}) {
477 177482 $sub->();
478 177482 ▶ $_->{subtree}->apply($sub) if $_->has_subtree();
Branch: 1 branch
BranchTrueFalseTotal
if $_->has_subtree 12245 165237 177482
479 }
480  
481 56298 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 27107 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
495 27107 my ($this, $start, $stop) = @_;
496 27107 ▶ 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 85396 ▶sub fold {
Subroutine fold: called CC=2 SCAR=6.9
Pod documented
515 85396 my ($this, $sub, $init) = @_;
516  
517 85396 my $out = $init;
518  
519 85396 for (@{$this->{children}}) {
520 200199 $out = $sub->($_, $out);
521 }
522  
523 85396 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 90329 ▶sub find_in_text {
Subroutine find_in_text: called CC=1 SCAR=0.0
Pod documented
547 90329 my ($this, $re, $child_start, $text_start, $child_bound, $text_bound) = @_;
548 # qr/^\b$/ is a regex that can’t match anything.
549 90329 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 90385 ▶sub find_balanced_in_text {
Subroutine find_balanced_in_text: called CC=12 SCAR=24.8
Pod documented
568 90385 my ($this, $open_re, $close_re, $child_start, $text_start, $child_bound, $text_bound) = @_;
569  
570 90385 my $open = 1;
571  
572 90385 ▶ for my $i ($child_start .. ($child_bound // $#{$this->{children}})) {
Condition100%2/2
$child_bound // $#{$$this{"children"};}
l!l
367586710
MC/DC100%1/1
$child_bound // $#{$$this{"children"};}
$child_bound
Truth table100%2/2
A
A= $child_bound
Aresult
11
00
573 157068 ▶ next unless $this->{children}[$i]{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"children"}[$i]{"type"} eq "text" 40137 116931 157068
574 116931 ▶ if ($i == $child_start && $text_start != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $child_start and $text_start != 0) { } 4318 112613 116931
Condition100%3/3
$i == $child_start and $text_start != 0
!ll&&!rl&&r
35150774634318
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 4318 pos($this->{children}[$i]{content}) = $text_start;
576 } else {
577 112613 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 116931 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"children"}[$i]{"content"} =~ / $open_re(?{ $open++; }) | $close_re(?{ $open--; }) /gux) 40766 76184 116950
583 12 $this->{children}[$i]{content} =~ m/ ${open_re}(?{$open++}) | ${close_re}(?{$open--}) /gx)
584 {
585 40766 ▶ 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 358 40408 40766
Condition100%5/5
$i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
!ll&&!rl&&r
4036444358
$child_bound // -1
l!l
40740359
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 40408 ▶ if ($open == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open == 0) 40389 19 40408
587 40389 ▶ return ($i, $LAST_MATCH_START[0], $LAST_MATCH_END[0]) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 40341 48 40389
588 48 return 1;
589 }
590 }
591 }
592  
593 49638 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 3521 ▶sub find_in_text_with_balanced_content {
Subroutine find_in_text_with_balanced_content: called CC=13 SCAR=25.9
Pod documented
614 3521 my ($this, $open_re, $close_re, $end_re, $child_start, $text_start, $child_bound, $text_bound) =
615 @_;
616  
617 3521 my $open = 0;
618  
619 3521 ▶ for my $i ($child_start .. ($child_bound // $#{$this->{children}})) {
Condition50%1/2
$child_bound // $#{$$this{"children"};}
l!l
03521
MC/DC0%0/1
$child_bound // $#{$$this{"children"};}
$child_bound
Truth table50%1/2
A
A= $child_bound
Aresult
11
00
620 4037 ▶ next unless $this->{children}[$i]{type} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"children"}[$i]{"type"} eq "text" 262 3775 4037
621 3775 ▶ if ($i == $child_start && $text_start != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $child_start and $text_start != 0) { } 3519 256 3775
Condition100%3/3
$i == $child_start and $text_start != 0
!ll&&!rl&&r
25423519
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 3519 pos($this->{children}[$i]{content}) = $text_start;
623 } else {
624 256 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 3775 my $done = 0;
631 3775 ▶ 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) 4175 855 5030
632 1615 m/ ${end_re}(?{$done = 1}) | ${open_re}(?{$open++}) | ${close_re}(?{$open--}) /gx) {
633 4175 ▶ 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 4175 4175
Condition40%2/5
$i == ($child_bound // -1) and $Markdown::Perl::InlineTree::LAST_MATCH_START[0] >= $text_bound
!ll&&!rl&&r
417500
$child_bound // -1
l!l
04175
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 4175 ▶ 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 2920 1255 4175
Condition100%6/6
$open == 0 and $done or $open < 0
l!l&&r!l&&!r
100819121255
$open == 0 and $done
!ll&&!rl&&r
29801871008
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 1255 $done = 0;
636 }
637 }
638  
639 601 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 55323 ▶sub render_html {
Subroutine render_html: called CC=1 SCAR=0.0
Pod documented
653 55323 my ($tree) = @_;
654 55323 return $tree->fold(\&render_node_html, '');
655 }
656  
657 175317 ▶sub render_node_html {
Subroutine render_node_html: called CC=14 SCAR=26.5
Pod undocumented
658 175317 my ($n, $acc) = @_;
659  
660 175317 ▶ confess 'Node should already be escaped when calling render_html' unless $n->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"escaped"} 0 175317 175317
661  
662 175317 ▶ 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') { } 150114 25203 175317
elsif ($n->{'type'} eq 'code') { } 3871 21332 25203
elsif ($n->{'type'} eq 'link') { } 11342 9990 21332
elsif ($n->{'type'} eq 'style') { } 9990 0 9990
Condition100%6/6
$n->{"type"} eq "text" or $n->{"type"} eq "literal" or $n->{"type"} eq "html"
l!l&&r!l&&!r
1259582415625203
$n->{"type"} eq "text" or $n->{"type"} eq "literal"
l!l&&r!l&&!r
1055522040649359
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 150114 return $acc.$n->{content};
664 } elsif ($n->{type} eq 'code') {
665 3871 return $acc.'<code>'.$n->{content}.'</code>';
666 } elsif ($n->{type} eq 'link') {
667 11342 my $title = '';
668 11342 ▶ if (exists $n->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $n->{"title"}) 97 11245 11342
669 97 $title = " title=\"$n->{title}\"";
670 }
671 11342 ▶ if ($n->{linktype} eq 'link' || $n->{linktype} eq 'autolink') {
Branch: 2 branches
BranchTrueFalseTotal
if ($n->{'linktype'} eq 'link' or $n->{'linktype'} eq 'autolink') { } 10496 846 11342
elsif ($n->{'linktype'} eq 'img') { } 846 0 846
Condition100%3/3
$n->{"linktype"} eq "link" or $n->{"linktype"} eq "autolink"
l!l&&r!l&&!r
12819215846
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 10496 ▶ my $content = exists $n->{content} ? $n->{content} : $n->{subtree}->render_html();
Branch: 1 branch
BranchTrueFalseTotal
exists $n->{'content'} ? : 9216 1280 10496
676 10496 return $acc."<a href=\"$n->{target}\"${title}>${content}</a>";
677 } elsif ($n->{linktype} eq 'img') {
678 846 my $content = $n->{subtree}->to_text();
679 846 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 9990 my $content = $n->{subtree}->render_html();
685 9990 ▶ if ($n->{tag} =~ m/^\.(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{'tag'} =~ /^\.(.*)$/u) { } 1 9989 9990
686 1 my $class = $1;
687 1 return $acc."<span class=\"${class}\">${content}</span>";
688 } else {
689 9989 my $tag = $n->{tag};
690 9989 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 974 ▶sub to_text {
Subroutine to_text: called CC=1 SCAR=0.0
Pod documented
710 974 my ($tree) = @_;
711 974 return $tree->fold(\&node_to_text, '');
712 }
713  
714 2164 ▶sub node_to_text {
Subroutine node_to_text: called CC=9 SCAR=22.1
Pod undocumented
715 2164 my ($n, $acc) = @_;
716 2164 ▶ confess 'Node should already be escaped when calling to_text' unless $n->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"escaped"} 0 2164 2164
717 2164 ▶ if ($n->{type} eq 'text') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n->{'type'} eq 'text') { } 1343 821 2164
elsif ($n->{'type'} eq 'style') { } 98 723 821
elsif ($n->{'type'} eq 'literal' or $n->{'type'} eq 'html') { } 562 161 723
elsif ($n->{'type'} eq 'link') { } 117 44 161
elsif ($n->{'type'} eq 'code') { } 44 0 44
718 1343 return $acc.$n->{content};
719 } elsif ($n->{type} eq 'style') {
720 98 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
214348161
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 562 return $acc.$n->{content};
726 } elsif ($n->{type} eq 'link') {
727 117 ▶ if ($n->{linktype} ne 'autolink') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{'linktype'} ne 'autolink') { } 30 87 117
728 30 return $acc.$n->{subtree}->to_text();
729 } else {
730 87 return $acc.$n->{content};
731 }
732 } elsif ($n->{type} eq 'code') {
733 44 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 29095 ▶sub to_source_text {
Subroutine to_source_text: called CC=1 SCAR=0.0
Pod documented
765 29095 my ($tree, $unescape_literal) = @_;
766 29095 return $tree->fold(node_to_source_text($unescape_literal), '');
767 }
768  
769 29095 ▶sub node_to_source_text {
Subroutine node_to_source_text: called CC=1 SCAR=0.0
Pod undocumented
770 29095 my ($unescape_literal) = @_;
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 22709 ▶ return sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
778 22709 my ($n, $acc) = @_;
779 22709 ▶ confess 'Node should not already be escaped when calling to_source_text' if $n->{escaped};
Branch: 1 branch
BranchTrueFalseTotal
if $n->{"escaped"} 0 22709 22709
780 22709 ▶ if ($n->{type} eq 'text') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n->{'type'} eq 'text') { } 21675 1034 22709
elsif ($n->{'type'} eq 'literal' and $unescape_literal) { } 8 1026 1034
elsif ($n->{'type'} eq 'literal' or $n->{'type'} eq 'html') { } 577 449 1026
elsif ($n->{'type'} eq 'code') { } 133 316 449
elsif ($n->{'type'} eq 'link') { } 316 0 316
781 21675 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
1006208
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
20557449
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 577 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 133 return $acc.'<code>'.$n->{content}.'</code>';
789 } elsif ($n->{type} eq 'link') {
790 316 ▶ if ($n->{linktype} eq 'autolink') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->{'linktype'} eq 'autolink') { } 278 38 316
791 278 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 38 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 29095 };
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 27107 ▶sub span_to_source_text {
Subroutine span_to_source_text: called CC=1 SCAR=0.0
Pod documented
817 27107 my ($tree, $child_start, $text_start, $child_end, $text_end, $unescape_literal) = @_;
818 27107 my $copy = $tree->clone($child_start, $child_end);
819 27107 my $extract = $copy->extract(0, $text_start, $child_end - $child_start, $text_end);
820 27107 return $extract->to_source_text($unescape_literal);
821 }
822  
823 32 1;
« Markdown/Perl/BlockParser.pm ↑ Summary Markdown/Perl/Inlines.pm »