initial import
[HTML-AutoConvert.git] / lib / HTML / AutoConvert.pm
1 package HTML::AutoConvert;
2
3 use warnings;
4 use strict;
5
6 =head1 NAME
7
8 HTML::AutoConvert - Best-effort HTML conversion of arbitrary files to HTML.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 our $VERSION = '0.01';
17
18 =head1 SYNOPSIS
19
20     use HTML::AutoConvert;
21
22     my $converter = HTML::AutoConvert->new();
23     #or to turn on debugging
24     my $converter = HTML::AutoConvert->new('debug'=>1);
25
26     my( $html, @images ) = $converter->html_convert( $file );
27
28     #turn on or off debugging later
29     $converter->debug(1);
30
31 =head1 DESCRIPTION
32
33 Convert arbitrary file types to HTML.
34
35 #=head1 EXPORT
36 #
37 #doc on also using html_convert functional interface
38
39 =head1 FUNCTIONS
40
41 =head2 new
42
43 =cut
44
45 sub new {
46   my $proto = shift;
47   my $class = ref($proto) || $proto;
48
49   my $opts = ref($_[0]) ? shift : { @_ };
50   my $self = $opts; #{};
51   bless ($self, $class);
52
53   $self->find_handlers;
54
55   $self;
56
57 }
58
59 =head2 html_convert FILENAME
60
61 Convert the given filename to HTML.  The HTML output is returned as a scalar.
62
63 =cut
64
65 sub html_convert {
66   my( $self, $file ) = ( shift, shift );
67   my $opt = ref($_[0]) ? shift : { @_ };
68
69   $self->{'file'} = $file;
70
71   my @handlers = $self->handlers
72     or die "no registered handlers for filetype ". $self->filetype( $file );
73
74   my( $converted, $html, $errors ) = ( 0, '', '' );
75   foreach my $handler ( @handlers ) {
76
77     my $module = 'HTML::AutoConvert::'. $handler->{'module'};
78     my $tmp_html = eval { $module->html_convert( $self->{'file'} ) };
79     if ( $@ ) {
80        my $tmp_err = "conversion with $module failed: $@\n";
81        warn $tmp_err if $self->{'debug'};
82        $errors .= $tmp_err;
83        next;
84     }
85
86     $converted = 1;
87     $html = $tmp_html;
88     last;
89   }
90
91   die "couldn't convert $file:\n$errors" unless $converted;
92
93   $html;
94
95 }
96
97 =head2 debug
98
99 Get or set the debugging level
100
101 =cut
102
103 sub debug {
104   my $self = shift;
105   $self->{'debug'} = shift if @_;
106   $self->{'debug'};
107 }
108
109 =head1 INTERNAL FUNCTIONS
110
111 =head2 find_handlers
112
113 Search for installed HTML::AutoConvert::* plugins.
114
115 =cut
116
117 sub find_handlers {
118   my $self = shift;
119
120   my %types;
121   foreach my $INC ( @INC ) {
122     warn "globbing $INC/HTML/AutoConvert/*.pm\n" if $self->{'debug'};
123     foreach my $file ( glob("$INC/HTML/AutoConvert/*.pm") ) {
124       warn "attempting to load handler info from $file\n" if $self->{'debug'};
125       $file =~ /\/(\w+)\.pm$/ or do {
126         warn "unrecognized file in $INC/HTML/AutoConvert/: $file\n";
127         next;
128       };
129       my $mod = $1;
130       my $info = eval "use HTML::AutoConvert::$mod; ".
131                       "\\%HTML::AutoConvert::$mod\::info;";
132       if ( $@ ) {
133         die "error using HTML::AutoConvert::$mod (skipping): $@\n" if $@;
134         next;
135       }
136       unless ( keys %$info ) {
137         warn "no %info hash in HTML::AutoConvert::$mod, skipping\n" if $self->{'debug'};
138         next;
139       }
140       warn "got handler info from HTML::AutoConvert::$mod: $info\n" if $self->{'debug'};
141       if ( exists($info->{'disabled'}) && $info->{'disabled'} ) {
142         warn "skipping disabled handler HTML::AutoConvert::$mod" if $self->{'debug'};
143         next;
144       }
145
146       my $types = $info->{'types'};
147       $types = [ $types ] unless ref($types);
148
149       foreach my $type ( @$types ) {
150         $types{lc($type)}->{$mod} = { 'module' => $mod, %$info };
151       }
152
153     }
154   }
155
156   $self->{'handlers'} = \%types;
157
158 }
159
160 =head2 handlers
161
162 Return the available handlers for the current file.
163
164 =cut
165
166 sub handlers {
167   my( $self ) = @_;
168
169   my $types = $self->{'handlers'};
170
171   my $type = $self->filetype;
172
173   sort { $a->{'weight'} <=> $b->{'weight'} }
174        values %{ $types->{lc($type)} };
175 }
176
177 =head2
178
179
180 =head2 filetype
181
182 Determine the type of the current file.
183
184 =cut
185
186 #just use the file extension...  could also use File::MMagic or something
187 sub filetype {
188   my $self = shift;
189
190   my $file = $self->{'file'};
191   $file =~ /\.(\w{3,4})$/ or die "can't parse $file for extension";
192   lc($1);
193 }
194
195 =head1 AUTHOR
196
197 Ivan Kohler, C<< <ivan-html-autoconvert at 420.am> >>
198
199 =head1 BUGS
200
201 Please report any bugs or feature requests to C<bug-html-autoconvert at rt.cpan.org>, or through
202 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-AutoConvert>.  I will be notified, and then you'll
203 automatically be notified of progress on your bug as I make changes.
204
205 =head1 SUPPORT
206
207 You can find documentation for this module with the perldoc command.
208
209     perldoc HTML::AutoConvert
210
211 You can also look for information at:
212
213 =over 4
214
215 =item * RT: CPAN's request tracker
216
217 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-AutoConvert>
218
219 =item * AnnoCPAN: Annotated CPAN documentation
220
221 L<http://annocpan.org/dist/HTML-AutoConvert>
222
223 =item * CPAN Ratings
224
225 L<http://cpanratings.perl.org/d/HTML-AutoConvert>
226
227 =item * Search CPAN
228
229 L<http://search.cpan.org/dist/HTML-AutoConvert>
230
231 =back
232
233
234 =head1 ACKNOWLEDGEMENTS
235
236
237
238 =head1 COPYRIGHT & LICENSE
239
240 Copyright 2008 Freeside Internet Services, Inc.
241 All rights reserved.
242
243 This program is free software; you can redistribute it and/or modify it
244 under the same terms as Perl itself.
245
246 =cut
247
248 1; # End of HTML::AutoConvert
249