line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Exporter::Table; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.2.1'; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
111854
|
use namespace::clean; |
|
2
|
|
|
|
|
63037
|
|
|
2
|
|
|
|
|
16
|
|
6
|
2
|
|
|
2
|
|
4422
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
294623
|
|
|
2
|
|
|
|
|
15
|
|
7
|
2
|
|
|
2
|
|
650
|
use Moo; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
8
|
2
|
|
|
2
|
|
2826
|
use Text::MarkdownTable; |
|
2
|
|
|
|
|
23443
|
|
|
2
|
|
|
|
|
93
|
|
9
|
2
|
|
|
2
|
|
24
|
use IO::Handle::Util (); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
41
|
|
10
|
2
|
|
|
2
|
|
14
|
use IO::File; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
391
|
|
11
|
2
|
|
|
2
|
|
2588
|
use JSON (); |
|
2
|
|
|
|
|
32754
|
|
|
2
|
|
|
|
|
1236
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
with 'Catmandu::Exporter'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# JSON Table Schema |
16
|
|
|
|
|
|
|
has schema => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
coerce => sub { |
19
|
|
|
|
|
|
|
my $schema = $_[0]; |
20
|
|
|
|
|
|
|
unless (ref $schema and ref $schema eq 'HASH') { |
21
|
|
|
|
|
|
|
$schema = \*STDIN if $schema eq '-'; |
22
|
|
|
|
|
|
|
my $fh = ref $schema |
23
|
|
|
|
|
|
|
? IO::Handle::Util::io_from_ref($schema) |
24
|
|
|
|
|
|
|
: IO::File->new($schema, "r"); |
25
|
|
|
|
|
|
|
die "failed to load JSON Table Schema from $schema" unless $fh; |
26
|
|
|
|
|
|
|
local $/; |
27
|
|
|
|
|
|
|
$schema = JSON::decode_json(<$fh>); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
$schema; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has fields => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
lazy => 1, |
36
|
|
|
|
|
|
|
builder => sub { |
37
|
4
|
100
|
|
4
|
|
968
|
return unless $_[0]->schema; |
38
|
2
|
|
|
|
|
5
|
[ map { $_->{name} } @{$_[0]->schema->{fields}} ]; |
|
4
|
|
|
|
|
20
|
|
|
2
|
|
|
|
|
10
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
has columns => ( |
42
|
|
|
|
|
|
|
is => 'ro', |
43
|
|
|
|
|
|
|
lazy => 1, |
44
|
|
|
|
|
|
|
builder => sub { |
45
|
5
|
100
|
|
5
|
|
839
|
return unless $_[0]->schema; |
46
|
2
|
|
66
|
|
|
4
|
[ map { $_->{title} // $_->{name} } @{$_[0]->schema->{fields}} ]; |
|
4
|
|
|
|
|
34
|
|
|
2
|
|
|
|
|
152
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has widths => (is => 'ro'); |
51
|
|
|
|
|
|
|
has condense => (is => 'ro'); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has _table => ( |
54
|
|
|
|
|
|
|
is => 'lazy', |
55
|
|
|
|
|
|
|
default => sub { |
56
|
|
|
|
|
|
|
Text::MarkdownTable->new( |
57
|
|
|
|
|
|
|
file => $_[0]->fh, |
58
|
|
|
|
|
|
|
map { $_ => $_[0]->$_ } |
59
|
|
|
|
|
|
|
grep { defined $_[0]->$_ } |
60
|
|
|
|
|
|
|
qw(fields columns widths condense) |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
}, |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub add { |
66
|
|
|
|
|
|
|
$_[0]->_table->add($_[1]) |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub commit { |
70
|
5
|
|
|
5
|
0
|
5659
|
$_[0]->_table->done |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 NAME |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Catmandu::Exporter::Table - ASCII/Markdown table exporter |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 SYNOPSIS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
echo '{"one":"my","two":"table"} {"one":"is","two":"nice"}]' | \ |
80
|
|
|
|
|
|
|
catmandu convert JSON --multiline 1 to Table |
81
|
|
|
|
|
|
|
| one | two | |
82
|
|
|
|
|
|
|
|-----|-------| |
83
|
|
|
|
|
|
|
| my | table | |
84
|
|
|
|
|
|
|
| is | nice | |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
catmandu convert CSV to Table --fields id,name --header ID,Name < sample.csv |
87
|
|
|
|
|
|
|
| ID | Name | |
88
|
|
|
|
|
|
|
|----|------| |
89
|
|
|
|
|
|
|
| 23 | foo | |
90
|
|
|
|
|
|
|
| 42 | bar | |
91
|
|
|
|
|
|
|
| 99 | doz | |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
#!/usr/bin/env perl |
95
|
|
|
|
|
|
|
use Catmandu::Exporter::Table; |
96
|
|
|
|
|
|
|
my $exp = Catmandu::Exporter::Table->new; |
97
|
|
|
|
|
|
|
$exp->add({ title => "The Hobbit", author => "Tolkien" }); |
98
|
|
|
|
|
|
|
$exp->add({ title => "Where the Wild Things Are", author => "Sendak" }); |
99
|
|
|
|
|
|
|
$exp->add({ title => "One Thousand and One Nights" }); |
100
|
|
|
|
|
|
|
$exp->commit; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
| author | title | |
103
|
|
|
|
|
|
|
|---------|-----------------------------| |
104
|
|
|
|
|
|
|
| Tolkien | The Hobbit | |
105
|
|
|
|
|
|
|
| Sendak | Where the Wild Things Are | |
106
|
|
|
|
|
|
|
| | One Thousand and One Nights | |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 DESCRIPTION |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This L exports data in tabular form, formatted in |
112
|
|
|
|
|
|
|
MultiMarkdown syntax. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
The output can be used for simple display, for instance to preview Excel files |
115
|
|
|
|
|
|
|
on the command line. Use L too |
116
|
|
|
|
|
|
|
further convert to other table formats, e.g. C, C, C: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
catmandu convert XLS to Table < sheet.xls | pandoc -t html5 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 CONFIGURATION |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Table output can be controlled with the options C, C, |
123
|
|
|
|
|
|
|
C, and C as documented in L. The |
124
|
|
|
|
|
|
|
additional option C can be used to supply fields and (optionally) |
125
|
|
|
|
|
|
|
columns in a L. |
126
|
|
|
|
|
|
|
The schema is a JSON file or HASH reference having the following structure: |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
{ |
129
|
|
|
|
|
|
|
"fields: [ |
130
|
|
|
|
|
|
|
{ "name": "field-name-1", "title": "column title 1 (optional)" }, |
131
|
|
|
|
|
|
|
{ "name": "field-name-2", "title": "column title 2 (optional)" }, |
132
|
|
|
|
|
|
|
... |
133
|
|
|
|
|
|
|
] |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Without C or C, columns are sorted alphabetically by field |
137
|
|
|
|
|
|
|
name. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 METHODS |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
See L for additional exporter and methods (C, C, |
142
|
|
|
|
|
|
|
C, C..., C, C...). |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SEE ALSO |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L, |
147
|
|
|
|
|
|
|
L |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |