CSS/JS: Git-Revision von HEAD als GET-Parameter verwenden
[kivitendo-erp.git] / SL / Template / OpenDocument.pm
index 085fa25..e570321 100644 (file)
@@ -70,30 +70,50 @@ my %html_replace = (
 sub _format_html {
   my ($self, $content, %params) = @_;
 
-  $content                      =~ s{ ^<p> | </p>$ }{}gx;
-  $content                      =~ s{ \r+ }{}gx;
-  $content                      =~ s{ \n+ }{ }gx;
-  $content                      =~ s{ \s+ }{ }gx;
-
-  my $in_p                      = 1;
-  my $p_start_tag               = qq|<text:p text:style-name="@{[ $self->{current_text_style} ]}">|;
-  my $ul_start_tag              = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
-  my $ol_start_tag              = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
-  my $ul_li_start_tag           = qq|<text:list-item><text:p text:style-name="PKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
-  my $ol_li_start_tag           = qq|<text:list-item><text:p text:style-name="PKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
+  my $in_p        = 0;
+  my $p_start_tag = qq|<text:p text:style-name="@{[ $self->{current_text_style} ]}">|;
+  my $prefix      = '';
+  my $suffix      = '';
+
+  my (@tags_to_open, @tags_to_close);
+  for (my $idx = scalar(@{ $self->{tag_stack} }) - 1; $idx >= 0; --$idx) {
+    my $tag = $self->{tag_stack}->[$idx];
+
+    next if $tag =~ m{/>$};
+    last if $tag =~ m{^<table};
+
+    if ($tag =~ m{^<text:p}) {
+      $in_p        = 1;
+      $p_start_tag = $tag;
+      last;
+
+    } else {
+      $suffix  =  "${tag}${suffix}";
+      $tag     =~ s{ .*>}{>};
+      $prefix .=  '</' . substr($tag, 1);
+    }
+  }
+
+  $content            =~ s{ ^<p> | </p>$ }{}gx if $in_p;
+  $content            =~ s{ \r+ }{}gx;
+  $content            =~ s{ \n+ }{ }gx;
+  $content            =~ s{ (?:\&nbsp;|\s)+ }{ }gx;
+
+  my $ul_start_tag    = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
+  my $ol_start_tag    = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
+  my $ul_li_start_tag = qq|<text:list-item><text:p text:style-name="PKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
+  my $ol_li_start_tag = qq|<text:list-item><text:p text:style-name="PKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
 
   my @parts = map {
     if (substr($_, 0, 1) eq '<') {
       s{ +}{}g;
       if ($_ eq '</p>') {
         $in_p--;
-        '</text:p>';
+        $in_p == 0 ? '</text:p>' : '';
 
       } elsif ($_ eq '<p>') {
-        if (!$in_p) {
-          $in_p = 1;
-          $p_start_tag;
-        }
+        $in_p++;
+        $in_p == 1 ? $p_start_tag : '';
 
       } elsif ($_ eq '<ul>') {
         $self->{used_list_styles}->{itemize}->{$self->{current_text_style}}   = 1;
@@ -114,10 +134,9 @@ sub _format_html {
     }
   } split(m{(<.*?>)}x, $content);
 
-  my $out  = join('', @parts);
-  $out    .= $p_start_tag if !$in_p;
+  my $out  = join('', $prefix, @parts, $suffix);
 
-  # $::lxdebug->message(0, "out $out");
+  # $::lxdebug->dump(0, "prefix parts suffix", [ $prefix, join('', @parts), $suffix ]);
 
   return $out;
 }
@@ -210,12 +229,14 @@ sub parse_block {
 
   while ($contents ne "") {
     if (substr($contents, 0, 1) eq "<") {
-      $contents =~ m|^<[^>]+>|;
-      my $tag = $&;
-      substr($contents, 0, length($&)) = "";
+      $contents =~ m|^(<[^>]+>)|;
+      my $tag = $1;
+      substr($contents, 0, length($1)) = "";
 
       $self->{current_text_style} = $1 if $tag =~ m|text:style-name\s*=\s*"([^"]+)"|;
 
+      push @{ $self->{tag_stack} }, $tag;
+
       if ($tag =~ m|<table:table-row|) {
         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
         my $table_row = $1;
@@ -224,10 +245,10 @@ sub parse_block {
         if ($table_row =~ m|\&lt;\%foreachrow\s+(.*?)\%\&gt;|) {
           my $var = $1;
 
-          $contents =~ m|\&lt;\%foreachrow\s+.*?\%\&gt;|;
-          substr($contents, length($`), length($&)) = "";
+          $contents =~ m|^(.*?)(\&lt;\%foreachrow\s+.*?\%\&gt;)|;
+          substr($contents, length($1), length($2)) = "";
 
-          ($table_row, $contents) = $self->find_end($contents, length($`));
+          ($table_row, $contents) = $self->find_end($contents, length($1));
           if (!$table_row) {
             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
             $main::lxdebug->leave_sub();
@@ -238,7 +259,7 @@ sub parse_block {
           $table_row .=  $1;
           $end_tag    =  $2;
 
-          substr $contents, 0, length($&), '';
+          substr $contents, 0, length($1) + length($2), '';
 
           my $new_text = $self->parse_foreach($var, $table_row, $tag, $end_tag, @indices);
           if (!defined($new_text)) {
@@ -261,9 +282,14 @@ sub parse_block {
         $new_contents .= $tag;
       }
 
+      if ($tag =~ m{^</ | />$}x) {
+        # $::lxdebug->message(0, "popping top tag is $tag top " . $self->{tag_stack}->[-1]);
+        pop @{ $self->{tag_stack} };
+      }
+
     } else {
-      $contents =~ /^[^<]+/;
-      my $text = $&;
+      $contents =~ /^([^<]+)/;
+      my $text = $1;
 
       my $pos_if = index($text, '&lt;%if');
       my $pos_foreach = index($text, '&lt;%foreach');
@@ -278,15 +304,15 @@ sub parse_block {
         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
         substr($contents, 0, $pos_foreach) = "";
 
-        if ($contents !~ m|^\&lt;\%foreach (.*?)\%\&gt;|) {
+        if ($contents !~ m|^(\&lt;\%foreach (.*?)\%\&gt;)|) {
           $self->{"error"} = "Malformed <\%foreach\%>.";
           $main::lxdebug->leave_sub();
           return undef;
         }
 
-        my $var = $1;
+        my $var = $2;
 
-        substr($contents, 0, length($&)) = "";
+        substr($contents, 0, length($1)) = "";
 
         my $block;
         ($block, $contents) = $self->find_end($contents);
@@ -356,8 +382,9 @@ sub parse {
   if ($self->{use_template_toolkit}) {
     my $additional_params = $::form;
 
-    $::form->init_template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
+    $::form->template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
   } else {
+    $self->{tag_stack} = [];
     $new_contents = $self->parse_block($contents);
   }
   if (!defined($new_contents)) {
@@ -483,7 +510,9 @@ sub spawn_xvfb {
 
   $main::lxdebug->message(LXDebug->DEBUG2(), "  xauthority $xauthority\n");
 
-  system("xauth add \"${display}\" . \"${mcookie}\"");
+  if (system("xauth add \"${display}\" . \"${mcookie}\"") == -1) {
+    die "system call to xauth failed: $!";
+  }
   if ($? != 0) {
     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
     $main::lxdebug->leave_sub();
@@ -646,10 +675,12 @@ sub convert_to_pdf {
   }
 
   if (!$::lx_office_conf{print_templates}->{openofficeorg_daemon}) {
-    system($::lx_office_conf{applications}->{openofficeorg_writer},
-           "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
-           "file:${filename}.odt",
-           "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()");
+    if (system($::lx_office_conf{applications}->{openofficeorg_writer},
+               "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
+               "file:${filename}.odt",
+               "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()") == -1) {
+      die "system call to $::lx_office_conf{applications}->{openofficeorg_writer} failed: $!";
+    }
   } else {
     if (!$self->spawn_openoffice()) {
       $main::lxdebug->leave_sub();