TableContent.pm

filter: Statementstmt 93.3% 154 / 165 Branchbran 82.1% 23 / 28 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 92.5% 25 / 27 Podpod 100% 18 / 18 totaltotal 91.0% 224 / 246 CC 30 SCAR 14.6
CC
30
Cov
91%
_create_from_aoh
9.0
filter_tables
7.8
_create_from_aoa
5.0
CRAP 4.3
« TableContent/Template/Base.pm ↑ Summary TableContent/Parser.pm »
1 package HTML::TableContent;
2  
3 18 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use HTML::TableContent::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use HTML::TableContent::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 our $VERSION = '1.00';
10  
11 has parser => (
12 is => 'rw',
13 lazy => 1,
14 122 ▶ default => sub { return HTML::TableContent::Parser->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 18 );
16  
17 has tables => (
18 is => 'rw',
19 lazy => 1,
20 126 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 18 );
22  
23 97 ▶sub all_tables { return @{ $_[0]->tables }; }
Subroutine all_tables: called CC=1 SCAR=0.0
Pod documented
24  
25 sub add_table {
26 9 ▶ my $table = HTML::TableContent::Table->new($_[1]);
Subroutine add_table: called CC=1 SCAR=0.0
Pod documented
27 9 push @{ $_[0]->tables }, $table;
28 9 return $table;
29 }
30  
31 3 ▶sub add_caption_selectors { return push (@{ $_[0]->parser->caption_selectors }, @{ $_[1] }); }
Subroutine add_caption_selectors: called CC=1 SCAR=0.0
Pod documented
32  
33 61 ▶sub get_table { return $_[0]->tables->[ $_[1] ]; }
Subroutine get_table: called CC=1 SCAR=0.0
Pod documented
34  
35 55 ▶sub get_first_table { return $_[0]->get_table(0); }
Subroutine get_first_table: called CC=1 SCAR=0.0
Pod documented
36  
37 0 ▶sub get_last_table { return $_[0]->get_table($_[0]->table_count - 1); }
Subroutine get_last_table: not called CC=1 SCAR=6.9
Pod documented
38  
39 3 ▶sub clear_table { return splice @{ $_[0]->tables }, $_[1], 1; }
Subroutine clear_table: called CC=1 SCAR=0.0
Pod documented
40  
41 1 ▶sub clear_first_table { return shift @{ $_[0]->tables }; }
Subroutine clear_first_table: called CC=1 SCAR=0.0
Pod documented
42  
43 1 ▶sub clear_last_table { return $_[0]->clear_table( $_[0]->table_count - 1 ); }
Subroutine clear_last_table: called CC=1 SCAR=0.0
Pod documented
44  
45 141 ▶sub table_count { return scalar @{ $_[0]->tables }; }
Subroutine table_count: called CC=1 SCAR=0.0
Pod documented
46  
47 sub filter_tables {
48 31 ▶ my ( $self, %args ) = @_;
Subroutine filter_tables: called CC=7 SCAR=20.5
Pod documented
49  
50 31 my $tables = [];
51  
52 31 my @headers = ();
53 31 ▶ if ( defined $args{headers} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'headers'}) { } 29 2 31
elsif (defined $args{'header'}) { } 2 0 2
54 29 push @headers, @{ $args{headers} };
55 }
56 elsif ( defined $args{header} ) {
57 2 push @headers, $args{header};
58 }
59  
60 31 foreach my $table ( $self->all_tables ) {
61 61 ▶ if ( $table->header_exists(@headers) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table->header_exists(@headers)) 49 12 61
62 49 $table->_filter_headers(@headers);
63 49 push @{$tables}, $table;
64 }
65 }
66  
67 31 ▶ if ( $args{flex} && !scalar @{$tables} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"flex"} and not scalar @{$tables;}) 0 31 31
Condition33%1/3
$args{"flex"} and not scalar @{$tables;}
!ll&&!rl&&r
3100
MC/DC0%0/2
$args{"flex"} and not scalar @{$tables;}
$args{"flex"} not scalar @{$tables;}
Truth table33%1/3
A and not(B)
A= $args{"flex"}B= not scalar @{$tables;}
ABresult
0X0
100
111
68 0 carp 'none of the passed headers exist in any of the tables aborting filter - %s',
69 join q{ }, @headers;
70 0 return;
71 }
72  
73 31 return $self->tables($tables);
74 }
75  
76 sub headers_spec {
77 56 ▶ my $self = shift;
Subroutine headers_spec: called CC=3 SCAR=11.0
Pod documented
78  
79 56 my $headers = {};
80 56 for my $table ( $self->all_tables ) {
81 106 for ( $table->all_headers ) { $headers->{ $_->lc_text }++ }
82 }
83  
84 56 return $headers;
85 }
86  
87 sub headers_exist {
88 2 ▶ my ( $self, @headers ) = @_;
Subroutine headers_exist: called CC=3 SCAR=11.7
Pod documented
89  
90 2 my $header_spec = $self->headers_spec;
91  
92 2 ▶ for (@headers) { return 1 if $header_spec->{ lc $_ } }
Branch: 1 branch
BranchTrueFalseTotal
if $$header_spec{lc $_} 2 0 2
93  
94 0 return 0;
95 }
96  
97 sub raw {
98 10 ▶ my $self = shift;
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
99  
100 10 my @tables = map { $_->raw } $self->all_tables;
101 10 return \@tables;
102 }
103  
104 sub render {
105 0 ▶ my $self = shift;
Subroutine render: not called CC=1 SCAR=6.9
Pod documented
106  
107 0 my @tables = map { $_->render } $self->all_tables;
108 0 my $html = sprintf '%s' x @tables, @tables;
109 0 return $html;
110 }
111  
112 sub parse {
113 61 ▶ my ( $self, $data ) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
114  
115 61 $data =~ s/\<\!--|--\!\>//g;
116 61 $self->parser->clear_current_tables;
117 61 my $current_tables = $self->parser->parse($data);
118 61 push @{ $self->tables }, @{$current_tables};
119 61 return $current_tables;
120 }
121  
122 sub parse_file {
123 61 ▶ my ( $self, $file ) = @_;
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
124  
125 61 $self->parser->clear_current_tables;
126 61 my $current_tables = $self->parser->parse_file($file);
127 61 push @{ $self->tables }, @{$current_tables};
128 61 return $current_tables;
129 }
130  
131 sub create_table {
132 8 ▶ my ($self, $options) = @_;
Subroutine create_table: called CC=4 SCAR=14.0
Pod documented
133  
134 8 ▶ my $table_options = delete $options->{table} || { };
Condition100%2/2
delete $options->{'table'} || {}
l!l
44
MC/DC100%1/1
delete $options->{'table'} || {}
delete $options->{'table'}
Truth table100%2/2
A
A= delete $options->{'table'}
Aresult
11
00
135  
136 8 my $table = $self->add_table($table_options);
137
138 8 ▶ if ( exists $options->{aoa} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options->{'aoa'}) { } 3 5 8
elsif (exists $options->{'aoh'}) { } 5 0 5
139 3 $table = $self->_create_from_aoa($table, $options);
140 }
141 elsif ( exists $options->{aoh} ) {
142 5 $table = $self->_create_from_aoh($table, $options);
143 }
144 else {
145 0 croak 'create_tables currently requires an array of arrays or an array of hashes representing the data.';
146 }
147  
148 8 return $table;
149 }
150  
151 sub _create_from_aoh {
152 5 ▶ my ($self, $table, $options) = @_;
Subroutine _create_from_aoh: called CC=9 SCAR=22.0
153  
154 5 my $aoh = $options->{aoh};
155 5 my @headers = keys %{ $aoh->[0] };
156
157 5 ▶ if (my $order = $options->{order}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $options->{"order"}) 5 0 5
158 5 my @heads = ( );
159 5 foreach my $header (@{ $order }) {
160 12 push @heads, grep { $_ =~ m/$header/ixms } @headers;
161 }
162 5 @headers = @heads;
163 }
164  
165 5 ▶ unless ( defined $options->{no_headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"no_headers"}) 4 1 5
166 4 for (@headers) {
167 9 my $header_options = $self->_create_options('header', $options);
168 9 $header_options->{text} = $_;
169 9 $table->add_header($header_options);
170 }
171 4 @headers = $table->all_headers;
172 }
173  
174 5 foreach my $hash ( @{ $aoh } ) {
175 17 $options->{cells} = $options->{rows}->[0]->{cells};
176 17 my $row_options = $self->_create_options('row', $options);
177 17 my $row = $table->add_row($row_options);
178 17 for (@headers) {
179 39 ▶ my $text = defined $options->{no_headers} ? $_ : $_->text;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'no_headers'} ? : 9 30 39
180 39 my $cell_options = $self->_create_options('cell', $options);
181 39 $cell_options->{text} = $hash->{$text};
182 39 my $cell = $row->add_cell($cell_options);
183 39 ▶ unless ( defined $options->{no_headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"no_headers"}) 30 9 39
184 30 $cell->header($_);
185 30 push @{ $_->cells }, $cell;
186 }
187 }
188 }
189  
190 5 return $table;
191 }
192  
193 sub _create_from_aoa {
194 3 ▶ my ($self, $table, $options) = @_;
Subroutine _create_from_aoa: called CC=5 SCAR=16.1
195  
196 3 my @aoa = @{ $options->{aoa}};
197  
198 3 ▶ unless ( defined $options->{no_headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options->{"no_headers"}) 2 1 3
199 2 my $headers = shift @aoa;
200  
201 2 foreach my $header ( @{ $headers }) {
202 6 my $header_options = $self->_create_options('header', $options);
203  
204 6 $header_options->{text} = $header;
205 6 $table->add_header($header_options);
206 }
207 }
208  
209 3 for my $array ( @aoa ) {
210 9 $options->{cells} = $options->{rows}->[0]->{cells};
211 9 my $row_options = $self->_create_options('row', $options);
212 9 my $row = $table->add_row($row_options);
213
214 9 for my $text ( @{ $array } ) {
215 27 my $cell_options = $self->_create_options('cell', $options);
216
217 27 $cell_options->{text} = $text;
218
219 27 my $cell = $row->add_cell($cell_options);
220 27 $table->parse_to_column($cell);
221 }
222 }
223  
224 3 return $table;
225 }
226  
227 sub _create_options {
228 107 ▶ my ($self, $element, $options) = @_;
Subroutine _create_options: called CC=5 SCAR=16.1
229  
230 107 my $element_options = { };
231
232 107 ▶ if ( my $base = $options->{$element} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $options->{$element}) 54 53 107
233 54 for ( keys %{ $base } ) {
234 54 $element_options->{$_} = $base->{$_};
235 }
236 }
237  
238 107 my $elements = $element . 's';
239  
240 107 ▶ if ( my $custom = $options->{$elements} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $custom = $options->{$elements}) 44 63 107
241 44 my $first = shift @{ $options->{$elements} };
242 44 for ( keys %{ $first } ) {
243 34 $element_options->{$_} = $first->{$_};
244 }
245 }
246  
247 107 return $element_options;
248 }
249  
250 18 __PACKAGE__->meta->make_immutable;
251  
252 18 1;
253  
254 __END__
« TableContent/Template/Base.pm ↑ Summary TableContent/Parser.pm »