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