initial import
[HTML-AutoConvert.git] / lib / HTML / AutoConvert / OpenOffice.pm
1 package HTML::AutoConvert::OpenOffice;
2
3 =head1 NAME
4
5 HTML::AutoConvert::antiword - antiword plugin for HTML::AutoConvert
6
7 =head1 PREREQUISITES
8
9 OpenOffice v2.3 or later
10
11 (currently)
12 Python
13 Python-UNO
14
15 (future)
16 Perl OpenOffice::UNO
17
18 =head1 SECURITY NOTE
19
20 This module starts and leaves an OpenOffice instance running.
21
22 The OpenOffice instance binds to and listens to a port on localhost for
23 commands.  Anything which can talk to this port can instruct OpenOffice to
24 read or write any file the current user has access to.
25
26 By default, port 8100 is used.  You can choose a different port by passing an
27 option to the new() constructor:
28
29   my $converter = HTML::AutoConvert->new('openoffice_port'=>5555);
30
31 =cut
32
33 use strict;
34 use vars qw( %info ); #$slept );
35 use IPC::Run qw( run timeout io );
36 use File::Slurp qw( slurp );
37
38 %info = (
39   'types'   => [qw( doc rtf odt sxw )],
40   'weight'  => 80,
41   'url'     => 'http://wvware.sourceforge.net/',
42 );
43
44 #$slept = 0;
45
46 #sub program { ( 'openoffice', '-headless' ); }
47
48 #half-ass using DocumentConverter.py for now
49 #need to recode with OpenOffice::UNO
50
51 sub html_convert {
52   my( $self, $file ) = ( shift, shift );
53   my $opt = ref($_[0]) ? shift : { @_ };
54
55   $self->start_openoffice($opt);
56
57   my $program = 'DocumentConverter.py';
58
59   my $timeout = 60; #?
60
61   use File::Temp qw/ tempfile /;
62   my($fh, $outfile) = tempfile(SUFFIX => '.html');
63   #hmm, it gets overwritten so $fh is bunk
64
65   my($out, $err) = ( '', '' );
66   local($SIG{CHLD}) = sub {};
67   run( [ $program, $file, $outfile ], \undef, \$out, \$err, timeout($timeout) )
68     or die "$program failed with exit status ". ( $? >> 8 ). ": $out\n";
69
70   my $html = slurp($outfile);
71
72   $html;
73
74 }
75
76 sub start_openoffice {
77   my( $self ) = ( shift, shift );
78   my $opt = ref($_[0]) ? shift : { @_ };
79   my $port = $opt->{'openoffice_port'} || 8100;
80
81   my $cmd = [ 'openoffice', '-headless',
82                             "-accept=socket,port=$port;urp",
83                             #'-splash-pipe=5',
84             ];
85
86   local($SIG{CHLD}) = sub {};
87   run $cmd, \undef, '>/dev/null', '2>/dev/null'
88     or die "can't launch openoffice: $@\n";
89
90   #it isn't ready to run commands right away :(
91   #it would be better if we could ping against UNO API somehow until ready...
92   #sleep 5 unless $slept++;
93
94 }
95
96 1;