line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::CSV2LaTeXTable; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Generate LaTeX table from CSV file |
4
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
4056
|
use v5.24; |
|
5
|
|
|
|
|
18
|
|
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
30
|
use Carp; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
282
|
|
8
|
5
|
|
|
5
|
|
30
|
use File::Basename; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
278
|
|
9
|
5
|
|
|
5
|
|
3053
|
use LaTeX::Table; |
|
5
|
|
|
|
|
5899199
|
|
|
5
|
|
|
|
|
374
|
|
10
|
5
|
|
|
5
|
|
4276
|
use Moo; |
|
5
|
|
|
|
|
72319
|
|
|
5
|
|
|
|
|
34
|
|
11
|
5
|
|
|
5
|
|
28505
|
use Text::CSV_XS; |
|
5
|
|
|
|
|
98298
|
|
|
5
|
|
|
|
|
342
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.1.0'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
5
|
|
|
5
|
|
2347
|
use experimental 'signatures'; |
|
5
|
|
|
|
|
5915
|
|
|
5
|
|
|
|
|
39
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has csv => ( is => 'ro', required => 1 ); |
18
|
|
|
|
|
|
|
has csv_param => ( is => 'ro', default => sub { [] } ); |
19
|
|
|
|
|
|
|
has latex => ( is => 'ro', required => 1 ); |
20
|
|
|
|
|
|
|
has latex_param => ( is => 'ro', default => sub { [] } ); |
21
|
|
|
|
|
|
|
has rotate => ( is => 'ro', default => sub { 0 } ); |
22
|
|
|
|
|
|
|
has split => ( is => 'ro', default => sub { 0 } ); |
23
|
|
|
|
|
|
|
|
24
|
5
|
|
|
5
|
1
|
134
|
sub run ($self) { |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
65
|
|
25
|
5
|
|
|
|
|
34
|
my %csv_params = map { split /=/, $_, 2 } $self->csv_param->@*; |
|
0
|
|
|
|
|
0
|
|
26
|
5
|
|
|
|
|
27
|
$csv_params{binary} = 1; |
27
|
|
|
|
|
|
|
|
28
|
5
|
|
|
|
|
71
|
my $csv = Text::CSV_XS->new(\%csv_params); |
29
|
|
|
|
|
|
|
|
30
|
5
|
50
|
|
|
|
1006
|
if ( !-f $self->csv ) { |
31
|
0
|
|
|
|
|
0
|
croak sprintf "File %s does not exist", $self->csv; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
5
|
|
|
|
|
20
|
my ($header, $data); |
35
|
|
|
|
|
|
|
|
36
|
5
|
50
|
|
5
|
|
43
|
open my $fh, '<:encoding(utf-8)', $self->csv or croak $!; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
40
|
|
|
5
|
|
|
|
|
224
|
|
37
|
5
|
|
|
|
|
81904
|
while ( my $row = $csv->getline( $fh ) ) { |
38
|
20
|
100
|
|
|
|
872
|
if ( $. == 1 ) { |
39
|
5
|
|
|
|
|
18
|
push $header->@*, $row; |
40
|
5
|
|
|
|
|
174
|
next; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
15
|
|
|
|
|
275
|
push $data->@*, $row; |
44
|
|
|
|
|
|
|
} |
45
|
5
|
50
|
|
|
|
283
|
close $fh or croak $!; |
46
|
|
|
|
|
|
|
|
47
|
5
|
|
|
|
|
17
|
my $table_sets = $data; |
48
|
|
|
|
|
|
|
|
49
|
5
|
100
|
|
|
|
51
|
if ( $self->split ) { |
50
|
1
|
|
|
|
|
4
|
$table_sets = []; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
4
|
my $per_set = $self->split; |
53
|
1
|
|
|
|
|
2
|
my $index = 0; |
54
|
1
|
|
|
|
|
3
|
my @all_data = $data->@*; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
2
|
my $counter = 1; |
57
|
1
|
|
|
|
|
4
|
while ( my $row = shift @all_data ) { |
58
|
3
|
|
|
|
|
7
|
push $table_sets->[$index]->@*, $row; |
59
|
|
|
|
|
|
|
|
60
|
3
|
100
|
|
|
|
9
|
if ( $counter++ == $per_set ) { |
61
|
1
|
|
|
|
|
2
|
$index++; |
62
|
1
|
|
|
|
|
4
|
$counter = 1; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
5
|
|
|
|
|
13
|
my $counter; |
68
|
5
|
|
|
|
|
27
|
while ( my $table_set = shift $table_sets->@* ) { |
69
|
12
|
|
|
|
|
84
|
my %latex_params = map { split /=/, $_, 2 } $self->latex_param->@*; |
|
6
|
|
|
|
|
34
|
|
70
|
|
|
|
|
|
|
|
71
|
12
|
|
|
|
|
35
|
my $suffix = ''; |
72
|
12
|
100
|
|
|
|
58
|
if ( $self->split ) { |
73
|
2
|
|
|
|
|
13
|
$suffix = sprintf "-%s", ++$counter; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
12
|
100
|
|
|
|
45
|
if ( $table_sets->@* ) { |
77
|
8
|
|
|
|
|
24
|
delete $latex_params{caption}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
12
|
50
|
|
|
|
56
|
if ( !defined $latex_params{label} ) { |
81
|
12
|
|
|
|
|
1172
|
my $basename = basename $self->csv, '.csv'; |
82
|
12
|
|
|
|
|
83
|
$latex_params{label} = 'table:' . $basename . $suffix; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
12
|
|
|
|
|
548
|
my $table = LaTeX::Table->new({ |
86
|
|
|
|
|
|
|
%latex_params, |
87
|
|
|
|
|
|
|
header => $header, |
88
|
|
|
|
|
|
|
data => $data, |
89
|
|
|
|
|
|
|
}); |
90
|
|
|
|
|
|
|
|
91
|
12
|
|
|
|
|
9284
|
my $latex_string = $table->generate_string; |
92
|
|
|
|
|
|
|
|
93
|
12
|
100
|
|
|
|
1889577
|
if ( $self->rotate ) { |
94
|
3
|
|
|
|
|
21
|
my $rotatebox = sprintf 'rotatebox{%s}{', $self->rotate; |
95
|
3
|
|
|
|
|
21
|
$latex_string =~ s{begin\{table\}}{$rotatebox}; |
96
|
3
|
|
|
|
|
20
|
$latex_string =~ s{\\end\{table\}$}{\}}; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
12
|
|
|
|
|
169
|
my $latex_file = $self->latex =~ s{(\.[^\.]+)}{$suffix$1}r; |
100
|
|
|
|
|
|
|
|
101
|
12
|
100
|
|
|
|
1885
|
open my $tex_fh, '>', $latex_file or croak $!; |
102
|
11
|
|
|
|
|
162
|
print $tex_fh $latex_string; |
103
|
11
|
50
|
|
|
|
1486
|
close $tex_fh or croak $!; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=pod |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=encoding UTF-8 |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 NAME |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
App::CSV2LaTeXTable - Generate LaTeX table from CSV file |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 VERSION |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
version 1.1.0 |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 SYNOPSIS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
use App::CSV2LaTeXTable; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $csv = '/path/to/a_csv_file.csv'; |
128
|
|
|
|
|
|
|
my $latex = '/path/to/resulting_latex_file.tex'; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $obj = App::CSV2LaTeXTable->new( |
131
|
|
|
|
|
|
|
csv => $csv, |
132
|
|
|
|
|
|
|
latex => $latex, |
133
|
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$obj->run; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Using this CSV file: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Name,Age,City |
140
|
|
|
|
|
|
|
Mr X,34,London |
141
|
|
|
|
|
|
|
Q,43,London |
142
|
|
|
|
|
|
|
M,55,London |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
This module generates this: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
\begin{table} |
147
|
|
|
|
|
|
|
\centering |
148
|
|
|
|
|
|
|
\begin{tabular}{lrl} |
149
|
|
|
|
|
|
|
\toprule |
150
|
|
|
|
|
|
|
Name & Age & City \\ |
151
|
|
|
|
|
|
|
\midrule |
152
|
|
|
|
|
|
|
Mr X & 34 & London \\ |
153
|
|
|
|
|
|
|
Q & 43 & London \\ |
154
|
|
|
|
|
|
|
M & 55 & London \\ |
155
|
|
|
|
|
|
|
\bottomrule |
156
|
|
|
|
|
|
|
\end{tabular} |
157
|
|
|
|
|
|
|
\label{table:a_csv_file} |
158
|
|
|
|
|
|
|
\end{table} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 DESCRIPTION |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This is the module behind L<csv2latextable>. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 4 |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * csv |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item * csv_param |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * latex |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item * latex_param |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item * rotate |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item * split |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 METHODS |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 run |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
my $obj = App::CSV2LaTeXTable->new( |
187
|
|
|
|
|
|
|
csv => 'A-csv-file.csv', |
188
|
|
|
|
|
|
|
latex => 'Target_file.tex', |
189
|
|
|
|
|
|
|
); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
$obj->run; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 SEE ALSO |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=over 4 |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * L<LaTeX::Table> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Used to generate the LaTeX code. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item * L<Text::CSV_XS> |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Used to parse the CSV file |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=back |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 AUTHOR |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Renee Baecker <reneeb@cpan.org> |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
This software is Copyright (c) 2022 by Renee Baecker. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This is free software, licensed under: |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |