| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Markdown Parser Only - ~/lib/Markdown/Parser/TableCell.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::TableCell; | ||
| 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 | our $VERSION = 'v0.3.0'; | ||
| 22 | }; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | |||
| 27 | sub init | ||
| 28 | { | ||
| 29 | my $self = shift( @_ ); | ||
| 30 | $self->{tag_name} = 'td'; | ||
| 31 | return( $self->SUPER::init( @_ ) ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub align { return( shift->_set_get_scalar_as_object( 'align', @_ ) ); } | ||
| 35 | |||
| 36 | sub as_css_grid | ||
| 37 | { | ||
| 38 | my $self = shift( @_ ); | ||
| 39 | return( $self->{_as_css_grid} ) if( $self->{_as_css_grid} ); | ||
| 40 | my $css = $self->css_inline; | ||
| 41 | ## Dummy rule container to contain all the inline rules | ||
| 42 | my $sel = $css->builder->select( ['#dummy'] ); | ||
| 43 | my $arr = $self->new_array; | ||
| 44 | my $span = $self->colspan; | ||
| 45 | my $align = $self->align; | ||
| 46 | my $tag; | ||
| 47 | if( $span > 1 ) | ||
| 48 | { | ||
| 49 | $sel->grid_column( "span $span" ); | ||
| 50 | } | ||
| 51 | if( $align eq 'center' ) | ||
| 52 | { | ||
| 53 | $sel->justify_self( 'center' ); | ||
| 54 | } | ||
| 55 | elsif( $align eq 'right' ) | ||
| 56 | { | ||
| 57 | $sel->justify_self( 'right' ); | ||
| 58 | } | ||
| 59 | |||
| 60 | my $elem = $self->new_array( [qw( <div )] ); | ||
| 61 | if( $sel->elements->length ) | ||
| 62 | { | ||
| 63 | my $style = $css->builder->as_string; | ||
| 64 | $elem->push( "style=\"$style\"" ); | ||
| 65 | } | ||
| 66 | if( $self->class->length > 0 ) | ||
| 67 | { | ||
| 68 | $elem->push( sprintf( 'class="%s"', $self->class->join( ' ' ) ) ); | ||
| 69 | } | ||
| 70 | $elem->push( '>' ); | ||
| 71 | |||
| 72 | $arr->push( $elem->join( ' ' ) ); | ||
| 73 | $arr->push( $self->children->map(sub{ $_->as_markdown })->join( "\n" )->scalar ); | ||
| 74 | $arr->push( "</div>" ); | ||
| 75 | $self->{_as_css_grid} = $arr->join( "\n" )->scalar; | ||
| 76 | return( $self->{_as_css_grid} ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub as_markdown { return( shift->formatted_lines->join( "\n" ) ); } | ||
| 80 | |||
| 81 | sub as_pod { return( shift->formatted_lines( type => 'pod' )->join( "\n" ) ); } | ||
| 82 | |||
| 83 | sub as_string | ||
| 84 | { | ||
| 85 | my $self = shift( @_ ); | ||
| 86 | return( $self->{_as_string} ) if( $self->{_as_string} ); | ||
| 87 | my $tag = $self->tag_name; | ||
| 88 | my $tag_open = $tag; | ||
| 89 | my $arr = $self->new_array; | ||
| 90 | $arr->push( "<$tag_open" ); | ||
| 91 | my $attr = $self->new_array; | ||
| 92 | $attr->push( $self->format_id ) if( $self->id->length ); | ||
| 93 | $attr->push( $self->format_class ) if( $self->class->length ); | ||
| 94 | my $attributes = $self->format_attributes; | ||
| 95 | $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length ); | ||
| 96 | $arr->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length ); | ||
| 97 | $arr->push( '>' ); | ||
| 98 | $arr->push( $self->children->map(sub{ $_->as_string })->join( "\n" )->scalar ); | ||
| 99 | $arr->push( "</$tag>" ); | ||
| 100 | $self->{_as_string} = $arr->join( '' )->scalar; | ||
| 101 | return( $self->{_as_string} ); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub class { return( shift->_set_get_array_as_object( 'class', @_ ) ); } | ||
| 105 | |||
| 106 | sub colspan { return( shift->_set_get_number( 'colspan', @_ ) ); } | ||
| 107 | |||
| 108 | # For Markdown formatting | ||
| 109 | sub formatted_lines | ||
| 110 | { | ||
| 111 | my $self = shift( @_ ); | ||
| 112 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 113 | $opts->{type} //= 'markdown'; | ||
| 114 | my $content; | ||
| 115 | if( $opts->{type} eq 'pod' ) | ||
| 116 | { | ||
| 117 | $content = $self->children->map(sub{ $_->as_pod })->join( "\n" )->split( "\n" ); | ||
| 118 | } | ||
| 119 | else | ||
| 120 | { | ||
| 121 | $content = $self->children->map(sub{ $_->as_markdown })->join( "\n" )->split( "\n" ); | ||
| 122 | } | ||
| 123 | my $align = $self->align || 'left'; | ||
| 124 | my $width = $self->width; | ||
| 125 | my $colspan = $self->colspan; | ||
| 126 | # We need to make room for the additional pipes for colspan | ||
| 127 | # Those space in width were added upon parsing, so now, we need to give it back | ||
| 128 | $width -= ( $colspan > 1 ? ( $colspan - 1 ) : 0 ); | ||
| 129 | my $cell_data = $self->new_array; | ||
| 130 | $content->foreach(sub | ||
| 131 | { | ||
| 132 | my $l = shift( @_ ); | ||
| 133 | if( length( $l ) == 0 ) | ||
| 134 | { | ||
| 135 | $cell_data->push( '' ); | ||
| 136 | } | ||
| 137 | elsif( $align eq 'left' ) | ||
| 138 | { | ||
| 139 | $cell_data->push( ' ' . $l . ( ' ' x ( $width - ( length( $l ) + 1 ) ) ) ); | ||
| 140 | } | ||
| 141 | elsif( $align eq 'right' ) | ||
| 142 | { | ||
| 143 | ## Remove 1 from the width to leave a blank space on the right hand side before the column pip separator | ||
| 144 | $cell_data->push( ( ' ' x ( ( $width - 1 ) - length( $l ) ) ) . $l . ' ' ); | ||
| 145 | } | ||
| 146 | elsif( $align eq 'center' ) | ||
| 147 | { | ||
| 148 | my $remain = $width - length( $l ); | ||
| 149 | my $left = POSIX::round( $remain / 2 ); | ||
| 150 | my $right = $remain - $left; | ||
| 151 | $cell_data->push( ( ' ' x $left ) . $l . ( ' ' x $right ) ); | ||
| 152 | } | ||
| 153 | }); | ||
| 154 | return( $cell_data ); | ||
| 155 | } | ||
| 156 | |||
| 157 | sub width { return( shift->_set_get_number( 'width', @_ ) ); } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | # NOTE: POD | ||
| 161 | __END__ |