line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Exporter::Template; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
59689
|
use namespace::clean; |
|
3
|
|
|
|
|
38661
|
|
|
3
|
|
|
|
|
17
|
|
4
|
3
|
|
|
3
|
|
1874
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
245902
|
|
|
3
|
|
|
|
|
28
|
|
5
|
3
|
|
|
3
|
|
3496
|
use Catmandu::Util qw(is_string); |
|
3
|
|
|
|
|
127639
|
|
|
3
|
|
|
|
|
502
|
|
6
|
3
|
|
|
3
|
|
1572
|
use Catmandu; |
|
3
|
|
|
|
|
544717
|
|
|
3
|
|
|
|
|
25
|
|
7
|
3
|
|
|
3
|
|
3443
|
use Template; |
|
3
|
|
|
|
|
54629
|
|
|
3
|
|
|
|
|
103
|
|
8
|
3
|
|
|
3
|
|
26
|
use Moo; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
95
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
with 'Catmandu::Exporter'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $XML_DECLARATION = qq(\n); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $ADD_TT_EXT = sub { |
17
|
|
|
|
|
|
|
my $tmpl = $_[0]; |
18
|
|
|
|
|
|
|
is_string($tmpl) && $tmpl !~ /\.\w{2,4}$/ ? "$tmpl.tt" : $tmpl; |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has xml => ( is => 'ro' ); |
22
|
|
|
|
|
|
|
has template_before => ( is => 'ro', coerce => $ADD_TT_EXT ); |
23
|
|
|
|
|
|
|
has template => ( is => 'ro', coerce => $ADD_TT_EXT, required => 1 ); |
24
|
|
|
|
|
|
|
has template_after => ( is => 'ro', coerce => $ADD_TT_EXT ); |
25
|
|
|
|
|
|
|
has start_tag => ( is => 'ro' ); |
26
|
|
|
|
|
|
|
has end_tag => ( is => 'ro' ); |
27
|
|
|
|
|
|
|
has tag_style => ( is => 'ro' ); |
28
|
|
|
|
|
|
|
has interpolate => ( is => 'ro' ); |
29
|
|
|
|
|
|
|
has eval_perl => ( is => 'ro' ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _tt { |
32
|
5
|
|
|
5
|
|
13
|
my $self = shift; |
33
|
5
|
|
|
|
|
13
|
local $Template::Stash::PRIVATE = 0; |
34
|
5
|
|
|
|
|
50
|
my %opts = ( |
35
|
|
|
|
|
|
|
ENCODING => 'utf8', |
36
|
|
|
|
|
|
|
ABSOLUTE => 1, |
37
|
|
|
|
|
|
|
RELATIVE => 1, |
38
|
|
|
|
|
|
|
ANYCASE => 0, |
39
|
|
|
|
|
|
|
INCLUDE_PATH => Catmandu->roots, |
40
|
|
|
|
|
|
|
VARIABLES => { |
41
|
|
|
|
|
|
|
_roots => Catmandu->roots, |
42
|
|
|
|
|
|
|
_root => Catmandu->root, |
43
|
|
|
|
|
|
|
_config => Catmandu->config, |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
5
|
|
|
|
|
78295
|
my @fields = qw/tag_style start_tag end_tag interpolate eval_perl/; |
48
|
5
|
100
|
|
|
|
15
|
map { $opts{ uc $_ } = $self->$_ if $self->$_; } @fields; |
|
25
|
|
|
|
|
149
|
|
49
|
|
|
|
|
|
|
|
50
|
5
|
|
|
|
|
78
|
state $tt = Template->new(%opts); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _process { |
54
|
5
|
|
|
5
|
|
10
|
my ( $self, $tmpl, $data ) = @_; |
55
|
5
|
50
|
50
|
|
|
21
|
unless ( $self->_tt->process( $tmpl, $data || {}, $self->fh ) ) { |
56
|
0
|
|
|
|
|
0
|
my $msg = "Template error"; |
57
|
0
|
0
|
|
|
|
0
|
$msg .= ": " . $self->_tt->error->info if $self->_tt->error; |
58
|
0
|
|
|
|
|
0
|
Catmandu::Error->throw($msg); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub add { |
63
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
64
|
|
|
|
|
|
|
if ( $self->count == 0 ) { |
65
|
|
|
|
|
|
|
$self->fh->print($XML_DECLARATION) if $self->xml; |
66
|
|
|
|
|
|
|
$self->_process( $self->template_before ) if $self->template_before; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
$self->_process( $self->template, $data ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub commit { |
72
|
1
|
|
|
1
|
1
|
45578
|
my ($self) = @_; |
73
|
1
|
50
|
|
|
|
10
|
$self->_process( $self->template_after ) if $self->template_after; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Catmandu::Exporter::Template - a TT2 Template exporter in Catmandu style |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 SYNOPSIS |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
use Catmandu::Exporter::Template; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $exporter = Catmandu::Exporter::Template->new( |
86
|
|
|
|
|
|
|
fix => 'myfix.txt' |
87
|
|
|
|
|
|
|
xml => 1, |
88
|
|
|
|
|
|
|
template_before => '/header.xml' , |
89
|
|
|
|
|
|
|
template => '/record.xml' , |
90
|
|
|
|
|
|
|
template_after => '/footer.xml' , |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$exporter->add_many($arrayref); |
94
|
|
|
|
|
|
|
$exporter->add_many($iterator); |
95
|
|
|
|
|
|
|
$exporter->add_many(sub { }); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$exporter->add($hashref); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$exporter->commit; # trigger the template_after |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
printf "exported %d objects\n" , $exporter->count; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 DESCRIPTION |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
This L can be used to export records using |
106
|
|
|
|
|
|
|
L. If you are new to Catmandu |
107
|
|
|
|
|
|
|
see L. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 METHODS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Catmandu::Exporter::Template derives from L with all of its |
112
|
|
|
|
|
|
|
methods (C, C, C, and C). The following methods are |
113
|
|
|
|
|
|
|
supported in addition: |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 new(%opts) |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
The only required argument is 'template' which points to a file to render for |
118
|
|
|
|
|
|
|
each exported object. Set the 'template_before' and 'template_before' to add |
119
|
|
|
|
|
|
|
output at the start and end of the export. Optionally provide an 'xml' |
120
|
|
|
|
|
|
|
indicator to include a XML header. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
template: Required. Must contain path to the template. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
xml: Optional. Value: 0 or 1. Prepends xml header to the template. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
template_before: Optional. Prepend template. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
template_after: Optional. Append template. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
fix: Optional. Apply Catmandu fixes while exporting. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item * |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
start_tag |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
end_tag |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
tag_style |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
interpolate |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item * |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
eval_perl |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 commit |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Commit all changes and execute the template_after if given. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 AUTHOR |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Nicolas Steenlant, C<< >> |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 CONTRIBUTOR |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Vitali Peil, C<< >> |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Jakob Voss, C<< >> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
183
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 SEE ALSO |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
L, L |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |