1 | package Vote::View::Latex; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use base 'Vote::View::TT'; |
---|
5 | use LaTeX::Driver; |
---|
6 | use File::Temp; |
---|
7 | |
---|
8 | __PACKAGE__->config( |
---|
9 | TEMPLATE_EXTENSION => '.tt', |
---|
10 | INCLUDE_PATH => Vote->path_to( 'root', 'latex' ), |
---|
11 | PRE_PROCESS => 'includes/header.tt', |
---|
12 | POST_PROCESS => 'includes/footer.tt', |
---|
13 | PLUGIN_BASE => 'Vote::Template::Plugin', |
---|
14 | ); |
---|
15 | |
---|
16 | =head1 NAME |
---|
17 | |
---|
18 | Vote::View::Latex - Latex View for Vote |
---|
19 | |
---|
20 | =head1 DESCRIPTION |
---|
21 | |
---|
22 | TT View for Vote. |
---|
23 | |
---|
24 | =cut |
---|
25 | |
---|
26 | sub process { |
---|
27 | my ($self, $c) = @_; |
---|
28 | |
---|
29 | $c->stash->{format} ||= 'pdf'; |
---|
30 | my $output = $self->render( |
---|
31 | $c, $c->stash->{template} |
---|
32 | ); |
---|
33 | |
---|
34 | if ($c->stash->{format} eq 'tex') { |
---|
35 | $c->res->body($output); |
---|
36 | return; |
---|
37 | } |
---|
38 | |
---|
39 | my ($fh, $filename) = File::Temp::tempfile( |
---|
40 | SUFFIX => ($c->stash->{format}), |
---|
41 | ); |
---|
42 | close($fh); |
---|
43 | |
---|
44 | LaTeX::Driver::hack->new( |
---|
45 | source => \$output, |
---|
46 | output => $filename, |
---|
47 | format => $c->stash->{format}, |
---|
48 | )->run; |
---|
49 | open($fh, '<', $filename); |
---|
50 | binmode($fh); |
---|
51 | $c->res->body(join('', <$fh>)); |
---|
52 | } |
---|
53 | |
---|
54 | # This is just a hack |
---|
55 | # Don't ask me why, but system() return -1 instead 0 |
---|
56 | # under catalyst... |
---|
57 | # This is just an ugly workaround |
---|
58 | |
---|
59 | package LaTeX::Driver::hack; |
---|
60 | use base qw(LaTeX::Driver); |
---|
61 | |
---|
62 | sub run_command { |
---|
63 | my ($self, @args) = @_; |
---|
64 | $self->SUPER::run_command(@args); |
---|
65 | return $self->SUPER::run_command(@args) <= 0 ? 0 : 1; |
---|
66 | } |
---|
67 | |
---|
68 | =head1 SEE ALSO |
---|
69 | |
---|
70 | L<Vote> |
---|
71 | |
---|
72 | =head1 AUTHOR |
---|
73 | |
---|
74 | Thauvin Olivier |
---|
75 | |
---|
76 | =head1 LICENSE |
---|
77 | |
---|
78 | This library is free software, you can redistribute it and/or modify |
---|
79 | it under the same terms as Perl itself or CeCILL. |
---|
80 | |
---|
81 | =cut |
---|
82 | |
---|
83 | 1; |
---|