summaryrefslogtreecommitdiff
path: root/lib/HTML/AutoConvert.pm
diff options
context:
space:
mode:
authorivan <ivan>2008-08-12 04:02:02 +0000
committerivan <ivan>2008-08-12 04:02:02 +0000
commit9fbeda1dc776c602ce14d3874368d4620c079b60 (patch)
tree1c4eeceb1a881caa99dd2e2d600c0c637bb87df2 /lib/HTML/AutoConvert.pm
parentcba80d78f46ea7541c37efd54262ab1c0dff67e9 (diff)
add image handling and prevent leaking temporary files (ourselves, Archive::Zip might be)
Diffstat (limited to 'lib/HTML/AutoConvert.pm')
-rw-r--r--lib/HTML/AutoConvert.pm39
1 files changed, 36 insertions, 3 deletions
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;
+ }
}