Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b849f0d3

Von Moritz Bunkus vor mehr als 10 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
9 9
use English qw(-no_match_vars);
10 10
use File::Basename;
11 11
use File::Temp;
12
use HTML::Entities ();
12 13
use List::MoreUtils qw(any);
13 14
use Unicode::Normalize qw();
14 15

  
15 16
use SL::DB::Default;
16 17

  
18
my %text_markup_replace = (
19
  b => 'textbf',
20
  i => 'textit',
21
  u => 'underline',
22
);
23

  
24
sub _format_text {
25
  my ($self, $content, %params) = @_;
26

  
27
  $content = $::locale->quote_special_chars('Template/LaTeX', $content);
28

  
29
  # Allow some HTML markup to be converted into the output format's
30
  # corresponding markup code, e.g. bold or italic.
31
  foreach my $key (keys(%text_markup_replace)) {
32
    my $new   =  $text_markup_replace{$key};
33
    $content =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
34
  }
35

  
36
  $content =~ s/[\x00-\x1f]//g;
37

  
38
  return $content;
39
}
40

  
41
my %html_replace = (
42
  '</p>'      => "\n\n",
43
  '<ul>'      => "\\begin{itemize} ",
44
  '</ul>'     => "\\end{itemize} ",
45
  '<ol>'      => "\\begin{enumerate} ",
46
  '</ol>'     => "\\end{enumerate} ",
47
  '<li>'      => "\\item ",
48
  '</li>'     => " ",
49
  '<b>'       => "\\textbf{",
50
  '</b>'      => "}",
51
  '<strong>'  => "\\textbf{",
52
  '</strong>' => "}",
53
  '<i>'       => "\\textit{",
54
  '</i>'      => "}",
55
  '<em>'      => "\\textit{",
56
  '</em>'     => "}",
57
  '<u>'       => "\\underline{",
58
  '</u>'      => "}",
59
  '<s>'       => "\\sout{",
60
  '</s>'      => "}",
61
  '<sub>'     => "\\textsubscript{",
62
  '</sub>'    => "}",
63
  '<sup>'     => "\\textsuperscript{",
64
  '</sup>'    => "}",
65
  '<br/>'     => "\\newline ",
66
  '<br>'      => "\\newline ",
67
);
68

  
69
sub _format_html {
70
  my ($self, $content, %params) = @_;
71

  
72
  $content =~ s{ \r+ }{}gx;
73
  $content =~ s{ \n+ }{ }gx;
74
  $content =~ s{ \s+ }{ }gx;
75

  
76
  my @parts = map {
77
    if (substr($_, 0, 1) eq '<') {
78
      s{ +}{}g;
79
      $html_replace{$_} || '';
80

  
81
    } else {
82
      $::locale->quote_special_chars('Template/LaTeX', HTML::Entities::decode_entities($_));
83
    }
84
  } split(m{(<.*?>)}x, $content);
85

  
86
  return join('', @parts);
87
}
88

  
89
my %formatters = (
90
  html => \&_format_html,
91
  text => \&_format_text,
92
);
93

  
17 94
sub new {
18 95
  my $type = shift;
19 96

  
......
23 100
}
24 101

  
25 102
sub format_string {
26
  my ($self, $variable) = @_;
27

  
28
  $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
29

  
30
  # Allow some HTML markup to be converted into the output format's
31
  # corresponding markup code, e.g. bold or italic.
32
  my %markup_replace = ('b' => 'textbf',
33
                        'i' => 'textit',
34
                        'u' => 'underline');
35

  
36
  foreach my $key (keys(%markup_replace)) {
37
    my $new = $markup_replace{$key};
38
    $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
39
  }
103
  my ($self, $content, $variable) = @_;
40 104

  
41
  $variable =~ s/[\x00-\x1f]//g;
105
  my $formatter =
106
       $formatters{ $self->{variable_content_types}->{$variable} }
107
    // $formatters{ $self->{default_content_type} }
108
    // $formatters{ text };
42 109

  
43
  return $variable;
110
  return $formatter->($self, $content, variable => $variable);
44 111
}
45 112

  
46 113
sub parse_foreach {
......
322 389
                      : defined($last_usepackage_line) ? $last_usepackage_line
323 390
                      :                                  scalar @{ $lines } - 1;
324 391

  
325
  foreach my $package (qw(textcomp)) {
392
  foreach my $package (qw(textcomp ulem)) {
326 393
    next if $used_packages{$package};
327 394
    splice @{ $lines }, $insertion_point, 0, "\\usepackage{${package}}\n";
328 395
    $insertion_point++;

Auch abrufbar als: Unified diff