- # Do we have to run LaTeX two times? This is needed if
- # the template contains page references.
- $two_passes = 0;
-
- # first we generate a tmpfile
- # read file and replace <%variable%>
- while ($_ = shift) {
-
- $par = "";
- $var = $_;
-
- # Switch <%analyse%> for template checking
- # If <%analyse%> is set in the template, you'll find the
- # parsed output in the user Directory for analysing
- # Latex errors
- # <%analyse%> is a switch (allways off, on if set), not a Variable
- # Set $form->{analysing}="" for system state: never analyse.
- # Set $form->{analysing}="1" for system state: ever analyse.
- $self->{analysing} = "1" if (/<%analyse%>/ && !defined $self->{analysing});
-
- $two_passes = 1 if (/\\pageref/);
-
- # { Moritz Bunkus
- # detect pagebreak block and its parameters
- if (/\s*<%pagebreak ([0-9]+) ([0-9]+) ([0-9]+)%>/) {
- $chars_per_line = $1;
- $lines_on_first_page = $2;
- $lines_on_second_page = $3;
-
- while ($_ = shift) {
- last if (/\s*<%end pagebreak%>/);
- $pagebreak .= $_;
- }
- }
-
- # } Moritz Bunkus
-
- if (/\s*<%foreach /) {
-
- # this one we need for the count
- chomp $var;
- $var =~ s/\s*<%foreach (.+?)%>/$1/;
- while ($_ = shift) {
- last if (/\s*<%end /);
-
- # store line in $par
- $par .= $_;
- }
-
- # display contents of $self->{number}[] array
- for $i (0 .. $#{ $self->{$var} }) {
-
- # { Moritz Bunkus
- # Try to detect whether a manual page break is necessary
- # but only if there was a <%pagebreak ...%> block before
-
- if ($chars_per_line) {
- my $lines =
- int(length($self->{"description"}[$i]) / $chars_per_line + 0.95);
- my $lpp;
-
- my $_description = $self->{"description"}[$i];
- while ($_description =~ /\\newline/) {
- $lines++;
- $_description =~ s/\\newline//;
- }
- $self->{"description"}[$i] =~ s/(\\newline\s?)*$//;
-
- if ($current_page == 1) {
- $lpp = $lines_on_first_page;
- } else {
- $lpp = $lines_on_second_page;
- }
-
- # Yes we need a manual page break -- or the user has forced one
- if (
- (($current_line + $lines) > $lpp)
- || ($self->{"_forced_pagebreaks"}
- && grep(/^${current_row}$/, @{ $self->{"_forced_pagebreaks"} }))
- ) {
- my $pb = $pagebreak;
-
- # replace the special variables <%sumcarriedforward%>
- # and <%lastpage%>
-
- my $psum = $self->format_amount($myconfig, $sum, 2);
- $pb =~ s/<%sumcarriedforward%>/$psum/g;
- $pb =~ s/<%lastpage%>/$current_page/g;
-
- # only "normal" variables are supported here
- # (no <%if, no <%foreach, no <%include)
-
- $pb =~ s/<%(.+?)%>/$self->{$1}/g;
-
- # page break block is ready to rock
- print(OUT $pb);
- $current_page++;
- $current_line = 1;
- }
- $current_line += $lines;
- $current_row++;
- }
- $sum += $self->parse_amount($myconfig, $self->{"linetotal"}[$i]);
-
- # } Moritz Bunkus
-
- # don't parse par, we need it for each line
- $_ = $par;
- s/<%(.+?)%>/$self->{$1}[$i]/mg;
- print OUT;
- }
- next;
- }
-
- # if not comes before if!
- if (/\s*<%if not /) {
-
- # check if it is not set and display
- chop;
- s/\s*<%if not (.+?)%>/$1/;
-
- unless ($self->{$_}) {
- while ($_ = shift) {
- last if (/\s*<%end /);
-
- # store line in $par
- $par .= $_;
- }
-
- $_ = $par;
-
- } else {
- while ($_ = shift) {
- last if (/\s*<%end /);
- }
- next;
- }
- }
-
- if (/\s*<%if /) {
-
- # check if it is set and display
- chop;
- s/\s*<%if (.+?)%>/$1/;
-
- if ($self->{$_}) {
- while ($_ = shift) {
- last if (/\s*<%end /);
-
- # store line in $par
- $par .= $_;
- }
-
- $_ = $par;
-
- } else {
- while ($_ = shift) {
- last if (/\s*<%end /);
- }
- next;
- }
- }
-
- # check for <%include filename%>
- if (/\s*<%include /) {
-
- # get the directory/filename
- chomp $var;
- $var =~ s/\s*<%include (.+?)%>/$1/;
-
- # mangle filename on basedir
- $var =~ s/^(\/|\.\.)//g;
-
- # prevent the infinite loop!
- next if ($self->{"$var"});
-
- open(INC, "$self->{templates}/$var")
- or $self->error($self->cleanup . "$self->{templates}/$var : $!");
- unshift(@_, <INC>);
- close(INC);
-
- $self->{"$var"} = 1;
-
- next;
- }
-
- s/<%(.+?)%>/$self->{$1}/g;
- s/<nobr><\/nobr>/ /g;
- print OUT;