Parser/TableCell.pm

filter: Statementstmt 22.7% 23 / 101 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 34.7% 8 / 23 Podpod 100% 10 / 10 totaltotal 23.5% 41 / 174 CC 20 SCAR 31.4
CC
20
Cov
17%
as_css_grid
56.0
as_string
42.0
formatted_lines
30.0
CRAP 23.2
« Parser/Image.pm ↑ Summary Parser/ListItem.pm »
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 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 ▶ use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our $VERSION = 'v0.3.0';
22 };
23  
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub init
28 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
29 0 my $self = shift( @_ );
30 0 $self->{tag_name} = 'td';
31 0 return( $self->SUPER::init( @_ ) );
32 }
33  
34 0 ▶sub align { return( shift->_set_get_scalar_as_object( 'align', @_ ) ); }
Subroutine align: not called CC=1 SCAR=6.9
Pod documented
35  
36 sub as_css_grid
37 0 ▶{
Subroutine as_css_grid: not called CC=7 SCAR=40.3
Pod documented
38 0 my $self = shift( @_ );
39 0 ▶ return( $self->{_as_css_grid} ) if( $self->{_as_css_grid} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_as_css_grid"} 0 0 0
40 0 my $css = $self->css_inline;
41 ## Dummy rule container to contain all the inline rules
42 0 my $sel = $css->builder->select( ['#dummy'] );
43 0 my $arr = $self->new_array;
44 0 my $span = $self->colspan;
45 0 my $align = $self->align;
46 0 my $tag;
47 0 ▶ if( $span > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($span > 1) 0 0 0
48 {
49 0 $sel->grid_column( "span $span" );
50 }
51 0 ▶ if( $align eq 'center' )
Branch: 2 branches
BranchTrueFalseTotal
if ($align eq 'center') { } 0 0 0
elsif ($align eq 'right') { } 0 0 0
52 {
53 0 $sel->justify_self( 'center' );
54 }
55 elsif( $align eq 'right' )
56 {
57 0 $sel->justify_self( 'right' );
58 }
59
60 0 my $elem = $self->new_array( [qw( <div )] );
61 0 ▶ if( $sel->elements->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->elements->length) 0 0 0
62 {
63 0 my $style = $css->builder->as_string;
64 0 $elem->push( "style=\"$style\"" );
65 }
66 0 ▶ if( $self->class->length > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->length > 0) 0 0 0
67 {
68 0 $elem->push( sprintf( 'class="%s"', $self->class->join( ' ' ) ) );
69 }
70 0 $elem->push( '>' );
71
72 0 $arr->push( $elem->join( ' ' ) );
73 0 ▶ $arr->push( $self->children->map(sub{ $_->as_markdown })->join( "\n" )->scalar );
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 $arr->push( "</div>" );
75 0 $self->{_as_css_grid} = $arr->join( "\n" )->scalar;
76 0 return( $self->{_as_css_grid} );
77 }
78  
79 0 ▶sub as_markdown { return( shift->formatted_lines->join( "\n" ) ); }
Subroutine as_markdown: not called CC=1 SCAR=6.9
Pod documented
80  
81 0 ▶sub as_pod { return( shift->formatted_lines( type => 'pod' )->join( "\n" ) ); }
Subroutine as_pod: not called CC=1 SCAR=6.9
Pod documented
82  
83 sub as_string
84 0 ▶{
Subroutine as_string: not called CC=6 SCAR=37.4
Pod documented
85 0 my $self = shift( @_ );
86 0 ▶ return( $self->{_as_string} ) if( $self->{_as_string} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_as_string"} 0 0 0
87 0 my $tag = $self->tag_name;
88 0 my $tag_open = $tag;
89 0 my $arr = $self->new_array;
90 0 $arr->push( "<$tag_open" );
91 0 my $attr = $self->new_array;
92 0 ▶ $attr->push( $self->format_id ) if( $self->id->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->id->length 0 0 0
93 0 ▶ $attr->push( $self->format_class ) if( $self->class->length );
Branch: 1 branch
BranchTrueFalseTotal
if $self->class->length 0 0 0
94 0 my $attributes = $self->format_attributes;
95 0 ▶ $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attributes->length 0 0 0
96 0 ▶ $arr->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
Branch: 1 branch
BranchTrueFalseTotal
if $attr->length 0 0 0
97 0 $arr->push( '>' );
98 0 ▶ $arr->push( $self->children->map(sub{ $_->as_string })->join( "\n" )->scalar );
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 $arr->push( "</$tag>" );
100 0 $self->{_as_string} = $arr->join( '' )->scalar;
101 0 return( $self->{_as_string} );
102 }
103  
104 0 ▶sub class { return( shift->_set_get_array_as_object( 'class', @_ ) ); }
Subroutine class: not called CC=1 SCAR=6.9
Pod documented
105  
106 0 ▶sub colspan { return( shift->_set_get_number( 'colspan', @_ ) ); }
Subroutine colspan: not called CC=1 SCAR=6.9
Pod documented
107  
108 # For Markdown formatting
109 sub formatted_lines
110 0 ▶{
Subroutine formatted_lines: not called CC=5 SCAR=34.0
Pod documented
111 0 my $self = shift( @_ );
112 0 my $opts = $self->_get_args_as_hash( @_ );
113 0 ▶ $opts->{type} //= 'markdown';
Condition0%0/2
$opts->{"type"} //= "markdown"
l!l
00
MC/DC0%0/1
$opts->{"type"} //= "markdown"
$opts->{"type"}
Truth table0%0/2
A
A= $opts->{"type"}
Aresult
11
00
114 0 my $content;
115 0 ▶ if( $opts->{type} eq 'pod' )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'type'} eq 'pod') { } 0 0 0
116 {
117 0 ▶ $content = $self->children->map(sub{ $_->as_pod })->join( "\n" )->split( "\n" );
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 }
119 else
120 {
121 0 ▶ $content = $self->children->map(sub{ $_->as_markdown })->join( "\n" )->split( "\n" );
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 }
123 0 ▶ my $align = $self->align || 'left';
Condition0%0/2
$self->align || 'left'
l!l
00
MC/DC0%0/1
$self->align || 'left'
$self->align
Truth table0%0/2
A
A= $self->align
Aresult
11
00
124 0 my $width = $self->width;
125 0 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 0 ▶ $width -= ( $colspan > 1 ? ( $colspan - 1 ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$colspan > 1 ? : 0 0 0
129 0 my $cell_data = $self->new_array;
130 $content->foreach(sub
131 0 ▶ {
Subroutine __ANON__: not called CC=5 SCAR=34.0
132 0 my $l = shift( @_ );
133 0 ▶ if( length( $l ) == 0 )
Branch: 4 branches
BranchTrueFalseTotal
if (length $l == 0) { } 0 0 0
elsif ($align eq 'left') { } 0 0 0
elsif ($align eq 'right') { } 0 0 0
elsif ($align eq 'center') { } 0 0 0
134 {
135 0 $cell_data->push( '' );
136 }
137 elsif( $align eq 'left' )
138 {
139 0 $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 0 $cell_data->push( ( ' ' x ( ( $width - 1 ) - length( $l ) ) ) . $l . ' ' );
145 }
146 elsif( $align eq 'center' )
147 {
148 0 my $remain = $width - length( $l );
149 0 my $left = POSIX::round( $remain / 2 );
150 0 my $right = $remain - $left;
151 0 $cell_data->push( ( ' ' x $left ) . $l . ( ' ' x $right ) );
152 }
153 0 });
154 0 return( $cell_data );
155 }
156  
157 0 ▶sub width { return( shift->_set_get_number( 'width', @_ ) ); }
Subroutine width: not called CC=1 SCAR=6.9
Pod documented
158  
159 1 1;
160 # NOTE: POD
161 __END__
« Parser/Image.pm ↑ Summary Parser/ListItem.pm »