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

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