| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Markdown Parser Only - ~/lib/Markdown/Parser/Table.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::Table; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use parent qw( Markdown::Parser::Element ); | ||
| 19 | use vars qw( $VERSION ); | ||
| 20 | use POSIX (); | ||
| 21 | use Scalar::Util (); | ||
| 22 | use constant CHAR2PIXELS => 8; | ||
| 23 | our $VERSION = 'v0.3.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | $self->{bodies} = []; | ||
| 33 | $self->{caption} = ''; | ||
| 34 | $self->{headers} = []; | ||
| 35 | $self->{tag_name} = 'table'; | ||
| 36 | $self->{use_css_grid} = 0; | ||
| 37 | return( $self->SUPER::init( @_ ) ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub as_css_grid | ||
| 41 | { | ||
| 42 | my $self = shift( @_ ); | ||
| 43 | return( $self->{_as_css_grid} ) if( $self->{_as_css_grid} ); | ||
| 44 | ## css(9 method is inherited from Markdown::Parser::Element | ||
| 45 | my $css = $self->css || return( $self->error( "No CSS::Object was set using the css() method. This should be set by Markdown::Parser::parse_table" ) ); | ||
| 46 | my $b = $css->builder; | ||
| 47 | my $id = Scalar::Util::refaddr( $self ); | ||
| 48 | my $stat = $self->stat; | ||
| 49 | my $cols = $self->new_array; | ||
| 50 | my $total_cols = $stat->{cols}->{total}; | ||
| 51 | my $total_rows = $stat->{rows}->{total}; | ||
| 52 | for( my $i = 0; $i < $total_cols; $i++ ) | ||
| 53 | { | ||
| 54 | $cols->push( sprintf( '%dpx', int( $stat->{cols}->{ $i } ) * 8 ) ) | ||
| 55 | } | ||
| 56 | # .grid | ||
| 57 | # { | ||
| 58 | # display: grid; | ||
| 59 | # grid-template-columns: repeat( 4, 1fr ); | ||
| 60 | # grid-template-rows: repeat( 3, 1fr ); | ||
| 61 | # gap: 1px 1px; | ||
| 62 | # } | ||
| 63 | ## Create the css for this table | ||
| 64 | my $grid = $b->select( [".grid-${id}"] ) | ||
| 65 | ->display( 'grid' ) | ||
| 66 | ->grid_template_columns( $cols->join( ' ' ) ) | ||
| 67 | ->grid_template_rows( sprintf( 'repeat %d, 1fr', $total_rows ) ) | ||
| 68 | ->gap( '1px 1px' ); | ||
| 69 | ## Set the table header css rule | ||
| 70 | my $hdr_rule = $css->get_rule_by_selector( '.table-header' ); | ||
| 71 | unless( $hdr_rule ) | ||
| 72 | { | ||
| 73 | $b->select( ['.table-header'] ) | ||
| 74 | ->font_weight( 'bold' ); | ||
| 75 | } | ||
| 76 | my $arr = $self->new_array; | ||
| 77 | my $cap = $self->caption; | ||
| 78 | my $hdr = $self->header; | ||
| 79 | my $bodies = $self->bodies; | ||
| 80 | $arr->push( $cap->as_css_grid ) if( $cap ); | ||
| 81 | $arr->push( "<div class=\"grid-${id}\">" ); | ||
| 82 | if( $hdr ) | ||
| 83 | { | ||
| 84 | $arr->push( $hdr->as_css_grid ); | ||
| 85 | } | ||
| 86 | $bodies->for(sub | ||
| 87 | { | ||
| 88 | my( $i, $body ) = @_; | ||
| 89 | ## If there is more than one body, we separate them with one blank line | ||
| 90 | $arr->push( "\n" ) if( $i > 0 ); | ||
| 91 | $arr->push( $body->as_css_grid ); | ||
| 92 | $arr->push( "\n" ); | ||
| 93 | }); | ||
| 94 | $arr->push( "</div>" ); | ||
| 95 | $self->{_as_css_grid} = $arr->join( "\n" )->scalar; | ||
| 96 | return( $self->{_as_css_grid} ); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub as_markdown | ||
| 100 | { | ||
| 101 | my $self = shift( @_ ); | ||
| 102 | return( $self->{_as_mardown} ) if( $self->{_as_mardown} ); | ||
| 103 | my $arr = $self->new_array; | ||
| 104 | my $cap = $self->caption; | ||
| 105 | my $hdr = $self->header; | ||
| 106 | my $bodies = $self->bodies; | ||
| 107 | |||
| 108 | $arr->push( ' ' . $cap->as_markdown ) if( $cap && $cap->position eq 'top' ); | ||
| 109 | my $tag = $self->tag_name; | ||
| 110 | my $tag_open = $tag; | ||
| 111 | my $tmp = $self->new_array; | ||
| 112 | $tmp->push( "<$tag_open" ); | ||
| 113 | my $attr = $self->new_array; | ||
| 114 | $attr->push( $self->format_id ) if( $self->id->length ); | ||
| 115 | $attr->push( $self->format_class ) if( $self->class->length ); | ||
| 116 | my $attributes = $self->format_attributes; | ||
| 117 | $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length ); | ||
| 118 | $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length ); | ||
| 119 | $tmp->push( '>' ); | ||
| 120 | $arr->push( $tmp->join( '' )->scalar ); | ||
| 121 | if( $hdr ) | ||
| 122 | { | ||
| 123 | $arr->push( $hdr->as_markdown ); | ||
| 124 | } | ||
| 125 | $arr->push( "\n" ) if( $bodies->length ); | ||
| 126 | |||
| 127 | $bodies->for(sub | ||
| 128 | { | ||
| 129 | my( $i, $body ) = @_; | ||
| 130 | ## If there is more than one body, we separate them with one blank line | ||
| 131 | $arr->push( "\n" ) if( $i > 0 ); | ||
| 132 | $arr->push( $body->as_markdown ); | ||
| 133 | $arr->push( "\n" ); | ||
| 134 | }); | ||
| 135 | $arr->push( ' ' . $cap->as_markdown ) if( $cap && $cap->position eq 'bottom' ); | ||
| 136 | $self->{_as_mardown} = $arr->join( '' )->scalar; | ||
| 137 | return( $self->{_as_mardown} ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub as_pod | ||
| 141 | { | ||
| 142 | my $self = shift( @_ ); | ||
| 143 | return( $self->{_as_pod} ) if( $self->{_as_pod} ); | ||
| 144 | my $arr = $self->new_array; | ||
| 145 | my $cap = $self->caption; | ||
| 146 | my $hdr = $self->header; | ||
| 147 | my $bodies = $self->bodies; | ||
| 148 | |||
| 149 | $arr->push( ' ' . $cap->as_pod ) if( $cap && $cap->position eq 'top' ); | ||
| 150 | my $tag = $self->tag_name; | ||
| 151 | my $tag_open = $tag; | ||
| 152 | if( $hdr ) | ||
| 153 | { | ||
| 154 | $arr->push( $hdr->as_pod ); | ||
| 155 | } | ||
| 156 | $arr->push( "\n" ) if( $bodies->length ); | ||
| 157 | |||
| 158 | $bodies->for(sub | ||
| 159 | { | ||
| 160 | my( $i, $body ) = @_; | ||
| 161 | # If there is more than one body, we separate them with one blank line | ||
| 162 | $arr->push( "\n" ) if( $i > 0 ); | ||
| 163 | $arr->push( $body->as_pod ); | ||
| 164 | $arr->push( "\n" ); | ||
| 165 | }); | ||
| 166 | $arr->push( ' ' . $cap->as_pod ) if( $cap && $cap->position eq 'bottom' ); | ||
| 167 | $self->{_as_pod} = $arr->join( '' )->scalar; | ||
| 168 | return( $self->{_as_pod} ); | ||
| 169 | } | ||
| 170 | |||
| 171 | # Ref: https://www.w3schools.com/TAgs/tag_col.asp | ||
| 172 | sub as_string | ||
| 173 | { | ||
| 174 | my $self = shift( @_ ); | ||
| 175 | return( $self->as_css_grid ) if( $self->use_css_grid ); | ||
| 176 | my $arr = $self->new_array; | ||
| 177 | my $tag = $self->tag_name; | ||
| 178 | my $tag_open = $tag; | ||
| 179 | my $caption = $self->caption; | ||
| 180 | $arr->push( "<$tag_open>" ); | ||
| 181 | $arr->push( $caption->as_string ) if( $caption ); | ||
| 182 | if( $self->header->children->length ) | ||
| 183 | { | ||
| 184 | $arr->push( $self->header->as_string ); | ||
| 185 | } | ||
| 186 | if( $self->bodies->length ) | ||
| 187 | { | ||
| 188 | $self->bodies->foreach(sub | ||
| 189 | { | ||
| 190 | $arr->push( $_->as_string ); | ||
| 191 | }); | ||
| 192 | } | ||
| 193 | $arr->push( "</$tag>" ); | ||
| 194 | return( $arr->join( "\n" )->scalar ); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub add_body | ||
| 198 | { | ||
| 199 | my $self = shift( @_ ); | ||
| 200 | my $val = shift( @_ ) || return; | ||
| 201 | my $base = $self->base_class; | ||
| 202 | return( $self->error( "Value provided (", overload::StrVal( $val ), ") is not a ${base}::TableBody object" ) ) if( !$self->_is_a( $val, "${base}::TableBody" ) ); | ||
| 203 | $val->parent( $self ); | ||
| 204 | $self->bodies->push( $val ); | ||
| 205 | return( $val ); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub add_header | ||
| 209 | { | ||
| 210 | my $self = shift( @_ ); | ||
| 211 | my $val = shift( @_ ) || return; | ||
| 212 | my $base = $self->base_class; | ||
| 213 | return( $self->error( "Value provided (", overload::StrVal( $val ), ") is not a ${base}::TableHeader object" ) ) if( !$self->_is_a( $val, "${base}::TableHeader" ) ); | ||
| 214 | $val->parent( $self ); | ||
| 215 | $self->headers->push( $val ); | ||
| 216 | return( $val ); | ||
| 217 | } | ||
| 218 | |||
| 219 | sub bodies { return( shift->_set_get_object_array_object( 'bodies', 'Markdown::Parser::TableBody', @_ ) ); } | ||
| 220 | |||
| 221 | # Alias | ||
| 222 | sub body { return( shift->bodies( @_ ) ); } | ||
| 223 | |||
| 224 | sub caption | ||
| 225 | { | ||
| 226 | my $self = shift( @_ ); | ||
| 227 | my $base = $self->base_class; | ||
| 228 | if( @_ ) | ||
| 229 | { | ||
| 230 | my $val = shift( @_ ); | ||
| 231 | return( $self->error( "Value provided (", overload::StrVal( $val ), ") is not a ${base}::TableCaption object" ) ) if( !$self->_is_a( $val, "${base}::TableCaption" ) ); | ||
| 232 | $val->parent( $self ); | ||
| 233 | $self->_set_get_object( 'caption', "${base}::TableCaption", $val ); | ||
| 234 | } | ||
| 235 | return( $self->_set_get_object( 'caption', "${base}::TableCaption" ) ); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub header | ||
| 239 | { | ||
| 240 | my $self = shift( @_ ); | ||
| 241 | my $base = $self->base_class; | ||
| 242 | if( @_ ) | ||
| 243 | { | ||
| 244 | my $val = shift( @_ ) || return; | ||
| 245 | return( $self->error( "Value provided (", overload::StrVal( $val ), ") is not a ${base}::TableHeader object" ) ) if( !$self->_is_a( $val, "${base}::TableHeader" ) ); | ||
| 246 | $val->parent( $self ); | ||
| 247 | $self->_set_get_object( 'header', "${base}::TableHeader", $val ); | ||
| 248 | } | ||
| 249 | return( $self->_set_get_object( 'header', "${base}::TableHeader" ) ); | ||
| 250 | } | ||
| 251 | |||
| 252 | sub headers { return( shift->_set_get_object_array_object( 'headers', 'Markdown::Parser::TableHeader', @_ ) ); } | ||
| 253 | |||
| 254 | sub remove_body | ||
| 255 | { | ||
| 256 | my $self = shift( @_ ); | ||
| 257 | my $val = shift( @_ ) || return; | ||
| 258 | my $base = $self->base_class; | ||
| 259 | return( $self->error( "Value provided (", overload::StrVal( $val ), ") is not a ${base}::TableBody object" ) ) if( !$self->_is_a( $val, "${base}::TableBody" ) ); | ||
| 260 | my $pos = $self->bodies->pos( $val ); | ||
| 261 | return if( !defined( $pos ) ); | ||
| 262 | ## Returned an array object of element removed | ||
| 263 | my $removed = $self->bodies->delete( $pos, 1 ); | ||
| 264 | return( $removed->[0] ); | ||
| 265 | } | ||
| 266 | |||
| 267 | sub reset_stat | ||
| 268 | { | ||
| 269 | my $self = shift( @_ ); | ||
| 270 | $self->stat->reset; | ||
| 271 | return( $self ); | ||
| 272 | } | ||
| 273 | |||
| 274 | # Contains an hash of property and values providing stats on the table and collected upon parsing | ||
| 275 | # They are used to produce the html table or css grid | ||
| 276 | # sub stat { return( shift->_set_get_hash_as_mix_object( 'stat', @_ ) ); } | ||
| 277 | sub stat | ||
| 278 | { | ||
| 279 | my $self = shift( @_ ); | ||
| 280 | my $info = $self->_set_get_hash_as_mix_object( 'stat' ); | ||
| 281 | return( $info ) if( $info && $info->length ); | ||
| 282 | my $get_width; | ||
| 283 | $get_width = sub | ||
| 284 | { | ||
| 285 | my $row = shift( @_ ); | ||
| 286 | $row->children->for(sub | ||
| 287 | { | ||
| 288 | my( $j, $cell ) = @_; | ||
| 289 | $info->{cols} = {} if( !ref( $info->{cols} ) ); | ||
| 290 | return( 1 ) if( length( $info->{cols}->{ $j } ) ); | ||
| 291 | ## Cells are merged into 1, so we cannot get an accurate width of each cell | ||
| 292 | return( 1 ) if( $cell->colspan > 1 ); | ||
| 293 | if( $cell->width ) | ||
| 294 | { | ||
| 295 | $info->{cols}->{ $j } = $cell->width; | ||
| 296 | } | ||
| 297 | ## Find out the width of the cell based on its content | ||
| 298 | else | ||
| 299 | { | ||
| 300 | my $cell_data = $cell->children->map(sub{ $_->as_markdown }); | ||
| 301 | ## Find the longest string in each line within this cell | ||
| 302 | my $max = 0; | ||
| 303 | $cell_data->foreach(sub | ||
| 304 | { | ||
| 305 | ## Split the string into lines | ||
| 306 | my $lines = $self->new_array( [split( /\n/, $_ )] ); | ||
| 307 | $lines->foreach(sub | ||
| 308 | { | ||
| 309 | my $l = shift( @_ ); | ||
| 310 | $max = length( $l ) if( length( $l ) > $max ); | ||
| 311 | }); | ||
| 312 | }); | ||
| 313 | ## Add 1 space on each side | ||
| 314 | $max += 2; | ||
| 315 | $info->{cols}->{ $j } = $max; | ||
| 316 | $cell->width( $max ); | ||
| 317 | } | ||
| 318 | }); | ||
| 319 | }; | ||
| 320 | ## Or else, collect stats | ||
| 321 | if( my $hdr = $self->header ) | ||
| 322 | { | ||
| 323 | $hdr->children->for(sub | ||
| 324 | { | ||
| 325 | my( $i, $row ) = @_; | ||
| 326 | $get_width->( $row ); | ||
| 327 | }); | ||
| 328 | ## Number of rows in the header | ||
| 329 | $info->{header}->{rows} = $hdr->children->length; | ||
| 330 | } | ||
| 331 | $self->bodies->foreach(sub | ||
| 332 | { | ||
| 333 | my $body = shift( @_ ); | ||
| 334 | $body->children->foreach(sub | ||
| 335 | { | ||
| 336 | my $row = shift( @_ ); | ||
| 337 | $get_width->( $row ); | ||
| 338 | }); | ||
| 339 | $info->{rows} = {} if( !ref( $info->{rows} ) ); | ||
| 340 | ## Number of rows in the body | ||
| 341 | $info->{rows}->{total} += $body->children->length; | ||
| 342 | }); | ||
| 343 | $info->{cols}->{total} = scalar( keys( %{$info->{cols}} ) ); | ||
| 344 | foreach my $col_n ( keys( %{$info->{cols}} ) ) | ||
| 345 | { | ||
| 346 | $info->{table}->{width} += $info->{cols}->{ $col_n }; | ||
| 347 | } | ||
| 348 | return( $info ); | ||
| 349 | } | ||
| 350 | |||
| 351 | sub use_css_grid { return( shift->_set_get_boolean( 'use_css_grid', @_ ) ); } | ||
| 352 | |||
| 353 | 1; | ||
| 354 | # NOTE: POD | ||
| 355 | __END__ |