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

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 112 CC 5 SCAR 14.1
CC
5
Cov
0%
add_reference
12.0
parse
6.0
text
6.0
CRAP 4.1
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 {
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->{reference} = [];
32 $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 $self->{unparsed} = '';
37 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 {
43 my $self = shift( @_ );
44 my $obj = shift( @_ ) || return;
45 return( $self->error( "Object '$obj' provided is not a 'Markdown::Parser::FootnoteReference' object." ) ) if( !$self->_is_a( $obj, 'Markdown::Parser::FootnoteReference' ) );
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 if( !$obj->id->length )
50 {
51 $obj->id( sprintf( '%d:%s', $self->references->length + 1, $self->id ) );
52 }
53 $self->references->push( $obj );
54 return( $self );
55 }
56  
57 sub as_markdown
58 {
59 my $self = shift( @_ );
60 return( sprintf( '*[%s]: %s', $self->name, $self->value ) );
61 }
62  
63 sub as_pod
64 {
65 my $self = shift( @_ );
66 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.&nbsp;<a href="#fnref:1" class="reversefootnote" role="doc-backlink">↩</a></p>
71 # </li>
72 sub as_string
73 {
74 my $self = shift( @_ );
75 my $arr = $self->new_array;
76 my $id = $self->id;
77 use utf8;
78 $arr->push( sprintf( '<li id="fn:%s" role="doc-endnote">', $id ) );
79 $arr->push( $self->children->map(sub{ $_->as_string })->join( '' )->scalar );
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 {
84 sprintf( '&nbsp:<a href="#fnref:%s" class="reversefootnote" title="Jump back to footnote %s in the text" role="doc-backlink">↩</a>', $_->id, $_->id );
85 })->join( ' ' )->scalar;
86 $arr->push( '</li>' );
87 my $str = $arr->join( '' )->scalar;
88 $str =~ s/\s\!{2}FN\!{2}/ $backref/;
89 return( $str );
90 }
91  
92 sub id { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); }
93  
94 sub parse
95 {
96 my $self = shift( @_ );
97 my $parser = shift( @_ );
98 return unless( $self->unparsed->length > 0 );
99 my $data = $self->unparsed->scalar;
100 $data =~ s/^(?:\s{4}|\t)//gm;
101 $parser->parse( $data, { element => $self });
102 $self->unparsed->undef;
103 return( $self );
104 }
105  
106 sub references { return( shift->_set_get_object_array_object( 'reference', 'Markdown::Parser::FootnoteReference', @_ ) ); }
107  
108 sub text
109 {
110 my $self = shift( @_ );
111 if( @_ )
112 {
113 my $text = shift( @_ );
114 return( $self->add_element( $self->create_text({ text => $text }) ) );
115 }
116 else
117 {
118 return( $self->children->map(sub{ $_->as_string })->join( '' ) );
119 }
120 }
121  
122 sub unparsed { return( shift->_set_get_scalar_as_object( 'unparsed', @_ ) ); }
123  
124 1;
125 # NOTE: POD
126 __END__