From b849f0d37e42677bb6eb850272ba2bb22c57363f Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 16 Jan 2014 13:22:28 +0100 Subject: [PATCH] =?utf8?q?SL::Template::LaTeX:=20Unterst=C3=BCtzung=20f?= =?utf8?q?=C3=BCr=20HTML-codierte=20Felder?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Template/LaTeX.pm | 101 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 17 deletions(-) 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++; -- 2.20.1