File: Table.pm

package info (click to toggle)
libcatmandu-exporter-table-perl 0.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 124 kB
  • sloc: perl: 71; makefile: 2
file content (204 lines) | stat: -rw-r--r-- 4,912 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Catmandu::Exporter::Table;

our $VERSION = '0.3.0';

use Catmandu::Sane;
use Moo;
use Text::MarkdownTable;
use IO::Handle::Util ();
use IO::File;
use JSON::XS ();

with 'Catmandu::Exporter';

# JSON Table Schema
has schema => (
    is => 'ro',
    coerce => sub {
        my $schema = $_[0];
        unless (ref $schema and ref $schema eq 'HASH') {
            $schema = \*STDIN if $schema eq '-';
            my $fh = ref $schema 
                    ? IO::Handle::Util::io_from_ref($schema) 
                    : IO::File->new($schema, "r");
            die "failed to load JSON Table Schema from $schema" unless $fh;
            local $/; 
            $schema = JSON::XS::decode_json(<$fh>);
        }
        $schema;
    }
);

has fields => (
    is      => 'ro',
    lazy    => 1,
    builder => sub {
        return unless $_[0]->schema;
        [ map { $_->{name} } @{$_[0]->schema->{fields}} ];
    }
);

has columns => (
    is      => 'ro',
    lazy    => 1,
    builder => sub {
        return unless $_[0]->schema;
        [ map { $_->{title} // $_->{name} } @{$_[0]->schema->{fields}} ];
    }
);

has widths   => (is => 'ro');
has condense => (is => 'ro');
has header   => (is => 'ro');

has _table => (
    is      => 'lazy',
    default => sub {
        Text::MarkdownTable->new(
            file => $_[0]->fh,
            map { $_ => $_[0]->$_ }
            grep { defined $_[0]->$_ }
            qw(fields columns widths condense header)
        );
    },
);

sub add { 
    $_[0]->_table->add($_[1]) 
}

sub commit { 
    $_[0]->_table->done 
}

1;
__END__

=head1 NAME

Catmandu::Exporter::Table - ASCII/Markdown table exporter

=begin markdown

# STATUS

[![Build Status](https://travis-ci.org/LibreCat/Catmandu-Exporter-Table.png)](https://travis-ci.org/LibreCat/Catmandu-Exporter-Table)
[![Coverage Status](https://coveralls.io/repos/LibreCat/Catmandu-Exporter-Table/badge.png)](https://coveralls.io/r/LibreCat/Catmandu-Exporter-Table)
[![Kwalitee Score](http://cpants.cpanauthors.org/dist/Catmandu-Exporter-Table.png)](http://cpants.cpanauthors.org/dist/Catmandu-Exporter-Table)

=end markdown

=head1 SYNOPSIS

With L<catmandu> command line client:

  echo '{"one":"my","two":"table"} {"one":"is","two":"nice"}' | \ 
  catmandu convert JSON --multiline 1 to Table
  | one | two   |
  |-----|-------|
  | my  | table |
  | is  | nice  |

  catmandu convert CSV to Table --fields id,name --columns ID,Name < sample.csv
  | ID | Name |
  |----|------|
  | 23 | foo  |
  | 42 | bar  |
  | 99 | doz  |

In Perl scripts:

  use Catmandu::Exporter::Table;
  my $exp = Catmandu::Exporter::Table->new;
  $exp->add({ title => "The Hobbit", author => "Tolkien" });
  $exp->add({ title => "Where the Wild Things Are", author => "Sendak" });
  $exp->add({ title => "One Thousand and One Nights" });
  $exp->commit;

  | author  | title                       |
  |---------|-----------------------------|
  | Tolkien | The Hobbit                  |
  | Sendak  | Where the Wild Things Are   |
  |         | One Thousand and One Nights |

=head1 DESCRIPTION

This L<Catmandu::Exporter> exports data in tabular form, formatted in
MultiMarkdown syntax.

The output can be used for simple display, for instance to preview Excel files
on the command line. Use L<Pandoc|http://johnmacfarlane.net/pandoc/> too
further convert to other table formats, e.g. C<latex>, C<html5>, C<mediawiki>:

    catmandu convert XLS to Table < sheet.xls | pandoc -t html5

By default columns are sorted alphabetically by field name.

=head1 CONFIGURATION

Table output can be controlled with the options C<fields>, C<columns>,
C<widths>, and C<condense> as documented in L<Text::MarkdownTable>. 

=over

=item file

=item fh

=item encoding

=item fix

Standard options of L<Catmandu:Exporter>

=item condense

Write table in condense format with unaligned columns.

=item fields

Field names as comma-separated list or array reference.

=item columns

Column names as comma-separated list or array reference. By default field
names are used as column names.

=item header

Include header lines. Enabled by default.

=item widths

Column widths as comma-separated list or array references. Calculated from all
rows by default. Long cell values can get truncated with this option.

=item schema

Supply fields and (optionally) columns in a L<JSON Table
Schema|http://dataprotocols.org/json-table-schema/> as JSON file or hash
reference having the following structure:

  {
    "fields: [
      { "name": "field-name-1", "title": "column title 1 (optional)" },
      { "name": "field-name-2", "title": "column title 2 (optional)" },
      ...
    ]
  }

=back

=head1 METHODS

See L<Catmandu::Exporter> 

=head1 SEE ALSO

This module is based on L<Text::MarkdownTable>.

Similar Catmandu Exporters for tabular data include
L<Catmandu::Exporter::CSV>, L<Catmandu::Exporter::XLS>, and
L<Catmandu::Exporter::XLSX>.

=cut