Parser/Element.pm

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