1 package HTML::AutoConvert;
8 HTML::AutoConvert - Best-effort HTML conversion of arbitrary files to HTML.
16 our $VERSION = '0.01';
20 use HTML::AutoConvert;
22 my $converter = HTML::AutoConvert->new();
23 #or to turn on debugging
24 my $converter = HTML::AutoConvert->new('debug'=>1);
26 my $html = $converter->html_convert( $file );
28 my( $html, @images ) = $converter->html_convert( $file );
30 #turn on or off debugging later
35 Convert arbitrary file types to HTML.
39 #doc on also using html_convert functional interface
49 my $class = ref($proto) || $proto;
51 my $opts = ref($_[0]) ? shift : { @_ };
52 my $self = $opts; #{};
53 bless ($self, $class);
61 =head2 html_convert FILENAME
63 Convert the given filename to HTML.
65 In a scalar context, simply returns the HTML output as a scalar.
67 my $html = $converter->html_convert( $file );
69 In a list context, returns a list consisting of the HTML output as a scalar,
70 followed by references for each image extracted, if any. Each image reference
71 is a list reference consisting of two elements: the first is the filename and
72 the second is the image itself.
74 my( $html, @images ) = $converter->html_convert( $file );
75 foreach my $image ( @images ) {
76 my( $filename, $data ) = @$image;
83 my( $self, $file ) = ( shift, shift );
84 my $opt = ref($_[0]) ? shift : { @_ };
86 $self->{'file'} = $file;
88 my @handlers = $self->handlers
89 or die "no registered handlers for filetype ". $self->filetype( $file );
91 my( $converted, $html, $errors ) = ( 0, '', '' );
93 foreach my $handler ( @handlers ) {
95 my $module = 'HTML::AutoConvert::'. $handler->{'module'};
99 if ( $handler->{'returns_images'} && wantarray ) {
100 ( $tmp_html, @tmp_imgs ) =
101 eval { $module->html_convert( $self->{'file'} ) };
104 eval { $module->html_convert( $self->{'file'} ) };
108 my $tmp_err = "conversion with $module failed: $@\n";
109 warn $tmp_err if $self->{'debug'};
120 die "couldn't convert $file:\n$errors" unless $converted;
132 Get or set the debugging level
138 $self->{'debug'} = shift if @_;
142 =head1 INTERNAL FUNCTIONS
146 Search for installed HTML::AutoConvert::* plugins.
154 foreach my $INC ( @INC ) {
155 warn "globbing $INC/HTML/AutoConvert/*.pm\n" if $self->{'debug'};
156 foreach my $file ( glob("$INC/HTML/AutoConvert/*.pm") ) {
157 warn "attempting to load handler info from $file\n" if $self->{'debug'};
158 $file =~ /\/(\w+)\.pm$/ or do {
159 warn "unrecognized file in $INC/HTML/AutoConvert/: $file\n";
163 my $info = eval "use HTML::AutoConvert::$mod; ".
164 "\\%HTML::AutoConvert::$mod\::info;";
166 die "error using HTML::AutoConvert::$mod (skipping): $@\n" if $@;
169 unless ( keys %$info ) {
170 warn "no %info hash in HTML::AutoConvert::$mod, skipping\n" if $self->{'debug'};
173 warn "got handler info from HTML::AutoConvert::$mod: $info\n" if $self->{'debug'};
174 if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
175 warn "skipping disabled handler HTML::AutoConvert::$mod" if $self->{'debug'};
179 my $types = $info->{'types'};
180 $types = [ $types ] unless ref($types);
182 foreach my $type ( @$types ) {
183 $types{lc($type)}->{$mod} = { 'module' => $mod, %$info };
189 $self->{'handlers'} = \%types;
195 Return the available handlers for the current file.
202 my $types = $self->{'handlers'};
204 my $type = $self->filetype;
206 sort { $a->{'weight'} <=> $b->{'weight'} }
207 values %{ $types->{lc($type)} };
215 Determine the type of the current file.
219 #just use the file extension... could also use File::MMagic or something
223 my $file = $self->{'file'};
224 $file =~ /\.(\w{3,4})$/ or die "can't parse $file for extension";
230 Ivan Kohler, C<< <ivan-html-autoconvert at 420.am> >>
234 Please report any bugs or feature requests to C<bug-html-autoconvert at rt.cpan.org>, or through
235 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-AutoConvert>. I will be notified, and then you'll
236 automatically be notified of progress on your bug as I make changes.
240 You can find documentation for this module with the perldoc command.
242 perldoc HTML::AutoConvert
244 You can also look for information at:
248 =item * RT: CPAN's request tracker
250 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-AutoConvert>
252 =item * AnnoCPAN: Annotated CPAN documentation
254 L<http://annocpan.org/dist/HTML-AutoConvert>
258 L<http://cpanratings.perl.org/d/HTML-AutoConvert>
262 L<http://search.cpan.org/dist/HTML-AutoConvert>
267 =head1 ACKNOWLEDGEMENTS
271 =head1 COPYRIGHT & LICENSE
273 Copyright 2008 Freeside Internet Services, Inc.
276 This program is free software; you can redistribute it and/or modify it
277 under the same terms as Perl itself.
281 1; # End of HTML::AutoConvert