X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=inline;f=SL%2FTemplate%2FOpenDocument.pm;h=85184c01986ff6b0f9971258967af27830081f1b;hb=c4ce237aaab9c25c41334c30852d2ea640730efe;hp=b4ea1f2a5d24d2f4a64da6c224766354a8c9f562;hpb=5d376151ff31e1d55fd985c64db97d609daf273b;p=kivitendo-erp.git
diff --git a/SL/Template/OpenDocument.pm b/SL/Template/OpenDocument.pm
index b4ea1f2a5..85184c019 100644
--- a/SL/Template/OpenDocument.pm
+++ b/SL/Template/OpenDocument.pm
@@ -4,9 +4,11 @@ use parent qw(SL::Template::Simple);
use Archive::Zip;
use Encode;
+use HTML::Entities;
use POSIX 'setsid';
use SL::Iconv;
+use SL::Template::OpenDocument::Styles;
use Cwd;
# use File::Copy;
@@ -16,14 +18,120 @@ use IO::File;
use strict;
+my %text_markup_replace = (
+ b => "BOLD",
+ i => "ITALIC",
+ s => "STRIKETHROUGH",
+ u => "UNDERLINE",
+ sup => "SUPER",
+ sub => "SUB",
+);
+
+sub _format_text {
+ my ($self, $content, %params) = @_;
+
+ $content = $::locale->quote_special_chars('Template/OpenDocument', $content);
+
+ # Allow some HTML markup to be converted into the output format's
+ # corresponding markup code, e.g. bold or italic.
+ foreach my $key (keys(%text_markup_replace)) {
+ my $value = $text_markup_replace{$key};
+ $content =~ s|\<${key}\>||gi; #"
+ $content =~ s|\</${key}\>||gi;
+ }
+
+ return $content;
+}
+
+my %html_replace = (
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '' => '',
+ '
' => '',
+ '
' => '',
+);
+
+sub _format_html {
+ my ($self, $content, %params) = @_;
+
+ $content =~ s{ ^
|
$ }{}gx;
+ $content =~ s{ \r+ }{}gx;
+ $content =~ s{ \n+ }{ }gx;
+ $content =~ s{ (?:\ |\s)+ }{ }gx;
+
+ my $in_p = 1;
+ my $p_start_tag = qq||;
+ my $ul_start_tag = qq||;
+ my $ol_start_tag = qq||;
+ my $ul_li_start_tag = qq||;
+ my $ol_li_start_tag = qq||;
+
+ my @parts = map {
+ if (substr($_, 0, 1) eq '<') {
+ s{ +}{}g;
+ if ($_ eq '') {
+ $in_p--;
+ '';
+
+ } elsif ($_ eq '') {
+ if (!$in_p) {
+ $in_p = 1;
+ $p_start_tag;
+ }
+
+ } elsif ($_ eq '
') {
+ $self->{used_list_styles}->{itemize}->{$self->{current_text_style}} = 1;
+ $html_replace{'- '} = $ul_li_start_tag;
+ $ul_start_tag;
+
+ } elsif ($_ eq '
') {
+ $self->{used_list_styles}->{enumerate}->{$self->{current_text_style}} = 1;
+ $html_replace{'- '} = $ol_li_start_tag;
+ $ol_start_tag;
+
+ } else {
+ $html_replace{$_} || '';
+ }
+
+ } else {
+ $::locale->quote_special_chars('Template/OpenDocument', HTML::Entities::decode_entities($_));
+ }
+ } split(m{(<.*?>)}x, $content);
+
+ my $out = join('', @parts);
+ $out .= $p_start_tag if !$in_p;
+
+ # $::lxdebug->message(0, "out $out");
+
+ return $out;
+}
+
+my %formatters = (
+ html => \&_format_html,
+ text => \&_format_text,
+);
+
sub new {
my $type = shift;
my $self = $type->SUPER::new(@_);
- $self->{"rnd"} = int(rand(1000000));
- $self->{"iconv"} = SL::Iconv->new($::lx_office_conf{system}->{dbcharset}, "UTF-8");
-
$self->set_tag_style('<%', '%>');
$self->{quot_re} = '"';
@@ -106,6 +214,8 @@ sub parse_block {
my $tag = $&;
substr($contents, 0, length($&)) = "";
+ $self->{current_text_style} = $1 if $tag =~ m|text:style-name\s*=\s*"([^"]+)"|;
+
if ($tag =~ m|]*>)|;
my $table_row = $1;
@@ -236,36 +346,39 @@ sub parse {
return 0;
}
- my $rnd = $self->{"rnd"};
- my $new_styles = qq|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-|;
-
- $contents =~ s||${new_styles}|;
- $contents =~ s|[\n\r]||gm;
-
- my $new_contents = $self->parse_block($contents);
+ $self->{current_text_style} = '';
+ $self->{used_list_styles} = {
+ itemize => {},
+ enumerate => {},
+ };
+
+ my $new_contents;
+ if ($self->{use_template_toolkit}) {
+ my $additional_params = $::form;
+
+ $::form->init_template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
+ } else {
+ $new_contents = $self->parse_block($contents);
+ }
if (!defined($new_contents)) {
$main::lxdebug->leave_sub();
return 0;
}
+ my $new_styles = SL::Template::OpenDocument::Styles->get_style('text_basic');
+
+ foreach my $type (qw(itemize enumerate)) {
+ foreach my $parent (sort { $a cmp $b } keys %{ $self->{used_list_styles}->{$type} }) {
+ $new_styles .= SL::Template::OpenDocument::Styles->get_style('text_list_item', TYPE => $type, PARENT => $parent)
+ . SL::Template::OpenDocument::Styles->get_style("list_${type}", TYPE => $type, PARENT => $parent);
+ }
+ }
+
+ # $::lxdebug->dump(0, "new_Styles", $new_styles);
+
+ $new_contents =~ s||${new_styles}|;
+ $new_contents =~ s|[\n\r]||gm;
+
# $new_contents =~ s|>|>\n|g;
$zip->contents("content.xml", Encode::encode('utf-8-strict', $new_contents));
@@ -416,11 +529,21 @@ sub spawn_xvfb {
return $display;
}
+sub _run_python_uno {
+ my ($self, @args) = @_;
+
+ local $ENV{PYTHONPATH};
+ $ENV{PYTHONPATH} = $::lx_office_conf{environment}->{python_uno_path} . ':' . $ENV{PYTHONPATH} if $::lx_office_conf{environment}->{python_uno_path};
+ my $cmd = $::lx_office_conf{applications}->{python_uno} . ' ' . join(' ', @args);
+ return `$cmd`;
+}
+
sub is_openoffice_running {
+ my ($self) = @_;
+
$main::lxdebug->enter_sub();
- my $cmd = "./scripts/oo-uno-test-conn.py " . $::lx_office_conf{print_templates}->{openofficeorg_daemon_port} . " 2> /dev/null";
- my $output = `$cmd`;
+ my $output = $self->_run_python_uno('./scripts/oo-uno-test-conn.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, ' 2> /dev/null');
chomp $output;
my $res = ($? == 0) || $output;
@@ -447,10 +570,19 @@ sub spawn_openoffice {
last;
}
+ if ($::dispatcher->interface_type eq 'FastCGI') {
+ $::dispatcher->{request}->Detach;
+ }
+
if (!$spawned_oo) {
my $pid = fork();
if (0 == $pid) {
$main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
+
+ if ($::dispatcher->interface_type eq 'FastCGI') {
+ $::dispatcher->{request}->Finish;
+ $::dispatcher->{request}->LastCall;
+ }
chdir('/');
open(STDIN, '/dev/null');
open(STDOUT, '>/dev/null');
@@ -464,6 +596,11 @@ sub spawn_openoffice {
"-accept=socket,host=localhost,port=" .
$::lx_office_conf{print_templates}->{openofficeorg_daemon_port} . ";urp;");
exec(@cmdline);
+ } else {
+ # parent
+ if ($::dispatcher->interface_type eq 'FastCGI') {
+ $::dispatcher->{request}->Attach;
+ }
}
$main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
@@ -508,27 +645,20 @@ sub convert_to_pdf {
return 0;
}
- my @cmdline;
if (!$::lx_office_conf{print_templates}->{openofficeorg_daemon}) {
- @cmdline = ($::lx_office_conf{applications}->{openofficeorg_writer},
- "-minimized", "-norestore", "-nologo", "-nolockcheck",
- "-headless",
- "file:${filename}.odt",
- "macro://" . (split('/', $filename))[-1] .
- "/Standard.Conversion.ConvertSelfToPDF()");
+ system($::lx_office_conf{applications}->{openofficeorg_writer},
+ "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
+ "file:${filename}.odt",
+ "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()");
} else {
if (!$self->spawn_openoffice()) {
$main::lxdebug->leave_sub();
return 0;
}
- @cmdline = ("./scripts/oo-uno-convert-pdf.py",
- $::lx_office_conf{print_templates}->{openofficeorg_daemon_port},
- "${filename}.odt");
+ $self->_run_python_uno('./scripts/oo-uno-convert-pdf.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, "${filename}.odt");
}
- system(@cmdline);
-
my $res = $?;
if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
$form->{"tmpfile"} =~ s/odt$/pdf/;
@@ -549,25 +679,14 @@ sub convert_to_pdf {
}
sub format_string {
- my ($self, $variable) = @_;
- my $form = $self->{"form"};
- my $iconv = $self->{"iconv"};
-
- $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
+ my ($self, $content, $variable) = @_;
- # Allow some HTML markup to be converted into the output format's
- # corresponding markup code, e.g. bold or italic.
- my $rnd = $self->{"rnd"};
- my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
- "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
-
- foreach my $key (keys(%markup_replace)) {
- my $value = $markup_replace{$key};
- $variable =~ s|\<${key}\>||gi; #"
- $variable =~ s|\</${key}\>||gi;
- }
+ my $formatter =
+ $formatters{ $self->{variable_content_types}->{$variable} }
+ // $formatters{ $self->{default_content_type} }
+ // $formatters{ text };
- return $iconv->convert($variable);
+ return $formatter->($self, $content, variable => $variable);
}
sub get_mime_type() {