X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FTemplate%2FLaTeX.pm;h=f5062ba838715e5ad3d15b1eb88740a08d13a738;hb=8d05386958d82c45b50062268bb549a08051486f;hp=889ed07130543b2351c0592c95fdd65b3da7bf99;hpb=0fba3edda47fca21bedb14eb88e0f5f8d983bb38;p=kivitendo-erp.git
diff --git a/SL/Template/LaTeX.pm b/SL/Template/LaTeX.pm
index 889ed0713..f5062ba83 100644
--- a/SL/Template/LaTeX.pm
+++ b/SL/Template/LaTeX.pm
@@ -1,12 +1,112 @@
package SL::Template::LaTeX;
-use vars qw(@ISA);
-
-use SL::Template;
-@ISA = qw(SL::Template::Simple);
+use parent qw(SL::Template::Simple);
use strict;
+use Carp;
+use Cwd;
+use English qw(-no_match_vars);
+use File::Basename;
+use File::Temp;
+use HTML::Entities ();
+use List::MoreUtils qw(any);
+use Unicode::Normalize qw();
+
+use SL::DB::Default;
+
+my %text_markup_replace = (
+ b => 'textbf',
+ i => 'textit',
+ u => 'underline',
+);
+
+sub _format_text {
+ my ($self, $content, %params) = @_;
+
+ $content = $::locale->quote_special_chars('Template/LaTeX', $content);
+
+ # Allow some HTML markup to be converted into the output format's
+ # corresponding markup code, e.g. bold or italic.
+ foreach my $key (keys(%text_markup_replace)) {
+ my $new = $text_markup_replace{$key};
+ $content =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
+ }
+
+ $content =~ s/[\x00-\x1f]//g;
+
+ return $content;
+}
+
+my %html_replace = (
+ '
' => "\n\n",
+ '' => "\\begin{itemize} ",
+ '
' => "\\end{itemize} ",
+ '' => "\\begin{enumerate} ",
+ '
' => "\\end{enumerate} ",
+ '' => "\\item ",
+ '' => " ",
+ '' => "\\textbf{",
+ '' => "}",
+ '' => "\\textbf{",
+ '' => "}",
+ '' => "\\textit{",
+ '' => "}",
+ '' => "\\textit{",
+ '' => "}",
+ '' => "\\underline{",
+ '' => "}",
+ '' => "\\sout{",
+ '' => "}",
+ '' => "\\textsubscript{",
+ '' => "}",
+ '' => "\\textsuperscript{",
+ '' => "}",
+ '
' => "\\newline ",
+ '
' => "\\newline ",
+);
+
+sub _lb_to_space {
+ my ($to_replace) = @_;
+
+ my $vspace = '\vspace*{0.5cm}';
+ return $vspace x (length($to_replace) / length($html_replace{'
'}));
+}
+
+sub _format_html {
+ my ($self, $content, %params) = @_;
+
+ $content =~ s{ \r+ }{}gx;
+ $content =~ s{ \n+ }{ }gx;
+ $content =~ s{ (?:\ |\s)+ }{ }gx;
+ $content =~ s{ (?:\ |\s)+$ }{}gx;
+ $content =~ s{ (?:
)+$ }{}gx;
+
+ my @parts = grep { $_ } map {
+ if (substr($_, 0, 1) eq '<') {
+ s{ +}{}g;
+ $html_replace{$_} || '';
+
+ } else {
+ $::locale->quote_special_chars('Template/LaTeX', HTML::Entities::decode_entities($_));
+ }
+ } split(m{(<.*?>)}x, $content);
+
+ $content = join '', @parts;
+ $content =~ s{ (?: [\n\s] | \\newline )+ $ }{}gx; # remove line breaks at the end of the text
+ $content =~ s{ ^ \s+ }{}gx; # remove white space at the start of the text
+ $content =~ s{ ^ ( \\newline \ )+ }{ _lb_to_space($1) }gxe; # convert line breaks at the start of the text to vertical space
+ $content =~ s{ ( \n\n+ ) ( \\newline \ )+ }{ $1 . _lb_to_space($2) }gxe; # convert line breaks at the start of a paragraph to vertical space
+ $content =~ s{ ( \\end\{ [^\}]+ \} \h* ) ( \\newline \ )+ }{ $1 . _lb_to_space($2) }gxe; # convert line breaks after LaTeX environments like lists to vertical space
+
+ return $content;
+}
+
+my %formatters = (
+ html => \&_format_html,
+ text => \&_format_text,
+);
+
sub new {
my $type = shift;
@@ -16,25 +116,14 @@ sub new {
}
sub format_string {
- my ($self, $variable) = @_;
- my $form = $self->{"form"};
+ my ($self, $content, $variable) = @_;
- $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
+ my $formatter =
+ $formatters{ $self->{variable_content_types}->{$variable} }
+ // $formatters{ $self->{default_content_type} }
+ // $formatters{ text };
- # Allow some HTML markup to be converted into the output format's
- # corresponding markup code, e.g. bold or italic.
- my %markup_replace = ('b' => 'textbf',
- 'i' => 'textit',
- 'u' => 'underline');
-
- foreach my $key (keys(%markup_replace)) {
- my $new = $markup_replace{$key};
- $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
- }
-
- $variable =~ s/[\x00-\x1f]//g;
-
- return $variable;
+ return $formatter->($self, $content, variable => $variable);
}
sub parse_foreach {
@@ -58,11 +147,12 @@ sub parse_foreach {
for (my $i = 0; $i < scalar(@{$ary}); $i++) {
# do magic markers
- $form->{"__first__"} = $i == 1;
+ $form->{"__first__"} = $i == 0;
$form->{"__last__"} = ($i + 1) == scalar(@{$ary});
$form->{"__odd__"} = (($i + 1) % 2) == 1;
$form->{"__counter__"} = $i + 1;
+ #everything from here to the next marker should be removed after the release of 2.7.0
if ( ref $description_array eq 'ARRAY'
&& scalar @{$description_array} == scalar @{$ary}
&& $self->{"chars_per_line"} != 0)
@@ -103,6 +193,7 @@ sub parse_foreach {
}
$current_line += $lines;
}
+ #stop removing code here.
if ( ref $linetotal_array eq 'ARRAY'
&& $i < scalar(@{$linetotal_array})) {
@@ -135,7 +226,7 @@ sub find_end {
my $keyword_pos = $pos - 1 + $tag_start_len;
- if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
+ if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 7) eq 'foreach')) {
$depth++;
} elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
@@ -189,15 +280,15 @@ sub parse_block {
$new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
substr($contents, 0, $pos_foreach) = "";
- if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
+ if ($contents !~ m|^($self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm})|) {
$self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
$main::lxdebug->leave_sub();
return undef;
}
- my $var = $1;
+ my $var = $2;
- substr($contents, 0, length($&)) = "";
+ substr($contents, 0, length($1)) = "";
my $block;
($block, $contents) = $self->find_end($contents);
@@ -255,6 +346,9 @@ sub _parse_config_option {
if ($key eq 'tag-style') {
$self->set_tag_style(split(m/\s+/, $value, 2));
}
+ if ($key eq 'use-template-toolkit') {
+ $self->set_use_template_toolkit($value);
+ }
}
sub _parse_config_lines {
@@ -267,7 +361,7 @@ sub _parse_config_lines {
$comment_start = '\s*%';
} elsif (ref $self eq 'SL::Template::HTML') {
$comment_start = '\s*