Parser/Link.pm

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