File Coverage

lib/Template/Latex.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             #============================================================= -*-perl-*-
2             #
3             # Template::Latex
4             #
5             # DESCRIPTION
6             # Provides an interface to Latex from the Template Toolkit.
7             #
8             # AUTHOR
9             # Chris Travers <chris.travers@gmail.com> (Current Maintainer)
10             # Andrew Ford <a.ford@ford-mason.co.uk>
11             # Andy Wardley <abw@wardley.org>
12             #
13             # COPYRIGHT
14             # Copyright (C) 2014 Chris Travers. All Rights Reserved.
15             # Copyright (C) 2006-2014 Andrew Ford. All Rights Reserved.
16             # Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved.
17             #
18             # This module is free software; you can redistribute it and/or
19             # modify it under the same terms as Perl itself.
20             #
21             # HISTORY
22             # * Latex plugin originally written by Craig Barratt, Apr 28 2001.
23             # * Win32 additions by Richard Tietjen.
24             # * Extracted into a separate Template::Latex module by Andy Wardley,
25             # May 2006
26             # * Removed the functionality to specify program pathnames on the FILTER call
27             # Andrew Ford, 05 June 2006
28             # * Now runs BibTeX and Makeindex if required
29             # Andrew Ford, 05 June 2006
30             # * Rewritten to set up TEXINPUTS so that inclusions can be found and to run
31             # latex, bibtex and makeindex repeatedly as needed until references stabilize
32             # (NOT YET FINISHED)
33             # Andrew Ford, September 2007
34             #
35             #========================================================================
36            
37             package Template::Latex;
38              
39 2     2   5254 use strict;
  2         4  
  2         74  
40 2     2   14 use warnings;
  2         2  
  2         65  
41 2     2   10 use base 'Template';
  2         13  
  2         2049  
42 2     2   58855 use Template::Exception;
  2         5  
  2         47  
43 2     2   1500 use Template::Plugin::Latex;
  0            
  0            
44             use LaTeX::Driver;
45              
46             our $VERSION = 3.06;
47             our $DEBUG = 0 unless defined $DEBUG;
48             our $ERROR = '';
49             our $FILTER = 'latex'; # default filter name
50             our $FORMAT = ''; # output format (auto-detect if unset)
51             our @PROGRAMS = qw( latex pdflatex bibtex makeindex dvips ps2pdf );
52              
53              
54             sub new {
55             my $class = shift;
56             my $config = @_ && ref $_[0] eq 'HASH' ? shift : { @_ };
57             my $self = $class->SUPER::new($config) || return;
58              
59             # # set default format from config option
60             # $self->latex_format($config->{ LATEX_FORMAT })
61             # if $config->{ LATEX_FORMAT };
62              
63             # # set latex paths from config options
64             # $self->latex_paths({
65             # latex => $config->{ LATEX_PATH },
66             # pdflatex => $config->{ PDFLATEX_PATH },
67             # dvips => $config->{ DVIPS_PATH },
68             # ps2pdf => $config->{ PS2PDF_PATH },
69             # bibtex => $config->{ BIBTEX_PATH },
70             # makeindex => $config->{ MAKEINDEX_PATH },
71             # });
72              
73              
74             # install the latex filter
75             Template::Plugin::Latex->new($self->context, {});
76              
77             #$self->define_filter( $self->context() );
78              
79             return $self;
80             }
81              
82              
83             #------------------------------------------------------------------------
84             # latex_format()
85             # latex_path()
86             # pdflatex_path()
87             # dvips_path()
88             #
89             # Methods to get/set the $FORMAT, $LATEX, $PDFLATEX and $DVIPS package
90             # variables that specify the default output format and the paths to
91             # the latex, pdflatex and dvips programs.
92             #------------------------------------------------------------------------
93              
94             sub latex_format {
95             my $class = shift;
96             return @_ ? ($FORMAT = shift) : $FORMAT;
97             }
98              
99             sub latex_path {
100             my $class = shift;
101             return LaTeX::Driver->program_path('latex', @_);
102             }
103              
104             sub pdflatex_path {
105             my $class = shift;
106             return LaTeX::Driver->program_path('pdflatex', @_);
107             }
108              
109             sub dvips_path {
110             my $class = shift;
111             return LaTeX::Driver->program_path('dvips', @_);
112             }
113              
114             sub ps2pdf_path {
115             my $class = shift;
116             return LaTeX::Driver->program_path('ps2pdf', @_);
117             }
118              
119             sub bibtex_path {
120             my $class = shift;
121             return LaTeX::Driver->program_path('bibtex', @_);
122             }
123              
124             sub makeindex_path {
125             my $class = shift;
126             return LaTeX::Driver->program_path('makeindex', @_);
127             }
128              
129              
130             #------------------------------------------------------------------------
131             # latex_paths()
132             #
133             # Method to get/set the above all in one go.
134             #------------------------------------------------------------------------
135              
136             sub latex_paths {
137             my $class = shift;
138             if (@_) {
139             my $args = ref $_[0] eq 'HASH' ? shift : { @_ };
140              
141             LaTeX::Driver->program_path($_, $args->{$_})
142             for qw( latex pdflatex bibtex makeindex dvips ps2pdf );
143             }
144             else {
145             return {
146             map { ( $_ => LaTeX::Driver->program_path($_) ) } @PROGRAMS
147             };
148             }
149             }
150              
151             1;
152              
153             __END__
154              
155              
156             #------------------------------------------------------------------------
157             # define_filter($context, $config)
158             #
159             # This method defines the latex filter in the context specified as the
160             # first argument. A list or hash ref of named parameters can follow
161             # providing default configuration options.
162             #------------------------------------------------------------------------
163              
164             # sub define_filter {
165             # my $class = shift;
166             # my $context = shift;
167             # my $default = @_ && ref $_[0] eq 'HASH' ? shift : { @_ };
168             # my $filter = $default->{ filter } || $FILTER;
169              
170             # # default any config item not set to values in package variables
171             # $default->{ format } ||= $FORMAT;
172             # $default->{ latex } ||= $LATEX;
173             # $default->{ pdflatex } ||= $PDFLATEX;
174             # $default->{ dvips } ||= $DVIPS;
175             # $default->{ ps2pdf } ||= $PS2PDF;
176             # $default->{ bibtex } ||= $BIBTEX;
177             # $default->{ makeindex } ||= $MAKEINDEX;
178              
179             # # define a factory subroutine to be called when the filter is used.
180             # my $factory = sub {
181             # my $context = shift;
182             # my $config = @_ && ref $_[0] eq 'HASH' ? pop : { };
183              
184             # # merge any configuration parameters specified when the filter
185             # # is used with the defaults provided when the filter was defined
186             # $config->{ $_ } ||= $default->{ $_ }
187             # for (qw( format latex pdflatex dvips ps2pdf bibtex makeindex ));
188              
189             # # output file can be specified as the first argument
190             # $config->{ output } = shift if @_;
191              
192             # $config->{ DEBUG } ||= $DEBUG;
193              
194             # # return an anonymous filter subroutine which calls the real
195             # # filter() method passing the context and merged config params
196             # return sub {
197             # Template::Latex::Driver->run(shift, $context, $config);
198             # };
199             # };
200              
201             # # install the filter factory in the context
202             # $context->define_filter( $filter => $factory, 1 );
203             # $context->define_filter( detex => \&detex_filter );
204             # }
205              
206              
207             1;
208              
209             __END__
210              
211              
212             =head1 NAME
213              
214             Template::Latex - Latex support for the Template Toolkit
215              
216             =head1 SYNOPSIS
217              
218             use Template::Latex;
219            
220             my $tt = Template::Latex->new({
221             INCLUDE_PATH => '/path/to/templates',
222             OUTPUT_PATH => '/path/to/pdf/output',
223             LATEX_FORMAT => 'pdf',
224             });
225             my $vars = {
226             title => 'Hello World',
227             }
228             $tt->process('example.tt2', $vars, 'example.pdf', binmode => 1)
229             || die $tt->error();
230              
231             =head1 DESCRIPTION
232              
233             The Template::Latex module is a wrapper of convenience around the
234             Template module, providing additional support for generating PDF,
235             PostScript and DVI documents from LaTeX templates.
236              
237             You use the Template::Latex module exactly as you would the Template
238             module.
239              
240             my $tt = Template::Latex->new(\%config);
241             $tt->process($input, \%vars, $output)
242             || die $t->error();
243              
244             It supports a number of additional configuration parameters. The
245             C<LATEX_PATH>, C<PDFLATEX_PATH> and C<DVIPS_PATH> options can be used
246             to specify the paths to the F<latex>, F<pdflatex> and F<dvips> program
247             on your system, respectively. These are usually hard-coded in the
248             Template::Latex C<$LATEX>, C<$PDFLATEX> and C<$DVIPS> package
249             variables based on the values set when you run C<perl Makefile.PL> to
250             configure Template::Latex at installation time. You only need to
251             specify these paths if they've moved since you installed
252             Template::Latex or if you want to use different versions for some
253             reason.
254              
255             my $tt = Template::Latex->new({
256             LATEX_PATH => '/usr/bin/latex',
257             PDFLATEX_PATH => '/usr/bin/pdflatex',
258             DVIPS_PATH => '/usr/bin/dvips',
259             });
260              
261             It also provides the C<LATEX_FORMAT> option to specify the default
262             output format. This can be set to C<pdf>, C<ps> or C<dvi>.
263              
264             my $tt = Template::Latex->new({
265             LATEX_FORMAT => 'pdf',
266             });
267              
268             The C<latex> filter is automatically defined when you use the
269             Template::Latex module. There's no need to load the Latex plugin in
270             this case, although you can if you want (e.g. to set some
271             configuration defaults). If you're using the regular Template module
272             then you should first load the Latex plugin to define the C<latex>
273             filter.
274              
275             [% USE Latex %]
276             [% FILTER latex('example.pdf') %]
277             ...LaTeX doc...
278             [% END %]
279              
280             =head1 PUBLIC METHODS
281              
282             The Template::Latex module is a subclass of the Template module and
283             inherits all its methods. Please consult the documentation for the
284             L<Template> module for further information on using it for template
285             processing. Wherever you see C<Template> substitute it for
286             C<Template::Latex>.
287              
288             In addition to those inherted from the Template module, the following
289             methods are also defined.
290              
291             =head2 latex_paths()
292              
293             Method to get or set the paths to the F<latex>, F<pdflatex> and
294             F<dvips> programs. These values are stored in the Template::Latex
295             C<$LATEX>, C<$PDFLATEX> and C<$DVIPS> package variables, respectively.
296             It can be called as either a class or object method.
297              
298             Template::Latex->latex_paths({
299             latex => '/usr/bin/latex',
300             pdflatex => '/usr/bin/pdflatex',
301             dvips => '/usr/bin/dvips',
302             });
303              
304             my $paths = Template::Latex->latex_paths();
305             print $paths->{ latex }; # /usr/bin/latex
306              
307             =head2 latex_path()
308              
309             Method to get or set the C<$Template::Latex::LATEX> package
310             variable which defines the location of the F<latex> program on your
311             system. It can be called as a class or object method.
312              
313             Template::Latex->latex_path('/usr/bin/latex');
314             print Template::Latex->latex_path(); # '/usr/bin/latex'
315              
316             =head2 pdflatex_path()
317              
318             Method to get or set the C<$Template::Latex::PDFLATEX> package
319             variable which defines the location of the F<pdflatex> program on your
320             system. It can be called as a class or object method.
321              
322             Template::Latex->pdflatex_path('/usr/bin/pdflatex');
323             print Template::Latex->pdflatex_path(); # '/usr/bin/pdflatex'
324              
325             =head2 dvips_path()
326              
327             Method to get or set the C<$Template::Latex::DVIPS> package
328             variable which defines the location of the F<dvips> program on your
329             system. It can be called as a class or object method.
330              
331             Template::Latex->dvips_path('/usr/bin/dvips');
332             print Template::Latex->dvips_path(); # '/usr/bin/dvips'
333              
334             =head2 bibtex_path()
335              
336             Method to get or set the C<$Template::Latex::BIBTEX> package
337             variable which defines the location of the F<bibtex> program on your
338             system. It can be called as a class or object method.
339              
340             Template::Latex->bibtex_path('/usr/bin/bibtex');
341             print Template::Latex->bibtex_path(); # '/usr/bin/bibtex'
342              
343             =head2 makeindex_path()
344              
345             Method to get or set the C<$Template::Latex::MAKEINDEX> package
346             variable which defines the location of the F<makeindex> program on your
347             system. It can be called as a class or object method.
348              
349             Template::Latex->makeindex_path('/usr/bin/makeindex');
350             print Template::Latex->makeindex_path(); # '/usr/bin/makeindex'
351              
352             =head1 INTERNALS
353              
354             This section is aimed at a technical audience. It documents the
355             internal methods and subroutines as a reference for the module's
356             developers, maintainers and anyone interesting in understanding how it
357             works. You don't need to know anything about them to use the module
358             and can safely skip this section.
359              
360             =head2 define_filter($context,\%config)
361              
362             This class method installs the C<latex> filter in the context passed
363             as the first argument. The second argument is a hash reference
364             containing any default filter parameters (e.g. those specified when
365             the Template::Plugin::Latex plugin is loaded via a C<USE> directive).
366              
367             Template::Latex->define_filter($context, { format => 'pdf' });
368              
369             The filter is installed as a I<dynamic filter factory>. This is just
370             a fancy way of saying that the filter generates a new filter
371             subroutine each time it is used to account for different invocation
372             parameters. The filter subroutine it creates is effectively a wrapper
373             (a "closure" in technical terms) around the C<filter()> subroutine
374             (see below) which does the real work. The closure keeps track of any
375             configuration parameters specified when the filter is first defined
376             and/or when the filter is invoked. It passes the merged configuration
377             as the second argument to the C<filter()> subroutine (see below).
378              
379             See the L<Template::Filters> module for further information on how
380             filters work.
381              
382             =head2 filter($text,\%config)
383              
384             This is the main LaTeX filter subroutine which is called by the
385             Template Toolkit to generate a LaTeX document from the text passed as
386             the first argument. The second argument is a reference to a hash
387             array of configuration parameters. These are usually provided by the
388             filter subroutine that is generated by the filter factory.
389              
390             Template::Latex::filter($latex, {
391             latex => '/usr/bin/latex',
392             pdflatex => '/usr/bin/pdflatex',
393             dvips => '/usr/bin/dvips',
394             output => 'example.pdf',
395             });
396              
397             =head2 throw($message)
398              
399             Subroutine which throws a L<Template::Exception> error using C<die>.
400             The exception type is set to C<latex>.
401              
402             Template::Latex::throw("I'm sorry Dave, I can't do that");
403              
404             =head2 debug($message)
405              
406             Debugging subroutine which print all argument to STDERR. Set the
407             C<$DEBUG> package variable to enable debugging messages.
408              
409             $Template::Latex::DEBUG = 1;
410              
411             =head1 AUTHOR
412              
413             Andrew Ford E<lt>a.ford@ford-mason.co.ukE<gt> (current maintainer)
414              
415             Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
416              
417             The original Latex plugin on which this is based was written by Craig
418             Barratt with additions for Win32 by Richard Tietjen.
419              
420             =head1 COPYRIGHT
421              
422             Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved.
423              
424             Copyright (C) 2006-2007 Andrew Ford. All Rights Reserved.
425              
426             This module is free software; you can redistribute it and/or
427             modify it under the same terms as Perl itself.
428              
429             =head1 SEE ALSO
430              
431             L<Template::Plugin::Latex>
432              
433             =cut
434              
435             # Local Variables:
436             # mode: perl
437             # perl-indent-level: 4
438             # indent-tabs-mode: nil
439             # End:
440             #
441             # vim: expandtab shiftwidth=4: