jberger / Mojolicious-Plugin-ReplyTable

Easily render rectangular data in many formats using Mojolicious
Other
4 stars 5 forks source link

=encoding utf8

=head1 NAME

Mojolicious::Plugin::ReplyTable - Easily render rectangular data in many formats using Mojolicious

=head1 SYNOPSIS

use Mojolicious::Lite; plugin 'ReplyTable';

my $format = [format => [qw( txt csv html json xls xlsx )]]; any '/table' => $format => sub { my $c = shift; my $data = [ [qw/a b c d/], [qw/e f g h/], ]; $c->reply->table($data); };

app->start;

=head1 DESCRIPTION

L adds the C<< reply->table >> helper which can render a table of data in one of several user-selected formats. The format is selected by the client via the usual Mojolicious L<Content Negotiation|Mojolicious::Guides::Rendering/"Content negotiation"> mechanisms.

Loading the plugin also sets up several MIME types (using L, see L<Mojolicious/types>), and appends the module to the list of rendering classes (See L<Mojolicious/renderer>).

=head1 HELPERS

=head2 reply->table

$c->reply->table([[...], [...], ... ]]); $c->reply->table($default => $data, html => sub { ... });

Renders an arrayref of arrayrefs (the inner arrayref being a row) in one of several formats listed below. An optional leading argument is used as the default format when one is not otherwise requested. Optional trailing key-value pairs are merged into the arguments to L<Mojolicious::Controller/respond_to>.

Any additional options, particularly those governing formatting details, are via stash keys prefixed by C. Note that the prefix C is reserved for internal use.

The formats currently include:

=head3 csv

Implemented via L using the default values with C enabled. To override these defaults set the stash key C to a hashref containing attributes to pass to Text::CSV. For example, to create a PSV (pipe delimited) file:

$c->stash('reply_table.csv_options' => { sep_char => "|" });

See L<Text::CSV/new> for available options.

=head3 html

Implemented via the standard L rendering functionality and a template named C. Setting the stash key C to a true value will cause the default template to use the first row as header values. This default template may be overloaded to change the formatting, the table is available to the template via the stash key C.

=head3 json

Implemented via the standard L handling.

=head3 txt

A textual representation of the table. This format is intended for human consumption and the specific formatting should not be relied upon.

If L is available, it will be used to format the data (can be overridden with C). It can be controlled via the stash keys C and C as noted in that module's documentation. Otherwise it is generated via L.

=head3 xls

Binary Microsoft Excel format (for older editions of Excel), provided by optional module L. If that module is not installed, the client will receive an error status 406.

=head3 xlsx

XML Microsoft Excel format (for newer editions of Excel), provided by optional module L. If that module is not installed, the client will receive an error status 406.

=head1 METHODS

This module inherits all the methods from L and implements the following new ones

=head2 register

The typical mechanism of loading a L. No pass-in options are currently available.

=head1 FUTURE WORK

Beyond what is mentioned in the specifc formats above, the following work is planned. If any of it tickles your fancy, pull-requests are always welcome.

=over

=item *

Better tests for generated Excel documents.

=item *

Exposing the formatters so that they can be used directly.

=item *

Add additional formats, like OpenOffice/LibreOffice. If needed these can be appended via additional handlers to the helper.

=back

=head1 A NOTE ON FORMAT DETECTION

As of L version 9.11, format detection is disabled by default. To enable it you can pass an array reference of C<< [format=>\@formats] >> to the route, where C<@formats> is the supported file extensions. You may also use the shortcut C< [format => 1] >> to enable detection of any format, though that may change in the future.

As of Mojolicious 9.16 you can inherit these formats from a parent route:

my $with_formats = $app->routes->any([format => \@formats]); $with_formats->get('/data')->to('MyController#my_action');

=head1 SEE ALSO

=over

=item L

=item Lhttps://metacpan.org/pod/Mojolicious::Plugin::WriteExcel

=back

=head1 SOURCE REPOSITORY

Lhttp://github.com/jberger/Mojolicious-Plugin-ReplyTable

=head1 SPECIAL THANKS

Pharmetika Software, Lhttp://pharmetika.com

=head1 AUTHOR

Joel Berger, Elt>joel.a.berger@gmail.comE<gt

=head1 CONTRIBUTORS

=over

Nils Diewald (Akron)

Красимир Беров (kberov)

Ryan Perry

Ilya Chesnokov (ichesnokov)

=back

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2015 by L and L. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.