line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Exporter::Template; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
51450
|
use namespace::clean; |
|
2
|
|
|
|
|
56101
|
|
|
2
|
|
|
|
|
11
|
|
4
|
2
|
|
|
2
|
|
1793
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
190027
|
|
|
2
|
|
|
|
|
13
|
|
5
|
2
|
|
|
2
|
|
2513
|
use Catmandu::Util qw(is_string); |
|
2
|
|
|
|
|
121349
|
|
|
2
|
|
|
|
|
324
|
|
6
|
2
|
|
|
2
|
|
1613
|
use Catmandu; |
|
2
|
|
|
|
|
481074
|
|
|
2
|
|
|
|
|
13
|
|
7
|
2
|
|
|
2
|
|
1784
|
use Template; |
|
2
|
|
|
|
|
41320
|
|
|
2
|
|
|
|
|
67
|
|
8
|
2
|
|
|
2
|
|
18
|
use Moo; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
17
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
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 !~ /\.tt$/ ? "$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
|
3
|
|
|
3
|
|
6
|
my $self = shift; |
33
|
3
|
|
|
|
|
9
|
local $Template::Stash::PRIVATE = 0; |
34
|
3
|
|
|
|
|
26
|
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
|
3
|
|
|
|
|
58070
|
my @fields = qw/tag_style start_tag end_tag interpolate eval_perl/; |
48
|
3
|
100
|
|
|
|
10
|
map { $opts{ uc $_ } = $self->$_ if $self->$_; } @fields; |
|
15
|
|
|
|
|
85
|
|
49
|
|
|
|
|
|
|
|
50
|
3
|
|
|
|
|
42
|
state $tt = Template->new(%opts); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _process { |
54
|
3
|
|
|
3
|
|
8
|
my ( $self, $tmpl, $data ) = @_; |
55
|
3
|
50
|
50
|
|
|
13
|
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
|
58656
|
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; |