lib/Markdown/Parser/Element.pm

filter: Statementstmt 33.4% 102 / 305 Branchbran 18.8% 26 / 138 Conditioncond 7.8% 3 / 38 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 33.7% 30 / 89 Podpod 95.7% 67 / 70 totaltotal 34.4% 228 / 662 CC 86 SCAR 39.8
CC
86
Cov
27%
__ANON__
380.0
add_attributes
72.0
remove
42.0
CRAP 53.5
« blib/lib/Markdown/Parser/Link.pm ↑ Summary blib/lib/Markdown/Parser/Element.pm »
1 ##----------------------------------------------------------------------------
2 ## Markdown Parser Only - ~/lib/Markdown/Parser/Element.pm
3 ## Version v0.2.1
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/08/23
7 ## Modified 2022/09/22
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Markdown::Parser::Element;
14 BEGIN
15 {
16 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use Nice::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use CSS::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶ our $VERSION = 'v0.2.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 };
26  
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub init
31 {
32 19 ▶ my $self = shift( @_ );
Subroutine init: called CC=14 SCAR=27.8
Pod documented
33 ## print( STDERR ref( $self ), "::new: Markdown::Parser::Element::init() Parameters provided are: '", join( "', '", @_ ), "'\n" );
34 19 my $opts = {};
35 19 ▶ $opts = shift( @_ ) if( $self->_is_hash( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_hash($_[0]) 15 4 19
36 19 ▶ if( !( scalar( @_ ) % 2 ) && scalar( keys( %$opts ) ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not scalar(@_) % 2 and scalar keys %$opts) { } 15 4 19
elsif (not scalar(@_) % 2) { } 4 0 4
Condition50%1/2
not scalar(@_) % 2 and scalar keys %$opts
!ll
019
MC/DC0%0/1
not scalar(@_) % 2 and scalar keys %$opts
not scalar(@_) % 2
Truth table50%1/2
not(A)
A= not scalar(@_) % 2
Aresult
00
11
37 {
38 15 my $hash = { @_ };
39 15 my @keys = keys( %$hash );
40 15 @$opts{ @keys } = @$hash{ @keys };
41 }
42 elsif( !( scalar( @_ ) % 2 ) )
43 {
44 4 $opts = { @_ };
45 }
46 19 ▶ $self->{attr} = {} unless( CORE::exists( $self->{attr} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"attr"} 19 0 19
47 19 ▶ $self->{children} = [] unless( CORE::exists( $self->{children} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"children"} 19 0 19
48 19 ▶ $self->{class} = [] unless( CORE::exists( $self->{class} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"class"} 19 0 19
49 ## To contain the CSS::Object
50 19 ▶ $self->{css} = '' unless( CORE::exists( $self->{css} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"css"} 19 0 19
51 ## Dictionary hash object of abbreviations
52 19 ▶ $self->{dict} = {} unless( CORE::exists( $self->{dict} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"dict"} 18 1 19
53 19 ▶ $self->{parent} = '' unless( CORE::exists( $self->{parent} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"parent"} 19 0 19
54 19 ▶ $self->{pos} = '' unless( CORE::exists( $self->{pos} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"pos"} 19 0 19
55 19 ▶ $self->{raw} = '' unless( CORE::exists( $self->{raw} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"raw"} 19 0 19
56 19 ▶ $self->{tag_name} = '' unless( CORE::exists( $self->{tag_name} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"tag_name"} 0 19 19
57 19 $self->{_init_strict_use_sub} = 1;
58 ## print( STDERR ref( $self ), "::new: Calling SUPER::init with: ", $self->dump( $opts ) );
59 19 return( $self->SUPER::init( $opts ) );
60 ## $self->SUPER::init( $opts );
61 ## print( STDERR ref( $self ), "::new: Debug value: ", $self->debug, "\n" );
62 ## return( $self );
63 }
64  
65 sub add_attributes
66 {
67 0 ▶ my $self = shift( @_ );
Subroutine add_attributes: not called CC=8 SCAR=42.8
Pod documented
68 ## A string something like {#id1} or {.cl} or {#id.cl.class}
69 0 my $def = shift( @_ );
70 0 ▶ return if( !length( $def ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $def 0 0 0
71 0 my( @ids, @classes, @attributes );
72 0 ▶ while( $def =~ s/
Branch: 1 branch
BranchTrueFalseTotal
if ($def =~ s/ [[:blank:]\h]* (?<attr_name>[a-zA-Z][\w\-]+) [[:blank:]\h]* = [[:blank:]\h]* (?<quote>["'])? (?(<quote>) (?:(?<attr_value>.*?)\g{quote}) | (?<attr_value>\S+) ) //sx) 0 0 0
73 [[:blank:]\h]*
74 (?<attr_name>[a-zA-Z][\w\-]+)
75 [[:blank:]\h]*
76 =
77 [[:blank:]\h]*
78 (?<quote>["'])?
79 (?(<quote>)
80 (?:(?<attr_value>.*?)\g{quote})
81 |
82 (?<attr_value>\S+)
83 )
84 //xs )
85 {
86 0 push( @attributes, [ $+{attr_name} => $+{attr_value} ] );
87 }
88 0 ▶ while( $def =~ s/[[:blank:]\h]*\#(?<id>[^[:space:]\h\.\#]+)//s )
Branch: 1 branch
BranchTrueFalseTotal
if ($def =~ s/[[:blank:]\h]*\#(?<id>[^[:space:]\h\.\#]+)//s) 0 0 0
89 {
90 0 push( @ids, $+{id} );
91 }
92 0 ▶ while( $def =~ s/[[:blank:]\h]*\.(?<class>[^[:space:]\h\.\#]+)//s )
Branch: 1 branch
BranchTrueFalseTotal
if ($def =~ s/[[:blank:]\h]*\.(?<class>[^[:space:]\h\.\#]+)//s) 0 0 0
93 {
94 0 push( @classes, $+{class} );
95 }
96 0 $self->id->push( @ids );
97 0 ▶ $self->class->push( @classes ) if( scalar( @classes ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @classes 0 0 0
98 0 ▶ if( scalar( @attributes ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @attributes) 0 0 0
99 {
100 0 foreach my $ref ( @attributes )
101 {
102 ## If the user had defined it twice, it might very well override previous data set
103 0 $self->attributes->set( $ref->[0] => $ref->[1] );
104 }
105 }
106 0 return( $self );
107 }
108  
109 sub add_element
110 {
111 18 ▶ my $self = shift( @_ );
Subroutine add_element: called CC=2 SCAR=7.0
Pod documented
112 18 my $obj = shift( @_ );
113 18 my $base = $self->base_class;
114 18 ▶ return( $self->error( "Element provided \"$obj\" is not an Markdown::Parser::Element object." ) ) if( !$self->_is_a( $obj, "${base}::Element" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($obj, "${base}::Element") 0 18 18
115 18 $obj->parent( $self );
116 18 $self->children->push( $obj );
117 18 return( $self );
118 }
119  
120 sub add_to
121 {
122 0 ▶ my $self = shift( @_ );
Subroutine add_to: not called CC=2 SCAR=17.9
Pod documented
123 0 my $obj = shift( @_ );
124 0 ▶ return( $self->error( "Element provided \"$obj\" is not an Markdown::Parser::Element object." ) ) if( !$self->_is_a( $obj, 'Markdown::Parser::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($obj, "Markdown::Parser::Element") 0 0 0
125 0 $obj->children->push( $self );
126 0 return( $self );
127 }
128  
129 sub as_markdown
130 {
131 0 ▶ my $self = shift( @_ );
Subroutine as_markdown: not called CC=1 SCAR=6.9
Pod documented
132 0 warn( "as_markdown method not implemented for ", ref( $self ), "\n" );
133 0 return;
134 }
135  
136 sub as_pod
137 {
138 0 ▶ my $self = shift( @_ );
Subroutine as_pod: not called CC=1 SCAR=6.9
Pod documented
139 0 warn( "as_pod method not implemented for ", ref( $self ), "\n" );
140 0 return;
141 }
142  
143 sub as_string
144 {
145 0 ▶ my $self = shift( @_ );
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
146 0 warn( "as_string method not implemented for ", ref( $self ), "\n" );
147 0 return;
148 }
149  
150 0 ▶sub attr { return( shift->_set_get_hash_as_mix_object( 'attr', @_ ) ); }
Subroutine attr: not called CC=1 SCAR=6.9
Pod documented
151  
152 # Alias
153 0 ▶sub attributes { return( shift-> _set_get_hash_as_mix_object( 'attr', @_ ) ); }
Subroutine attributes: not called CC=1 SCAR=6.9
Pod documented
154  
155 sub base_class
156 {
157 37 ▶ my $self = shift( @_ );
Subroutine base_class: called CC=2 SCAR=7.4
Pod documented
158 37 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
3700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
159 37 my @frag = split( /\::/, $class );
160 37 return( join( '::', @frag[ 0..( $#frag-1 ) ] ) );
161 }
162  
163 24 ▶sub children { return( shift->_set_get_array_as_object( 'children', @_ ) ); }
Subroutine children: called CC=1 SCAR=0.0
Pod documented
164  
165 1 ▶sub class { return( shift->_set_get_array_as_object( 'class', @_ ) ); }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
166  
167 # Returns the closest non-new line element or the one specified
168 sub closest
169 {
170 0 ▶ my $self = shift( @_ );
Subroutine closest: not called CC=2 SCAR=17.9
Pod documented
171 0 my $target;
172 0 ▶ $target = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
173 0 my $elem;
174 # Starting from the latest to the oldest
175 $self->children->reverse->foreach(sub
176 {
177 0 ▶ return(1) if( $_->tag_name eq 'nl' );
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if $_->tag_name eq "nl" 0 0 0
178 # We exit (return false) after the first element we found if there is no target specified
179 0 ▶ if( !defined( $target ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $target or defined $target and $_->tag_name eq $target) 0 0 0
Condition0%0/6
not defined $target or defined $target and $_->tag_name eq $target
l!l&&r!l&&!r
000
defined $target and $_->tag_name eq $target
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $target or defined $target and $_->tag_name eq $target
not defined $target defined $target $_->tag_name eq $target
Truth table0%0/4
not(A) or B and C
A= not defined $targetB= defined $targetC= $_->tag_name eq $target
ABCresult
1XX1
0111
00X0
0100
180 ( defined( $target ) && $_->tag_name eq $target ) )
181 {
182 0 $elem = $_;
183 0 return;
184 }
185 0 return(1);
186 0 });
187 0 return( $elem );
188 }
189  
190 0 ▶sub create_abbreviation { return( shift->_create_element( 'Abbr', @_ ) ); }
Subroutine create_abbreviation: not called CC=1 SCAR=6.9
Pod documented
191  
192 0 ▶sub create_blockquote { return( shift->_create_element( 'Blockquote', @_ ) ); }
Subroutine create_blockquote: not called CC=1 SCAR=6.9
Pod documented
193  
194 0 ▶sub create_bold { return( shift->_create_element( 'Bold', @_ ) ); }
Subroutine create_bold: not called CC=1 SCAR=6.9
Pod documented
195  
196 0 ▶sub create_checkbox { return( shift->_create_element( 'Checkbox', @_ ) ); }
Subroutine create_checkbox: not called CC=1 SCAR=6.9
Pod documented
197  
198 1 ▶sub create_code { return( shift->_create_element( 'Code', @_ ) ); }
Subroutine create_code: called CC=1 SCAR=0.0
Pod documented
199  
200 0 ▶sub create_em { return( shift->_create_element( 'Emphasis', @_ ) ); }
Subroutine create_em: not called CC=1 SCAR=6.9
Pod documented
201  
202 0 ▶sub create_footnote { return( shift->_create_element( 'Footnote', @_ ) ); }
Subroutine create_footnote: not called CC=1 SCAR=6.9
Pod documented
203  
204 0 ▶sub create_footnote_ref { return( shift->_create_element( 'FootnoteReference', @_ ) ); }
Subroutine create_footnote_ref: not called CC=1 SCAR=6.9
Pod documented
205  
206 0 ▶sub create_header { return( shift->_create_element( 'Header', @_ ) ); }
Subroutine create_header: not called CC=1 SCAR=6.9
Pod documented
207  
208 0 ▶sub create_image { return( shift->_create_element( 'Image', @_ ) ); }
Subroutine create_image: not called CC=1 SCAR=6.9
Pod documented
209  
210 1 ▶sub create_html { return( shift->_create_element( 'HTML', @_ ) ); }
Subroutine create_html: called CC=1 SCAR=0.0
Pod documented
211  
212 1 ▶sub create_insertion { return( shift->_create_element( 'Insertion', @_ ) ); }
Subroutine create_insertion: called CC=1 SCAR=0.0
Pod documented
213  
214 0 ▶sub create_line { return( shift->_create_element( 'Line', @_ ) ); }
Subroutine create_line: not called CC=1 SCAR=6.9
Pod documented
215  
216 0 ▶sub create_link_definition { return( shift->_create_element( 'LinkDefinition', @_ ) ); }
Subroutine create_link_definition: not called CC=1 SCAR=6.9
Pod documented
217  
218 1 ▶sub create_link { return( shift->_create_element( 'Link', @_ ) ); }
Subroutine create_link: called CC=1 SCAR=0.0
Pod documented
219  
220 0 ▶sub create_list { return( shift->_create_element( 'List', @_ ) ); }
Subroutine create_list: not called CC=1 SCAR=6.9
Pod documented
221  
222 0 ▶sub create_list_item { return( shift->_create_element( 'ListItem', @_ ) ); }
Subroutine create_list_item: not called CC=1 SCAR=6.9
Pod documented
223  
224 0 ▶sub create_new_line { return( shift->_create_element( 'NewLine', @_ ) ); }
Subroutine create_new_line: not called CC=1 SCAR=6.9
Pod documented
225  
226 1 ▶sub create_paragraph { return( shift->_create_element( 'Paragraph', @_ ) ); }
Subroutine create_paragraph: called CC=1 SCAR=0.0
Pod documented
227  
228 1 ▶sub create_strikethrough { return( shift->_create_element( 'StrikeThrough', @_ ) ); }
Subroutine create_strikethrough: called CC=1 SCAR=0.0
Pod documented
229  
230 0 ▶sub create_subscript { return( shift->_create_element( 'Subscript', @_ ) ); }
Subroutine create_subscript: not called CC=1 SCAR=6.9
Pod documented
231  
232 0 ▶sub create_superscript { return( shift->_create_element( 'Superscript', @_ ) ); }
Subroutine create_superscript: not called CC=1 SCAR=6.9
Pod documented
233  
234 0 ▶sub create_table { return( shift->_create_element( 'Table', @_ ) ); }
Subroutine create_table: not called CC=1 SCAR=6.9
Pod documented
235  
236 0 ▶sub create_table_body { return( shift->_create_element( 'TableBody', @_ ) ); }
Subroutine create_table_body: not called CC=1 SCAR=6.9
Pod documented
237  
238 0 ▶sub create_table_caption { return( shift->_create_element( 'TableCaption', @_ ) ); }
Subroutine create_table_caption: not called CC=1 SCAR=6.9
Pod documented
239  
240 0 ▶sub create_table_cell { return( shift->_create_element( 'TableCell', @_ ) ); }
Subroutine create_table_cell: not called CC=1 SCAR=6.9
Pod documented
241  
242 0 ▶sub create_table_column { return( shift->_create_element( 'TableColumn', @_ ) ); }
Subroutine create_table_column: not called CC=1 SCAR=6.9
Pod documented
243  
244 0 ▶sub create_table_header { return( shift->_create_element( 'TableHeader', @_ ) ); }
Subroutine create_table_header: not called CC=1 SCAR=6.9
Pod documented
245  
246 0 ▶sub create_table_row { return( shift->_create_element( 'TableRow', @_ ) ); }
Subroutine create_table_row: not called CC=1 SCAR=6.9
Pod documented
247  
248 12 ▶sub create_text { return( shift->_create_element( 'Text', @_ ) ); }
Subroutine create_text: called CC=1 SCAR=0.0
Pod documented
249  
250 0 ▶sub css { return( shift->_set_get_object( 'css', 'CSS::Object', @_ ) ); }
Subroutine css: not called CC=1 SCAR=6.9
Pod documented
251  
252 sub css_inline
253 {
254 0 ▶ return( CSS::Object->new( format => 'CSS::Object::Format::Inline' ) );
Subroutine css_inline: not called CC=1 SCAR=6.9
Pod documented
255 }
256  
257 sub detach
258 {
259 0 ▶ my $self = shift( @_ );
Subroutine detach: not called CC=1 SCAR=6.9
Pod documented
260 0 my $arr = $self->children->clone;
261 0 $self->children->reset;
262 0 return( $arr );
263 }
264  
265 18 ▶sub document { return( shift->_set_get_object( 'document', 'Markdown::Parser::Document', @_ ) ); }
Subroutine document: called CC=1 SCAR=0.0
Pod documented
266  
267 sub empty
268 {
269 0 ▶ my $self = shift( @_ );
Subroutine empty: not called CC=1 SCAR=6.9
Pod documented
270 0 $self->children->reset;
271 0 return( $self );
272 }
273  
274 sub encode_html
275 {
276 1 ▶ my $self = shift( @_ );
Subroutine encode_html: called CC=9 SCAR=23.5
Pod documented
277 1 my( $what, $text ) = @_;
278 1 ▶ return if( !defined( $text ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
279 1 my $map =
280 {
281 '<' => '&lt;',
282 '>' => '&gt;',
283 ## Also &#38;
284 '&' => '&amp;',
285 ## Also &#63;
286 '?' => '&quest;',
287 ## Also known as &num;
288 '#' => '&#35;',
289 '"' => '&quot;',
290 "'" => '&#39;',
291 };
292 1 ▶ my $data = $self->_is_scalar( $text ) ? $text : \$text;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($text) ? : 1 0 1
293 1 ▶ $what = [keys( %$map )] if( $what eq 'all' );
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "all" 0 1 1
294 1 ▶ return( $self->error( "List of items to encode is not an array reference." ) ) if( !$self->_is_array( $what ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_array($what) 0 1 1
295 1 ▶ return( $$data ) if( !scalar( @$what ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$what 0 1 1
296 ## return( $self->error( "Text to encode is not a scalar reference." ) ) if( !$self->_is_scalar( $text ) );
297 1 my $todo = {};
298 ## Make a hash of characters to encode
299 1 @$todo{ @$what } = ( 1 ) x scalar( @$what );
300 1 ▶ if( my $amp = delete( $todo->{ '&' } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $amp = delete $todo->{"&"}) 1 0 1
301 {
302 ## 1 while( $$data =~ s/(?<!\\)(\&)(?!\d+\;)/$map->{ $1 }/gs );
303 1 $$data =~ s/(?<!\\)(\&)(?!(?:amp|gt|lt)\;)/$map->{ $1 }/gs;
304 }
305 1 ▶ if( my $amp = delete( $todo->{ '#' } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $amp = delete $todo->{"#"}) 0 1 1
306 {
307 ## 1 while( $$data =~ s/(?<!\\)(\#)(?!(?:amp|gt|lt)\;)/$map->{ $1 }/gs );
308 0 $$data =~ s/(?:(?<!\\)|(?<!\&))(\#)(?!\d+\;)/$map->{ $1 }/gs;
309 }
310 1 ▶ if( scalar( %$todo ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %$todo) 1 0 1
311 {
312 1 my $re = join( '|', map( quotemeta( $_ ), keys( %$todo ) ) );
313 ## 1 while( $$data =~ s/(?<!\\)($re)/$map->{ $1 }/gs );
314 1 $$data =~ s/(?<!\\)($re)/$map->{ $1 }/gs;
315 }
316 # return( $self );
317 1 return( $$data );
318 }
319  
320 sub extract_links
321 {
322 0 ▶ my $self = shift( @_ );
Subroutine extract_links: not called CC=1 SCAR=6.9
Pod documented
323 # my $links = $self->new_array;
324 # $self->children->for(sub
325 # {
326 # my( $i, $link ) = @_;
327 # if( $self->_is_a( $link => 'Markdown::Parser::Link' ) )
328 # {
329 # $links->push( $link );
330 # }
331 # if( $link->children->length > 0 )
332 # {
333 # my $more_links = $link->extract_links;
334 # $links->push( $more_links->list ) if( $more_links->length );
335 # }
336 # });
337 0 my $links = $self->look_down( tag => 'link' );
338 0 return( $links );
339 }
340  
341 sub format_attributes
342 {
343 0 ▶ my $self = shift( @_ );
Subroutine format_attributes: not called CC=2 SCAR=17.9
Pod documented
344 0 my $attr = $self->attributes;
345 ## Return an empty array if there is nothing
346 0 ▶ return( $self->new_array ) if( !$attr->length );
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->length 0 0 0
347 0 ▶ return( $attr->map_array(sub{ sprintf( '%s="%s"', $_, $attr->{ $_ } ) }) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 }
349  
350 sub format_class
351 {
352 0 ▶ my $self = shift( @_ );
Subroutine format_class: not called CC=2 SCAR=17.9
Pod documented
353 0 ▶ if( $self->class->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->length) 0 0 0
354 {
355 0 return( sprintf( 'class="%s"', $self->class->join( ' ' )->scalar ) );
356 }
357 0 return( '' );
358 }
359  
360 sub format_id
361 {
362 0 ▶ my $self = shift( @_ );
Subroutine format_id: not called CC=2 SCAR=17.9
Pod documented
363 0 ▶ if( $self->id->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id->length) 0 0 0
364 {
365 0 return( sprintf( 'id="%s"', $self->id->join( ' ' )->scalar ) );
366 }
367 0 return( '' );
368 }
369  
370 1 ▶sub id { return( shift->_set_get_array_as_object( 'id', @_ ) ); }
Subroutine id: called CC=1 SCAR=0.0
Pod documented
371  
372 sub insert_after
373 {
374 0 ▶ my $self = shift( @_ );
Subroutine insert_after: not called CC=5 SCAR=34.0
Pod documented
375 0 my $elem = shift( @_ );
376 0 ▶ return if( !length( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $elem 0 0 0
377 0 my $base_class = $self->base_class;
378 0 ▶ return( $self->error( "Element provided is not ${base_class}::Element object." ) ) if( !$self->_is_a( "${base_class}::Element", $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a("${base_class}::Element", $elem) 0 0 0
379 0 ▶ return if( !$self->parent );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent 0 0 0
380 0 my $children = $self->parent->children;
381 0 my $pos = $children->pos( $self );
382 0 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
383 0 $children->splice( $pos + 1, 0, $elem );
384 0 return( $elem );
385 }
386  
387 sub insert_before
388 {
389 0 ▶ my $self = shift( @_ );
Subroutine insert_before: not called CC=5 SCAR=34.0
Pod documented
390 0 my $elem = shift( @_ );
391 0 ▶ return if( !length( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $elem 0 0 0
392 0 my $base_class = $self->base_class;
393 0 ▶ return( $self->error( "Element provided is not ${base_class}::Element object." ) ) if( !$self->_is_a( "${base_class}::Element", $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a("${base_class}::Element", $elem) 0 0 0
394 0 ▶ return if( !$self->parent );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent 0 0 0
395 0 my $children = $self->parent->children;
396 0 my $pos = $children->pos( $self );
397 0 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
398 0 $children->splice( $pos, 0, $elem );
399 0 return( $elem );
400 }
401  
402 sub look
403 {
404 0 ▶ my $self = shift( @_ );
Subroutine look: not called CC=4 SCAR=30.0
Pod undocumented
405 0 my $opts = $self->_get_args_as_hash( @_ );
406 0 ▶ $opts->{direction} //= 'down';
Condition0%0/2
$opts->{"direction"} //= "down"
l!l
00
MC/DC0%0/1
$opts->{"direction"} //= "down"
$opts->{"direction"}
Truth table0%0/2
A
A= $opts->{"direction"}
Aresult
11
00
407 0 ▶ unless( $opts->{direction} eq 'down' ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"direction"} eq "down" or $opts->{"direction"} eq "up") 0 0 0
Condition0%0/3
$opts->{"direction"} eq "down" or $opts->{"direction"} eq "up"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"direction"} eq "down" or $opts->{"direction"} eq "up"
$opts->{"direction"} eq "down" $opts->{"direction"} eq "up"
Truth table0%0/3
A or B
A= $opts->{"direction"} eq "down"B= $opts->{"direction"} eq "up"
ABresult
1X1
011
000
408 $opts->{direction} eq 'up' )
409 {
410 0 return( $self->error( "Unknown direction provided '$opts->{direction}'" ) );
411 }
412 0 my $a = $self->new_array;
413 0 my( $check_elem, $crawl_down );
414 $check_elem = sub
415 {
416 0 ▶ my $e = shift( @_ );
Subroutine __ANON__: not called CC=19 SCAR=59.4
417 0 my $def = shift( @_ );
418 0 ▶ $def->{level} //= 0;
Condition0%0/2
$def->{"level"} //= 0
l!l
00
MC/DC0%0/1
$def->{"level"} //= 0
$def->{"level"}
Truth table0%0/2
A
A= $def->{"level"}
Aresult
11
00
419 # Assume not ok, then check otherwise
420 0 my $ok = 0;
421 0 ▶ if( defined( $opts->{tag} ) && length( $opts->{tag} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"tag"} and length $opts->{"tag"}) 0 0 0
Condition0%0/3
defined $opts->{"tag"} and length $opts->{"tag"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"tag"} and length $opts->{"tag"}
defined $opts->{"tag"} length $opts->{"tag"}
Truth table0%0/3
A and B
A= defined $opts->{"tag"}B= length $opts->{"tag"}
ABresult
0X0
100
111
422 {
423 0 ▶ if( ref( $opts->{tag} ) eq 'Regexp' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts->{'tag'} eq 'Regexp') { } 0 0 0
424 {
425 0 ▶ $ok = 1 if( $e->tag_name =~ /$opts->{tag}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $e->tag_name =~ /$opts->{"tag"}/ 0 0 0
426 }
427 else
428 {
429 0 ▶ $ok = 1 if( $e->tag_name eq $opts->{tag} );
Branch: 1 branch
BranchTrueFalseTotal
if $e->tag_name eq $opts->{"tag"} 0 0 0
430 }
431 }
432 0 ▶ if( defined( $opts->{class} ) && length( $opts->{class} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"class"} and length $opts->{"class"}) 0 0 0
Condition0%0/3
defined $opts->{"class"} and length $opts->{"class"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts->{"class"} and length $opts->{"class"}
defined $opts->{"class"} length $opts->{"class"}
Truth table0%0/3
A and B
A= defined $opts->{"class"}B= length $opts->{"class"}
ABresult
0X0
100
111
433 {
434 0 ▶ $ok = $self->_is_a( $e => $opts->{class} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_a($e, $opts->{'class'}) ? : 0 0 0
435 }
436
437 # We passed all checks, no checking our children
438 0 ▶ $a->push( $e ) if( $ok );
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
439 # Stop here since we reached the maximum number of matches
440 0 ▶ return if( CORE::exists( $opts->{max_match} ) && $a->length >= $opts->{max_match} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"max_match"} and $a->length >= $opts->{"max_match"} 0 0 0
Condition0%0/3
exists $opts->{"max_match"} and $a->length >= $opts->{"max_match"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"max_match"} and $a->length >= $opts->{"max_match"}
exists $opts->{"max_match"} $a->length >= $opts->{"max_match"}
Truth table0%0/3
A and B
A= exists $opts->{"max_match"}B= $a->length >= $opts->{"max_match"}
ABresult
0X0
100
111
441 # Don't go down or up further if we reached the maximum level
442 0 ▶ return(1) if( CORE::exists( $opts->{max_level} ) && ( $def->{level} + 1 ) > $opts->{max_level} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"max_level"} and $def->{"level"} + 1 > $opts->{"max_level"} 0 0 0
Condition0%0/3
exists $opts->{"max_level"} and $def->{"level"} + 1 > $opts->{"max_level"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"max_level"} and $def->{"level"} + 1 > $opts->{"max_level"}
exists $opts->{"max_level"} $def->{"level"} + 1 > $opts->{"max_level"}
Truth table0%0/3
A and B
A= exists $opts->{"max_level"}B= $def->{"level"} + 1 > $opts->{"max_level"}
ABresult
0X0
100
111
443 0 $def->{level}++;
444 0 ▶ if( $opts->{direction} eq 'down' )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'direction'} eq 'down') { } 0 0 0
elsif ($opts->{'direction'} eq 'up') { } 0 0 0
445 {
446 0 ▶ $crawl_down->( $e->children, $def ) if( $e->children->length > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $e->children->length > 0 0 0 0
447 }
448 elsif( $opts->{direction} eq 'up' )
449 {
450 0 ▶ $check_elem->( $e->parent ) if( $e->parent );
Branch: 1 branch
BranchTrueFalseTotal
if $e->parent 0 0 0
451 }
452 0 $def->{level}--;
453 0 return(1);
454 0 };
455
456 $crawl_down = sub
457 {
458 0 ▶ my $kids = shift( @_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
459 0 my $def = shift( @_ );
460 $kids->foreach(sub
461 {
462 0 ▶ $check_elem->( $_, $def );
Subroutine __ANON__: not called CC=1 SCAR=6.9
463 0 });
464 0 };
465
466 0 my $def = { level => 0 };
467 0 $check_elem->( $self, $def );
468 # return( $a->length > 0 ? $a : '' );
469 0 return( $a );
470 }
471  
472 sub look_down
473 {
474 0 ▶ my $self = shift( @_ );
Subroutine look_down: not called CC=1 SCAR=6.9
Pod undocumented
475 0 my $opts = $self->_get_args_as_hash( @_ );
476 0 $opts->{direction} = 'down';
477 0 return( $self->look( $opts ) );
478 }
479  
480 sub look_up
481 {
482 0 ▶ my $self = shift( @_ );
Subroutine look_up: not called CC=1 SCAR=6.9
Pod undocumented
483 0 my $opts = $self->_get_args_as_hash( @_ );
484 0 $opts->{direction} = 'up';
485 0 return( $self->look( $opts ) );
486 }
487  
488 sub make_html_parser
489 {
490 0 ▶ my $self = shift( @_ );
Subroutine make_html_parser: not called CC=1 SCAR=6.9
Pod documented
491 0 require HTML::Object;
492 0 return( HTML::Object->new );
493 }
494  
495 sub object_id
496 {
497 0 ▶ my $self = shift( @_ );
Subroutine object_id: not called CC=2 SCAR=17.9
Pod documented
498 0 ▶ return( $self->{object_id} ) if( length( $self->{object_id} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"object_id"} 0 0 0
499 0 $self->{object_id} = Scalar::Util::refaddr( $self );
500 0 return( $self->{object_id} );
501 }
502  
503 0 ▶sub package { return( ref( $_[0] ) ); }
Subroutine package: not called CC=1 SCAR=6.9
Pod documented
504  
505 18 ▶sub parent { return( shift->_set_get_object( 'parent', 'Markdown::Parser::Element', @_ ) ); }
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
506  
507 sub parse_html
508 {
509 0 ▶ my $self = shift( @_ );
Subroutine parse_html: not called CC=3 SCAR=24.8
Pod documented
510 0 ▶ my $html = shift( @_ ) || return;
Condition0%0/2
shift @_ || (return)
l!l
00
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
511 0 my $p = $self->make_html_parser;
512 0 ▶ my $doc = $p->parse_data( $html ) ||
Condition0%0/2
$p->parse_data($html) || (return $self->pass_error($p->error))
l!l
00
MC/DC0%0/1
$p->parse_data($html) || (return $self->pass_error($p->error))
$p->parse_data($html)
Truth table0%0/2
A
A= $p->parse_data($html)
Aresult
11
00
513 return( $self->pass_error( $p->error ) );
514 0 return( $doc );
515 }
516  
517 0 ▶sub pos { return( shift->_set_get_number_as_object( 'pos', @_ ) ); }
Subroutine pos: not called CC=1 SCAR=6.9
Pod documented
518  
519 3 ▶sub raw { return( shift->_set_get_scalar_as_object( 'raw', @_ ) ); }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
520  
521 sub remove
522 {
523 0 ▶ my $self = shift( @_ );
Subroutine remove: not called CC=6 SCAR=37.4
Pod documented
524 0 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif ($self->_is_object($self->parent)) { } 0 0 0
525 {
526 0 my $elem = shift( @_ );
527 0 ▶ return if( !length( $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $elem 0 0 0
528 0 my $base_class = $self->base_class;
529 0 ▶ return( $self->error( "Element provided is not a ${base_class}::Element object." ) ) if( !$self->_is_a( "${base_class}::Element", $elem ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a("${base_class}::Element", $elem) 0 0 0
530 0 my $pos = $self->children->pos( $elem );
531 0 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
532 0 return( $self->children->delete( $pos, 1 ) );
533 }
534 ## Called without argument: remove itself from its parent
535 elsif( $self->_is_object( $self->parent ) )
536 {
537 0 return( $self->parent->remove( $self ) );
538 }
539 0 return;
540 }
541  
542 0 ▶sub remove_children { return( shift->children->reset ); }
Subroutine remove_children: not called CC=1 SCAR=6.9
Pod documented
543  
544 1 ▶sub tag_name { return( shift->_set_get_scalar_as_object( 'tag_name', @_ ) ); }
Subroutine tag_name: called CC=1 SCAR=0.0
Pod documented
545  
546 sub wrap
547 {
548 0 ▶ my $self = shift( @_ );
Subroutine wrap: not called CC=2 SCAR=17.9
Pod documented
549 0 my $elem = shift( @_ );
550 0 my $base_class = $self->base_class;
551 0 ▶ return( $self->error( "Element provided to wrap children is not a ${base_class}::Element object." ) ) if( !$self->_is_a( $elem, "${base_class}::Element" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($elem, "${base_class}::Element") 0 0 0
552 ## Copy our children to the element provided and set the parent property accordingly
553 $self->children->foreach(sub
554 {
555 0 ▶ $elem->add_element( $_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
556 0 });
557 0 $self->remove_children;
558 ## Set this element as our only child and set the parent property accordingly
559 0 $self->add_element( $elem );
560 0 return( $self );
561 }
562  
563 sub _append_text
564 {
565 0 ▶ my $self = shift( @_ );
Subroutine _append_text: not called CC=5 SCAR=34.0
566 0 ▶ my $what = shift( @_ ) || return;
Condition0%0/2
shift @_ || (return)
l!l
00
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
567 0 my $elem_class = $self->base_class . '::Element';
568 0 ▶ if( $self->_is_a( $what, $elem_class ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($what, $elem_class)) { } 0 0 0
elsif (ref $what) { } 0 0 0
569 {
570 0 $self->children->push( $what );
571 }
572 elsif( ref( $what ) )
573 {
574 0 return( $self->error( "I do not know what to do with this reference \"$what\". I was expecting either a string or a $elem_class class object." ) );
575 }
576 ## Simple string, i.e. a text chunk
577 else
578 {
579 0 ▶ if( $self->children->last->tag_name eq 'text' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->children->last->tag_name eq 'text') { } 0 0 0
580 {
581 0 $self->children->last->append( " $what" );
582 }
583 else
584 {
585 0 $self->children->push( $self->create_text({
586 text => $what,
587 raw => $what,
588 }) );
589 }
590 }
591 0 return( $self );
592 }
593  
594 sub _create_element
595 {
596 18 ▶ my $self = shift( @_ );
Subroutine _create_element: called CC=5 SCAR=17.8
597 18 my $mod = shift( @_ );
598 18 my $base = $self->base_class;
599 18 my $class = "${base}::${mod}";
600 18 ▶ $self->_load_class( $class ) || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return 0 18 18
601 18 ▶ my $obj = $class->new( @_, debug => $self->debug ) || return( $self->pass_error( $class->error ) );
Condition50%1/2
$class->new(@_, 'debug', $self->debug) || (return $self->pass_error($class->error))
l!l
180
MC/DC0%0/1
$class->new(@_, 'debug', $self->debug) || (return $self->pass_error($class->error))
$class->new(@_, 'debug', $self->debug)
Truth table50%1/2
A
A= $class->new(@_, 'debug', $self->debug)
Aresult
11
00
602 ## Assign the top document object, which is used by children element to access the document object methods
603 18 ▶ if( $self->isa( 'Markdown::Parser::Document' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->isa('Markdown::Parser::Document')) { } 18 0 18
elsif (my $doc = $self->document) { } 0 0 0
604 {
605 18 $obj->document( $self );
606 }
607 elsif( my $doc = $self->document )
608 {
609 0 $obj->document( $doc );
610 }
611 18 return( $obj );
612 }
613  
614 2 1;
615 # NOTE: POD
616 __END__
« blib/lib/Markdown/Parser/Link.pm ↑ Summary blib/lib/Markdown/Parser/Element.pm »