| 1 | ## -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Markdown Parser Only - ~/lib/Markdown/Parser/LinkDefinition.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::LinkDefinition; | ||
| 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->{id} = ''; | ||
| 31 | $self->{link_id} = ''; | ||
| 32 | $self->{tag_name} = 'link_def'; | ||
| 33 | $self->{title} = ''; | ||
| 34 | $self->{url} = ''; | ||
| 35 | return( $self->SUPER::init( @_ ) ); | ||
| 36 | } | ||
| 37 | |||
| 38 | # [foo]: http://example.com/ "Optional Title Here" | ||
| 39 | sub as_markdown | ||
| 40 | { | ||
| 41 | my $self = shift( @_ ); | ||
| 42 | my $arr = $self->new_array; | ||
| 43 | $arr->push( sprintf( '[%d]: %s', $self->link_id, $self->url ) ); | ||
| 44 | $arr->push( sprintf( ' "%s"', $self->title ) ) if( $self->title->length ); | ||
| 45 | if( $self->class->length || $self->id->length ) | ||
| 46 | { | ||
| 47 | my $def = $self->new_array; | ||
| 48 | $def->push( $self->id->map(sub{ "\#${_}" })->list ); | ||
| 49 | $def->push( $self->class->map(sub{ ".$_" })->list ); | ||
| 50 | $arr->push( '{' . $def->join( ' ' )->scalar . '}' ); | ||
| 51 | } | ||
| 52 | return( $arr->join( '' )->scalar ); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub as_pod | ||
| 56 | { | ||
| 57 | my $self = shift( @_ ); | ||
| 58 | my $arr = $self->new_array; | ||
| 59 | if( $self->url && $self->title ) | ||
| 60 | { | ||
| 61 | $arr->push( sprintf( '[%d]: L<%s|%s>', $self->link_id, $self->url, $self->title ) ); | ||
| 62 | } | ||
| 63 | elsif( $self->url ) | ||
| 64 | { | ||
| 65 | $arr->push( sprintf( '[%d]: L<%s>', $self->link_id, $self->url ) ); | ||
| 66 | } | ||
| 67 | return( $arr->join( '' )->scalar ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub as_string | ||
| 71 | { | ||
| 72 | my $self = shift( @_ ); | ||
| 73 | return( '' ) if( !$self->id ); | ||
| 74 | my $arr = $self->new_array; | ||
| 75 | $arr->push( sprintf( '[%s]:', $self->link_id ) ); | ||
| 76 | $arr->push( $self->url . '' ) if( $self->url ); | ||
| 77 | $arr->push( sprintf( '"%s"', $self->title ) ) if( $self->title ); | ||
| 78 | if( $self->class->length || $self->id->length ) | ||
| 79 | { | ||
| 80 | my $def = $self->new_array; | ||
| 81 | $def->push( $self->id->map(sub{ "\#${_}" })->list ); | ||
| 82 | $def->push( $self->class->map(sub{ ".$_" })->list ); | ||
| 83 | $arr->push( '{' . $def->join( ' ' )->scalar . '}' ); | ||
| 84 | } | ||
| 85 | return( $arr->join( ' ' ) ) if( $arr->length ); | ||
| 86 | return( '' ); | ||
| 87 | } | ||
| 88 | |||
| 89 | ## Copy the link definition information to the target object, such as a Link or an Image | ||
| 90 | sub copy_to | ||
| 91 | { | ||
| 92 | my $self = shift( @_ ); | ||
| 93 | my $obj = shift( @_ ) || return( $self->error( "No object was provided to copy the link definition information to." ) ); | ||
| 94 | 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' ) ); | ||
| 95 | $obj->url( $self->url ) if( $obj->can( 'url' ) ); | ||
| 96 | $obj->title( $self->title ) if( $obj->can( 'title' ) ); | ||
| 97 | $obj->id( $self->id ) if( $obj->can( 'id' ) && $self->id->length ); | ||
| 98 | $obj->class( $self->class ) if( $obj->can( 'class' ) && $self->class->length ); | ||
| 99 | $obj->attributes( $self->attributes ) if( $obj->can( 'attributes' ) && $self->attributes->length ); | ||
| 100 | ## For chaining | ||
| 101 | return( $obj ); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub link_id { return( shift->_set_get_scalar_as_object( 'link_id', @_ ) ); } | ||
| 105 | |||
| 106 | sub title { return( shift->_set_get_scalar_as_object( 'title', @_ ) ); } | ||
| 107 | |||
| 108 | sub url { return( shift->_set_get_uri( 'url', @_ ) ); } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | # NOTE: POD | ||
| 112 | __END__ |