1 package SL::Template::OpenDocument;
3 use parent qw(SL::Template::Simple);
11 use SL::Template::OpenDocument::Styles;
16 # use File::Temp qw(:mktemp);
21 my %text_markup_replace = (
31 my ($self, $content, %params) = @_;
33 $content = $::locale->quote_special_chars('Template/OpenDocument', $content);
35 # Allow some HTML markup to be converted into the output format's
36 # corresponding markup code, e.g. bold or italic.
37 foreach my $key (keys(%text_markup_replace)) {
38 my $value = $text_markup_replace{$key};
39 $content =~ s|\<${key}\>|<text:span text:style-name=\"TKIVITENDO${value}\">|gi; #"
40 $content =~ s|\</${key}\>|</text:span>|gi;
47 '</ul>' => '</text:list>',
48 '</ol>' => '</text:list>',
49 '</li>' => '</text:p></text:list-item>',
50 '<b>' => '<text:span text:style-name="TKIVITENDOBOLD">',
51 '</b>' => '</text:span>',
52 '<strong>' => '<text:span text:style-name="TKIVITENDOBOLD">',
53 '</strong>' => '</text:span>',
54 '<i>' => '<text:span text:style-name="TKIVITENDOITALIC">',
55 '</i>' => '</text:span>',
56 '<em>' => '<text:span text:style-name="TKIVITENDOITALIC">',
57 '</em>' => '</text:span>',
58 '<u>' => '<text:span text:style-name="TKIVITENDOUNDERLINE">',
59 '</u>' => '</text:span>',
60 '<s>' => '<text:span text:style-name="TKIVITENDOSTRIKETHROUGH">',
61 '</s>' => '</text:span>',
62 '<sub>' => '<text:span text:style-name="TKIVITENDOSUB">',
63 '</sub>' => '</text:span>',
64 '<sup>' => '<text:span text:style-name="TKIVITENDOSUPER">',
65 '</sup>' => '</text:span>',
66 '<br/>' => '<text:line-break/>',
67 '<br>' => '<text:line-break/>',
71 my ($self, $content, %params) = @_;
73 $content =~ s{ ^<p> | </p>$ }{}gx;
74 $content =~ s{ \r+ }{}gx;
75 $content =~ s{ \n+ }{ }gx;
76 $content =~ s{ (?:\ |\s)+ }{ }gx;
79 my $p_start_tag = qq|<text:p text:style-name="@{[ $self->{current_text_style} ]}">|;
80 my $ul_start_tag = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
81 my $ol_start_tag = qq|<text:list xml:id="list@{[ int rand(9999999999999999) ]}" text:style-name="LKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
82 my $ul_li_start_tag = qq|<text:list-item><text:p text:style-name="PKIVITENDOitemize@{[ $self->{current_text_style} ]}">|;
83 my $ol_li_start_tag = qq|<text:list-item><text:p text:style-name="PKIVITENDOenumerate@{[ $self->{current_text_style} ]}">|;
86 if (substr($_, 0, 1) eq '<') {
92 } elsif ($_ eq '<p>') {
98 } elsif ($_ eq '<ul>') {
99 $self->{used_list_styles}->{itemize}->{$self->{current_text_style}} = 1;
100 $html_replace{'<li>'} = $ul_li_start_tag;
103 } elsif ($_ eq '<ol>') {
104 $self->{used_list_styles}->{enumerate}->{$self->{current_text_style}} = 1;
105 $html_replace{'<li>'} = $ol_li_start_tag;
109 $html_replace{$_} || '';
113 $::locale->quote_special_chars('Template/OpenDocument', HTML::Entities::decode_entities($_));
115 } split(m{(<.*?>)}x, $content);
117 my $out = join('', @parts);
118 $out .= $p_start_tag if !$in_p;
120 # $::lxdebug->message(0, "out $out");
126 html => \&_format_html,
127 text => \&_format_text,
133 my $self = $type->SUPER::new(@_);
135 $self->set_tag_style('<%', '%>');
136 $self->{quot_re} = '"';
142 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
144 my ($form, $new_contents) = ($self->{"form"}, "");
146 my $ary = $self->_get_loop_variable($var, 1, @indices);
148 for (my $i = 0; $i < scalar(@{$ary || []}); $i++) {
149 $form->{"__first__"} = $i == 0;
150 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
151 $form->{"__odd__"} = (($i + 1) % 2) == 1;
152 $form->{"__counter__"} = $i + 1;
153 my $new_text = $self->parse_block($text, (@indices, $i));
154 return undef unless (defined($new_text));
155 $new_contents .= $start_tag . $new_text . $end_tag;
157 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
159 return $new_contents;
163 my ($self, $text, $pos, $var, $not) = @_;
166 $pos = 0 unless ($pos);
168 while ($pos < length($text)) {
171 next if (substr($text, $pos - 1, 5) ne '<%');
173 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
176 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
178 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
182 my $block = substr($text, 0, $pos - 1);
183 substr($text, 0, $pos - 1) = "";
184 $text =~ s!^\<\%[^\%]+\%\>!!;
185 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
187 return ($block, $text);
189 } elsif (substr($text, $pos + 4, 3) eq 'end') {
192 my $block = substr($text, 0, $pos - 1);
193 substr($text, 0, $pos - 1) = "";
194 $text =~ s!^\<\%[^\%]+\%\>!!;
196 return ($block, $text);
205 $main::lxdebug->enter_sub();
207 my ($self, $contents, @indices) = @_;
209 my $new_contents = "";
211 while ($contents ne "") {
212 if (substr($contents, 0, 1) eq "<") {
213 $contents =~ m|^<[^>]+>|;
215 substr($contents, 0, length($&)) = "";
217 $self->{current_text_style} = $1 if $tag =~ m|text:style-name\s*=\s*"([^"]+)"|;
219 if ($tag =~ m|<table:table-row|) {
220 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
224 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
227 $contents =~ m|\<\%foreachrow\s+.*?\%\>|;
228 substr($contents, length($`), length($&)) = "";
230 ($table_row, $contents) = $self->find_end($contents, length($`));
232 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
233 $main::lxdebug->leave_sub();
237 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
241 substr $contents, 0, length($&), '';
243 my $new_text = $self->parse_foreach($var, $table_row, $tag, $end_tag, @indices);
244 if (!defined($new_text)) {
245 $main::lxdebug->leave_sub();
248 $new_contents .= $new_text;
251 substr($contents, 0, length($table_row) + length($end_tag)) = "";
252 my $new_text = $self->parse_block($table_row, @indices);
253 if (!defined($new_text)) {
254 $main::lxdebug->leave_sub();
257 $new_contents .= $tag . $new_text . $end_tag;
261 $new_contents .= $tag;
265 $contents =~ /^[^<]+/;
268 my $pos_if = index($text, '<%if');
269 my $pos_foreach = index($text, '<%foreach');
271 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
272 substr($contents, 0, length($text)) = "";
273 $new_contents .= $self->substitute_vars($text, @indices);
277 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
278 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
279 substr($contents, 0, $pos_foreach) = "";
281 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
282 $self->{"error"} = "Malformed <\%foreach\%>.";
283 $main::lxdebug->leave_sub();
289 substr($contents, 0, length($&)) = "";
292 ($block, $contents) = $self->find_end($contents);
294 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
295 $main::lxdebug->leave_sub();
299 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
300 if (!defined($new_text)) {
301 $main::lxdebug->leave_sub();
304 $new_contents .= $new_text;
307 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
308 $main::lxdebug->leave_sub();
315 $main::lxdebug->leave_sub();
317 return $new_contents;
321 $main::lxdebug->enter_sub();
324 my $form = $self->{"form"};
329 if ($form->{"IN"} =~ m|^/|) {
330 $file_name = $form->{"IN"};
332 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
335 my $zip = Archive::Zip->new();
336 if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
337 $self->{"error"} = "File not found/is not a OpenDocument file.";
338 $main::lxdebug->leave_sub();
342 my $contents = Encode::decode('utf-8-strict', $zip->contents("content.xml"));
344 $self->{"error"} = "File is not a OpenDocument file.";
345 $main::lxdebug->leave_sub();
349 $self->{current_text_style} = '';
350 $self->{used_list_styles} = {
356 if ($self->{use_template_toolkit}) {
357 my $additional_params = $::form;
359 $::form->init_template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
361 $new_contents = $self->parse_block($contents);
363 if (!defined($new_contents)) {
364 $main::lxdebug->leave_sub();
368 my $new_styles = SL::Template::OpenDocument::Styles->get_style('text_basic');
370 foreach my $type (qw(itemize enumerate)) {
371 foreach my $parent (sort { $a cmp $b } keys %{ $self->{used_list_styles}->{$type} }) {
372 $new_styles .= SL::Template::OpenDocument::Styles->get_style('text_list_item', TYPE => $type, PARENT => $parent)
373 . SL::Template::OpenDocument::Styles->get_style("list_${type}", TYPE => $type, PARENT => $parent);
377 # $::lxdebug->dump(0, "new_Styles", $new_styles);
379 $new_contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
380 $new_contents =~ s|[\n\r]||gm;
382 # $new_contents =~ s|>|>\n|g;
384 $zip->contents("content.xml", Encode::encode('utf-8-strict', $new_contents));
386 my $styles = Encode::decode('utf-8-strict', $zip->contents("styles.xml"));
388 my $new_styles = $self->parse_block($styles);
389 if (!defined($new_contents)) {
390 $main::lxdebug->leave_sub();
393 $zip->contents("styles.xml", Encode::encode('utf-8-strict', $new_styles));
396 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
399 if ($form->{"format"} =~ /pdf/) {
400 $res = $self->convert_to_pdf();
403 $main::lxdebug->leave_sub();
407 sub is_xvfb_running {
408 $main::lxdebug->enter_sub();
413 my $dfname = $self->{"userspath"} . "/xvfb_display";
416 $main::lxdebug->message(LXDebug->DEBUG2(), " Looking for $dfname\n");
417 if ((-f $dfname) && open(IN, $dfname)) {
422 my $xauthority = <IN>;
426 $main::lxdebug->message(LXDebug->DEBUG2(), " found with $pid and $display\n");
428 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
429 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #1\n");
430 unlink($dfname, $xauthority);
431 $main::lxdebug->leave_sub();
436 if ($line !~ /xvfb/i) {
437 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #2\n");
438 unlink($dfname, $xauthority);
439 $main::lxdebug->leave_sub();
443 $ENV{"XAUTHORITY"} = $xauthority;
444 $ENV{"DISPLAY"} = $display;
446 $main::lxdebug->message(LXDebug->DEBUG2(), " not found\n");
449 $main::lxdebug->leave_sub();
455 $main::lxdebug->enter_sub();
459 $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
461 my $display = $self->is_xvfb_running();
464 $main::lxdebug->leave_sub();
469 while ( -f "/tmp/.X${display}-lock") {
472 $display = ":${display}";
473 $main::lxdebug->message(LXDebug->DEBUG2(), " display $display\n");
475 my $mcookie = `mcookie`;
476 die("Installation error: mcookie not found.") if ($? != 0);
479 $main::lxdebug->message(LXDebug->DEBUG2(), " mcookie $mcookie\n");
481 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
482 $ENV{"XAUTHORITY"} = $xauthority;
484 $main::lxdebug->message(LXDebug->DEBUG2(), " xauthority $xauthority\n");
486 system("xauth add \"${display}\" . \"${mcookie}\"");
488 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
489 $main::lxdebug->leave_sub();
493 $main::lxdebug->message(LXDebug->DEBUG2(), " about to fork()\n");
497 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
498 exec($::lx_office_conf{applications}->{xvfb}, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
501 $main::lxdebug->message(LXDebug->DEBUG2(), " parent dont sleeping\n");
504 my $dfname = $self->{"userspath"} . "/xvfb_display";
505 if (!open(OUT, ">", $dfname)) {
506 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
509 $main::lxdebug->leave_sub();
512 print(OUT "$pid\n$display\n$xauthority\n");
515 $main::lxdebug->message(LXDebug->DEBUG2(), " parent re-testing\n");
517 if (!$self->is_xvfb_running()) {
518 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
519 unlink($xauthority, $dfname);
521 $main::lxdebug->leave_sub();
525 $main::lxdebug->message(LXDebug->DEBUG2(), " spawn OK\n");
527 $main::lxdebug->leave_sub();
532 sub _run_python_uno {
533 my ($self, @args) = @_;
535 local $ENV{PYTHONPATH};
536 $ENV{PYTHONPATH} = $::lx_office_conf{environment}->{python_uno_path} . ':' . $ENV{PYTHONPATH} if $::lx_office_conf{environment}->{python_uno_path};
537 my $cmd = $::lx_office_conf{applications}->{python_uno} . ' ' . join(' ', @args);
541 sub is_openoffice_running {
544 $main::lxdebug->enter_sub();
546 my $output = $self->_run_python_uno('./scripts/oo-uno-test-conn.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, ' 2> /dev/null');
549 my $res = ($? == 0) || $output;
550 $main::lxdebug->message(LXDebug->DEBUG2(), " is_openoffice_running(): res $res\n");
552 $main::lxdebug->leave_sub();
557 sub spawn_openoffice {
558 $main::lxdebug->enter_sub();
562 $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
564 my ($try, $spawned_oo, $res);
567 for ($try = 0; $try < 15; $try++) {
568 if ($self->is_openoffice_running()) {
573 if ($::dispatcher->interface_type eq 'FastCGI') {
574 $::dispatcher->{request}->Detach;
580 $main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
582 if ($::dispatcher->interface_type eq 'FastCGI') {
583 $::dispatcher->{request}->Finish;
584 $::dispatcher->{request}->LastCall;
587 open(STDIN, '/dev/null');
588 open(STDOUT, '>/dev/null');
589 my $new_pid = fork();
591 my $ssres = setsid();
592 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
593 my @cmdline = ($::lx_office_conf{applications}->{openofficeorg_writer},
594 "-minimized", "-norestore", "-nologo", "-nolockcheck",
596 "-accept=socket,host=localhost,port=" .
597 $::lx_office_conf{print_templates}->{openofficeorg_daemon_port} . ";urp;");
601 if ($::dispatcher->interface_type eq 'FastCGI') {
602 $::dispatcher->{request}->Attach;
606 $main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
611 sleep($try >= 5 ? 2 : 1);
615 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
616 "OpenOffice could not be started.";
619 $main::lxdebug->leave_sub();
625 $main::lxdebug->enter_sub();
629 my $form = $self->{"form"};
631 my $filename = $form->{"tmpfile"};
632 $filename =~ s/.odt$//;
633 if (substr($filename, 0, 1) ne "/") {
634 $filename = getcwd() . "/${filename}";
637 if (substr($self->{"userspath"}, 0, 1) eq "/") {
638 $ENV{'HOME'} = $self->{"userspath"};
640 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
643 if (!$self->spawn_xvfb()) {
644 $main::lxdebug->leave_sub();
648 if (!$::lx_office_conf{print_templates}->{openofficeorg_daemon}) {
649 system($::lx_office_conf{applications}->{openofficeorg_writer},
650 "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
651 "file:${filename}.odt",
652 "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()");
654 if (!$self->spawn_openoffice()) {
655 $main::lxdebug->leave_sub();
659 $self->_run_python_uno('./scripts/oo-uno-convert-pdf.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, "${filename}.odt");
663 if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
664 $form->{"tmpfile"} =~ s/odt$/pdf/;
666 unlink($filename . ".odt");
668 $main::lxdebug->leave_sub();
673 unlink($filename . ".odt", $filename . ".pdf");
674 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
677 $main::lxdebug->leave_sub();
682 my ($self, $content, $variable) = @_;
685 $formatters{ $self->{variable_content_types}->{$variable} }
686 // $formatters{ $self->{default_content_type} }
687 // $formatters{ text };
689 return $formatter->($self, $content, variable => $variable);
692 sub get_mime_type() {
695 if ($self->{"form"}->{"format"} =~ /pdf/) {
696 return "application/pdf";
698 return "application/vnd.oasis.opendocument.text";