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