| 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 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | use parent qw( Markdown::Parser::Element ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use vars qw( $VERSION ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 1 | ▶ | use Devel::Confess; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | our $VERSION = 'v0.2.0'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | }; | ||||||||||||
| 24 | |||||||||||||
| 25 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | sub init | ||||||||||||
| 29 | { | ||||||||||||
| 30 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 31 | 0 | $self->{id} = ''; | |||||||||||
| 32 | 0 | $self->{reference} = []; | |||||||||||
| 33 | 0 | $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 | 0 | $self->{unparsed} = ''; | |||||||||||
| 38 | 0 | 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 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
add_reference: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 45 | 0 | ▶ | my $obj = shift( @_ ) || return; | ||||||||||
|
Condition0%0/2
shift @_ || (return)
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||
| 46 | 0 | ▶ | return( $self->error( "Object '$obj' provided is not a 'Markdown::Parser::FootnoteReference' object." ) ) if( !$self->_is_a( $obj, 'Markdown::Parser::FootnoteReference' ) ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | if( !$obj->id->length ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | { | ||||||||||||
| 52 | 0 | $obj->id( sprintf( '%d:%s', $self->references->length + 1, $self->id ) ); | |||||||||||
| 53 | } | ||||||||||||
| 54 | 0 | $self->references->push( $obj ); | |||||||||||
| 55 | 0 | return( $self ); | |||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | sub as_markdown | ||||||||||||
| 59 | { | ||||||||||||
| 60 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
as_markdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 61 | 0 | return( sprintf( '*[%s]: %s', $self->name, $self->value ) ); | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | sub as_pod | ||||||||||||
| 65 | { | ||||||||||||
| 66 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
as_pod: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 67 | 0 | 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 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=4.1
Pod
documented
| |||||||||||||
| 76 | 0 | my $arr = $self->new_array; | |||||||||||
| 77 | 0 | my $id = $self->id; | |||||||||||
| 78 | 1 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | 0 | $arr->push( sprintf( '<li id="fn:%s" role="doc-endnote">', $id ) ); | |||||||||||
| 80 | 0 | ▶ | $arr->push( $self->children->map(sub{ $_->as_string })->join( '' )->scalar ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 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 | 0 | ▶ | sprintf( ' :<a href="#fnref:%s" class="reversefootnote" title="Jump back to footnote %s in the text" role="doc-backlink">↩</a>', $_->id, $_->id ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 86 | 0 | })->join( ' ' )->scalar; | |||||||||||
| 87 | 0 | $arr->push( '</li>' ); | |||||||||||
| 88 | 0 | my $str = $arr->join( '' )->scalar; | |||||||||||
| 89 | 0 | $str =~ s/\s\!{2}FN\!{2}/ $backref/; | |||||||||||
| 90 | 0 | return( $str ); | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | sub id { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); } | ||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 94 | |||||||||||||
| 95 | sub parse | ||||||||||||
| 96 | { | ||||||||||||
| 97 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
parse: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 98 | 0 | my $parser = shift( @_ ); | |||||||||||
| 99 | 0 | ▶ | return unless( $self->unparsed->length > 0 ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | my $data = $self->unparsed->scalar; | |||||||||||
| 101 | 0 | $data =~ s/^(?:\s{4}|\t)//gm; | |||||||||||
| 102 | 0 | $parser->parse( $data, { element => $self }); | |||||||||||
| 103 | 0 | $self->unparsed->undef; | |||||||||||
| 104 | 0 | return( $self ); | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub references { return( shift->_set_get_object_array_object( 'reference', 'Markdown::Parser::FootnoteReference', @_ ) ); } | ||||||||||
|
Subroutine
references: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 108 | |||||||||||||
| 109 | sub text | ||||||||||||
| 110 | { | ||||||||||||
| 111 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||
|
Subroutine
text: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 112 | 0 | ▶ | if( @_ ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | { | ||||||||||||
| 114 | 0 | my $text = shift( @_ ); | |||||||||||
| 115 | 0 | return( $self->add_element( $self->create_text({ text => $text }) ) ); | |||||||||||
| 116 | } | ||||||||||||
| 117 | else | ||||||||||||
| 118 | { | ||||||||||||
| 119 | 0 | ▶ | return( $self->children->map(sub{ $_->as_string })->join( '' ) ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 120 | } | ||||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 0 | ▶ | sub unparsed { return( shift->_set_get_scalar_as_object( 'unparsed', @_ ) ); } | ||||||||||
|
Subroutine
unparsed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 124 | |||||||||||||
| 125 | 1 | 1; | |||||||||||
| 126 | # NOTE: POD | ||||||||||||
| 127 | __END__ | ||||||||||||