| 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 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use parent qw( Markdown::Parser::Element ); | ||
| 20 | use vars qw( $VERSION ); | ||
| 21 | our $VERSION = 'v0.3.0'; | ||
| 22 | }; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | |||
| 27 | sub init | ||
| 28 | { | ||
| 29 | my $self = shift( @_ ); | ||
| 30 | $self->{encrypt} = ''; | ||
| 31 | $self->{id} = []; | ||
| 32 | $self->{link_id} = ''; | ||
| 33 | $self->{name} = ''; | ||
| 34 | $self->{original} = ''; | ||
| 35 | $self->{tag_name} = 'link'; | ||
| 36 | $self->{title} = ''; | ||
| 37 | $self->{url} = ''; | ||
| 38 | return( $self->SUPER::init( @_ ) ); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub as_markdown | ||
| 42 | { | ||
| 43 | my $self = shift( @_ ); | ||
| 44 | my $arr = $self->new_array; | ||
| 45 | my $name = $self->children->map(sub{ $_->as_markdown })->join( '' ); | ||
| 46 | $arr->push( sprintf( '[%s]', $name ) ); | ||
| 47 | if( $self->link_id ) | ||
| 48 | { | ||
| 49 | $arr->push( sprintf( '[%s]', $self->link_id ) ); | ||
| 50 | } | ||
| 51 | elsif( $self->url || $self->title ) | ||
| 52 | { | ||
| 53 | $arr->push( '(' ); | ||
| 54 | $arr->push( sprintf( '%s', $self->url ) ) if( $self->url ); | ||
| 55 | $arr->push( ' ' ) if( $self->url && $self->title ); | ||
| 56 | $arr->push( sprintf( '"%s"', $self->title ) ) if( $self->title ); | ||
| 57 | $arr->push( ')' ); | ||
| 58 | } | ||
| 59 | if( $self->class->length || $self->id->length ) | ||
| 60 | { | ||
| 61 | my $def = $self->new_array; | ||
| 62 | $def->push( $self->id->map(sub{ "\#${_}" })->list ); | ||
| 63 | $def->push( $self->class->map(sub{ ".$_" })->list ); | ||
| 64 | $arr->push( '{' . $def->join( ' ' )->scalar . '}' ); | ||
| 65 | } | ||
| 66 | return( $arr->join( '' )->scalar ); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub as_pod | ||
| 70 | { | ||
| 71 | my $self = shift( @_ ); | ||
| 72 | my $arr = $self->new_array; | ||
| 73 | my $name = $self->children->map(sub{ $_->as_pod })->join( '' ); | ||
| 74 | if( $self->url && $name ) | ||
| 75 | { | ||
| 76 | $arr->push( sprintf( 'L<%s|%s>', $name, $self->url ) ); | ||
| 77 | } | ||
| 78 | elsif( $self->url ) | ||
| 79 | { | ||
| 80 | $arr->push( sprintf( 'L<%s>', $self->url ) ); | ||
| 81 | } | ||
| 82 | return( $arr->join( '' )->scalar ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub as_string | ||
| 86 | { | ||
| 87 | my $self = shift( @_ ); | ||
| 88 | my $arr = $self->new_array; | ||
| 89 | my $tag = 'a'; | ||
| 90 | my $tag_open = $tag; | ||
| 91 | my $url = $self->url; | ||
| 92 | my $orig = $self->original->scalar; | ||
| 93 | my $url_str = "$url"; | ||
| 94 | my $encrypt = $self->encrypt; | ||
| 95 | my $scheme = ''; | ||
| 96 | $scheme = $url->scheme if( ref( $url ) ); | ||
| 97 | $arr->push( "<${tag_open}" ); | ||
| 98 | my $attr = $self->new_array; | ||
| 99 | $attr->push( $self->format_id ) if( $self->id->length ); | ||
| 100 | $attr->push( $self->format_class ) if( $self->class->length ); | ||
| 101 | my $attributes = $self->format_attributes; | ||
| 102 | $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length ); | ||
| 103 | $arr->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length ); | ||
| 104 | if( $scheme eq 'mailto' ) | ||
| 105 | { | ||
| 106 | if( $encrypt eq 'obfuscate' ) | ||
| 107 | { | ||
| 108 | $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 | my $email = $orig; | ||
| 111 | my $user = substr( $email, 0, rindex( $email, '@' ) ); | ||
| 112 | my $host = substr( $email, rindex( $email, '@' ) + 1 ); | ||
| 113 | # Use of a decoy email address | ||
| 114 | if( $self->document->default_email->length > 0 ) | ||
| 115 | { | ||
| 116 | $arr->push( sprintf( ' href="%s"', $self->document->default_email->scalar ) ); | ||
| 117 | } | ||
| 118 | else | ||
| 119 | { | ||
| 120 | $arr->push( " href=\"mailto:dave.null\@${host}\"" ); | ||
| 121 | } | ||
| 122 | my $data_user = $self->document->email_obfuscate_data_user->scalar || 'user'; | ||
| 123 | my $data_host = $self->document->email_obfuscate_data_host->scalar || 'host'; | ||
| 124 | $arr->push( sprintf( ' data-%s="%s"', $data_user, $self->encode_html( ['"', '&', '?', '#'], join( '', reverse( split( //, $user ) ) ) ) ) ); | ||
| 125 | $arr->push( sprintf( ' data-%s="%s"', $data_host, $self->encode_html( ['"', '&', '?', '#'], join( '', reverse( split( //, $host ) ) ) ) ) ); | ||
| 126 | $self->class->push( $self->document->email_obfuscate_class ) if( !$self->class->has( $self->document->email_obfuscate_class->scalar ) ); | ||
| 127 | $arr->push( sprintf( ' class="%s"', $self->class->join( ', ' )->scalar ) ); | ||
| 128 | } | ||
| 129 | elsif( $encrypt eq 'encode' || $encrypt ) | ||
| 130 | { | ||
| 131 | $url_str = $self->encode_email_address( $orig ); | ||
| 132 | $arr->push( " href=\"$url_str\"" ); | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | $arr->push( " href=\"$url_str\"" ); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | $arr->push( " href=\"$url_str\"" ); | ||
| 142 | } | ||
| 143 | if( $self->title->length ) | ||
| 144 | { | ||
| 145 | $arr->push( sprintf( ' title="%s"', $self->encode_html( 'all', $self->title ) ) ); | ||
| 146 | } | ||
| 147 | $arr->push( ">" ); | ||
| 148 | if( $scheme eq 'mailto' && $encrypt eq 'obfuscate' ) | ||
| 149 | { | ||
| 150 | ## We do nothing | ||
| 151 | } | ||
| 152 | elsif( $self->children->length ) | ||
| 153 | { | ||
| 154 | $arr->push( $self->children->map(sub{ $_->as_string })->list ); | ||
| 155 | } | ||
| 156 | else | ||
| 157 | { | ||
| 158 | my $link_text = $scheme eq 'mailto' ? $self->encode_email_address( $orig ) : $orig; | ||
| 159 | $arr->push( $link_text ); | ||
| 160 | } | ||
| 161 | $arr->push( "</${tag}>" ); | ||
| 162 | return( $arr->join( '' )->scalar ); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub copy_from | ||
| 166 | { | ||
| 167 | my $self = shift( @_ ); | ||
| 168 | my $def = shift( @_ ) || return( $self->error( "No link definition object was provided." ) ); | ||
| 169 | 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' ) ); | ||
| 170 | return( $def->copy_to( $self ) ); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub encrypt { return( shift->_set_get_scalar_as_object( 'encrypt', @_ ) ); } | ||
| 174 | |||
| 175 | sub encode_email_address | ||
| 176 | { | ||
| 177 | my $self = shift( @_ ); | ||
| 178 | my $addr = shift( @_ ); | ||
| 179 | return( '' ) if( !length( $addr ) ); | ||
| 180 | ## Borrowed from Markdown original author John Gruber | ||
| 181 | 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 | sub{ '&#' . ord( shift ) . ';' }, | ||
| 187 | sub{ '&#x' . sprintf( "%X", ord( shift ) ) . ';' }, | ||
| 188 | sub{ shift }, | ||
| 189 | ); | ||
| 190 | my @chars = split( //, $addr ); | ||
| 191 | for my $i ( 0..$#chars ) | ||
| 192 | { | ||
| 193 | if( $chars[$i] eq '@' ) | ||
| 194 | { | ||
| 195 | ## Markdown's original author John Gruber says: "this *must* be encoded. I insist." | ||
| 196 | $chars[$i] = $encode[ int( rand( 1 ) ) ]->( $chars[$i] ); | ||
| 197 | } | ||
| 198 | elsif( $chars[$i] ne ':' ) | ||
| 199 | { | ||
| 200 | ## leave ':' alone (to spot mailto: later) | ||
| 201 | my $r = rand(); | ||
| 202 | # roughly 10% raw, 45% hex, 45% dec | ||
| 203 | $chars[$i] = ( | ||
| 204 | $r > .9 ? $encode[2]->( $chars[$i] ) : | ||
| 205 | $r < .45 ? $encode[1]->( $chars[$i] ) : | ||
| 206 | $encode[0]->( $chars[$i] ) | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | return( join( '', @chars ) ); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub link_id { return( shift->_set_get_scalar_as_object( 'link_id', @_ ) ); } | ||
| 214 | |||
| 215 | sub name | ||
| 216 | { | ||
| 217 | my $self = shift( @_ ); | ||
| 218 | if( @_ ) | ||
| 219 | { | ||
| 220 | my $text = shift( @_ ); | ||
| 221 | return( $self->add_element( $self->create_text({ text => $text }) ) ); | ||
| 222 | } | ||
| 223 | else | ||
| 224 | { | ||
| 225 | return( $self->children->map(sub{ $_->as_string })->join( '' ) ); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | sub original { return( shift->_set_get_scalar_as_object( 'original', @_ ) ); } | ||
| 230 | |||
| 231 | sub title { return( shift->_set_get_scalar_as_object( 'title', @_ ) ); } | ||
| 232 | |||
| 233 | sub url { return( shift->_set_get_uri( 'url', @_ ) ); } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | # NOTE: POD | ||
| 237 | __END__ |