From: Moritz Bunkus Date: Thu, 16 Jan 2014 12:22:28 +0000 (+0100) Subject: SL::Template::LaTeX: Unterstützung für HTML-codierte Felder X-Git-Tag: release-3.2.0beta~467^2~39 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=b849f0d37e42677bb6eb850272ba2bb22c57363f;p=kivitendo-erp.git SL::Template::LaTeX: Unterstützung für HTML-codierte Felder --- diff --git a/SL/Template/LaTeX.pm b/SL/Template/LaTeX.pm index 2f99f7e18..4bb4ce63d 100644 --- a/SL/Template/LaTeX.pm +++ b/SL/Template/LaTeX.pm @@ -9,11 +9,88 @@ 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", + '' => "\\end{itemize} ", + '
    ' => "\\begin{enumerate} ", + '
' => "\\end{enumerate} ", + '
  • ' => "\\item ", + '
  • ' => " ", + '' => "\\textbf{", + '' => "}", + '' => "\\textbf{", + '' => "}", + '' => "\\textit{", + '' => "}", + '' => "\\textit{", + '' => "}", + '' => "\\underline{", + '' => "}", + '' => "\\sout{", + '' => "}", + '' => "\\textsubscript{", + '' => "}", + '' => "\\textsuperscript{", + '' => "}", + '
    ' => "\\newline ", + '
    ' => "\\newline ", +); + +sub _format_html { + my ($self, $content, %params) = @_; + + $content =~ s{ \r+ }{}gx; + $content =~ s{ \n+ }{ }gx; + $content =~ s{ \s+ }{ }gx; + + my @parts = 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); + + return join('', @parts); +} + +my %formatters = ( + html => \&_format_html, + text => \&_format_text, +); + sub new { my $type = shift; @@ -23,24 +100,14 @@ sub new { } sub format_string { - my ($self, $variable) = @_; - - $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable); - - # 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; - } + my ($self, $content, $variable) = @_; - $variable =~ s/[\x00-\x1f]//g; + my $formatter = + $formatters{ $self->{variable_content_types}->{$variable} } + // $formatters{ $self->{default_content_type} } + // $formatters{ text }; - return $variable; + return $formatter->($self, $content, variable => $variable); } sub parse_foreach { @@ -322,7 +389,7 @@ sub _force_mandatory_packages { : defined($last_usepackage_line) ? $last_usepackage_line : scalar @{ $lines } - 1; - foreach my $package (qw(textcomp)) { + foreach my $package (qw(textcomp ulem)) { next if $used_packages{$package}; splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n"; $insertion_point++;