summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/LaTeX.pm
blob: 973cb7243a6402da4f4310a5d666fda978ad62dc (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 warnings;
  29. use strict;
  30. use Error qw(:try);
  31. use Template::Latex;
  32. use LedgerSMB::Template::TTI18N;
  33. sub get_template {
  34. my $name = shift;
  35. return "${name}.tex";
  36. }
  37. sub preprocess {
  38. my $rawvars = shift;
  39. my $vars;
  40. my $type = ref $rawvars;
  41. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  42. return unless defined $type;
  43. if ($type eq 'ARRAY') {
  44. for (@{$rawvars}) {
  45. push @{$vars}, preprocess($_);
  46. }
  47. } elsif (!$type or $type eq 'SCALAR') {
  48. if ($type eq 'SCALAR') {
  49. $vars = $$rawvars;
  50. } else {
  51. $vars = $rawvars;
  52. }
  53. #XXX Fix escaping
  54. $vars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g;
  55. $vars =~ s/"(.*)"/``$1''/gs;
  56. } else {
  57. for ( keys %{$rawvars} ) {
  58. $vars->{$_} = preprocess($rawvars->{$_});
  59. }
  60. }
  61. return $vars;
  62. }
  63. sub process {
  64. my $parent = shift;
  65. my $cleanvars = shift;
  66. my $template;
  67. my $source;
  68. $parent->{outputfile} ||=
  69. "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
  70. if (ref $parent->{template} eq 'SCALAR') {
  71. $source = $parent->{template};
  72. } elsif (ref $parent->{template} eq 'ARRAY') {
  73. $source = join "\n", @{$parent->{template}};
  74. } else {
  75. $source = get_template($parent->{template});
  76. }
  77. $Template::Latex::DEBUG = 1 if $parent->{debug};
  78. my $format = 'ps';
  79. if ($parent->{format_args}{filetype} eq 'dvi') {
  80. $format = 'dvi';
  81. } elsif ($parent->{format_args}{filetype} eq 'pdf') {
  82. $format = 'pdf';
  83. }
  84. $template = Template::Latex->new({
  85. LATEX_FORMAT => $format,
  86. INCLUDE_PATH => $parent->{include_path},
  87. START_TAG => quotemeta('<?lsmb'),
  88. END_TAG => quotemeta('?>'),
  89. DELIMITER => ';',
  90. DEBUG => ($parent->{debug})? 'dirs': undef,
  91. DEBUG_FORMAT => '',
  92. }) || throw Error::Simple Template::Latex->error();
  93. if (not $template->process(
  94. $source,
  95. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  96. 'escape' => \&preprocess},
  97. "$parent->{outputfile}.$format", binmode => 1)) {
  98. throw Error::Simple $template->error();
  99. }
  100. if (lc $format eq 'dvi') {
  101. $parent->{mimetype} = 'application/x-dvi';
  102. } elsif (lc $format eq 'pdf') {
  103. $parent->{mimetype} = 'application/pdf';
  104. } else {
  105. $parent->{mimetype} = 'application/postscript';
  106. }
  107. $parent->{rendered} = "$parent->{outputfile}.$format";
  108. }
  109. sub postprocess {
  110. my $parent = shift;
  111. return $parent->{rendered};
  112. }
  113. 1;