lib/Markdown/Parser/Link.pm

filter: Statementstmt 35.7% 49 / 137 Branchbran 10.3% 6 / 58 Conditioncond 12.5% 3 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 48.2% 14 / 29 Podpod 100% 12 / 12 totaltotal 30.5% 84 / 275 CC 40 SCAR 47.6
CC
40
Cov
26%
as_string
380.0
encode_email_address
56.0
as_pod
20.0
CRAP 117.2
« blib/lib/Markdown/Parser/Code.pm ↑ Summary lib/Markdown/Parser/Code.pm »
1 ## -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Markdown Parser Only - ~/lib/Markdown/Parser/Link.pm
4 ## Version v0.2.1
5 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2021/08/23
8 ## Modified 2022/09/22
9 ## All rights reserved
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Markdown::Parser::Link;
15 BEGIN
16 {
17 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use parent qw( Markdown::Parser::Element );
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 Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ our $VERSION = 'v0.2.1';
Subroutine BEGIN: called CC=1 SCAR=0.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 {
30 1 ▶ my $self = shift( @_ );
Subroutine init: called CC=1 SCAR=0.0
Pod documented
31 1 $self->{encrypt} = '';
32 1 $self->{id} = [];
33 1 $self->{link_id} = '';
34 1 $self->{name} = '';
35 1 $self->{original} = '';
36 1 $self->{tag_name} = 'link';
37 1 $self->{title} = '';
38 1 $self->{url} = '';
39 1 return( $self->SUPER::init( @_ ) );
40 }
41  
42 sub as_markdown
43 {
44 1 ▶ my $self = shift( @_ );
Subroutine as_markdown: called CC=10 SCAR=29.9
Pod documented
45 1 my $arr = $self->new_array;
46 1 ▶ my $name = $self->children->map(sub{ $_->as_markdown })->join( '' );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 $arr->push( sprintf( '[%s]', $name ) );
48 1 ▶ if( $self->link_id )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->link_id) { } 0 1 1
elsif ($self->url or $self->title) { } 1 0 1
49 {
50 0 $arr->push( sprintf( '[%s]', $self->link_id ) );
51 }
52 ▶ elsif( $self->url || $self->title )
Condition33%1/3
$self->url or $self->title
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->url or $self->title
$self->url $self->title
Truth table33%1/3
A or B
A= $self->urlB= $self->title
ABresult
1X1
011
000
53 {
54 1 $arr->push( '(' );
55 1 ▶ $arr->push( sprintf( '%s', $self->url ) ) if( $self->url );
Branch: 1 branch
BranchTrueFalseTotal
if $self->url 1 0 1
56 1 ▶ $arr->push( ' ' ) if( $self->url && $self->title );
Branch: 1 branch
BranchTrueFalseTotal
if $self->url and $self->title 0 1 1
Condition33%1/3
$self->url and $self->title
!ll&&!rl&&r
010
MC/DC0%0/2
$self->url and $self->title
$self->url $self->title
Truth table33%1/3
A and B
A= $self->urlB= $self->title
ABresult
0X0
100
111
57 1 ▶ $arr->push( sprintf( '"%s"', $self->title ) ) if( $self->title );
Branch: 1 branch
BranchTrueFalseTotal
if $self->title 0 1 1
58 1 $arr->push( ')' );
59 }
60 1 ▶ if( $self->class->length || $self->id->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->length or $self->id->length) 0 1 1
Condition33%1/3
$self->class->length or $self->id->length
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->class->length or $self->id->length
$self->class->length $self->id->length
Truth table33%1/3
A or B
A= $self->class->lengthB= $self->id->length
ABresult
1X1
011
000
61 {
62 0 my $def = $self->new_array;
63 0 ▶ $def->push( $self->id->map(sub{ "\#${_}" })->list );
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 ▶ $def->push( $self->class->map(sub{ ".$_" })->list );
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $arr->push( '{' . $def->join( ' ' )->scalar . '}' );
66 }
67 1 return( $arr->join( '' )->scalar );
68 }
69  
70 sub as_pod
71 {
72 0 ▶ my $self = shift( @_ );
Subroutine as_pod: not called CC=4 SCAR=30.0
Pod documented
73 0 my $arr = $self->new_array;
74 0 ▶ my $name = $self->children->map(sub{ $_->as_pod })->join( '' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ if( $self->url && $name )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->url and $name) { } 0 0 0
elsif ($self->url) { } 0 0 0
Condition0%0/3
$self->url and $name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->url and $name
$self->url $name
Truth table0%0/3
A and B
A= $self->urlB= $name
ABresult
0X0
100
111
76 {
77 0 $arr->push( sprintf( 'L<%s|%s>', $name, $self->url ) );
78 }
79 elsif( $self->url )
80 {
81 0 $arr->push( sprintf( 'L<%s>', $self->url ) );
82 }
83 0 return( $arr->join( '' )->scalar );
84 }
85  
86 sub as_string
87 {
88 0 ▶ my $self = shift( @_ );
Subroutine as_string: not called CC=19 SCAR=59.4
Pod documented
89 0 my $arr = $self->new_array;
90 0 my $tag = 'a';
91 0 my $tag_open = $tag;
92 0 my $url = $self->url;
93 0 my $orig = $self->original->scalar;
94 0 my $url_str = "$url";
95 0 my $encrypt = $self->encrypt;
96 0 my $scheme = '';
97 0 ▶ $scheme = $url->scheme if( ref( $url ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $url 0 0 0
98 0 $arr->push( "<${tag_open}" );
99 0 my $attr = $self->new_array;
100 0 ▶ $attr->push( $self->format_id ) if( $self->id->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->id->length 0 0 0
101 0 ▶ $attr->push( $self->format_class ) if( $self->class->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->class->length 0 0 0
102 0 my $attributes = $self->format_attributes;
103 0 ▶ $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attributes->length 0 0 0
104 0 ▶ $arr->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attr->length 0 0 0
105 0 ▶ if( $scheme eq 'mailto' )
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'mailto') { } 0 0 0
106 {
107 0 ▶ if( $encrypt eq 'obfuscate' )
Branch: 2 branches
BranchTrueFalseTotal
if ($encrypt eq 'obfuscate') { } 0 0 0
elsif ($encrypt eq 'encode' or $encrypt) { } 0 0 0
108 {
109 0 $self->document->setup_email_obfuscation;
110 # We use the original e-mail address as it as provided in the text, because URI alters it by url-encoding it
111 0 my $email = $orig;
112 0 my $user = substr( $email, 0, rindex( $email, '@' ) );
113 0 my $host = substr( $email, rindex( $email, '@' ) + 1 );
114 # Use of a decoy email address
115 0 ▶ if( $self->document->default_email->length > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document->default_email->length > 0) { } 0 0 0
116 {
117 0 $arr->push( sprintf( ' href="%s"', $self->document->default_email->scalar ) );
118 }
119 else
120 {
121 0 $arr->push( " href=\"mailto:dave.null\@${host}\"" );
122 }
123 0 ▶ my $data_user = $self->document->email_obfuscate_data_user->scalar || 'user';
Condition0%0/2
$self->document->email_obfuscate_data_user->scalar || 'user'
l!l
00
MC/DC0%0/1
$self->document->email_obfuscate_data_user->scalar || 'user'
$self->document->email_obfuscate_data_user->scalar
Truth table0%0/2
A
A= $self->document->email_obfuscate_data_user->scalar
Aresult
11
00
124 0 ▶ my $data_host = $self->document->email_obfuscate_data_host->scalar || 'host';
Condition0%0/2
$self->document->email_obfuscate_data_host->scalar || 'host'
l!l
00
MC/DC0%0/1
$self->document->email_obfuscate_data_host->scalar || 'host'
$self->document->email_obfuscate_data_host->scalar
Truth table0%0/2
A
A= $self->document->email_obfuscate_data_host->scalar
Aresult
11
00
125 0 $arr->push( sprintf( ' data-%s="%s"', $data_user, $self->encode_html( ['"', '&', '?', '#'], join( '', reverse( split( //, $user ) ) ) ) ) );
126 0 $arr->push( sprintf( ' data-%s="%s"', $data_host, $self->encode_html( ['"', '&', '?', '#'], join( '', reverse( split( //, $host ) ) ) ) ) );
127 0 ▶ $self->class->push( $self->document->email_obfuscate_class ) if( !$self->class->has( $self->document->email_obfuscate_class->scalar ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->class->has($self->document->email_obfuscate_class->scalar) 0 0 0
128 0 $arr->push( sprintf( ' class="%s"', $self->class->join( ', ' )->scalar ) );
129 }
130 ▶ elsif( $encrypt eq 'encode' || $encrypt )
Condition0%0/3
$encrypt eq "encode" or $encrypt
l!l&&r!l&&!r
000
MC/DC0%0/2
$encrypt eq "encode" or $encrypt
$encrypt eq "encode" $encrypt
Truth table0%0/3
A or B
A= $encrypt eq "encode"B= $encrypt
ABresult
1X1
011
000
131 {
132 0 $url_str = $self->encode_email_address( $orig );
133 0 $arr->push( " href=\"$url_str\"" );
134 }
135 else
136 {
137 0 $arr->push( " href=\"$url_str\"" );
138 }
139 }
140 else
141 {
142 0 $arr->push( " href=\"$url_str\"" );
143 }
144 0 ▶ if( $self->title->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->title->length) 0 0 0
145 {
146 0 $arr->push( sprintf( ' title="%s"', $self->encode_html( 'all', $self->title ) ) );
147 }
148 0 $arr->push( ">" );
149 0 ▶ if( $scheme eq 'mailto' && $encrypt eq 'obfuscate' )
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'mailto' and $encrypt eq 'obfuscate') { } 0 0 0
elsif ($self->children->length) { } 0 0 0
Condition0%0/3
$scheme eq "mailto" and $encrypt eq "obfuscate"
!ll&&!rl&&r
000
MC/DC0%0/2
$scheme eq "mailto" and $encrypt eq "obfuscate"
$scheme eq "mailto" $encrypt eq "obfuscate"
Truth table0%0/3
A and B
A= $scheme eq "mailto"B= $encrypt eq "obfuscate"
ABresult
0X0
100
111
150 {
151 ## We do nothing
152 }
153 elsif( $self->children->length )
154 {
155 0 ▶ $arr->push( $self->children->map(sub{ $_->as_string })->list );
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 }
157 else
158 {
159 0 ▶ my $link_text = $scheme eq 'mailto' ? $self->encode_email_address( $orig ) : $orig;
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'mailto' ? : 0 0 0
160 0 $arr->push( $link_text );
161 }
162 0 $arr->push( "</${tag}>" );
163 0 return( $arr->join( '' )->scalar );
164 }
165  
166 sub copy_from
167 {
168 0 ▶ my $self = shift( @_ );
Subroutine copy_from: not called CC=3 SCAR=24.8
Pod documented
169 0 ▶ my $def = shift( @_ ) || return( $self->error( "No link definition object was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No link definition object was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No link definition object was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
170 0 ▶ return( $self->error( "Link definition object provided to copy information from \"", overload::StrVal( $def ), "\" is not a Markdown::Parser::LinkDefinition object." ) ) if( !$self->_is_a( $def, 'Markdown::Parser::LinkDefinition' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($def, "Markdown::Parser::LinkDefinition") 0 0 0
171 0 return( $def->copy_to( $self ) );
172 }
173  
174 0 ▶sub encrypt { return( shift->_set_get_scalar_as_object( 'encrypt', @_ ) ); }
Subroutine encrypt: not called CC=1 SCAR=6.9
Pod documented
175  
176 sub encode_email_address
177 {
178 0 ▶ my $self = shift( @_ );
Subroutine encode_email_address: not called CC=7 SCAR=40.3
Pod documented
179 0 my $addr = shift( @_ );
180 0 ▶ return( '' ) if( !length( $addr ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $addr 0 0 0
181 ## Borrowed from Markdown original author John Gruber
182 0 srand();
183 ## Use either decimal, hexadecimal or no encoding
184 ## https://stackoverflow.com/questions/20357800/html-entities-when-to-use-decimal-vs-hex
185 my @encode =
186 (
187 0 ▶ sub{ '&#' . ord( shift ) . ';' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 ▶ sub{ '&#x' . sprintf( "%X", ord( shift ) ) . ';' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 ▶ sub{ shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 );
191 0 my @chars = split( //, $addr );
192 0 for my $i ( 0..$#chars )
193 {
194 0 ▶ if( $chars[$i] eq '@' )
Branch: 2 branches
BranchTrueFalseTotal
if ($chars[$i] eq '@') { } 0 0 0
elsif ($chars[$i] ne ':') { } 0 0 0
195 {
196 ## Markdown's original author John Gruber says: "this *must* be encoded. I insist."
197 0 $chars[$i] = $encode[ int( rand( 1 ) ) ]->( $chars[$i] );
198 }
199 elsif( $chars[$i] ne ':' )
200 {
201 ## leave ':' alone (to spot mailto: later)
202 0 my $r = rand();
203 # roughly 10% raw, 45% hex, 45% dec
204 0 ▶ $chars[$i] = (
Branch: 2 branches
BranchTrueFalseTotal
$r > 0.9 ? : 0 0 0
$r < 0.45 ? : 0 0 0
205 $r > .9 ? $encode[2]->( $chars[$i] ) :
206 $r < .45 ? $encode[1]->( $chars[$i] ) :
207 $encode[0]->( $chars[$i] )
208 );
209 }
210 }
211 0 return( join( '', @chars ) );
212 }
213  
214 1 ▶sub link_id { return( shift->_set_get_scalar_as_object( 'link_id', @_ ) ); }
Subroutine link_id: called CC=1 SCAR=0.0
Pod documented
215  
216 sub name
217 {
218 0 ▶ my $self = shift( @_ );
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
219 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
220 {
221 0 my $text = shift( @_ );
222 0 return( $self->add_element( $self->create_text({ text => $text }) ) );
223 }
224 else
225 {
226 0 ▶ return( $self->children->map(sub{ $_->as_string })->join( '' ) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 }
228 }
229  
230 0 ▶sub original { return( shift->_set_get_scalar_as_object( 'original', @_ ) ); }
Subroutine original: not called CC=1 SCAR=6.9
Pod documented
231  
232 2 ▶sub title { return( shift->_set_get_scalar_as_object( 'title', @_ ) ); }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
233  
234 5 ▶sub url { return( shift->_set_get_uri( 'url', @_ ) ); }
Subroutine url: called CC=1 SCAR=0.0
Pod documented
235  
236 2 1;
237 # NOTE: POD
238 __END__
« blib/lib/Markdown/Parser/Code.pm ↑ Summary lib/Markdown/Parser/Code.pm »