Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b849f0d3

Von Moritz Bunkus vor etwa 11 Jahren hinzugefügt

  • ID b849f0d37e42677bb6eb850272ba2bb22c57363f
  • Vorgänger 3e466ad1
  • Nachfolger 3b32e1dd

SL::Template::LaTeX: Unterstützung für HTML-codierte Felder

Unterschiede anzeigen:

SL/Template/LaTeX.pm
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 = (
'</p>' => "\n\n",
'<ul>' => "\\begin{itemize} ",
'</ul>' => "\\end{itemize} ",
'<ol>' => "\\begin{enumerate} ",
'</ol>' => "\\end{enumerate} ",
'<li>' => "\\item ",
'</li>' => " ",
'<b>' => "\\textbf{",
'</b>' => "}",
'<strong>' => "\\textbf{",
'</strong>' => "}",
'<i>' => "\\textit{",
'</i>' => "}",
'<em>' => "\\textit{",
'</em>' => "}",
'<u>' => "\\underline{",
'</u>' => "}",
'<s>' => "\\sout{",
'</s>' => "}",
'<sub>' => "\\textsubscript{",
'</sub>' => "}",
'<sup>' => "\\textsuperscript{",
'</sup>' => "}",
'<br/>' => "\\newline ",
'<br>' => "\\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;
......
}
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 {
......
: 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++;

Auch abrufbar als: Unified diff