| 1 | #============================================================= -*-perl-*- | ||
| 2 | # | ||
| 3 | # LaTeX::Driver::FilterProgram | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Implements the guts of the latex2xxx filter programs | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andrew Ford <a.ford@ford-mason.co.uk> | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (C) 2007 Andrew Ford. All Rights Reserved. | ||
| 13 | # | ||
| 14 | # This module is free software; you can redistribute it and/or | ||
| 15 | # modify it under the same terms as Perl itself. | ||
| 16 | # | ||
| 17 | # HISTORY | ||
| 18 | # | ||
| 19 | # $Id: Paths.pm 45 2007-09-28 10:33:19Z andrew $ | ||
| 20 | #======================================================================== | ||
| 21 | |||
| 22 | package LaTeX::Driver::FilterProgram; | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | use warnings; | ||
| 26 | use Carp; | ||
| 27 | |||
| 28 | use LaTeX::Driver; | ||
| 29 | use Getopt::Long; | ||
| 30 | use File::Slurp; | ||
| 31 | |||
| 32 | sub execute { | ||
| 33 | my ($class, %options) = @_; | ||
| 34 | my ($source, $output, $tt2mode, $debug, @vars, %var); | ||
| 35 | |||
| 36 | GetOptions( 'output:s' => \$output, | ||
| 37 | 'tt2mode' => \$tt2mode, | ||
| 38 | 'define:s' => \@vars, | ||
| 39 | 'debug' => \$debug ); | ||
| 40 | |||
| 41 | if ( @ARGV ) { | ||
| 42 | $source = shift @ARGV; | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | my $input = join '', <STDIN>; | ||
| 46 | $source = \$input; | ||
| 47 | } | ||
| 48 | |||
| 49 | if ($tt2mode) { | ||
| 50 | eval { | ||
| 51 | require Template; | ||
| 52 | }; | ||
| 53 | if ($@) { | ||
| 54 | die "Cannot load the Template Toolkit - tt2 mode is unavailable\n"; | ||
| 55 | } | ||
| 56 | if (!ref $source) { | ||
| 57 | ${$source} = read_file($source); | ||
| 58 | } | ||
| 59 | |||
| 60 | foreach (@vars) { | ||
| 61 | my ($name, $value) = split / \s* = \s* /mx; | ||
| 62 | printf(STDERR "defining %s as '%s'\n", $name, $value) if $debug; | ||
| 63 | $var{$name} = $value; | ||
| 64 | } | ||
| 65 | |||
| 66 | my $input; | ||
| 67 | my $tt2 = Template->new({}); | ||
| 68 | $tt2->process($source, \%var, \$input) | ||
| 69 | or die $tt2->error(), "\n"; | ||
| 70 | |||
| 71 | $source = \$input; | ||
| 72 | } | ||
| 73 | |||
| 74 | if (!$output or $output eq '-') { | ||
| 75 | my $tmp; | ||
| 76 | $output = \$tmp; | ||
| 77 | } | ||
| 78 | eval { | ||
| 79 | my $drv = LaTeX::Driver->new( source => $source, | ||
| 80 | output => $output, | ||
| 81 | format => $options{format} ); | ||
| 82 | $drv->run; | ||
| 83 | }; | ||
| 84 | if (my $e = LaTeX::Driver::Exception->caught()) { | ||
| 85 | $e->show_trace(1); | ||
| 86 | # my $extra = sprintf("\nat %s line %d (%s)\n%s", $e->file, $e->line, $e->package, $e->trace); | ||
| 87 | die $e; #sprintf("%s\n%s", "$e", $e->trace); | ||
| 88 | } | ||
| 89 | |||
| 90 | if (ref $output) { | ||
| 91 | print ${$output}; | ||
| 92 | } | ||
| 93 | |||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ |