File Coverage

blib/lib/App/CSV2LaTeXTable.pm
Criterion Covered Total %
statement 44 47 93.6
branch 6 10 60.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 60 67 89.5


line stmt bran cond sub pod time code
1             package App::CSV2LaTeXTable;
2              
3             # ABSTRACT: Generate LaTeX table from CSV file
4              
5 1     1   908 use v5.24;
  1         4  
6              
7 1     1   5 use Carp;
  1         2  
  1         58  
8 1     1   5 use File::Basename;
  1         2  
  1         71  
9 1     1   671 use LaTeX::Table;
  1         1181316  
  1         62  
10 1     1   964 use Moo;
  1         14913  
  1         4  
11 1     1   5734 use Text::CSV_XS;
  1         20959  
  1         77  
12              
13             our $VERSION = '1.0.0'; # VERSION
14              
15 1     1   634 use experimental 'signatures';
  1         1184  
  1         6  
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              
22 1     1 1 5 sub run ($self) {
  1         3  
  1         5  
23 1         21 my %csv_params = map { split /=/, $_, 2 } $self->csv_param->@*;
  0         0  
24 1         3 $csv_params{binary} = 1;
25              
26 1         6 my $csv = Text::CSV_XS->new(\%csv_params);
27              
28 1 50       156 if ( !-f $self->csv ) {
29 0         0 croak sprintf "File %s does not exist", $self->csv;
30             }
31              
32 1         3 my ($header, $data);
33              
34 1 50   1   11 open my $fh, '<:encoding(utf-8)', $self->csv or croak $!;
  1         2  
  1         11  
  1         74  
35 1         16484 while ( my $row = $csv->getline( $fh ) ) {
36 4 100       192 if ( $. == 1 ) {
37 1         2 push $header->@*, $row;
38 1         27 next;
39             }
40              
41 3         54 push $data->@*, $row;
42             }
43 1 50       61 close $fh or croak $!;
44              
45 1         7 my %latex_params = map { split /=/, $_, 2 } $self->latex_param->@*;
  0         0  
46              
47 1 50       6 if ( !defined $latex_params{label} ) {
48 1         101 my $basename = basename $self->csv, '.csv';
49 1         6 $latex_params{label} = 'table:' . $basename;
50             }
51              
52 1         44 my $table = LaTeX::Table->new({
53             %latex_params,
54             filename => $self->latex,
55             header => $header,
56             data => $data,
57             });
58              
59 1         865 $table->generate;
60             }
61              
62             1;
63              
64             __END__
65              
66             =pod
67              
68             =encoding UTF-8
69              
70             =head1 NAME
71              
72             App::CSV2LaTeXTable - Generate LaTeX table from CSV file
73              
74             =head1 VERSION
75              
76             version 1.0.0
77              
78             =head1 SYNOPSIS
79              
80             use App::CSV2LaTeXTable;
81              
82             my $csv = '/path/to/a_csv_file.csv';
83             my $latex = '/path/to/resulting_latex_file.tex';
84              
85             my $obj = App::CSV2LaTeXTable->new(
86             csv => $csv,
87             latex => $latex,
88             );
89              
90             $obj->run;
91              
92             Using this CSV file:
93              
94             Name,Age,City
95             Mr X,34,London
96             Q,43,London
97             M,55,London
98              
99             This module generates this:
100              
101             \begin{table}
102             \centering
103             \begin{tabular}{lrl}
104             \toprule
105             Name & Age & City \\
106             \midrule
107             Mr X & 34 & London \\
108             Q & 43 & London \\
109             M & 55 & London \\
110             \bottomrule
111             \end{tabular}
112             \label{table:a_csv_file}
113             \end{table}
114              
115             =head1 DESCRIPTION
116              
117             This is the module behind L<csv2latextable>.
118              
119             =head1 ATTRIBUTES
120              
121             =over 4
122              
123             =item * csv
124              
125             =item * csv_param
126              
127             =item * latex
128              
129             =item * latex_param
130              
131             =back
132              
133             =head1 METHODS
134              
135             =head2 run
136              
137             my $obj = App::CSV2LaTeXTable->new(
138             csv => 'A-csv-file.csv',
139             latex => 'Target_file.tex',
140             );
141              
142             $obj->run;
143              
144             =head1 AUTHOR
145              
146             Renee Baecker <reneeb@cpan.org>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is Copyright (c) 2022 by Renee Baecker.
151              
152             This is free software, licensed under:
153              
154             The Artistic License 2.0 (GPL Compatible)
155              
156             =cut