summaryrefslogtreecommitdiff
path: root/lib/HTML/AutoConvert.pm
diff options
context:
space:
mode:
authorivan <ivan>2008-08-11 07:38:09 +0000
committerivan <ivan>2008-08-11 07:38:09 +0000
commitb5e00dd5534d7f6ec852b537f551bb872931fa04 (patch)
tree3091d93f430e74f823397559d324d7f4ce882f7d /lib/HTML/AutoConvert.pm
initial importSTART
Diffstat (limited to 'lib/HTML/AutoConvert.pm')
-rw-r--r--lib/HTML/AutoConvert.pm249
1 files changed, 249 insertions, 0 deletions
diff --git a/lib/HTML/AutoConvert.pm b/lib/HTML/AutoConvert.pm
new file mode 100644
index 0000000..7df3b82
--- /dev/null
+++ b/lib/HTML/AutoConvert.pm
@@ -0,0 +1,249 @@
+package HTML::AutoConvert;
+
+use warnings;
+use strict;
+
+=head1 NAME
+
+HTML::AutoConvert - Best-effort HTML conversion of arbitrary files to HTML.
+
+=head1 VERSION
+
+Version 0.01
+
+=cut
+
+our $VERSION = '0.01';
+
+=head1 SYNOPSIS
+
+ use HTML::AutoConvert;
+
+ my $converter = HTML::AutoConvert->new();
+ #or to turn on debugging
+ my $converter = HTML::AutoConvert->new('debug'=>1);
+
+ my( $html, @images ) = $converter->html_convert( $file );
+
+ #turn on or off debugging later
+ $converter->debug(1);
+
+=head1 DESCRIPTION
+
+Convert arbitrary file types to HTML.
+
+#=head1 EXPORT
+#
+#doc on also using html_convert functional interface
+
+=head1 FUNCTIONS
+
+=head2 new
+
+=cut
+
+sub new {
+ my $proto = shift;
+ my $class = ref($proto) || $proto;
+
+ my $opts = ref($_[0]) ? shift : { @_ };
+ my $self = $opts; #{};
+ bless ($self, $class);
+
+ $self->find_handlers;
+
+ $self;
+
+}
+
+=head2 html_convert FILENAME
+
+Convert the given filename to HTML. The HTML output is returned as a scalar.
+
+=cut
+
+sub html_convert {
+ my( $self, $file ) = ( shift, shift );
+ my $opt = ref($_[0]) ? shift : { @_ };
+
+ $self->{'file'} = $file;
+
+ my @handlers = $self->handlers
+ or die "no registered handlers for filetype ". $self->filetype( $file );
+
+ my( $converted, $html, $errors ) = ( 0, '', '' );
+ foreach my $handler ( @handlers ) {
+
+ my $module = 'HTML::AutoConvert::'. $handler->{'module'};
+ my $tmp_html = eval { $module->html_convert( $self->{'file'} ) };
+ if ( $@ ) {
+ my $tmp_err = "conversion with $module failed: $@\n";
+ warn $tmp_err if $self->{'debug'};
+ $errors .= $tmp_err;
+ next;
+ }
+
+ $converted = 1;
+ $html = $tmp_html;
+ last;
+ }
+
+ die "couldn't convert $file:\n$errors" unless $converted;
+
+ $html;
+
+}
+
+=head2 debug
+
+Get or set the debugging level
+
+=cut
+
+sub debug {
+ my $self = shift;
+ $self->{'debug'} = shift if @_;
+ $self->{'debug'};
+}
+
+=head1 INTERNAL FUNCTIONS
+
+=head2 find_handlers
+
+Search for installed HTML::AutoConvert::* plugins.
+
+=cut
+
+sub find_handlers {
+ my $self = shift;
+
+ my %types;
+ foreach my $INC ( @INC ) {
+ warn "globbing $INC/HTML/AutoConvert/*.pm\n" if $self->{'debug'};
+ foreach my $file ( glob("$INC/HTML/AutoConvert/*.pm") ) {
+ warn "attempting to load handler info from $file\n" if $self->{'debug'};
+ $file =~ /\/(\w+)\.pm$/ or do {
+ warn "unrecognized file in $INC/HTML/AutoConvert/: $file\n";
+ next;
+ };
+ my $mod = $1;
+ my $info = eval "use HTML::AutoConvert::$mod; ".
+ "\\%HTML::AutoConvert::$mod\::info;";
+ if ( $@ ) {
+ die "error using HTML::AutoConvert::$mod (skipping): $@\n" if $@;
+ next;
+ }
+ unless ( keys %$info ) {
+ warn "no %info hash in HTML::AutoConvert::$mod, skipping\n" if $self->{'debug'};
+ next;
+ }
+ warn "got handler info from HTML::AutoConvert::$mod: $info\n" if $self->{'debug'};
+ if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
+ warn "skipping disabled handler HTML::AutoConvert::$mod" if $self->{'debug'};
+ next;
+ }
+
+ my $types = $info->{'types'};
+ $types = [ $types ] unless ref($types);
+
+ foreach my $type ( @$types ) {
+ $types{lc($type)}->{$mod} = { 'module' => $mod, %$info };
+ }
+
+ }
+ }
+
+ $self->{'handlers'} = \%types;
+
+}
+
+=head2 handlers
+
+Return the available handlers for the current file.
+
+=cut
+
+sub handlers {
+ my( $self ) = @_;
+
+ my $types = $self->{'handlers'};
+
+ my $type = $self->filetype;
+
+ sort { $a->{'weight'} <=> $b->{'weight'} }
+ values %{ $types->{lc($type)} };
+}
+
+=head2
+
+
+=head2 filetype
+
+Determine the type of the current file.
+
+=cut
+
+#just use the file extension... could also use File::MMagic or something
+sub filetype {
+ my $self = shift;
+
+ my $file = $self->{'file'};
+ $file =~ /\.(\w{3,4})$/ or die "can't parse $file for extension";
+ lc($1);
+}
+
+=head1 AUTHOR
+
+Ivan Kohler, C<< <ivan-html-autoconvert at 420.am> >>
+
+=head1 BUGS
+
+Please report any bugs or feature requests to C<bug-html-autoconvert at rt.cpan.org>, or through
+the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-AutoConvert>. I will be notified, and then you'll
+automatically be notified of progress on your bug as I make changes.
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+ perldoc HTML::AutoConvert
+
+You can also look for information at:
+
+=over 4
+
+=item * RT: CPAN's request tracker
+
+L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-AutoConvert>
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/HTML-AutoConvert>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/HTML-AutoConvert>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/HTML-AutoConvert>
+
+=back
+
+
+=head1 ACKNOWLEDGEMENTS
+
+
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2008 Freeside Internet Services, Inc.
+All rights reserved.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
+1; # End of HTML::AutoConvert
+