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) = @_;
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 #everything from here to the next marker should be removed after the release of 2.7.0
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;
106 #stop removing code here.
108 if ( ref $linetotal_array eq 'ARRAY'
109 && $i < scalar(@{$linetotal_array})) {
110 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
113 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
115 my $new_text = $self->parse_block($text, (@indices, $i));
116 return undef unless (defined($new_text));
117 $new_contents .= $start_tag . $new_text . $end_tag;
119 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
121 return $new_contents;
125 my ($self, $text, $pos, $var, $not) = @_;
127 my $tag_start_len = length $self->{tag_start};
130 $pos = 0 unless ($pos);
132 while ($pos < length($text)) {
135 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
137 my $keyword_pos = $pos - 1 + $tag_start_len;
139 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 7) eq 'foreach')) {
142 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
145 "$self->{tag_start}else$self->{tag_end} outside of "
146 . "$self->{tag_start}if$self->{tag_end} / "
147 . "$self->{tag_start}ifnot$self->{tag_end}.";
151 my $block = substr($text, 0, $pos - 1);
152 substr($text, 0, $pos - 1) = "";
153 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
154 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
156 return ($block, $text);
158 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
161 my $block = substr($text, 0, $pos - 1);
162 substr($text, 0, $pos - 1) = "";
163 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
165 return ($block, $text);
174 $main::lxdebug->enter_sub();
176 my ($self, $contents, @indices) = @_;
178 my $new_contents = "";
180 while ($contents ne "") {
181 my $pos_if = index($contents, $self->{tag_start} . 'if');
182 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
184 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
185 $new_contents .= $self->substitute_vars($contents, @indices);
189 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
190 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
191 substr($contents, 0, $pos_foreach) = "";
193 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
194 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
195 $main::lxdebug->leave_sub();
201 substr($contents, 0, length($&)) = "";
204 ($block, $contents) = $self->find_end($contents);
206 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
207 $main::lxdebug->leave_sub();
211 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
212 if (!defined($new_text)) {
213 $main::lxdebug->leave_sub();
216 $new_contents .= $new_text;
219 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
220 $main::lxdebug->leave_sub();
226 $main::lxdebug->leave_sub();
228 return $new_contents;
231 sub parse_first_line {
233 my $line = shift || "";
235 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
237 $self->{error} = "The tag start and end markers must not be equal.";
241 $self->set_tag_style($1, $2);
247 sub _parse_config_option {
254 my ($key, $value) = split m/\s*=\s*/, $line, 2;
256 if ($key eq 'tag-style') {
257 $self->set_tag_style(split(m/\s+/, $value, 2));
259 if ($key eq 'use-template-toolkit') {
260 $self->set_use_template_toolkit($value);
264 sub _parse_config_lines {
268 my ($comment_start, $comment_end) = ("", "");
270 if (ref $self eq 'SL::Template::LaTeX') {
271 $comment_start = '\s*%';
272 } elsif (ref $self eq 'SL::Template::HTML') {
273 $comment_start = '\s*<!--';
274 $comment_end = '(?:--)?>\s*';
276 $comment_start = '\s*\#';
279 my $num_lines = scalar @{ $lines };
282 while ($i < $num_lines) {
283 my $line = $lines->[$i];
285 if ($line !~ m/^${comment_start}\s*config\s*:(.*?)${comment_end}$/i) {
290 $self->_parse_config_option($1);
291 splice @{ $lines }, $i, 1;
296 sub _force_mandatory_packages {
300 my (%used_packages, $document_start_line, $last_usepackage_line);
302 foreach my $i (0 .. scalar @{ $lines } - 1) {
303 if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
304 $used_packages{$1} = 1;
305 $last_usepackage_line = $i;
307 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
308 $document_start_line = $i;
314 my $insertion_point = defined($document_start_line) ? $document_start_line
315 : defined($last_usepackage_line) ? $last_usepackage_line
316 : scalar @{ $lines } - 1;
318 foreach my $package (qw(textcomp)) {
319 next if $used_packages{$package};
320 splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n";
328 my $form = $self->{"form"};
330 if (!open(IN, "$form->{templates}/$form->{IN}")) {
331 $self->{"error"} = "$!";
334 binmode IN, ":utf8" if $::locale->is_utf8;
338 $self->_parse_config_lines(\@lines);
339 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'SL::Template::LaTeX');
341 my $contents = join("", @lines);
343 # detect pagebreak block and its parameters
344 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) {
345 $self->{"chars_per_line"} = $1;
346 $self->{"lines_on_first_page"} = $2;
347 $self->{"lines_on_second_page"} = $3;
348 $self->{"pagebreak_block"} = $4;
350 substr($contents, length($`), length($&)) = "";
353 $self->{"forced_pagebreaks"} = [];
356 if ($self->{use_template_toolkit}) {
357 my $additional_params = $::form;
359 $::form->init_template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
361 $new_contents = $self->parse_block($contents);
363 if (!defined($new_contents)) {
364 $main::lxdebug->leave_sub();
368 if ($::locale->is_utf8) {
369 binmode OUT, ":utf8";
370 print OUT Unicode::Normalize::normalize('C', $new_contents);
373 print OUT $new_contents;
376 if ($form->{"format"} =~ /postscript/i) {
377 return $self->convert_to_postscript();
378 } elsif ($form->{"format"} =~ /pdf/i) {
379 return $self->convert_to_pdf();
385 sub convert_to_postscript {
387 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
389 # Convert the tex file to postscript
391 if (!chdir("$userspath")) {
392 $self->{"error"} = "chdir : $!";
397 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
399 my $latex = $self->_get_latex_path();
400 my $old_home = $ENV{HOME};
401 my $old_openin_any = $ENV{openin_any};
402 $ENV{HOME} = $userspath =~ m|^/| ? $userspath : getcwd();
403 $ENV{openin_any} = "p";
405 for (my $run = 1; $run <= 2; $run++) {
406 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
407 "> $form->{tmpfile}.err");
409 $ENV{HOME} = $old_home;
410 $ENV{openin_any} = $old_openin_any;
411 $self->{"error"} = $form->cleanup($latex);
416 $form->{tmpfile} =~ s/tex$/dvi/;
418 system("dvips $form->{tmpfile} -o -q > /dev/null");
419 $ENV{HOME} = $old_home;
420 $ENV{openin_any} = $old_openin_any;
423 $self->{"error"} = "dvips : $!";
424 $self->cleanup('dvips');
427 $form->{tmpfile} =~ s/dvi$/ps/;
436 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
438 # Convert the tex file to PDF
440 if (!chdir("$userspath")) {
441 $self->{"error"} = "chdir : $!";
446 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
448 my $latex = $self->_get_latex_path();
449 my $old_home = $ENV{HOME};
450 my $old_openin_any = $ENV{openin_any};
451 $ENV{HOME} = $userspath =~ m|^/| ? $userspath : getcwd();
452 $ENV{openin_any} = "p";
454 for (my $run = 1; $run <= 2; $run++) {
455 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
456 "> $form->{tmpfile}.err");
458 $ENV{HOME} = $old_home;
459 $ENV{openin_any} = $old_openin_any;
460 $self->{error} = $form->cleanup($latex);
465 $ENV{HOME} = $old_home;
466 $ENV{openin_any} = $old_openin_any;
467 $form->{tmpfile} =~ s/tex$/pdf/;
472 sub _get_latex_path {
473 return $::lx_office_conf{applications}->{latex} || 'pdflatex';
476 sub get_mime_type() {
479 if ($self->{"form"}->{"format"} =~ /postscript/i) {
480 return "application/postscript";
482 return "application/pdf";