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