summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/PS.pm
blob: 7ab41dc22f6f75f8a9e5a467f0ffaab6ecf7f7c2 (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::PS Template support module for LedgerSMB
  3. =head1 METHODS
  4. =over
  5. =item get_template ($name)
  6. Returns the appropriate template filename for this format.
  7. =item preprocess ($vars)
  8. Currently does nothing.
  9. =item process ($parent, $cleanvars)
  10. Processes the template for Postscript.
  11. =item postprocess ($parent)
  12. Currently does nothing.
  13. =back
  14. =head1 Copyright (C) 2007, The LedgerSMB core team.
  15. This work contains copyrighted information from a number of sources all used
  16. with permission.
  17. It is released under the GNU General Public License Version 2 or, at your
  18. option, any later version. See COPYRIGHT file for details. For a full list
  19. including contact information of contributors, maintainers, and copyright
  20. holders, see the CONTRIBUTORS file.
  21. =cut
  22. package LedgerSMB::Template::PS;
  23. use Error qw(:try);
  24. use Template::Latex;
  25. use LedgerSMB::Template::TTI18N;
  26. sub get_template {
  27. my $name = shift;
  28. return "${name}.tex";
  29. }
  30. sub preprocess {
  31. my $rawvars = shift;
  32. my $vars;
  33. my $type = ref $rawvars;
  34. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  35. if ($type eq 'ARRAY') {
  36. for (@{$rawvars}) {
  37. push @{$vars}, preprocess($_);
  38. }
  39. } elsif (!$type) {
  40. #XXX Fix escaping
  41. $rawvars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g;
  42. $rawvars =~ s/"(.*)"/``$1''/gs;
  43. return $rawvars;
  44. } else {
  45. for ( keys %{$rawvars} ) {
  46. $vars->{$_} = preprocess($rawvars->{$_});
  47. }
  48. }
  49. return $vars;
  50. }
  51. sub process {
  52. my $parent = shift;
  53. my $cleanvars = shift;
  54. my $template;
  55. $parent->{outputfile} ||=
  56. "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
  57. $template = Template::Latex->new({
  58. LATEX_FORMAT => 'ps',
  59. INCLUDE_PATH => $parent->{include_path},
  60. START_TAG => quotemeta('<?lsmb'),
  61. END_TAG => quotemeta('?>'),
  62. DELIMITER => ';',
  63. DEBUG => ($parent->{debug})? 'dirs': undef,
  64. DEBUG_FORMAT => '',
  65. }) || throw Error::Simple Template::Latex->error();
  66. if (not $template->process(
  67. get_template($parent->{template}),
  68. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  69. 'escape' => \&preprocess},
  70. "$parent->{outputfile}.ps", binmode => 1)) {
  71. throw Error::Simple $template->error();
  72. }
  73. $parent->{mimetype} = 'application/postscript';
  74. }
  75. sub postprocess {
  76. my $parent = shift;
  77. $parent->{rendered} = "$parent->{outputfile}.ps";
  78. return "$parent->{outputfile}.ps";
  79. }
  80. 1;