From fc2fbbca36f5ac516fdae260e9322fc3d357497c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 17 Jan 2014 13:46:17 +0100 Subject: [PATCH] =?utf8?q?SL::Template::OpenDocument:=20Unterst=C3=BCtzung?= =?utf8?q?=20f=C3=BCr=20HTML-codierte=20Felder?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Template/OpenDocument.pm | 179 ++++++++++++++++++++++------- SL/Template/OpenDocument/Styles.pm | 161 ++++++++++++++++++++++++++ 2 files changed, 299 insertions(+), 41 deletions(-) create mode 100644 SL/Template/OpenDocument/Styles.pm diff --git a/SL/Template/OpenDocument.pm b/SL/Template/OpenDocument.pm index a90a9aeea..085fa25db 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,13 +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{'
    1. '} = $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->set_tag_style('<%', '%>'); $self->{quot_re} = '"'; @@ -105,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; @@ -235,29 +346,11 @@ sub parse { return 0; } - my $rnd = $self->{"rnd"}; - my $new_styles = qq| - - - - - - - - - - - - - - - - - -|; - - $contents =~ s||${new_styles}|; - $contents =~ s|[\n\r]||gm; + $self->{current_text_style} = ''; + $self->{used_list_styles} = { + itemize => {}, + enumerate => {}, + }; my $new_contents; if ($self->{use_template_toolkit}) { @@ -272,6 +365,20 @@ sub parse { 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)); @@ -572,24 +679,14 @@ sub convert_to_pdf { } sub format_string { - my ($self, $variable) = @_; - my $form = $self->{"form"}; + my ($self, $content, $variable) = @_; - $variable = $main::locale->quote_special_chars('Template/OpenDocument', $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 $variable; + return $formatter->($self, $content, variable => $variable); } sub get_mime_type() { diff --git a/SL/Template/OpenDocument/Styles.pm b/SL/Template/OpenDocument/Styles.pm new file mode 100644 index 000000000..3f73fc04e --- /dev/null +++ b/SL/Template/OpenDocument/Styles.pm @@ -0,0 +1,161 @@ +package SL::Template::OpenDocument::Styles; + +use strict; +use utf8; + +use Carp; + +my %styles = ( + text_basic => qq| + + + + + + + + + + + + + + + + + + + + + + +|, + + text_list_item => qq| + + + +|, + + list_itemize => qq| + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + |, + + list_enumerate => qq| + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + |, +); + +sub get_style { + my ($class, $style_name, %replacements) = @_; + + my $copy = "". $styles{$style_name} || croak("Unknown style $style_name"); + + $copy =~ s{^ +}{}gm; + $copy =~ s{[\r\n]+}{}gm; + $copy =~ s{__${_}__}{ $replacements{$_} }ge for keys %replacements; + + return $copy; +} + +1; -- 2.20.1