From 9fbeda1dc776c602ce14d3874368d4620c079b60 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 12 Aug 2008 04:02:02 +0000 Subject: add image handling and prevent leaking temporary files (ourselves, Archive::Zip might be) --- lib/HTML/AutoConvert.pm | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'lib/HTML/AutoConvert.pm') diff --git a/lib/HTML/AutoConvert.pm b/lib/HTML/AutoConvert.pm index 7df3b82..bdbc5fd 100644 --- a/lib/HTML/AutoConvert.pm +++ b/lib/HTML/AutoConvert.pm @@ -23,6 +23,8 @@ our $VERSION = '0.01'; #or to turn on debugging my $converter = HTML::AutoConvert->new('debug'=>1); + my $html = $converter->html_convert( $file ); + # OR my( $html, @images ) = $converter->html_convert( $file ); #turn on or off debugging later @@ -58,7 +60,22 @@ sub new { =head2 html_convert FILENAME -Convert the given filename to HTML. The HTML output is returned as a scalar. +Convert the given filename to HTML. + +In a scalar context, simply returns the HTML output as a scalar. + + my $html = $converter->html_convert( $file ); + +In a list context, returns a list consisting of the HTML output as a scalar, +followed by references for each image extracted, if any. Each image reference +is a list reference consisting of two elements: the first is the filename and +the second is the image itself. + + my( $html, @images ) = $converter->html_convert( $file ); + foreach my $image ( @images ) { + my( $filename, $data ) = @$image; + #... + } =cut @@ -72,10 +89,21 @@ sub html_convert { or die "no registered handlers for filetype ". $self->filetype( $file ); my( $converted, $html, $errors ) = ( 0, '', '' ); + my @imgs = (); foreach my $handler ( @handlers ) { my $module = 'HTML::AutoConvert::'. $handler->{'module'}; - my $tmp_html = eval { $module->html_convert( $self->{'file'} ) }; + + my $tmp_html = ''; + my @tmp_imgs = (); + if ( $handler->{'returns_images'} && wantarray ) { + ( $tmp_html, @tmp_imgs ) = + eval { $module->html_convert( $self->{'file'} ) }; + } else { + $tmp_html = + eval { $module->html_convert( $self->{'file'} ) }; + } + if ( $@ ) { my $tmp_err = "conversion with $module failed: $@\n"; warn $tmp_err if $self->{'debug'}; @@ -85,12 +113,17 @@ sub html_convert { $converted = 1; $html = $tmp_html; + @imgs = @tmp_imgs; last; } die "couldn't convert $file:\n$errors" unless $converted; - $html; + if ( wantarray ) { + ( $html, @imgs ); + } else { + $html; + } } -- cgit v1.2.1