1 package SL::Template::LaTeX;
 
   3 use parent qw(SL::Template::Simple);
 
   8 use Unicode::Normalize qw();
 
  13   my $self = $type->SUPER::new(@_);
 
  19   my ($self, $variable) = @_;
 
  20   my $form = $self->{"form"};
 
  22   $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
 
  24   # Allow some HTML markup to be converted into the output format's
 
  25   # corresponding markup code, e.g. bold or italic.
 
  26   my %markup_replace = ('b' => 'textbf',
 
  30   foreach my $key (keys(%markup_replace)) {
 
  31     my $new = $markup_replace{$key};
 
  32     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
  35   $variable =~ s/[\x00-\x1f]//g;
 
  41   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
  43   my ($form, $new_contents) = ($self->{"form"}, "");
 
  45   my $ary = $self->_get_loop_variable($var, 1, @indices);
 
  49   my ($current_line, $corrent_row) = (0, 1);
 
  50   my $description_array            = $self->_get_loop_variable("description",     1);
 
  51   my $longdescription_array        = $self->_get_loop_variable("longdescription", 1);
 
  52   my $linetotal_array              = $self->_get_loop_variable("linetotal",       1);
 
  54   $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
 
  56   # forech block hasn't given us an array. ignore
 
  57   return $new_contents unless ref $ary eq 'ARRAY';
 
  59   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
  61     $form->{"__first__"}   = $i == 0;
 
  62     $form->{"__last__"}    = ($i + 1) == scalar(@{$ary});
 
  63     $form->{"__odd__"}     = (($i + 1) % 2) == 1;
 
  64     $form->{"__counter__"} = $i + 1;
 
  66     if (   ref $linetotal_array eq 'ARRAY'
 
  67         && $i < scalar(@{$linetotal_array})) {
 
  68       $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
 
  71     $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
  73     my $new_text = $self->parse_block($text, (@indices, $i));
 
  74     return undef unless (defined($new_text));
 
  75     $new_contents .= $start_tag . $new_text . $end_tag;
 
  77   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
  83   my ($self, $text, $pos, $var, $not) = @_;
 
  85   my $tag_start_len = length $self->{tag_start};
 
  88   $pos = 0 unless ($pos);
 
  90   while ($pos < length($text)) {
 
  93     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
  95     my $keyword_pos = $pos - 1 + $tag_start_len;
 
  97     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 7) eq 'foreach')) {
 
 100     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 103             "$self->{tag_start}else$self->{tag_end} outside of "
 
 104           . "$self->{tag_start}if$self->{tag_end} / "
 
 105           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 109       my $block = substr($text, 0, $pos - 1);
 
 110       substr($text, 0, $pos - 1) = "";
 
 111       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 112       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 114       return ($block, $text);
 
 116     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 119         my $block = substr($text, 0, $pos - 1);
 
 120         substr($text, 0, $pos - 1) = "";
 
 121         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 123         return ($block, $text);
 
 132   $main::lxdebug->enter_sub();
 
 134   my ($self, $contents, @indices) = @_;
 
 136   my $new_contents = "";
 
 138   while ($contents ne "") {
 
 139     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 140     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 142     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 143       $new_contents .= $self->substitute_vars($contents, @indices);
 
 147     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 148       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 149       substr($contents, 0, $pos_foreach) = "";
 
 151       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 152         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 153         $main::lxdebug->leave_sub();
 
 159       substr($contents, 0, length($&)) = "";
 
 162       ($block, $contents) = $self->find_end($contents);
 
 164         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 165         $main::lxdebug->leave_sub();
 
 169       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 170       if (!defined($new_text)) {
 
 171         $main::lxdebug->leave_sub();
 
 174       $new_contents .= $new_text;
 
 177       if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
 
 178         $main::lxdebug->leave_sub();
 
 184   $main::lxdebug->leave_sub();
 
 186   return $new_contents;
 
 189 sub parse_first_line {
 
 191   my $line = shift || "";
 
 193   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 195       $self->{error} = "The tag start and end markers must not be equal.";
 
 199     $self->set_tag_style($1, $2);
 
 205 sub _parse_config_option {
 
 212   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 214   if ($key eq 'tag-style') {
 
 215     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 219 sub _parse_config_lines {
 
 223   my ($comment_start, $comment_end) = ("", "");
 
 225   if (ref $self eq 'SL::Template::LaTeX') {
 
 226     $comment_start = '\s*%';
 
 227   } elsif (ref $self eq 'SL::Template::HTML') {
 
 228     $comment_start = '\s*<!--';
 
 229     $comment_end   = '(?:--)?>\s*';
 
 231     $comment_start = '\s*\#';
 
 234   my $num_lines = scalar @{ $lines };
 
 237   while ($i < $num_lines) {
 
 238     my $line = $lines->[$i];
 
 240     if ($line !~ m/^${comment_start}\s*config\s*:(.*?)${comment_end}$/i) {
 
 245     $self->_parse_config_option($1);
 
 246     splice @{ $lines }, $i, 1;
 
 251 sub _force_mandatory_packages {
 
 255   my (%used_packages, $document_start_line, $last_usepackage_line);
 
 257   foreach my $i (0 .. scalar @{ $lines } - 1) {
 
 258     if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
 
 259       $used_packages{$1} = 1;
 
 260       $last_usepackage_line = $i;
 
 262     } elsif ($lines->[$i] =~ m/\\begin{document}/) {
 
 263       $document_start_line = $i;
 
 269   my $insertion_point = defined($document_start_line)  ? $document_start_line
 
 270                       : defined($last_usepackage_line) ? $last_usepackage_line
 
 271                       :                                  scalar @{ $lines } - 1;
 
 273   foreach my $package (qw(textcomp)) {
 
 274     next if $used_packages{$package};
 
 275     splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n";
 
 283   my $form = $self->{"form"};
 
 285   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 286     $self->{"error"} = "$!";
 
 289   binmode IN, ":utf8" if $::locale->is_utf8;
 
 293   $self->_parse_config_lines(\@lines);
 
 294   $self->_force_mandatory_packages(\@lines) if (ref $self eq 'SL::Template::LaTeX');
 
 296   my $contents = join("", @lines);
 
 298   my $new_contents = $self->parse_block($contents);
 
 299   if (!defined($new_contents)) {
 
 300     $main::lxdebug->leave_sub();
 
 304   if ($::locale->is_utf8) {
 
 305     binmode OUT, ":utf8";
 
 306     print OUT Unicode::Normalize::normalize('C', $new_contents);
 
 309     print OUT $new_contents;
 
 312   if ($form->{"format"} =~ /postscript/i) {
 
 313     return $self->convert_to_postscript();
 
 314   } elsif ($form->{"format"} =~ /pdf/i) {
 
 315     return $self->convert_to_pdf();
 
 321 sub convert_to_postscript {
 
 323   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 325   # Convert the tex file to postscript
 
 327   if (!chdir("$userspath")) {
 
 328     $self->{"error"} = "chdir : $!";
 
 333   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 335   my $latex = $self->_get_latex_path();
 
 336   my $old_home = $ENV{HOME};
 
 337   my $old_openin_any = $ENV{openin_any};
 
 338   $ENV{HOME}   = $userspath =~ m|^/| ? $userspath : getcwd();
 
 339   $ENV{openin_any} = "p";
 
 341   for (my $run = 1; $run <= 2; $run++) {
 
 342     system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
 
 343            "> $form->{tmpfile}.err");
 
 345       $ENV{HOME} = $old_home;
 
 346       $ENV{openin_any} = $old_openin_any;
 
 347       $self->{"error"} = $form->cleanup($latex);
 
 352   $form->{tmpfile} =~ s/tex$/dvi/;
 
 354   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 355   $ENV{HOME} = $old_home;
 
 356   $ENV{openin_any} = $old_openin_any;
 
 359     $self->{"error"} = "dvips : $!";
 
 360     $self->cleanup('dvips');
 
 363   $form->{tmpfile} =~ s/dvi$/ps/;
 
 372   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 374   # Convert the tex file to PDF
 
 376   if (!chdir("$userspath")) {
 
 377     $self->{"error"} = "chdir : $!";
 
 382   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 384   my $latex = $self->_get_latex_path();
 
 385   my $old_home = $ENV{HOME};
 
 386   my $old_openin_any = $ENV{openin_any};
 
 387   $ENV{HOME}   = $userspath =~ m|^/| ? $userspath : getcwd();
 
 388   $ENV{openin_any} = "p";
 
 390   for (my $run = 1; $run <= 2; $run++) {
 
 391     system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
 
 392            "> $form->{tmpfile}.err");
 
 394       $ENV{HOME}     = $old_home;
 
 395       $ENV{openin_any} = $old_openin_any;
 
 396       $self->{error} = $form->cleanup($latex);
 
 401   $ENV{HOME} = $old_home;
 
 402   $ENV{openin_any} = $old_openin_any;
 
 403   $form->{tmpfile} =~ s/tex$/pdf/;
 
 408 sub _get_latex_path {
 
 409   return $::lx_office_conf{applications}->{latex} || 'pdflatex';
 
 412 sub get_mime_type() {
 
 415   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 416     return "application/postscript";
 
 418     return "application/pdf";