Parser/Code.pm

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