blib/lib/Markdown/Parser/LinkDefinition.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 154 CC 16 SCAR 31.7
CC
16
Cov
0%
copy_to
56.0
as_string
42.0
as_markdown
12.0
CRAP 23.9
1 ## -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Markdown Parser Only - ~/lib/Markdown/Parser/LinkDefinition.pm
4 ## Version v0.2.0
5 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2021/08/23
8 ## Modified 2022/09/19
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::LinkDefinition;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 use parent qw( Markdown::Parser::Element );
20 use vars qw( $VERSION );
21 use Devel::Confess;
22 our $VERSION = 'v0.2.0';
23 };
24  
25 use strict;
26 use warnings;
27  
28 sub init
29 {
30 my $self = shift( @_ );
31 $self->{id} = '';
32 $self->{link_id} = '';
33 $self->{tag_name} = 'link_def';
34 $self->{title} = '';
35 $self->{url} = '';
36 return( $self->SUPER::init( @_ ) );
37 }
38  
39 # [foo]: http://example.com/ "Optional Title Here"
40 sub as_markdown
41 {
42 my $self = shift( @_ );
43 my $arr = $self->new_array;
44 $arr->push( sprintf( '[%d]: %s', $self->link_id, $self->url ) );
45 $arr->push( sprintf( ' "%s"', $self->title ) ) if( $self->title->length );
46 if( $self->class->length || $self->id->length )
47 {
48 my $def = $self->new_array;
49 $def->push( $self->id->map(sub{ "\#${_}" })->list );
50 $def->push( $self->class->map(sub{ ".$_" })->list );
51 $arr->push( '{' . $def->join( ' ' )->scalar . '}' );
52 }
53 return( $arr->join( '' )->scalar );
54 }
55  
56 sub as_pod
57 {
58 my $self = shift( @_ );
59 my $arr = $self->new_array;
60 if( $self->url && $self->title )
61 {
62 $arr->push( sprintf( '[%d]: L<%s|%s>', $self->link_id, $self->url, $self->title ) );
63 }
64 elsif( $self->url )
65 {
66 $arr->push( sprintf( '[%d]: L<%s>', $self->link_id, $self->url ) );
67 }
68 return( $arr->join( '' )->scalar );
69 }
70  
71 sub as_string
72 {
73 my $self = shift( @_ );
74 return( '' ) if( !$self->id );
75 my $arr = $self->new_array;
76 $arr->push( sprintf( '[%s]:', $self->link_id ) );
77 $arr->push( $self->url . '' ) if( $self->url );
78 $arr->push( sprintf( '"%s"', $self->title ) ) if( $self->title );
79 if( $self->class->length || $self->id->length )
80 {
81 my $def = $self->new_array;
82 $def->push( $self->id->map(sub{ "\#${_}" })->list );
83 $def->push( $self->class->map(sub{ ".$_" })->list );
84 $arr->push( '{' . $def->join( ' ' )->scalar . '}' );
85 }
86 return( $arr->join( ' ' ) ) if( $arr->length );
87 return( '' );
88 }
89  
90 ## Copy the link definition information to the target object, such as a Link or an Image
91 sub copy_to
92 {
93 my $self = shift( @_ );
94 my $obj = shift( @_ ) || return( $self->error( "No object was provided to copy the link definition information to." ) );
95 return( $self->error( "The object provided \"", overload::StrVal( $obj ), "\" is not an Markdown::Parser::Element object or one of its inheriting modules." ) ) if( !$self->_is_a( $obj, 'Markdown::Parser::Element' ) );
96 $obj->url( $self->url ) if( $obj->can( 'url' ) );
97 $obj->title( $self->title ) if( $obj->can( 'title' ) );
98 $obj->id( $self->id ) if( $obj->can( 'id' ) && $self->id->length );
99 $obj->class( $self->class ) if( $obj->can( 'class' ) && $self->class->length );
100 $obj->attributes( $self->attributes ) if( $obj->can( 'attributes' ) && $self->attributes->length );
101 ## For chaining
102 return( $obj );
103 }
104  
105 sub link_id { return( shift->_set_get_scalar_as_object( 'link_id', @_ ) ); }
106  
107 sub title { return( shift->_set_get_scalar_as_object( 'title', @_ ) ); }
108  
109 sub url { return( shift->_set_get_uri( 'url', @_ ) ); }
110  
111 1;
112 # NOTE: POD
113 __END__