1 package SL::Template::LaTeX;
3 use parent qw(SL::Template::Simple);
10 my $self = $type->SUPER::new(@_);
16 my ($self, $variable) = @_;
17 my $form = $self->{"form"};
19 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
21 # Allow some HTML markup to be converted into the output format's
22 # corresponding markup code, e.g. bold or italic.
23 my %markup_replace = ('b' => 'textbf',
27 foreach my $key (keys(%markup_replace)) {
28 my $new = $markup_replace{$key};
29 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
32 $variable =~ s/[\x00-\x1f]//g;
38 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
40 my ($form, $new_contents) = ($self->{"form"}, "");
42 my $ary = $self->_get_loop_variable($var, 1, @indices);
46 my ($current_line, $corrent_row) = (0, 1);
47 my $description_array = $self->_get_loop_variable("description", 1);
48 my $longdescription_array = $self->_get_loop_variable("longdescription", 1);
49 my $linetotal_array = $self->_get_loop_variable("linetotal", 1);
51 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
53 # forech block hasn't given us an array. ignore
54 return $new_contents unless ref $ary eq 'ARRAY';
56 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
58 $form->{"__first__"} = $i == 1;
59 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
60 $form->{"__odd__"} = (($i + 1) % 2) == 1;
61 $form->{"__counter__"} = $i + 1;
63 if ( ref $description_array eq 'ARRAY'
64 && scalar @{$description_array} == scalar @{$ary}
65 && $self->{"chars_per_line"} != 0)
67 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
70 $description_array->[$i] =~ s/(\\newline\s?)*$//;
71 $lines++ while ($description_array->[$i] =~ m/\\newline/g);
74 if ($current_page == 1) {
75 $lpp = $self->{"lines_on_first_page"};
77 $lpp = $self->{"lines_on_second_page"};
80 # Yes we need a manual page break -- or the user has forced one
81 if ( (($current_line + $lines) > $lpp)
82 || ($description_array->[$i] =~ /<pagebreak>/)
83 || ( ref $longdescription_array eq 'ARRAY'
84 && $longdescription_array->[$i] =~ /<pagebreak>/)) {
85 my $pb = $self->{"pagebreak_block"};
87 # replace the special variables <%sumcarriedforward%>
90 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
91 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
92 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
94 my $new_text = $self->parse_block($pb, (@indices, $i));
95 return undef unless (defined($new_text));
96 $new_contents .= $new_text;
101 $current_line += $lines;
104 if ( ref $linetotal_array eq 'ARRAY'
105 && $i < scalar(@{$linetotal_array})) {
106 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
109 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
111 my $new_text = $self->parse_block($text, (@indices, $i));
112 return undef unless (defined($new_text));
113 $new_contents .= $start_tag . $new_text . $end_tag;
115 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
117 return $new_contents;
121 my ($self, $text, $pos, $var, $not) = @_;
123 my $tag_start_len = length $self->{tag_start};
126 $pos = 0 unless ($pos);
128 while ($pos < length($text)) {
131 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
133 my $keyword_pos = $pos - 1 + $tag_start_len;
135 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
138 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
141 "$self->{tag_start}else$self->{tag_end} outside of "
142 . "$self->{tag_start}if$self->{tag_end} / "
143 . "$self->{tag_start}ifnot$self->{tag_end}.";
147 my $block = substr($text, 0, $pos - 1);
148 substr($text, 0, $pos - 1) = "";
149 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
150 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
152 return ($block, $text);
154 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
157 my $block = substr($text, 0, $pos - 1);
158 substr($text, 0, $pos - 1) = "";
159 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
161 return ($block, $text);
170 $main::lxdebug->enter_sub();
172 my ($self, $contents, @indices) = @_;
174 my $new_contents = "";
176 while ($contents ne "") {
177 my $pos_if = index($contents, $self->{tag_start} . 'if');
178 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
180 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
181 $new_contents .= $self->substitute_vars($contents, @indices);
185 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
186 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
187 substr($contents, 0, $pos_foreach) = "";
189 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
190 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
191 $main::lxdebug->leave_sub();
197 substr($contents, 0, length($&)) = "";
200 ($block, $contents) = $self->find_end($contents);
202 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
203 $main::lxdebug->leave_sub();
207 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
208 if (!defined($new_text)) {
209 $main::lxdebug->leave_sub();
212 $new_contents .= $new_text;
215 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
216 $main::lxdebug->leave_sub();
222 $main::lxdebug->leave_sub();
224 return $new_contents;
227 sub parse_first_line {
229 my $line = shift || "";
231 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
233 $self->{error} = "The tag start and end markers must not be equal.";
237 $self->set_tag_style($1, $2);
243 sub _parse_config_option {
250 my ($key, $value) = split m/\s*=\s*/, $line, 2;
252 if ($key eq 'tag-style') {
253 $self->set_tag_style(split(m/\s+/, $value, 2));
257 sub _parse_config_lines {
261 my ($comment_start, $comment_end) = ("", "");
263 if (ref $self eq 'SL::Template::LaTeX') {
264 $comment_start = '\s*%';
265 } elsif (ref $self eq 'SL::Template::HTML') {
266 $comment_start = '\s*<!--';
267 $comment_end = '>\s*';
269 $comment_start = '\s*\#';
272 my $num_lines = scalar @{ $lines };
275 while ($i < $num_lines) {
276 my $line = $lines->[$i];
278 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
283 $self->_parse_config_option($1);
284 splice @{ $lines }, $i, 1;
289 sub _force_mandatory_packages {
293 my (%used_packages, $document_start_line);
295 foreach my $i (0 .. scalar @{ $lines } - 1) {
296 if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
297 $used_packages{$1} = 1;
299 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
300 $document_start_line = $i;
306 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
308 if (!$used_packages{textcomp}) {
309 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
310 $document_start_line++;
317 my $form = $self->{"form"};
319 if (!open(IN, "$form->{templates}/$form->{IN}")) {
320 $self->{"error"} = "$!";
326 $self->_parse_config_lines(\@lines);
327 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'SL::Template::LaTeX');
329 my $contents = join("", @lines);
331 # detect pagebreak block and its parameters
332 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) {
333 $self->{"chars_per_line"} = $1;
334 $self->{"lines_on_first_page"} = $2;
335 $self->{"lines_on_second_page"} = $3;
336 $self->{"pagebreak_block"} = $4;
338 substr($contents, length($`), length($&)) = "";
341 $self->{"forced_pagebreaks"} = [];
343 my $new_contents = $self->parse_block($contents);
344 if (!defined($new_contents)) {
345 $main::lxdebug->leave_sub();
349 print(OUT $new_contents);
351 if ($form->{"format"} =~ /postscript/i) {
352 return $self->convert_to_postscript();
353 } elsif ($form->{"format"} =~ /pdf/i) {
354 return $self->convert_to_pdf();
360 sub convert_to_postscript {
362 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
364 # Convert the tex file to postscript
366 if (!chdir("$userspath")) {
367 $self->{"error"} = "chdir : $!";
372 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
374 my $latex = $self->_get_latex_path();
376 for (my $run = 1; $run <= 2; $run++) {
377 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
378 "> $form->{tmpfile}.err");
380 $self->{"error"} = $form->cleanup();
386 $form->{tmpfile} =~ s/tex$/dvi/;
388 system("dvips $form->{tmpfile} -o -q > /dev/null");
390 $self->{"error"} = "dvips : $!";
394 $form->{tmpfile} =~ s/dvi$/ps/;
403 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
405 # Convert the tex file to PDF
407 if (!chdir("$userspath")) {
408 $self->{"error"} = "chdir : $!";
413 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
415 my $latex = $self->_get_latex_path();
417 for (my $run = 1; $run <= 2; $run++) {
418 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
419 "> $form->{tmpfile}.err");
421 $self->{"error"} = $form->cleanup();
427 $form->{tmpfile} =~ s/tex$/pdf/;
432 sub _get_latex_path {
433 return $main::latex_bin || 'pdflatex';
436 sub get_mime_type() {
439 if ($self->{"form"}->{"format"} =~ /postscript/i) {
440 return "application/postscript";
442 return "application/pdf";