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

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