1 package SL::Template::LaTeX;
3 use parent qw(SL::Template::Simple);
12 my $self = $type->SUPER::new(@_);
18 my ($self, $variable) = @_;
19 my $form = $self->{"form"};
21 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
23 # Allow some HTML markup to be converted into the output format's
24 # corresponding markup code, e.g. bold or italic.
25 my %markup_replace = ('b' => 'textbf',
29 foreach my $key (keys(%markup_replace)) {
30 my $new = $markup_replace{$key};
31 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
34 $variable =~ s/[\x00-\x1f]//g;
40 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
42 my ($form, $new_contents) = ($self->{"form"}, "");
44 my $ary = $self->_get_loop_variable($var, 1, @indices);
48 my ($current_line, $corrent_row) = (0, 1);
49 my $description_array = $self->_get_loop_variable("description", 1);
50 my $longdescription_array = $self->_get_loop_variable("longdescription", 1);
51 my $linetotal_array = $self->_get_loop_variable("linetotal", 1);
53 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
55 # forech block hasn't given us an array. ignore
56 return $new_contents unless ref $ary eq 'ARRAY';
58 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
60 $form->{"__first__"} = $i == 0;
61 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
62 $form->{"__odd__"} = (($i + 1) % 2) == 1;
63 $form->{"__counter__"} = $i + 1;
65 if ( ref $description_array eq 'ARRAY'
66 && scalar @{$description_array} == scalar @{$ary}
67 && $self->{"chars_per_line"} != 0)
69 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
72 $description_array->[$i] =~ s/(\\newline\s?)*$//;
73 $lines++ while ($description_array->[$i] =~ m/\\newline/g);
76 if ($current_page == 1) {
77 $lpp = $self->{"lines_on_first_page"};
79 $lpp = $self->{"lines_on_second_page"};
82 # Yes we need a manual page break -- or the user has forced one
83 if ( (($current_line + $lines) > $lpp)
84 || ($description_array->[$i] =~ /<pagebreak>/)
85 || ( ref $longdescription_array eq 'ARRAY'
86 && $longdescription_array->[$i] =~ /<pagebreak>/)) {
87 my $pb = $self->{"pagebreak_block"};
89 # replace the special variables <%sumcarriedforward%>
92 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
93 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
94 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
96 my $new_text = $self->parse_block($pb, (@indices, $i));
97 return undef unless (defined($new_text));
98 $new_contents .= $new_text;
103 $current_line += $lines;
106 if ( ref $linetotal_array eq 'ARRAY'
107 && $i < scalar(@{$linetotal_array})) {
108 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
111 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
113 my $new_text = $self->parse_block($text, (@indices, $i));
114 return undef unless (defined($new_text));
115 $new_contents .= $start_tag . $new_text . $end_tag;
117 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
119 return $new_contents;
123 my ($self, $text, $pos, $var, $not) = @_;
125 my $tag_start_len = length $self->{tag_start};
128 $pos = 0 unless ($pos);
130 while ($pos < length($text)) {
133 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
135 my $keyword_pos = $pos - 1 + $tag_start_len;
137 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'foreach')) {
140 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
143 "$self->{tag_start}else$self->{tag_end} outside of "
144 . "$self->{tag_start}if$self->{tag_end} / "
145 . "$self->{tag_start}ifnot$self->{tag_end}.";
149 my $block = substr($text, 0, $pos - 1);
150 substr($text, 0, $pos - 1) = "";
151 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
152 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
154 return ($block, $text);
156 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
159 my $block = substr($text, 0, $pos - 1);
160 substr($text, 0, $pos - 1) = "";
161 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
163 return ($block, $text);
172 $main::lxdebug->enter_sub();
174 my ($self, $contents, @indices) = @_;
176 my $new_contents = "";
178 while ($contents ne "") {
179 my $pos_if = index($contents, $self->{tag_start} . 'if');
180 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
182 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
183 $new_contents .= $self->substitute_vars($contents, @indices);
187 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
188 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
189 substr($contents, 0, $pos_foreach) = "";
191 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
192 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
193 $main::lxdebug->leave_sub();
199 substr($contents, 0, length($&)) = "";
202 ($block, $contents) = $self->find_end($contents);
204 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
205 $main::lxdebug->leave_sub();
209 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
210 if (!defined($new_text)) {
211 $main::lxdebug->leave_sub();
214 $new_contents .= $new_text;
217 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
218 $main::lxdebug->leave_sub();
224 $main::lxdebug->leave_sub();
226 return $new_contents;
229 sub parse_first_line {
231 my $line = shift || "";
233 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
235 $self->{error} = "The tag start and end markers must not be equal.";
239 $self->set_tag_style($1, $2);
245 sub _parse_config_option {
252 my ($key, $value) = split m/\s*=\s*/, $line, 2;
254 if ($key eq 'tag-style') {
255 $self->set_tag_style(split(m/\s+/, $value, 2));
259 sub _parse_config_lines {
263 my ($comment_start, $comment_end) = ("", "");
265 if (ref $self eq 'SL::Template::LaTeX') {
266 $comment_start = '\s*%';
267 } elsif (ref $self eq 'SL::Template::HTML') {
268 $comment_start = '\s*<!--';
269 $comment_end = '(?:--)?>\s*';
271 $comment_start = '\s*\#';
274 my $num_lines = scalar @{ $lines };
277 while ($i < $num_lines) {
278 my $line = $lines->[$i];
280 if ($line !~ m/^${comment_start}\s*config\s*:(.*?)${comment_end}$/i) {
285 $self->_parse_config_option($1);
286 splice @{ $lines }, $i, 1;
291 sub _force_mandatory_packages {
295 my (%used_packages, $document_start_line, $last_usepackage_line);
297 foreach my $i (0 .. scalar @{ $lines } - 1) {
298 if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
299 $used_packages{$1} = 1;
300 $last_usepackage_line = $i;
302 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
303 $document_start_line = $i;
309 my $insertion_point = defined($document_start_line) ? $document_start_line
310 : defined($last_usepackage_line) ? $last_usepackage_line
311 : scalar @{ $lines } - 1;
313 foreach my $package (qw(textcomp)) {
314 next if $used_packages{$package};
315 splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n";
323 my $form = $self->{"form"};
325 if (!open(IN, "$form->{templates}/$form->{IN}")) {
326 $self->{"error"} = "$!";
329 binmode IN, ":utf8" if $::locale->is_utf8;
333 $self->_parse_config_lines(\@lines);
334 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'SL::Template::LaTeX');
336 my $contents = join("", @lines);
338 # detect pagebreak block and its parameters
339 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) {
340 $self->{"chars_per_line"} = $1;
341 $self->{"lines_on_first_page"} = $2;
342 $self->{"lines_on_second_page"} = $3;
343 $self->{"pagebreak_block"} = $4;
345 substr($contents, length($`), length($&)) = "";
348 $self->{"forced_pagebreaks"} = [];
350 my $new_contents = $self->parse_block($contents);
351 if (!defined($new_contents)) {
352 $main::lxdebug->leave_sub();
356 binmode OUT, ":utf8" if $::locale->is_utf8;
357 print(OUT $new_contents);
359 if ($form->{"format"} =~ /postscript/i) {
360 return $self->convert_to_postscript();
361 } elsif ($form->{"format"} =~ /pdf/i) {
362 return $self->convert_to_pdf();
368 sub convert_to_postscript {
370 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
372 # Convert the tex file to postscript
374 if (!chdir("$userspath")) {
375 $self->{"error"} = "chdir : $!";
380 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
382 my $latex = $self->_get_latex_path();
383 my $old_home = $ENV{HOME};
384 $ENV{HOME} = $userspath =~ m|^/| ? $userspath : getcwd() . "/" . $userspath;
386 for (my $run = 1; $run <= 2; $run++) {
387 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
388 "> $form->{tmpfile}.err");
390 $ENV{HOME} = $old_home;
391 $self->{"error"} = $form->cleanup();
397 $form->{tmpfile} =~ s/tex$/dvi/;
399 system("dvips $form->{tmpfile} -o -q > /dev/null");
400 $ENV{HOME} = $old_home;
403 $self->{"error"} = "dvips : $!";
407 $form->{tmpfile} =~ s/dvi$/ps/;
416 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
418 # Convert the tex file to PDF
420 if (!chdir("$userspath")) {
421 $self->{"error"} = "chdir : $!";
426 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
428 my $latex = $self->_get_latex_path();
429 my $old_home = $ENV{HOME};
430 $ENV{HOME} = $userspath =~ m|^/| ? $userspath : getcwd() . "/" . $userspath;
432 for (my $run = 1; $run <= 2; $run++) {
433 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
434 "> $form->{tmpfile}.err");
436 $ENV{HOME} = $old_home;
437 $self->{"error"} = $form->cleanup();
443 $ENV{HOME} = $old_home;
444 $form->{tmpfile} =~ s/tex$/pdf/;
449 sub _get_latex_path {
450 return $main::latex_bin || 'pdflatex';
453 sub get_mime_type() {
456 if ($self->{"form"}->{"format"} =~ /postscript/i) {
457 return "application/postscript";
459 return "application/pdf";