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