| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use Catmandu::Sane; |
|
3
|
3
|
|
|
3
|
|
275088
|
use Catmandu::Util qw(is_string); |
|
|
3
|
|
|
|
|
500374
|
|
|
|
3
|
|
|
|
|
23
|
|
|
4
|
3
|
|
|
3
|
|
880
|
use Catmandu; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
135
|
|
|
5
|
3
|
|
|
3
|
|
2159
|
use Template; |
|
|
3
|
|
|
|
|
321563
|
|
|
|
3
|
|
|
|
|
16
|
|
|
6
|
3
|
|
|
3
|
|
2162
|
use Storable qw(freeze); |
|
|
3
|
|
|
|
|
49352
|
|
|
|
3
|
|
|
|
|
101
|
|
|
7
|
3
|
|
|
3
|
|
1594
|
use Moo; |
|
|
3
|
|
|
|
|
8692
|
|
|
|
3
|
|
|
|
|
205
|
|
|
8
|
3
|
|
|
3
|
|
26
|
use namespace::clean; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
22
|
|
|
9
|
3
|
|
|
3
|
|
1114
|
|
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
with 'Catmandu::Exporter'; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $TT_INSTANCES = {}; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $XML_DECLARATION = qq(<?xml version="1.0" encoding="UTF-8"?>\n); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $ADD_TT_EXT = sub { |
|
19
|
|
|
|
|
|
|
my $tmpl = $_[0]; |
|
20
|
|
|
|
|
|
|
is_string($tmpl) && $tmpl !~ /\.\w{2,4}$/ ? "$tmpl.tt" : $tmpl; |
|
21
|
|
|
|
|
|
|
}; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $OWN_OPTS = { |
|
24
|
|
|
|
|
|
|
map {($_ => 1)} |
|
25
|
|
|
|
|
|
|
qw( |
|
26
|
|
|
|
|
|
|
log_category |
|
27
|
|
|
|
|
|
|
autocommit |
|
28
|
|
|
|
|
|
|
count |
|
29
|
|
|
|
|
|
|
file |
|
30
|
|
|
|
|
|
|
fh |
|
31
|
|
|
|
|
|
|
xml |
|
32
|
|
|
|
|
|
|
template |
|
33
|
|
|
|
|
|
|
template_before |
|
34
|
|
|
|
|
|
|
template_after |
|
35
|
|
|
|
|
|
|
) |
|
36
|
|
|
|
|
|
|
}; |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has xml => (is => 'ro'); |
|
39
|
|
|
|
|
|
|
has template_before => (is => 'ro', coerce => $ADD_TT_EXT); |
|
40
|
|
|
|
|
|
|
has template => (is => 'ro', coerce => $ADD_TT_EXT, required => 1); |
|
41
|
|
|
|
|
|
|
has template_after => (is => 'ro', coerce => $ADD_TT_EXT); |
|
42
|
|
|
|
|
|
|
has _tt_opts => (is => 'lazy', init_arg => undef); |
|
43
|
|
|
|
|
|
|
has _tt => (is => 'lazy', init_arg => undef); |
|
44
|
|
|
|
|
|
|
has _before_done => (is => 'rw', init_arg => undef); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my ($self, $opts) = @_; |
|
47
|
|
|
|
|
|
|
my $tt_opts = $self->_tt_opts; |
|
48
|
15
|
|
|
15
|
0
|
161
|
for my $key (keys %$opts) { |
|
49
|
15
|
|
|
|
|
223
|
$tt_opts->{uc $key} = $opts->{$key} unless $OWN_OPTS->{$key}; |
|
50
|
15
|
|
|
|
|
78052
|
} |
|
51
|
46
|
100
|
|
|
|
186
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
+{ |
|
54
|
|
|
|
|
|
|
ENCODING => 'utf8', |
|
55
|
|
|
|
|
|
|
ABSOLUTE => 1, |
|
56
|
|
|
|
|
|
|
RELATIVE => 1, |
|
57
|
15
|
|
|
15
|
|
170
|
ANYCASE => 0, |
|
58
|
|
|
|
|
|
|
INCLUDE_PATH => Catmandu->root, |
|
59
|
|
|
|
|
|
|
}; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my ($self) = @_; |
|
63
|
|
|
|
|
|
|
my $opts = $self->_tt_opts; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $instance_key = do { |
|
66
|
15
|
|
|
15
|
|
590
|
local $Storable::canonical = 1; |
|
67
|
15
|
|
|
|
|
189
|
freeze($opts); |
|
68
|
|
|
|
|
|
|
}; |
|
69
|
15
|
|
|
|
|
89
|
if (my $instance = $TT_INSTANCES->{$instance_key}) { |
|
70
|
15
|
|
|
|
|
29
|
return $instance; |
|
71
|
15
|
|
|
|
|
53
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
15
|
100
|
|
|
|
1072
|
my $vars = $opts->{VARIABLES} ||= {}; |
|
74
|
8
|
|
|
|
|
176
|
$vars->{_root} = Catmandu->root; |
|
75
|
|
|
|
|
|
|
$vars->{_config} = Catmandu->config; |
|
76
|
|
|
|
|
|
|
local $Template::Stash::PRIVATE = 0; |
|
77
|
7
|
|
50
|
|
|
53
|
$TT_INSTANCES->{$instance_key} = Template->new({%$opts}); |
|
78
|
7
|
|
|
|
|
40
|
} |
|
79
|
7
|
|
|
|
|
132
|
|
|
80
|
7
|
|
|
|
|
88
|
my ($self, $tmpl, $data) = @_; |
|
81
|
7
|
|
|
|
|
80
|
unless ($self->_tt->process($tmpl, $data || {}, $self->fh)) { |
|
82
|
|
|
|
|
|
|
my $msg = "Template error"; |
|
83
|
|
|
|
|
|
|
$msg .= ": " . $self->_tt->error->info if $self->_tt->error; |
|
84
|
|
|
|
|
|
|
Catmandu::Error->throw($msg); |
|
85
|
13
|
|
|
13
|
|
26
|
} |
|
86
|
13
|
50
|
100
|
|
|
201
|
} |
|
87
|
0
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
my ($self, $data) = @_; |
|
89
|
0
|
|
|
|
|
|
unless ($self->_before_done) { |
|
90
|
|
|
|
|
|
|
$self->fh->print($XML_DECLARATION) if $self->xml; |
|
91
|
|
|
|
|
|
|
$self->_process($self->template_before) if $self->template_before; |
|
92
|
|
|
|
|
|
|
$self->_before_done(1); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
$self->_process($self->template, $data); |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my ($self) = @_; |
|
98
|
|
|
|
|
|
|
unless ($self->_before_done) { |
|
99
|
|
|
|
|
|
|
$self->fh->print($XML_DECLARATION) if $self->xml; |
|
100
|
|
|
|
|
|
|
$self->_process($self->template_before) if $self->template_before; |
|
101
|
|
|
|
|
|
|
$self->_before_done(1); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
$self->_process($self->template_after) if $self->template_after; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 NAME |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Catmandu::Exporter::Template - a TT2 Template exporter in Catmandu style |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# From the command line |
|
113
|
|
|
|
|
|
|
echo '{"colors":["red","green","blue"]}' | |
|
114
|
|
|
|
|
|
|
catmandu convert JSON to Template --template `pwd`/xml.tt |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
where xml.tt like: |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
<colors> |
|
119
|
|
|
|
|
|
|
[% FOREACH c IN colors %] |
|
120
|
|
|
|
|
|
|
<color>[% c %]</color> |
|
121
|
|
|
|
|
|
|
[% END %] |
|
122
|
|
|
|
|
|
|
</colors> |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# From perl |
|
125
|
|
|
|
|
|
|
use Catmandu::Exporter::Template; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $exporter = Catmandu::Exporter::Template->new( |
|
128
|
|
|
|
|
|
|
fix => 'myfix.txt' |
|
129
|
|
|
|
|
|
|
xml => 1, |
|
130
|
|
|
|
|
|
|
template_before => '<path>/header.xml' , |
|
131
|
|
|
|
|
|
|
template => '<path>/record.xml' , |
|
132
|
|
|
|
|
|
|
template_after => '<path>/footer.xml' , |
|
133
|
|
|
|
|
|
|
); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$exporter->add_many($arrayref); |
|
136
|
|
|
|
|
|
|
$exporter->add_many($iterator); |
|
137
|
|
|
|
|
|
|
$exporter->add_many(sub { }); |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$exporter->add($hashref); |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$exporter->commit; # trigger the template_after |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
printf "exported %d objects\n" , $exporter->count; |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This L<Catmandu::Exporter> can be used to export records using |
|
148
|
|
|
|
|
|
|
L<Template Toolkit|Template::Manual>. If you are new to Catmandu |
|
149
|
|
|
|
|
|
|
see L<Catmandu::Tutorial>. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item template |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Required. Must contain path to the template. |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item xml |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Optional. Value: 0 or 1. Prepends xml header to the template. |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item template_before |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Optional. Prepend output to the export. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item template_after |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Optional. Append output to the export. |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item fix |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Optional. Apply Catmandu fixes while exporting. |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item [Template Toolkit configuration options] |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
You can also pass all Template Toolkit configuration options. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=back |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
L<Catmandu::Exporter>, L<Template> |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
1; |