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

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