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 $description_array       eq 'ARRAY'
 
  67         && scalar @{$description_array} == scalar @{$ary}
 
  68         && $self->{"chars_per_line"}    != 0)
 
  70       my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
 
  73       $description_array->[$i] =~ s/(\\newline\s?)*$//;
 
  74       $lines++ while ($description_array->[$i] =~ m/\\newline/g);
 
  77       if ($current_page == 1) {
 
  78         $lpp = $self->{"lines_on_first_page"};
 
  80         $lpp = $self->{"lines_on_second_page"};
 
  83       # Yes we need a manual page break -- or the user has forced one
 
  84       if (   (($current_line + $lines) > $lpp)
 
  85           || ($description_array->[$i]     =~ /<pagebreak>/)
 
  86           || (   ref $longdescription_array eq 'ARRAY'
 
  87               && $longdescription_array->[$i] =~ /<pagebreak>/)) {
 
  88         my $pb = $self->{"pagebreak_block"};
 
  90         # replace the special variables <%sumcarriedforward%>
 
  93         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
  94         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
 
  95         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
 
  97         my $new_text = $self->parse_block($pb, (@indices, $i));
 
  98         return undef unless (defined($new_text));
 
  99         $new_contents .= $new_text;
 
 104       $current_line += $lines;
 
 107     if (   ref $linetotal_array eq 'ARRAY'
 
 108         && $i < scalar(@{$linetotal_array})) {
 
 109       $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
 
 112     $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 114     my $new_text = $self->parse_block($text, (@indices, $i));
 
 115     return undef unless (defined($new_text));
 
 116     $new_contents .= $start_tag . $new_text . $end_tag;
 
 118   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 120   return $new_contents;
 
 124   my ($self, $text, $pos, $var, $not) = @_;
 
 126   my $tag_start_len = length $self->{tag_start};
 
 129   $pos = 0 unless ($pos);
 
 131   while ($pos < length($text)) {
 
 134     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
 136     my $keyword_pos = $pos - 1 + $tag_start_len;
 
 138     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 7) eq 'foreach')) {
 
 141     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 144             "$self->{tag_start}else$self->{tag_end} outside of "
 
 145           . "$self->{tag_start}if$self->{tag_end} / "
 
 146           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 150       my $block = substr($text, 0, $pos - 1);
 
 151       substr($text, 0, $pos - 1) = "";
 
 152       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 153       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 155       return ($block, $text);
 
 157     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 160         my $block = substr($text, 0, $pos - 1);
 
 161         substr($text, 0, $pos - 1) = "";
 
 162         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 164         return ($block, $text);
 
 173   $main::lxdebug->enter_sub();
 
 175   my ($self, $contents, @indices) = @_;
 
 177   my $new_contents = "";
 
 179   while ($contents ne "") {
 
 180     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 181     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 183     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 184       $new_contents .= $self->substitute_vars($contents, @indices);
 
 188     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 189       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 190       substr($contents, 0, $pos_foreach) = "";
 
 192       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 193         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 194         $main::lxdebug->leave_sub();
 
 200       substr($contents, 0, length($&)) = "";
 
 203       ($block, $contents) = $self->find_end($contents);
 
 205         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 206         $main::lxdebug->leave_sub();
 
 210       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 211       if (!defined($new_text)) {
 
 212         $main::lxdebug->leave_sub();
 
 215       $new_contents .= $new_text;
 
 218       if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
 
 219         $main::lxdebug->leave_sub();
 
 225   $main::lxdebug->leave_sub();
 
 227   return $new_contents;
 
 230 sub parse_first_line {
 
 232   my $line = shift || "";
 
 234   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 236       $self->{error} = "The tag start and end markers must not be equal.";
 
 240     $self->set_tag_style($1, $2);
 
 246 sub _parse_config_option {
 
 253   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 255   if ($key eq 'tag-style') {
 
 256     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 260 sub _parse_config_lines {
 
 264   my ($comment_start, $comment_end) = ("", "");
 
 266   if (ref $self eq 'SL::Template::LaTeX') {
 
 267     $comment_start = '\s*%';
 
 268   } elsif (ref $self eq 'SL::Template::HTML') {
 
 269     $comment_start = '\s*<!--';
 
 270     $comment_end   = '(?:--)?>\s*';
 
 272     $comment_start = '\s*\#';
 
 275   my $num_lines = scalar @{ $lines };
 
 278   while ($i < $num_lines) {
 
 279     my $line = $lines->[$i];
 
 281     if ($line !~ m/^${comment_start}\s*config\s*:(.*?)${comment_end}$/i) {
 
 286     $self->_parse_config_option($1);
 
 287     splice @{ $lines }, $i, 1;
 
 292 sub _force_mandatory_packages {
 
 296   my (%used_packages, $document_start_line, $last_usepackage_line);
 
 298   foreach my $i (0 .. scalar @{ $lines } - 1) {
 
 299     if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
 
 300       $used_packages{$1} = 1;
 
 301       $last_usepackage_line = $i;
 
 303     } elsif ($lines->[$i] =~ m/\\begin{document}/) {
 
 304       $document_start_line = $i;
 
 310   my $insertion_point = defined($document_start_line)  ? $document_start_line
 
 311                       : defined($last_usepackage_line) ? $last_usepackage_line
 
 312                       :                                  scalar @{ $lines } - 1;
 
 314   foreach my $package (qw(textcomp)) {
 
 315     next if $used_packages{$package};
 
 316     splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n";
 
 324   my $form = $self->{"form"};
 
 326   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 327     $self->{"error"} = "$!";
 
 330   binmode IN, ":utf8" if $::locale->is_utf8;
 
 334   $self->_parse_config_lines(\@lines);
 
 335   $self->_force_mandatory_packages(\@lines) if (ref $self eq 'SL::Template::LaTeX');
 
 337   my $contents = join("", @lines);
 
 339   # detect pagebreak block and its parameters
 
 340   if ($contents =~ /$self->{tag_start_qm}pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*$self->{tag_end_qm}(.*?)$self->{tag_start_qm}end(\s*pagebreak)?$self->{tag_end_qm}/s) {
 
 341     $self->{"chars_per_line"} = $1;
 
 342     $self->{"lines_on_first_page"} = $2;
 
 343     $self->{"lines_on_second_page"} = $3;
 
 344     $self->{"pagebreak_block"} = $4;
 
 346     substr($contents, length($`), length($&)) = "";
 
 349   $self->{"forced_pagebreaks"} = [];
 
 351   my $new_contents = $self->parse_block($contents);
 
 352   if (!defined($new_contents)) {
 
 353     $main::lxdebug->leave_sub();
 
 357   if ($::locale->is_utf8) {
 
 358     binmode OUT, ":utf8";
 
 359     print OUT Unicode::Normalize::normalize('C', $new_contents);
 
 362     print OUT $new_contents;
 
 365   if ($form->{"format"} =~ /postscript/i) {
 
 366     return $self->convert_to_postscript();
 
 367   } elsif ($form->{"format"} =~ /pdf/i) {
 
 368     return $self->convert_to_pdf();
 
 374 sub convert_to_postscript {
 
 376   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 378   # Convert the tex file to postscript
 
 380   if (!chdir("$userspath")) {
 
 381     $self->{"error"} = "chdir : $!";
 
 386   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 388   my $latex = $self->_get_latex_path();
 
 389   my $old_home = $ENV{HOME};
 
 390   my $old_openin_any = $ENV{openin_any};
 
 391   $ENV{HOME}   = $userspath =~ m|^/| ? $userspath : getcwd();
 
 392   $ENV{openin_any} = "p";
 
 394   for (my $run = 1; $run <= 2; $run++) {
 
 395     system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
 
 396            "> $form->{tmpfile}.err");
 
 398       $ENV{HOME} = $old_home;
 
 399       $ENV{openin_any} = $old_openin_any;
 
 400       $self->{"error"} = $form->cleanup($latex);
 
 405   $form->{tmpfile} =~ s/tex$/dvi/;
 
 407   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 408   $ENV{HOME} = $old_home;
 
 409   $ENV{openin_any} = $old_openin_any;
 
 412     $self->{"error"} = "dvips : $!";
 
 413     $self->cleanup('dvips');
 
 416   $form->{tmpfile} =~ s/dvi$/ps/;
 
 425   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 427   # Convert the tex file to PDF
 
 429   if (!chdir("$userspath")) {
 
 430     $self->{"error"} = "chdir : $!";
 
 435   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 437   my $latex = $self->_get_latex_path();
 
 438   my $old_home = $ENV{HOME};
 
 439   my $old_openin_any = $ENV{openin_any};
 
 440   $ENV{HOME}   = $userspath =~ m|^/| ? $userspath : getcwd();
 
 441   $ENV{openin_any} = "p";
 
 443   for (my $run = 1; $run <= 2; $run++) {
 
 444     system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
 
 445            "> $form->{tmpfile}.err");
 
 447       $ENV{HOME}     = $old_home;
 
 448       $ENV{openin_any} = $old_openin_any;
 
 449       $self->{error} = $form->cleanup($latex);
 
 454   $ENV{HOME} = $old_home;
 
 455   $ENV{openin_any} = $old_openin_any;
 
 456   $form->{tmpfile} =~ s/tex$/pdf/;
 
 461 sub _get_latex_path {
 
 462   return $::lx_office_conf{applications}->{latex} || 'pdflatex';
 
 465 sub get_mime_type() {
 
 468   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 469     return "application/postscript";
 
 471     return "application/pdf";