]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Template/Plugin/KiviLatex.pm
Plugin LxLatex in KiviLatex umbenannt
[mfinanz.git] / SL / Template / Plugin / KiviLatex.pm
diff --git a/SL/Template/Plugin/KiviLatex.pm b/SL/Template/Plugin/KiviLatex.pm
new file mode 100644 (file)
index 0000000..21b0779
--- /dev/null
@@ -0,0 +1,75 @@
+package SL::Template::Plugin::KiviLatex;
+
+use strict;
+use parent qw( Template::Plugin::Filter );
+
+my $cached_instance;
+
+sub new {
+  my $class = shift;
+
+  return $cached_instance ||= $class->SUPER::new(@_);
+}
+
+sub init {
+  my $self = shift;
+
+  $self->install_filter($self->{ _ARGS }->[0] || 'KiviLatex');
+
+  return $self;
+}
+
+sub filter {
+  my ($self, $text, $args) = @_;
+  return $::locale->quote_special_chars('Template/LaTeX', $text);
+}
+
+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 filter_html {
+  my ($self, $text, $args) = @_;
+
+  $text =~ s{ \r+ }{}gx;
+  $text =~ s{ \n+ }{ }gx;
+  $text =~ s{ (?:\&nbsp;|\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, $text);
+
+  return join('', @parts);
+}
+
+return 'SL::Template::Plugin::KiviLatex';