| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use LaTeX::Table; |
| 7 |
use Carp; |
| 8 |
|
| 9 |
use version; our $VERSION = qv('1.0.6'); |
| 10 |
|
| 11 |
my $line_id = 0; |
| 12 |
my @input; |
| 13 |
my $paramline; |
| 14 |
|
| 15 |
while ( my $line = <> ) { |
| 16 |
chomp $line; |
| 17 |
$line =~ s{\s+}{ }xmsg; |
| 18 |
$line =~ s{\A \s* | \s* \z}{}xmsg; |
| 19 |
|
| 20 |
# remove trailing \\, we do that later |
| 21 |
$line =~ s{\A \\\\ \z}{}xmsg; |
| 22 |
|
| 23 |
if ( $line_id == 0 ) { #first line contains the parameters |
| 24 |
$paramline = $line; |
| 25 |
} |
| 26 |
else { |
| 27 |
push @input, $line; |
| 28 |
} |
| 29 |
$line_id++; |
| 30 |
} |
| 31 |
|
| 32 |
# remove trailing empty lines |
| 33 |
while ( !length $input[-1] ) { |
| 34 |
pop @input; |
| 35 |
} |
| 36 |
|
| 37 |
# uncomment original input |
| 38 |
_say( join q{}, map {"\n % $_"} ( $paramline, @input ) ); |
| 39 |
|
| 40 |
# create rows/column array[array_ref], remove leading and trailing spaces |
| 41 |
@input = map { |
| 42 |
[ map { m{\A \s* (.*) \s* \z}xms ? $1 : $_ } split m{ & }xms ] |
| 43 |
} @input; |
| 44 |
|
| 45 |
# store the paramline in a hash |
| 46 |
my %params = map { _parse_param($_) } split m{ ; }xms, $paramline; |
| 47 |
|
| 48 |
my $table = LaTeX::Table->new( |
| 49 |
{ header => [ $input[0] ], |
| 50 |
data => [ @input[ 1 .. $#input ] ], |
| 51 |
%params, |
| 52 |
} |
| 53 |
); |
| 54 |
|
| 55 |
_say( "\n" . $table->generate_string() ); |
| 56 |
|
| 57 |
sub _parse_param { |
| 58 |
my ($s) = @_; |
| 59 |
if ( $s =~ m{ \A (.*?) = (.*) \z}xms ) { |
| 60 |
return $1 => $2; |
| 61 |
} |
| 62 |
else { |
| 63 |
croak q{Invalid parameter line}; |
| 64 |
} |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
sub _say { |
| 69 |
my ($s) = @_; |
| 70 |
print "$s\n" or croak q{Can't print to stdout.}; |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
__END__ |
| 75 |
|
| 76 |
=head1 NAME |
| 77 |
|
| 78 |
ltpretty - Use LaTeX::Table from within your text editor. |
| 79 |
|
| 80 |
=head1 SYNOPSIS |
| 81 |
|
| 82 |
ltpretty < lazytable |
| 83 |
|
| 84 |
=head1 DESCRIPTION |
| 85 |
|
| 86 |
This program takes a I<lazy formatted> LaTeX table from STDIN (typically piped |
| 87 |
from Vim or emacs) and outputs a completely formatted table. |
| 88 |
|
| 89 |
=head1 LAZY FORMAT |
| 90 |
|
| 91 |
The first line must contain the L<LaTeX::Table> options (separated by |
| 92 |
semicolons C<;>) in the format C<optionname=value>. The |
| 93 |
second line must contain the header, the following lines the data. |
| 94 |
Columns are separated by C<&>. Trailing empty lines are ignored, other |
| 95 |
empty lines generate horizontal rules (as in L<LaTeX::Table>). |
| 96 |
|
| 97 |
=head1 EXAMPLE |
| 98 |
|
| 99 |
theme=Meyrin;label=test;position=htb |
| 100 |
Item:2c & Price |
| 101 |
Gnat& per gram& 13.65 |
| 102 |
& each& 0.01 |
| 103 |
|
| 104 |
Gnu& stuffed& 92.59 |
| 105 |
Emu& stuffed& 33.33 |
| 106 |
Armadillo& frozen& 8.99 |
| 107 |
|
| 108 |
=head1 VIM |
| 109 |
|
| 110 |
=over |
| 111 |
|
| 112 |
=item 1. Select the text in C<VISUAL LINE> mode (C<shift+v>). |
| 113 |
|
| 114 |
=item 2. Type C<!ltpretty> |
| 115 |
|
| 116 |
=back |
| 117 |
|
| 118 |
=head1 EMACS |
| 119 |
|
| 120 |
=over |
| 121 |
|
| 122 |
=item 1. Select the text |
| 123 |
|
| 124 |
=item 2. Press C<alt+|> |
| 125 |
|
| 126 |
=item 3. Now "Shell command on region" should appear. Type C<ltpretty> |
| 127 |
|
| 128 |
=back |
| 129 |
|
| 130 |
=head1 OUTPUT |
| 131 |
|
| 132 |
% theme=Meyrin;label=test;position=htb |
| 133 |
% Item:2c & Price |
| 134 |
% Gnat& per gram& 13.65 |
| 135 |
% & each& 0.01 |
| 136 |
% |
| 137 |
% Gnu& stuffed& 92.59 |
| 138 |
% Emu& stuffed& 33.33 |
| 139 |
% Armadillo& frozen& 8.99 |
| 140 |
|
| 141 |
\begin{table}[htb] |
| 142 |
\centering |
| 143 |
\begin{tabular}{llr} |
| 144 |
\toprule |
| 145 |
\multicolumn{2}{c}{Item} & Price \\ |
| 146 |
\midrule |
| 147 |
Gnat & per gram & 13.65 \\ |
| 148 |
& each & 0.01 \\ |
| 149 |
\midrule |
| 150 |
Gnu & stuffed & 92.59 \\ |
| 151 |
Emu & stuffed & 33.33 \\ |
| 152 |
Armadillo & frozen & 8.99 \\ |
| 153 |
\bottomrule |
| 154 |
\end{tabular} |
| 155 |
\label{test} |
| 156 |
\end{table} |
| 157 |
|
| 158 |
|
| 159 |
=head1 CONFIGURATION AND ENVIRONMENT |
| 160 |
|
| 161 |
C<ltpretty> does not support configuration files or environment variables. |
| 162 |
|
| 163 |
=head1 DEPENDENCIES |
| 164 |
|
| 165 |
L<LaTeX::Table> |
| 166 |
|
| 167 |
=head1 BUGS AND LIMITATIONS |
| 168 |
|
| 169 |
Our I<lazy format> does not support multi-line headers. Just add the other |
| 170 |
header lines afterwards. More importantly, it currently does not support |
| 171 |
non-scalar L<LaTeX::Table> options like for example C<columns_like_header>. |
| 172 |
A future version may add this functionality. |
| 173 |
|
| 174 |
Please report any bugs or feature requests to |
| 175 |
C<[email protected]>, or through the web interface at |
| 176 |
L<http://rt.cpan.org>. |
| 177 |
|
| 178 |
=head1 LICENSE AND COPYRIGHT |
| 179 |
|
| 180 |
Copyright (c) 2006-2010, C<< <[email protected]> >>. |
| 181 |
|
| 182 |
This program is free software; you can redistribute it and/or |
| 183 |
modify it under the same terms as Perl itself. See L<perlartistic>. |
| 184 |
|
| 185 |
=head1 DISCLAIMER OF WARRANTY |
| 186 |
|
| 187 |
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
| 188 |
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
| 189 |
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
| 190 |
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 191 |
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 192 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 193 |
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
| 194 |
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
| 195 |
NECESSARY SERVICING, REPAIR, OR CORRECTION. |
| 196 |
|
| 197 |
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
| 198 |
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
| 199 |
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE |
| 200 |
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
| 201 |
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
| 202 |
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
| 203 |
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
| 204 |
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
| 205 |
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
| 206 |
SUCH DAMAGES. |
| 207 |
|
| 208 |
=cut |
| 209 |
|
| 210 |
# vim: ft=perl sw=4 ts=4 expandtab |