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

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 143 CC 11 SCAR 27.0
CC
11
Cov
0%
as_string
42.0
as_markdown
12.0
as_pod
12.0
CRAP 14.8
1 ##----------------------------------------------------------------------------
2 ## Markdown Parser Only - ~/lib/Markdown/Parser/TableHeader.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::TableHeader;
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->{rows} = [];
31 $self->{tag_name} = 'thead';
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 my $row_data = $self->new_array;
41 ## Check each row
42 $self->children->for(sub
43 {
44 my( $i, $row ) = @_;
45 ## Set the class header for cell of the header row
46 $row->children->map(sub{ $_->class( 'table-header' ) });
47 $row_data->push( $row->as_css_grid->scalar );
48 });
49 $self->{_as_css_grid} = $row_data->join( "\n" )->scalar;
50 return( $self->{_as_css_grid} );
51 }
52  
53 sub as_markdown
54 {
55 my $self = shift( @_ );
56 return( $self->{_as_markdown} ) if( $self->{_as_markdown} );
57 my $row_data = $self->new_array;
58 # Check each row
59 $self->children->for(sub
60 {
61 my( $i, $row ) = @_;
62 my $sep = $self->new_array;
63 $sep->push( '+' );
64 $row->children->foreach(sub
65 {
66 my $cell = shift( @_ );
67 $sep->push( ( '-' x $cell->width ) . '+' );
68 });
69 # Push top horizontal line if this is the first upper line
70 $row_data->push( $sep->join( '' )->scalar ) if( $i == 0 );
71 my $row_str = $row->as_markdown;
72 $row_data->push( $row_str->scalar );
73 $row_data->push( $sep->join( '' )->scalar );
74 });
75 $self->{_as_markdown} = $row_data->join( "\n" )->scalar;
76 return( $self->{_as_markdown} );
77 }
78  
79 sub as_pod
80 {
81 my $self = shift( @_ );
82 return( $self->{_as_pod} ) if( $self->{_as_pod} );
83 my $row_data = $self->new_array;
84 # Check each row
85 $self->children->for(sub
86 {
87 my( $i, $row ) = @_;
88 my $sep = $self->new_array;
89 $sep->push( '+' );
90 $row->children->foreach(sub
91 {
92 my $cell = shift( @_ );
93 $sep->push( ( '-' x $cell->width ) . '+' );
94 });
95 # Push top horizontal line if this is the first upper line
96 $row_data->push( $sep->join( '' )->scalar ) if( $i == 0 );
97 my $row_str = $row->as_pod;
98 $row_data->push( $row_str->scalar );
99 $row_data->push( $sep->join( '' )->scalar );
100 });
101 $self->{_as_pod} = $row_data->join( "\n" )->scalar;
102 return( $self->{_as_pod} );
103 }
104  
105 sub as_string
106 {
107 my $self = shift( @_ );
108 return( $self->{_as_string} ) if( $self->{_as_string} );
109 my $arr = $self->new_array;
110 my $tag = $self->tag_name;
111 my $tag_open = $tag;
112 my $tmp = $self->new_array;
113 $tmp->push( "<$tag_open" );
114 my $attr = $self->new_array;
115 $attr->push( $self->format_id ) if( $self->id->length );
116 $attr->push( $self->format_class ) if( $self->class->length );
117 my $attributes = $self->format_attributes;
118 $attr->push( $attributes->join( ' ' )->scalar ) if( $attributes->length );
119 $tmp->push( ' ' . $attr->join( ' ' )->scalar ) if( $attr->length );
120 $tmp->push( '>' );
121 $arr->push( $tmp->join( '' )->scalar );
122 $arr->push( $self->children->map(sub
123 {
124 $_->as_string;
125 })->list );
126 $arr->push( "</$tag>" );
127 $self->{_as_string} = $arr->join( "\n" )->scalar;
128 return( $self->{_as_string} );
129 }
130  
131 sub reset
132 {
133 my $self = shift( @_ );
134 delete( @$self{ qw( _as_css_grid _as_markdown _as_string ) } );
135 return( $self );
136 }
137  
138 1;
139 # NOTE: POD
140 __END__