| 1 | ## -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Markdown Parser Only - ~/lib/Markdown/Parser/Footnote.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::Footnote; | ||
| 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->{reference} = []; | ||
| 33 | $self->{tag_name} = 'footnote'; | ||
| 34 | ## Used to contain the unparsed footnote text | ||
| 35 | ## Footnotes are parsed once all footnotes are registered. | ||
| 36 | ## This is done at the beginning of the Markdown::Parser::parse method | ||
| 37 | $self->{unparsed} = ''; | ||
| 38 | return( $self->SUPER::init( @_ ) ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # Add an Markdown::Parser::FootnoteReference object to our stack of footnote references and set its id for backlinks if necessary | ||
| 42 | sub add_reference | ||
| 43 | { | ||
| 44 | my $self = shift( @_ ); | ||
| 45 | my $obj = shift( @_ ) || return; | ||
| 46 | return( $self->error( "Object '$obj' provided is not a 'Markdown::Parser::FootnoteReference' object." ) ) if( !$self->_is_a( $obj, 'Markdown::Parser::FootnoteReference' ) ); | ||
| 47 | ## Set the id unless it is already provided. | ||
| 48 | ## Typically the order of entry of this reference followed by our own id | ||
| 49 | ## This makes it visually easy to identify, debug and track when hovering the links generated | ||
| 50 | if( !$obj->id->length ) | ||
| 51 | { | ||
| 52 | $obj->id( sprintf( '%d:%s', $self->references->length + 1, $self->id ) ); | ||
| 53 | } | ||
| 54 | $self->references->push( $obj ); | ||
| 55 | return( $self ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub as_markdown | ||
| 59 | { | ||
| 60 | my $self = shift( @_ ); | ||
| 61 | return( sprintf( '*[%s]: %s', $self->name, $self->value ) ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub as_pod | ||
| 65 | { | ||
| 66 | my $self = shift( @_ ); | ||
| 67 | return( sprintf( '[C<%s>]: %s', $self->name, $self->value ) ); | ||
| 68 | } | ||
| 69 | |||
| 70 | # <li id="fn:1" role="doc-endnote"> | ||
| 71 | # <p>This is the first footnote. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">↩</a></p> | ||
| 72 | # </li> | ||
| 73 | sub as_string | ||
| 74 | { | ||
| 75 | my $self = shift( @_ ); | ||
| 76 | my $arr = $self->new_array; | ||
| 77 | my $id = $self->id; | ||
| 78 | use utf8; | ||
| 79 | $arr->push( sprintf( '<li id="fn:%s" role="doc-endnote">', $id ) ); | ||
| 80 | $arr->push( $self->children->map(sub{ $_->as_string })->join( '' )->scalar ); | ||
| 81 | ## There could be multiple backlinks for one footnote, given it is possible that there are multiple reference to one footnote. | ||
| 82 | ## The id of the reference is set at creation, and in the add_reference method | ||
| 83 | my $backref = $self->references->map(sub | ||
| 84 | { | ||
| 85 | sprintf( ' :<a href="#fnref:%s" class="reversefootnote" title="Jump back to footnote %s in the text" role="doc-backlink">↩</a>', $_->id, $_->id ); | ||
| 86 | })->join( ' ' )->scalar; | ||
| 87 | $arr->push( '</li>' ); | ||
| 88 | my $str = $arr->join( '' )->scalar; | ||
| 89 | $str =~ s/\s\!{2}FN\!{2}/ $backref/; | ||
| 90 | return( $str ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub id { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); } | ||
| 94 | |||
| 95 | sub parse | ||
| 96 | { | ||
| 97 | my $self = shift( @_ ); | ||
| 98 | my $parser = shift( @_ ); | ||
| 99 | return unless( $self->unparsed->length > 0 ); | ||
| 100 | my $data = $self->unparsed->scalar; | ||
| 101 | $data =~ s/^(?:\s{4}|\t)//gm; | ||
| 102 | $parser->parse( $data, { element => $self }); | ||
| 103 | $self->unparsed->undef; | ||
| 104 | return( $self ); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub references { return( shift->_set_get_object_array_object( 'reference', 'Markdown::Parser::FootnoteReference', @_ ) ); } | ||
| 108 | |||
| 109 | sub text | ||
| 110 | { | ||
| 111 | my $self = shift( @_ ); | ||
| 112 | if( @_ ) | ||
| 113 | { | ||
| 114 | my $text = shift( @_ ); | ||
| 115 | return( $self->add_element( $self->create_text({ text => $text }) ) ); | ||
| 116 | } | ||
| 117 | else | ||
| 118 | { | ||
| 119 | return( $self->children->map(sub{ $_->as_string })->join( '' ) ); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | sub unparsed { return( shift->_set_get_scalar_as_object( 'unparsed', @_ ) ); } | ||
| 124 | |||
| 125 | 1; | ||
| 126 | # NOTE: POD | ||
| 127 | __END__ |