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 #everything from here to the next marker should be removed after the release of 2.7.0
67 if ( ref $description_array eq 'ARRAY'
68 && scalar @{$description_array} == scalar @{$ary}
69 && $self->{"chars_per_line"} != 0)
71 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
74 $description_array->[$i] =~ s/(\\newline\s?)*$//;
75 $lines++ while ($description_array->[$i] =~ m/\\newline/g);
78 if ($current_page == 1) {
79 $lpp = $self->{"lines_on_first_page"};
81 $lpp = $self->{"lines_on_second_page"};
84 # Yes we need a manual page break -- or the user has forced one
85 if ( (($current_line + $lines) > $lpp)
86 || ($description_array->[$i] =~ /<pagebreak>/)
87 || ( ref $longdescription_array eq 'ARRAY'
88 && $longdescription_array->[$i] =~ /<pagebreak>/)) {
89 my $pb = $self->{"pagebreak_block"};
91 # replace the special variables <%sumcarriedforward%>
94 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
95 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
96 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
98 my $new_text = $self->parse_block($pb, (@indices, $i));
99 return undef unless (defined($new_text));
100 $new_contents .= $new_text;
105 $current_line += $lines;
107 #stop removing code here.
109 if ( ref $linetotal_array eq 'ARRAY'
110 && $i < scalar(@{$linetotal_array})) {
111 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
114 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
116 my $new_text = $self->parse_block($text, (@indices, $i));
117 return undef unless (defined($new_text));
118 $new_contents .= $start_tag . $new_text . $end_tag;
120 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
122 return $new_contents;
126 my ($self, $text, $pos, $var, $not) = @_;
128 my $tag_start_len = length $self->{tag_start};
131 $pos = 0 unless ($pos);
133 while ($pos < length($text)) {
136 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
138 my $keyword_pos = $pos - 1 + $tag_start_len;
140 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 7) eq 'foreach')) {
143 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
146 "$self->{tag_start}else$self->{tag_end} outside of "
147 . "$self->{tag_start}if$self->{tag_end} / "
148 . "$self->{tag_start}ifnot$self->{tag_end}.";
152 my $block = substr($text, 0, $pos - 1);
153 substr($text, 0, $pos - 1) = "";
154 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
155 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
157 return ($block, $text);
159 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
162 my $block = substr($text, 0, $pos - 1);
163 substr($text, 0, $pos - 1) = "";
164 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
166 return ($block, $text);
175 $main::lxdebug->enter_sub();
177 my ($self, $contents, @indices) = @_;
179 my $new_contents = "";
181 while ($contents ne "") {
182 my $pos_if = index($contents, $self->{tag_start} . 'if');
183 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
185 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
186 $new_contents .= $self->substitute_vars($contents, @indices);
190 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
191 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
192 substr($contents, 0, $pos_foreach) = "";
194 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
195 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
196 $main::lxdebug->leave_sub();
202 substr($contents, 0, length($&)) = "";
205 ($block, $contents) = $self->find_end($contents);
207 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
208 $main::lxdebug->leave_sub();
212 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
213 if (!defined($new_text)) {
214 $main::lxdebug->leave_sub();
217 $new_contents .= $new_text;
220 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
221 $main::lxdebug->leave_sub();
227 $main::lxdebug->leave_sub();
229 return $new_contents;
232 sub parse_first_line {
234 my $line = shift || "";
236 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
238 $self->{error} = "The tag start and end markers must not be equal.";
242 $self->set_tag_style($1, $2);
248 sub _parse_config_option {
255 my ($key, $value) = split m/\s*=\s*/, $line, 2;
257 if ($key eq 'tag-style') {
258 $self->set_tag_style(split(m/\s+/, $value, 2));
262 sub _parse_config_lines {
266 my ($comment_start, $comment_end) = ("", "");
268 if (ref $self eq 'SL::Template::LaTeX') {
269 $comment_start = '\s*%';
270 } elsif (ref $self eq 'SL::Template::HTML') {
271 $comment_start = '\s*<!--';
272 $comment_end = '(?:--)?>\s*';
274 $comment_start = '\s*\#';
277 my $num_lines = scalar @{ $lines };
280 while ($i < $num_lines) {
281 my $line = $lines->[$i];
283 if ($line !~ m/^${comment_start}\s*config\s*:(.*?)${comment_end}$/i) {
288 $self->_parse_config_option($1);
289 splice @{ $lines }, $i, 1;
294 sub _force_mandatory_packages {
298 my (%used_packages, $document_start_line, $last_usepackage_line);
300 foreach my $i (0 .. scalar @{ $lines } - 1) {
301 if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
302 $used_packages{$1} = 1;
303 $last_usepackage_line = $i;
305 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
306 $document_start_line = $i;
312 my $insertion_point = defined($document_start_line) ? $document_start_line
313 : defined($last_usepackage_line) ? $last_usepackage_line
314 : scalar @{ $lines } - 1;
316 foreach my $package (qw(textcomp)) {
317 next if $used_packages{$package};
318 splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n";
326 my $form = $self->{"form"};
328 if (!open(IN, "$form->{templates}/$form->{IN}")) {
329 $self->{"error"} = "$!";
332 binmode IN, ":utf8" if $::locale->is_utf8;
336 $self->_parse_config_lines(\@lines);
337 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'SL::Template::LaTeX');
339 my $contents = join("", @lines);
341 # detect pagebreak block and its parameters
342 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) {
343 $self->{"chars_per_line"} = $1;
344 $self->{"lines_on_first_page"} = $2;
345 $self->{"lines_on_second_page"} = $3;
346 $self->{"pagebreak_block"} = $4;
348 substr($contents, length($`), length($&)) = "";
351 $self->{"forced_pagebreaks"} = [];
353 my $new_contents = $self->parse_block($contents);
354 if (!defined($new_contents)) {
355 $main::lxdebug->leave_sub();
359 if ($::locale->is_utf8) {
360 binmode OUT, ":utf8";
361 print OUT Unicode::Normalize::normalize('C', $new_contents);
364 print OUT $new_contents;
367 if ($form->{"format"} =~ /postscript/i) {
368 return $self->convert_to_postscript();
369 } elsif ($form->{"format"} =~ /pdf/i) {
370 return $self->convert_to_pdf();
376 sub convert_to_postscript {
378 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
380 # Convert the tex file to postscript
382 if (!chdir("$userspath")) {
383 $self->{"error"} = "chdir : $!";
388 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
390 my $latex = $self->_get_latex_path();
391 my $old_home = $ENV{HOME};
392 my $old_openin_any = $ENV{openin_any};
393 $ENV{HOME} = $userspath =~ m|^/| ? $userspath : getcwd();
394 $ENV{openin_any} = "p";
396 for (my $run = 1; $run <= 2; $run++) {
397 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
398 "> $form->{tmpfile}.err");
400 $ENV{HOME} = $old_home;
401 $ENV{openin_any} = $old_openin_any;
402 $self->{"error"} = $form->cleanup($latex);
407 $form->{tmpfile} =~ s/tex$/dvi/;
409 system("dvips $form->{tmpfile} -o -q > /dev/null");
410 $ENV{HOME} = $old_home;
411 $ENV{openin_any} = $old_openin_any;
414 $self->{"error"} = "dvips : $!";
415 $self->cleanup('dvips');
418 $form->{tmpfile} =~ s/dvi$/ps/;
427 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
429 # Convert the tex file to PDF
431 if (!chdir("$userspath")) {
432 $self->{"error"} = "chdir : $!";
437 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
439 my $latex = $self->_get_latex_path();
440 my $old_home = $ENV{HOME};
441 my $old_openin_any = $ENV{openin_any};
442 $ENV{HOME} = $userspath =~ m|^/| ? $userspath : getcwd();
443 $ENV{openin_any} = "p";
445 for (my $run = 1; $run <= 2; $run++) {
446 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
447 "> $form->{tmpfile}.err");
449 $ENV{HOME} = $old_home;
450 $ENV{openin_any} = $old_openin_any;
451 $self->{error} = $form->cleanup($latex);
456 $ENV{HOME} = $old_home;
457 $ENV{openin_any} = $old_openin_any;
458 $form->{tmpfile} =~ s/tex$/pdf/;
463 sub _get_latex_path {
464 return $::lx_office_conf{applications}->{latex} || 'pdflatex';
467 sub get_mime_type() {
470 if ($self->{"form"}->{"format"} =~ /postscript/i) {
471 return "application/postscript";
473 return "application/pdf";