Plugin LxLatex in KiviLatex umbenannt
[kivitendo-erp.git] / SL / Template / Plugin / KiviLatex.pm
1 package SL::Template::Plugin::KiviLatex;
2
3 use strict;
4 use parent qw( Template::Plugin::Filter );
5
6 my $cached_instance;
7
8 sub new {
9   my $class = shift;
10
11   return $cached_instance ||= $class->SUPER::new(@_);
12 }
13
14 sub init {
15   my $self = shift;
16
17   $self->install_filter($self->{ _ARGS }->[0] || 'KiviLatex');
18
19   return $self;
20 }
21
22 sub filter {
23   my ($self, $text, $args) = @_;
24   return $::locale->quote_special_chars('Template/LaTeX', $text);
25 }
26
27 my %html_replace = (
28   '</p>'      => "\n\n",
29   '<ul>'      => "\\begin{itemize} ",
30   '</ul>'     => "\\end{itemize} ",
31   '<ol>'      => "\\begin{enumerate} ",
32   '</ol>'     => "\\end{enumerate} ",
33   '<li>'      => "\\item ",
34   '</li>'     => " ",
35   '<b>'       => "\\textbf{",
36   '</b>'      => "}",
37   '<strong>'  => "\\textbf{",
38   '</strong>' => "}",
39   '<i>'       => "\\textit{",
40   '</i>'      => "}",
41   '<em>'      => "\\textit{",
42   '</em>'     => "}",
43   '<u>'       => "\\underline{",
44   '</u>'      => "}",
45   '<s>'       => "\\sout{",
46   '</s>'      => "}",
47   '<sub>'     => "\\textsubscript{",
48   '</sub>'    => "}",
49   '<sup>'     => "\\textsuperscript{",
50   '</sup>'    => "}",
51   '<br/>'     => "\\newline ",
52   '<br>'      => "\\newline ",
53 );
54
55 sub filter_html {
56   my ($self, $text, $args) = @_;
57
58   $text =~ s{ \r+ }{}gx;
59   $text =~ s{ \n+ }{ }gx;
60   $text =~ s{ (?:\&nbsp;|\s)+ }{ }gx;
61
62   my @parts = map {
63     if (substr($_, 0, 1) eq '<') {
64       s{ +}{}g;
65       $html_replace{$_} || '';
66
67     } else {
68       $::locale->quote_special_chars('Template/LaTeX', HTML::Entities::decode_entities($_));
69     }
70   } split(m{(<.*?>)}x, $text);
71
72   return join('', @parts);
73 }
74
75 return 'SL::Template::Plugin::KiviLatex';