lib/Markdown/Parser/Code.pm

filter: Statementstmt 48.2% 69 / 143 Branchbran 11.6% 7 / 60 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 57.1% 20 / 35 Podpod 100% 12 / 12 totaltotal 41.5% 108 / 260 CC 34 SCAR 45.6
CC
34
Cov
36%
as_string
342.0
as_pod
30.0
add_element
13.0
CRAP 95.7
« lib/Markdown/Parser/Link.pm ↑ Summary blib/lib/Markdown/Parser/Link.pm »
1 ##----------------------------------------------------------------------------
2 ## Markdown Parser Only - ~/lib/Markdown/Parser/Code.pm
3 ## Version v0.2.1
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/08/23
7 ## Modified 2022/09/22
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Markdown::Parser::Code;
14 BEGIN
15 {
16 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use parent qw( Markdown::Parser::Element );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ our $VERSION = 'v0.2.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use constant MERMAID_RSRC_URI => 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use constant MERMAID_INIT => "mermaid.initialize({startOnLoad:true});";
Subroutine BEGIN: called CC=1 SCAR=0.0
24 # https://katex.org/
25 2 ▶ use constant KATEX_CSS_URI => 'https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶ use constant KATEX_JS_URI => 'https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶ use constant KATEX_AUTO_RENDERER => 'https://cdn.jsdelivr.net/npm/katex/dist/contrib/auto-render.min.js';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶ use constant KATEX_FONT_URI => 'https://cdn.jsdelivr.net/npm/webfontloader/webfontloader.js';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 };
30  
31 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub init
35 {
36 1 ▶ my $self = shift( @_ );
Subroutine init: called CC=1 SCAR=0.0
Pod documented
37 1 $self->{fenced} = 0;
38 1 $self->{id} = [];
39 1 $self->{css_rsrc} = [qw( https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/default.min.css )];
40 1 $self->{highlight} = 0;
41 1 $self->{inline} = 0;
42 1 $self->{js_data} = 'hljs.initHighlightingOnLoad();';
43 1 $self->{js_rsrc} = [qw( https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js )];
44 1 $self->{tag_name} = 'code';
45 1 return( $self->SUPER::init( @_ ) );
46 }
47  
48 sub as_markdown
49 {
50 1 ▶ my $self = shift( @_ );
Subroutine as_markdown: called CC=4 SCAR=20.3
Pod documented
51 1 ▶ my $str = $self->children->map(sub{ $_->as_markdown })->join( '' );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 ▶ if( $self->fenced )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->fenced) { } 0 1 1
elsif ($self->inline) { } 1 0 1
elsif ($str->index("\n") != -1) { } 0 0 0
53 {
54 0 return( "```\n${str}\n```\n" );
55 }
56 elsif( $self->inline )
57 {
58 1 return( "`${str}`" );
59 }
60 elsif( $str->index( "\n" ) != -1 )
61 {
62 0 my $lines = $str->split( "\n" );
63 $lines->for(sub
64 {
65 0 ▶ my( $i, $val ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 substr( $lines->[ $i ], 0, 0 ) = ' ';
67 0 });
68 0 return( $lines->join( "\n" )->scalar );
69 }
70 else
71 {
72 0 return( "`${str}`" );
73 }
74 }
75  
76 sub as_pod
77 {
78 0 ▶ my $self = shift( @_ );
Subroutine as_pod: not called CC=5 SCAR=34.0
Pod documented
79 0 ▶ my $str = $self->children->map(sub{ $_->as_pod })->join( '' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 ▶ if( $self->fenced )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->fenced) { } 0 0 0
elsif ($self->inline) { } 0 0 0
81 {
82 0 my $lines = $str->split( qr/\n/ );
83 0 return( ' ' . $lines->join( "\n " )->scalar );
84 }
85 elsif( $self->inline )
86 {
87 0 ▶ if( $str->index( '>' ) != -1 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($str->index('>') != -1 or $str->index('<') != -1) { } 0 0 0
Condition0%0/3
$str->index(">") != -1 or $str->index("<") != -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$str->index(">") != -1 or $str->index("<") != -1
$str->index(">") != -1 $str->index("<") != -1
Truth table0%0/3
A or B
A= $str->index(">") != -1B= $str->index("<") != -1
ABresult
1X1
011
000
88 $str->index( '<' ) != -1 )
89 {
90 0 return( "C<< ${str} >>" );
91 }
92 else
93 {
94 0 return( "C<${str}>" );
95 }
96 }
97 # Essentially same as 'fenced'. Might need to improve the code
98 else
99 {
100 0 my $lines = $str->split( qr/\n/ );
101 $lines->for(sub
102 {
103 0 ▶ my( $i, $val ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 substr( $lines->[ $i ], 0, 0 ) = ' ';
105 0 });
106 0 return( $lines->join( "\n" )->scalar );
107 }
108 }
109  
110 sub as_string
111 {
112 0 ▶ my $self = shift( @_ );
Subroutine as_string: not called CC=18 SCAR=58.3
Pod documented
113 0 my $tag = 'code';
114 0 my $tag_open = $tag;
115 0 ▶ if( $self->id->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id->length) 0 0 0
116 {
117 0 ▶ $tag_open .= ' ' . $self->id->map(sub{ sprintf( 'id="%s"', $_ ) })->join( ' ' )->scalar;
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 }
119 0 ▶ if( $self->class->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->length) 0 0 0
120 {
121 0 $tag_open .= ' class="' . $self->class->join( ' ' )->scalar . '"';
122 ## This does not apply to inline code
123 0 ▶ if( !$self->inline )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->inline) 0 0 0
124 {
125 0 ▶ if( $self->class->has( 'mermaid' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->class->has('mermaid')) { } 0 0 0
elsif ($self->class->has('katex')) { } 0 0 0
126 {
127 0 $self->document->setup_mermaid;
128 }
129 elsif( $self->class->has( 'katex' ) )
130 {
131 0 $self->document->setup_katex;
132 }
133 else
134 {
135 $self->css_rsrc->foreach(sub
136 {
137 0 ▶ $self->document->add_css_link( $_ ) if( length( $_ ) );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if length $_ 0 0 0
138 0 });
139 $self->js_rsrc->foreach(sub
140 {
141 0 ▶ $self->document->add_js_link( $_ ) if( length( $_ ) );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if length $_ 0 0 0
142 0 });
143 0 ▶ $self->document->add_js_data( $self->js_data->scalar ) if( $self->js_data->length > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->js_data->length > 0 0 0 0
144 }
145 }
146 }
147 0 my $arr = $self->new_array;
148 0 my $ct = $self->new_array;
149 0 my $attributes = $self->format_attributes;
150 ## If the class is mermaid, we return a div with the content as per the Mermaid manual instructions:
151 ## https://mermaid-js.github.io/mermaid/#/n00b-gettingStarted
152 0 ▶ if( $self->class->has( 'mermaid' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->has('mermaid')) { } 0 0 0
153 {
154 0 ▶ $arr->push( sprintf( '<div %sclass="%s"%s>', ( $self->id->length > 0 ? 'id="' . $self->id->first . '" ' : '' ), $self->class->join( ' ' )->scalar, ( $attributes->length > 0 ? ' ' . $attributes->join( ' ' )->scalar : '' ) ) );
Branch: 2 branches
BranchTrueFalseTotal
$self->id->length > 0 ? : 0 0 0
$attributes->length > 0 ? : 0 0 0
155 $ct->push( $self->children->map(sub
156 {
157 0 ▶ $_->as_string;
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 })->list );
159 0 $arr->push( $ct->join( "\n" )->scalar );
160 0 $arr->push( "</div>" );
161 0 return( $arr->join( "\n" )->scalar );
162 }
163 else
164 {
165 0 ▶ $arr->push( "<pre>" ) if( !$self->inline );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->inline 0 0 0
166 0 my $tmp = $self->new_array( [ "<$tag_open" ] );
167 0 my $attr = $self->new_array;
168 0 ▶ $attr->push( $self->format_id ) if( $self->id->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->id->length 0 0 0
169 0 ▶ $attr->push( $self->format_class ) if( $self->class->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->class->length 0 0 0
170 0 my $attributes = $self->format_attributes;
171 0 ▶ $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attributes->length 0 0 0
172 0 ▶ $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attr->length 0 0 0
173 0 $tmp->push( '>' );
174 0 $arr->push( $tmp->join( '' )->scalar );
175 $ct->push( $self->children->map(sub
176 {
177 0 ▶ $_->as_string;
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 })->list );
179 0 $arr->push( $ct->join( "<br />\n" )->scalar );
180 0 $arr->push( "</$tag>" );
181 0 ▶ $arr->push( "</pre>" ) if( !$self->inline );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->inline 0 0 0
182 0 ▶ return( $arr->join( ( $self->children->length > 1 || $ct->join( '' )->index( "\n" ) != -1 ) ? "\n" : '' )->scalar );
Branch: 1 branch
BranchTrueFalseTotal
$self->children->length > 1 || $ct->join('')->index("\n") != -1 ? : 0 0 0
Condition0%0/3
$self->children->length > 1 or $ct->join("")->index("\n") != -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->children->length > 1 or $ct->join("")->index("\n") != -1
$self->children->length > 1 $ct->join("")->index("\n") != -1
Truth table0%0/3
A or B
A= $self->children->length > 1B= $ct->join("")->index("\n") != -1
ABresult
1X1
011
000
183 }
184 }
185  
186 sub add_element
187 {
188 1 ▶ my $self = shift( @_ );
Subroutine add_element: called CC=8 SCAR=25.7
Pod documented
189 1 my $elem = shift( @_ );
190 1 my $base = $self->base_class;
191 1 ▶ return( $self->error( "Element provided \"$elem\" is not an ${base}::Element object." ) ) if( !$self->_is_a( $elem, "${base}::Element" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($elem, "${base}::Element") 0 1 1
192 1 ▶ return( $self->error( "Code accepts only ${base}::Text elements." ) ) if( !$self->_is_a( $elem, "${base}::Text" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($elem, "${base}::Text") 0 1 1
193 1 my $text = $elem->text;
194 1 ▶ if( $text )
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 1 0 1
195 {
196 1 ▶ $self->encode_html( [qw( < > & )] => $text ) || warn( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_html(["<", ">", "&"], $text) or warn $self->error 0 1 1
197 1 ▶ if( $self->inline )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->inline) { } 1 0 1
elsif ($self->fenced) { } 0 0 0
198 {
199 1 $elem->text( $text );
200 }
201 elsif( $self->fenced )
202 {
203 0 my @lines = split( /\n/, $text );
204 0 for( @lines )
205 {
206 0 s/^(?:\s{4}|\t)//;
207 }
208 0 $elem->text( join( "\n", @lines ) );
209 }
210 ## Broke it down in 3 conditions for readability
211 else
212 {
213 0 $elem->text( $text );
214 }
215 }
216 1 return( $self->SUPER::add_element( $elem ) );
217 }
218  
219 0 ▶sub css_rsrc { return( shift->_set_get_array_as_object( 'css_rsrc', @_ ) ); }
Subroutine css_rsrc: not called CC=1 SCAR=6.9
Pod documented
220  
221 2 ▶sub fenced { return( shift->_set_get_boolean( 'fenced', @_ ) ); }
Subroutine fenced: called CC=1 SCAR=0.0
Pod documented
222  
223 0 ▶sub highlight { return( shift->_set_get_boolean( 'highlight', @_ ) ); }
Subroutine highlight: not called CC=1 SCAR=6.9
Pod documented
224  
225 0 ▶sub id { return( shift->_set_get_array_as_object( 'id', @_ ) ); }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
226  
227 3 ▶sub inline { return( shift->_set_get_boolean( 'inline', @_ ) ); }
Subroutine inline: called CC=1 SCAR=0.0
Pod documented
228  
229 0 ▶sub js_data { return( shift->_set_get_scalar_as_object( 'js_data', @_ ) ); }
Subroutine js_data: not called CC=1 SCAR=6.9
Pod documented
230  
231 0 ▶sub js_rsrc { return( shift->_set_get_array_as_object( 'js_rsrc', @_ ) ); }
Subroutine js_rsrc: not called CC=1 SCAR=6.9
Pod documented
232  
233 2 1;
234 # NOTE: POD
235 __END__
« lib/Markdown/Parser/Link.pm ↑ Summary blib/lib/Markdown/Parser/Link.pm »