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