KiviLatex-Plugin: Dokumentation ergänzt
[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>'       => "\\uline{",
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 sub required_packages_for_html {
76   my ($self) = @_;
77
78   return <<EOLATEX;
79 \\usepackage{ulem}
80 EOLATEX
81 }
82
83 return 'SL::Template::Plugin::KiviLatex';
84 __END__
85
86 =pod
87
88 =encoding utf8
89
90 =head1 NAME
91
92 SL::Template::Plugin::KiviLatex - Template::Toolkit plugin for
93 escaping text for use in LaTeX templates
94
95 =head1 SYNOPSIS
96
97 From within a LaTeX template. Activate both Template::Toolkit in
98 general and this plugin in particular; must be located before
99 C<\begin{document}>:
100
101   % config: use-template-toolkit=1
102   % config: tag-style=$( )$
103   $( USE KiviLatex )$
104
105 Later escape some text:
106
107   $( KiviLatex.format(longdescription) )$
108
109 =head1 FUNCTIONS
110
111 =over 4
112
113 =item C<filter $text>
114
115 Escapes characters in C<$text> with the appropriate LaTeX
116 constructs. Expects normal text without any markup (no HTML, no XML
117 etc). Returns the whole escaped text.
118
119 =item C<filter_html $html>
120
121 Converts HTML markup in C<$html> to the appropriate LaTeX
122 constructs. Only the following HTML elements are supported:
123
124 =over 2
125
126 =item * C<b>, C<strong> – bold text
127
128 =item * C<it>, C<em> – italic text
129
130 =item * C<ul> – underlined text
131
132 =item * C<s> – striked out text
133
134 =item * C<sub>, C<sup> – subscripted and superscripted text
135
136 =item * C<ul>, C<ol>, C<li> – unordered lists (converted to an itemized
137 list), ordered lists (converted to enumerated lists) and their list
138 items
139
140 =item * C<p>, C<br> – Paragraph markers and line breaks
141
142 =back
143
144 This function is tailored for working on the input of CKEditor, not on
145 arbitrary HTML content. It works nicely in tandem with the
146 Rose::DB::Object helper functions C<…_as_restricted_html> (see
147 L<SL::DB::Helper::AttrHTML/attr_html>).
148
149 Attributes are silently removed and ignored. All other markup and the
150 normal text are escaped the same as in L</filter>.
151
152 =item C<init>
153
154 =item C<new>
155
156 Initializes the plugin. Automatically called by Template::Toolkit when
157 the plugin is loaded.
158
159 =item C<required_packages_for_html>
160
161 Returns LaTeX code loading packages that are required for the
162 formatting done with L</filter_html>. This function must be called and
163 its output inserted before the C<\begin{document}> line if that
164 function is used within the document.
165
166 It is not required for normal text escaping with L</filter>.
167
168 =back
169
170 =head1 BUGS
171
172 Nothing here yet.
173
174 =head1 AUTHOR
175
176 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
177
178 =cut