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

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 260 CC 30 SCAR 49.6
CC
30
Cov
0%
as_string
380.0
add_element
42.0
as_markdown
20.0
CRAP 142.1
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 {
16 use strict;
17 use warnings;
18 use parent qw( Markdown::Parser::Element );
19 use vars qw( $VERSION );
20 our $VERSION = 'v0.3.0';
21 use constant MERMAID_RSRC_URI => 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js';
22 use constant MERMAID_INIT => "mermaid.initialize({startOnLoad:true});";
23 # https://katex.org/
24 use constant KATEX_CSS_URI => 'https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css';
25 use constant KATEX_JS_URI => 'https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js';
26 use constant KATEX_AUTO_RENDERER => 'https://cdn.jsdelivr.net/npm/katex/dist/contrib/auto-render.min.js';
27 use constant KATEX_FONT_URI => 'https://cdn.jsdelivr.net/npm/webfontloader/webfontloader.js';
28 };
29  
30 use strict;
31 use warnings;
32  
33 sub init
34 {
35 my $self = shift( @_ );
36 $self->{fenced} = 0;
37 $self->{id} = [];
38 $self->{css_rsrc} = [qw( https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/default.min.css )];
39 $self->{highlight} = 0;
40 $self->{inline} = 0;
41 $self->{js_data} = 'hljs.initHighlightingOnLoad();';
42 $self->{js_rsrc} = [qw( https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js )];
43 $self->{tag_name} = 'code';
44 return( $self->SUPER::init( @_ ) );
45 }
46  
47 sub as_markdown
48 {
49 my $self = shift( @_ );
50 my $str = $self->children->map(sub{ $_->as_markdown })->join( '' );
51 if( $self->fenced )
52 {
53 return( "```\n${str}\n```\n" );
54 }
55 elsif( $self->inline )
56 {
57 return( "`${str}`" );
58 }
59 elsif( $str->index( "\n" ) != -1 )
60 {
61 my $lines = $str->split( "\n" );
62 $lines->for(sub
63 {
64 my( $i, $val ) = @_;
65 substr( $lines->[ $i ], 0, 0 ) = ' ';
66 });
67 return( $lines->join( "\n" )->scalar );
68 }
69 else
70 {
71 return( "`${str}`" );
72 }
73 }
74  
75 sub as_pod
76 {
77 my $self = shift( @_ );
78 my $str = $self->children->map(sub{ $_->as_pod })->join( '' );
79 if( $self->fenced )
80 {
81 my $lines = $str->split( qr/\n/ );
82 return( ' ' . $lines->join( "\n " )->scalar );
83 }
84 elsif( $self->inline )
85 {
86 if( $str->index( '>' ) != -1 ||
87 $str->index( '<' ) != -1 )
88 {
89 return( "C<< ${str} >>" );
90 }
91 else
92 {
93 return( "C<${str}>" );
94 }
95 }
96 # Essentially same as 'fenced'. Might need to improve the code
97 else
98 {
99 my $lines = $str->split( qr/\n/ );
100 $lines->for(sub
101 {
102 my( $i, $val ) = @_;
103 substr( $lines->[ $i ], 0, 0 ) = ' ';
104 });
105 return( $lines->join( "\n" )->scalar );
106 }
107 }
108  
109 sub as_string
110 {
111 my $self = shift( @_ );
112 my $tag = 'code';
113 my $tag_open = $tag;
114 if( $self->id->length )
115 {
116 $tag_open .= ' ' . $self->id->map(sub{ sprintf( 'id="%s"', $_ ) })->join( ' ' )->scalar;
117 }
118 if( $self->class->length )
119 {
120 $tag_open .= ' class="' . $self->class->join( ' ' )->scalar . '"';
121 ## This does not apply to inline code
122 if( !$self->inline )
123 {
124 if( $self->class->has( 'mermaid' ) )
125 {
126 $self->document->setup_mermaid;
127 }
128 elsif( $self->class->has( 'katex' ) )
129 {
130 $self->document->setup_katex;
131 }
132 else
133 {
134 $self->css_rsrc->foreach(sub
135 {
136 $self->document->add_css_link( $_ ) if( length( $_ ) );
137 });
138 $self->js_rsrc->foreach(sub
139 {
140 $self->document->add_js_link( $_ ) if( length( $_ ) );
141 });
142 $self->document->add_js_data( $self->js_data->scalar ) if( $self->js_data->length > 0 );
143 }
144 }
145 }
146 my $arr = $self->new_array;
147 my $ct = $self->new_array;
148 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 if( $self->class->has( 'mermaid' ) )
152 {
153 $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 : '' ) ) );
154 $ct->push( $self->children->map(sub
155 {
156 $_->as_string;
157 })->list );
158 $arr->push( $ct->join( "\n" )->scalar );
159 $arr->push( "</div>" );
160 return( $arr->join( "\n" )->scalar );
161 }
162 else
163 {
164 $arr->push( "<pre>" ) if( !$self->inline );
165 my $tmp = $self->new_array( [ "<$tag_open" ] );
166 my $attr = $self->new_array;
167 $attr->push( $self->format_id ) if( $self->id->length );
168 $attr->push( $self->format_class ) if( $self->class->length );
169 my $attributes = $self->format_attributes;
170 $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
171 $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
172 $tmp->push( '>' );
173 $arr->push( $tmp->join( '' )->scalar );
174 $ct->push( $self->children->map(sub
175 {
176 $_->as_string;
177 })->list );
178 $arr->push( $ct->join( "<br />\n" )->scalar );
179 $arr->push( "</$tag>" );
180 $arr->push( "</pre>" ) if( !$self->inline );
181 return( $arr->join( ( $self->children->length > 1 || $ct->join( '' )->index( "\n" ) != -1 ) ? "\n" : '' )->scalar );
182 }
183 }
184  
185 sub add_element
186 {
187 my $self = shift( @_ );
188 my $elem = shift( @_ );
189 my $base = $self->base_class;
190 return( $self->error( "Element provided \"$elem\" is not an ${base}::Element object." ) ) if( !$self->_is_a( $elem, "${base}::Element" ) );
191 return( $self->error( "Code accepts only ${base}::Text elements." ) ) if( !$self->_is_a( $elem, "${base}::Text" ) );
192 my $text = $elem->text;
193 if( $text )
194 {
195 $self->encode_html( [qw( < > & )] => $text ) || warn( $self->error );
196 if( $self->inline )
197 {
198 $elem->text( $text );
199 }
200 elsif( $self->fenced )
201 {
202 my @lines = split( /\n/, $text );
203 for( @lines )
204 {
205 s/^(?:\s{4}|\t)//;
206 }
207 $elem->text( join( "\n", @lines ) );
208 }
209 ## Broke it down in 3 conditions for readability
210 else
211 {
212 $elem->text( $text );
213 }
214 }
215 return( $self->SUPER::add_element( $elem ) );
216 }
217  
218 sub css_rsrc { return( shift->_set_get_array_as_object( 'css_rsrc', @_ ) ); }
219  
220 sub fenced { return( shift->_set_get_boolean( 'fenced', @_ ) ); }
221  
222 sub highlight { return( shift->_set_get_boolean( 'highlight', @_ ) ); }
223  
224 sub id { return( shift->_set_get_array_as_object( 'id', @_ ) ); }
225  
226 sub inline { return( shift->_set_get_boolean( 'inline', @_ ) ); }
227  
228 sub js_data { return( shift->_set_get_scalar_as_object( 'js_data', @_ ) ); }
229  
230 sub js_rsrc { return( shift->_set_get_array_as_object( 'js_rsrc', @_ ) ); }
231  
232 1;
233 # NOTE: POD
234 __END__