- $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{ (?:\ |\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} ]}">|;