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

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 183 CC 18 SCAR 34.4
CC
18
Cov
0%
as_markdown
56.0
as_pod
56.0
as_string
30.0
CRAP 31.2
1 ##----------------------------------------------------------------------------
2 ## Markdown Parser Only - ~/lib/Markdown/Parser/TableRow.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::TableRow;
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.0';
22 };
23  
24 use strict;
25 use warnings;
26  
27 sub init
28 {
29 my $self = shift( @_ );
30 $self->{cols} = [];
31 $self->{height} = '';
32 $self->{tag_name} = 'tr';
33 $self->{_as_markdown} = '';
34 return( $self->SUPER::init( @_ ) );
35 }
36  
37 sub as_css_grid
38 {
39 my $self = shift( @_ );
40 return( $self->{_as_css_grid} ) if( $self->{_as_css_grid} );
41 ## There is no "row" in css grid. We just get the cells as divs and pass them along
42 my $cells = $self->children->map(sub{ $_->as_css_grid });
43 $self->{_as_css_grid} = $cells->join( "\n" );
44 return( $self->{_as_css_grid} );
45 }
46  
47 sub as_markdown
48 {
49 my $self = shift( @_ );
50 return( $self->{_as_markdown} ) if( $self->{_as_markdown} );
51 my $cell_data = $self->new_array;
52 my $cells = $self->new_array;
53 $self->children->foreach(sub
54 {
55 my $cell = shift( @_ );
56 my $data = $cell->formatted_lines;
57 $cell_data->push( $data );
58 $cells->push( $cell );
59 # Get the height of the row by taking the highest cell
60 $self->height( $data->length ) if( $data->length > $self->height );
61 });
62 my $row_height = $self->height;
63 # $row_height->debug( 3 );
64 my $row_data = $self->new_array;
65 for( my $i = 0; $i < $row_height; $i++ )
66 {
67 # Pipes at start of row
68 if( $i == 0 )
69 {
70 $row_data->push( '|' );
71 }
72 else
73 {
74 $row_data->push( ':' );
75 }
76 $cell_data->for(sub
77 {
78 my( $j, $data ) = @_;
79 my $cell = $cells->get( $j );
80 # This cell is bigger or equal to the current height position, push its data
81 if( $data->size >= $i )
82 {
83 $row_data->push( $data->get( $i ) );
84 }
85 # We are processing more new lines than there is in this cell, so we put blanks instead
86 else
87 {
88 $row_data->push( ' ' x $cell->width );
89 }
90
91 # Pipes after this cell, possibly multiple pipes for cells spanning multiple cells
92 if( $i == 0 )
93 {
94 $row_data->push( '|' x $cell->colspan );
95 }
96 else
97 {
98 $row_data->push( ':' x $cell->colspan );
99 }
100 });
101 $row_data->push( "\n" ) if( $row_height > 1 && $i < ( $row_height - 1 ) );
102 }
103 $self->{_as_markdown} = $row_data->join( '' );
104 return( $self->{_as_markdown} );
105 }
106  
107 sub as_pod
108 {
109 my $self = shift( @_ );
110 return( $self->{_as_pod} ) if( $self->{_as_pod} );
111 my $cell_data = $self->new_array;
112 my $cells = $self->new_array;
113 $self->children->foreach(sub
114 {
115 my $cell = shift( @_ );
116 my $data = $cell->formatted_lines( type => 'pod' );
117 $cell_data->push( $data );
118 $cells->push( $cell );
119 # Get the height of the row by taking the highest cell
120 $self->height( $data->length ) if( $data->length > $self->height );
121 });
122 my $row_height = $self->height;
123 my $row_data = $self->new_array;
124 for( my $i = 0; $i < $row_height; $i++ )
125 {
126 # Pipes at start of row
127 if( $i == 0 )
128 {
129 $row_data->push( '|' );
130 }
131 else
132 {
133 $row_data->push( ':' );
134 }
135 $cell_data->for(sub
136 {
137 my( $j, $data ) = @_;
138 my $cell = $cells->get( $j );
139 # This cell is bigger or equal to the current height position, push its data
140 if( $data->size >= $i )
141 {
142 $row_data->push( $data->get( $i ) );
143 }
144 # We are processing more new lines than there is in this cell, so we put blanks instead
145 else
146 {
147 $row_data->push( ' ' x $cell->width );
148 }
149
150 # Pipes after this cell, possibly multiple pipes for cells spanning multiple cells
151 if( $i == 0 )
152 {
153 $row_data->push( '|' x $cell->colspan );
154 }
155 else
156 {
157 $row_data->push( ':' x $cell->colspan );
158 }
159 });
160 $row_data->push( "\n" ) if( $row_height > 1 && $i < ( $row_height - 1 ) );
161 }
162 $self->{_as_pod} = $row_data->join( '' );
163 return( $self->{_as_pod} );
164 }
165  
166 sub as_string
167 {
168 my $self = shift( @_ );
169 my $arr = $self->new_array;
170 my $tag = $self->tag_name;
171 my $tag_open = $tag;
172 my $tmp = $self->new_array;
173 $tmp->push( "<$tag_open" );
174 my $attr = $self->new_array;
175 $attr->push( $self->format_id ) if( $self->id->length );
176 $attr->push( $self->format_class ) if( $self->class->length );
177 my $attributes = $self->format_attributes;
178 $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
179 $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
180 $tmp->push( '>' );
181 $arr->push( $tmp->join( '' )->scalar );
182 $arr->push( $self->children->map(sub
183 {
184 $_->as_string;
185 })->list );
186 $arr->push( "</$tag>" );
187 return( $arr->join( "\n" )->scalar );
188 }
189  
190 sub height { return( shift->_set_get_number( 'height', @_ ) ); }
191  
192 1;
193 # NOTE: POD
194 __END__