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