1 package SL::Template::Plugin::KiviLatex;
4 use parent qw( Template::Plugin::Filter );
11 return $cached_instance ||= $class->SUPER::new(@_);
17 $self->install_filter($self->{ _ARGS }->[0] || 'KiviLatex');
23 my ($self, $text, $args) = @_;
24 return $::locale->quote_special_chars('Template/LaTeX', $text);
29 '<ul>' => "\\begin{itemize} ",
30 '</ul>' => "\\end{itemize} ",
31 '<ol>' => "\\begin{enumerate} ",
32 '</ol>' => "\\end{enumerate} ",
37 '<strong>' => "\\textbf{",
41 '<em>' => "\\textit{",
47 '<sub>' => "\\textsubscript{",
49 '<sup>' => "\\textsuperscript{",
51 '<br/>' => "\\newline ",
52 '<br>' => "\\newline ",
56 my ($self, $text, $args) = @_;
58 $text =~ s{ \r+ }{}gx;
59 $text =~ s{ \n+ }{ }gx;
60 $text =~ s{ (?:\ |\s)+ }{ }gx;
61 $text =~ s{ <ul>\s*</ul> | <ol>\s*</ol> }{}gx; # Remove lists without items. Can happen with copy & paste from e.g. LibreOffice.
64 if (substr($_, 0, 1) eq '<') {
66 $html_replace{$_} || '';
69 $::locale->quote_special_chars('Template/LaTeX', HTML::Entities::decode_entities($_));
71 } split(m{(<.*?>)}x, $text);
73 return join('', @parts);
76 sub required_packages_for_html {
84 return 'SL::Template::Plugin::KiviLatex';
93 SL::Template::Plugin::KiviLatex - Template::Toolkit plugin for
94 escaping text for use in LaTeX templates
98 From within a LaTeX template. Activate both Template::Toolkit in
99 general and this plugin in particular; must be located before
102 % config: use-template-toolkit=1
103 % config: tag-style=$( )$
106 Later escape some text:
108 $( KiviLatex.format(longdescription) )$
114 =item C<filter $text>
116 Escapes characters in C<$text> with the appropriate LaTeX
117 constructs. Expects normal text without any markup (no HTML, no XML
118 etc). Returns the whole escaped text.
120 =item C<filter_html $html>
122 Converts HTML markup in C<$html> to the appropriate LaTeX
123 constructs. Only the following HTML elements are supported:
127 =item * C<b>, C<strong> – bold text
129 =item * C<it>, C<em> – italic text
131 =item * C<ul> – underlined text
133 =item * C<s> – striked out text
135 =item * C<sub>, C<sup> – subscripted and superscripted text
137 =item * C<ul>, C<ol>, C<li> – unordered lists (converted to an itemized
138 list), ordered lists (converted to enumerated lists) and their list
141 =item * C<p>, C<br> – Paragraph markers and line breaks
145 This function is tailored for working on the input of CKEditor, not on
146 arbitrary HTML content. It works nicely in tandem with the
147 Rose::DB::Object helper functions C<…_as_restricted_html> (see
148 L<SL::DB::Helper::AttrHTML/attr_html>).
150 Attributes are silently removed and ignored. All other markup and the
151 normal text are escaped the same as in L</filter>.
157 Initializes the plugin. Automatically called by Template::Toolkit when
158 the plugin is loaded.
160 =item C<required_packages_for_html>
162 Returns LaTeX code loading packages that are required for the
163 formatting done with L</filter_html>. This function must be called and
164 its output inserted before the C<\begin{document}> line if that
165 function is used within the document.
167 It is not required for normal text escaping with L</filter>.
177 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>