File Coverage

blib/lib/POD/Walker.pm
Criterion Covered Total %
statement 24 147 16.3
branch 0 88 0.0
condition 0 9 0.0
subroutine 8 10 80.0
pod 2 2 100.0
total 34 256 13.2


line stmt bran cond sub pod time code
1             package POD::Walker;
2              
3 1     1   29506 use warnings;
  1         3  
  1         31  
4 1     1   87 use strict;
  1         2  
  1         36  
5 1     1   760 use Script::isAperlScript;
  1         9766  
  1         38  
6 1     1   1518 use Pod::Html;
  1         852172  
  1         78  
7 1     1   1875 use Pod::LaTeX;
  1         18428  
  1         69  
8 1     1   1332 use Pod::Man;
  1         30654  
  1         50  
9 1     1   11392 use Pod::Text;
  1         12092  
  1         83  
10 1     1   929 use File::Copy;
  1         2933  
  1         1244  
11              
12             =head1 NAME
13              
14             POD::Walker - Walks a directory and runs any Perl files through the specified POD converter.
15              
16             =head1 VERSION
17              
18             Version 0.0.0
19              
20             =cut
21              
22             our $VERSION = '0.0.0';
23              
24              
25             =head1 SYNOPSIS
26              
27             use POD::Walker;
28             my $returned=POD::Walker->run({in=>"/input/path", out=>"/output/path", format=>"html" });
29             if($returned->{error}){
30             print "Error: ".$returned->{error}."\n";
31             }
32              
33             =head1 FUNCTION
34              
35             =head2 run
36              
37             Process a directory try and ignore any hidden directories or files.
38              
39             The returned value is a hash. See the section "RETURN HASH" for more information.
40              
41             =head3 args hash ref
42              
43             =head4 changesCopy
44              
45             This copies any "Changes" files.
46              
47             This defaults to "1".
48              
49             =head4 in
50              
51             The directory to start in.
52              
53             =head4 format
54              
55             The output type. This can be any of the ones listed below.
56              
57             html
58             latex
59             man
60             text
61              
62             If one is not specified, 'html' will be used.
63              
64             =head4 manifestCopy
65              
66             This copies any "MANIFEST" files.
67              
68             This defaults to "1".
69              
70             =head4 readmeCopy
71              
72             This copies any "README" files.
73              
74             This defaults to "1".
75              
76             =head4 out
77              
78             This is the directory to output to.
79              
80             =cut
81              
82             sub run{
83             #make sure we have something passed to us
84 0 0   0 1   if (!defined($_[1])) {
85 0           return {error=>1};
86             }
87 0           my %args=%{$_[1]};
  0            
88              
89             #make sure all arguements are defined
90 0 0         if (!defined($args{in})) {
91 0           return {error=>2};
92             }
93 0 0         if (!defined($args{out})) {
94 0           return {error=>3};
95             }
96 0 0         if (!defined($args{format})) {
97 0           $args{format}='html';
98             }
99              
100             #make sure the input directory is usable
101 0 0         if (! -d $args{in}) {
102 0           return {error=>4};
103             }
104 0 0         if (! -r $args{in}) {
105 0           return {error=>5};
106             }
107              
108             #make sure the output directory is usable
109 0 0         if (! -d $args{out}) {
110 0 0         if (!mkdir($args{out})) {
111 0           return {error=>6};
112             }
113             }
114 0 0         if (! -w $args{out}) {
115 0           return {error=>7};
116             }
117 0 0         if (! -w $args{out}) {
118 0           return {error=>8};
119             }
120              
121             #default to 1 for MANIFEST copying
122 0 0         if (!defined( $args{manifestCopy} )) {
123 0           $args{manifestCopy}=1;
124             }
125              
126             #default to 1 for README copying
127 0 0         if (!defined( $args{readmeCopy} )) {
128 0           $args{readmeCopy}=1;
129             }
130              
131             #default to 1 for Changes copying
132 0 0         if (!defined( $args{changesCopy} )) {
133 0           $args{changesCopy}=1;
134             }
135              
136             #starts processing and returns it
137 0           return process(\%args);
138             }
139              
140             =head2 process
141              
142             This is a internal function.
143              
144             =cut
145              
146             sub process{
147 0     0 1   my %args=%{$_[0]};
  0            
148              
149             #inits the return value
150 0           my %toreturn;
151 0           $toreturn{error}=undef;
152              
153             #holds any thing that errored
154 0           my @errored;
155              
156             #make sure all arguements are defined
157 0 0         if (!defined($args{in})) {
158 0           $toreturn{errored}=\@errored;
159 0           $toreturn{error}=2;
160 0           return \%toreturn;
161             }
162 0 0         if (!defined($args{out})) {
163 0           $toreturn{errored}=\@errored;
164 0           $toreturn{error}=3;
165 0           return \%toreturn;
166             }
167 0 0         if (!defined($args{format})) {
168 0           $args{format}='html';
169             }
170              
171             #make sure the input directory is usable
172 0 0         if (! -d $args{in}) {
173 0           $toreturn{errored}=\@errored;
174 0           $toreturn{error}=4;
175 0           return \%toreturn;
176             }
177 0 0         if (! -r $args{in}) {
178 0           $toreturn{errored}=\@errored;
179 0           $toreturn{error}=5;
180 0           return \%toreturn;
181             }
182              
183             #make sure the output directory is usable
184 0 0         if (! -d $args{out}) {
185 0 0         if (!mkdir($args{out})){
186 0           $toreturn{errored}=\@errored;
187 0           $toreturn{error}=6;
188 0           return \%toreturn;
189             }
190             }
191 0 0         if (! -w $args{out}) {
192 0           $toreturn{errored}=\@errored;
193 0           $toreturn{error}=7;
194 0           return \%toreturn;
195             }
196 0 0         if (! -w $args{out}) {
197 0           $toreturn{errored}=\@errored;
198 0           $toreturn{error}=8;
199 0           return \%toreturn;
200             }
201              
202             #processes the input directory
203 0           my $dir;
204 0 0         if (opendir($dir, $args{in})) {
205             #removes hidden files/directories
206 0           my @dirEntries=grep(!/^\./ , readdir($dir));
207 0           closedir($dir);
208              
209             #process each entry
210 0           my $int=0;
211 0           while (defined( $dirEntries[$int] )) {
212 0           my %newArgs=%args;
213 0           $newArgs{in}=$args{in}.'/'.$dirEntries[$int];
214 0           $newArgs{out}=$args{out}.'/'.$dirEntries[$int];
215            
216             #The directory and file stuff like this is split to simplify handling odd stuff the path in question.
217             #handles directories
218 0 0         if (-d $newArgs{in}) {
219             #process it if it was a directory
220 0           my $returned=process(\%newArgs);
221             #push what failed onto the list, if needed
222 0 0         if ($returned->{error}) {
223 0           my @errors=@{$returned->{errored}};
  0            
224              
225 0           print $returned->{error}." ".$newArgs{in}."\n";
226              
227 0           push(@errored, @errors);
228 0           push(@errored, $newArgs{in});
229             }
230             }
231             #handles files
232 0 0         if (-f $newArgs{in}) {
233             #we don't process a file by default
234 0           my $process=0;
235              
236             #checks if we should process a file
237 0 0         if ( $newArgs{in} =~ /\.[Pp][Mm]$/ ) {
238 0           $process=1;
239             }
240 0 0 0       if ( ( $newArgs{in} =~ /\.[Pp][Ll]$/ ) && (!$process) ) {
241 0           $process=1;
242             }
243 0 0 0       if ( ( $newArgs{in} =~ /\.[Pp][Oo][Dd]$/ ) && (!$process) ) {
244 0           $process=1;
245             }
246 0 0 0       if ( ( -x $newArgs{in} ) && (!$process) ) {
247 0 0         if ( !isAperlScript( $newArgs{in} ) ) {
248 0           $process=1;
249             }
250             }
251              
252             #handles it if it is one of the copy types
253 0 0         if ($dirEntries[$int] eq "Changes") {
254 0 0         if ($args{changesCopy}) {
255 0           copy($newArgs{in}, $newArgs{out});
256             }
257             }
258 0 0         if ($dirEntries[$int] eq "README") {
259 0 0         if ($args{readmeCopy}) {
260 0           copy($newArgs{in}, $newArgs{out});
261             }
262             }
263 0 0         if ($dirEntries[$int] eq "MANIFEST") {
264 0 0         if ($args{manifestCopy}) {
265 0           copy($newArgs{in}, $newArgs{out});
266             }
267             }
268              
269             #process a file if needed
270 0 0         if ($process) {
271 0 0         if ($args{format} eq "html") {
272 0           pod2html("--flush", "--infile=".$newArgs{in}, "--outfile=".$newArgs{out}.".html");
273 0 0         if (-f "pod2htmd.tmp") {
274 0           unlink("pod2htmd.tmp");
275             }
276 0 0         if (-f "pod2htmi.tmp") {
277 0           unlink("pod2htmi.tmp");
278             }
279             }
280              
281 0 0         if ($args{format} eq "latex") {
282 0           my $parser = Pod::LaTeX->new;
283 0           $parser->parse_from_file ($newArgs{in}, $newArgs{out}.".latex");
284             }
285              
286 0 0         if ($args{format} eq "man") {
287 0           my $parser = Pod::Man->new;
288 0           $parser->parse_from_file ($newArgs{in}, $newArgs{out}.".man");
289             }
290              
291 0 0         if ($args{format} eq "text") {
292 0           my $parser = Pod::Text->new;
293 0           $parser->parse_from_file ($newArgs{in}, $newArgs{out}.".text");
294             }
295              
296             }
297              
298             }
299              
300 0           $int++;
301             }
302              
303             }else {
304 0           $toreturn{errored}=\@errored;
305 0           $toreturn{error}=9;
306 0           return \%toreturn;
307             }
308              
309 0           $toreturn{errored}=\@errored;
310              
311 0           return \%toreturn;
312             }
313              
314             =head1 RETURN HASH
315              
316             =head2 error
317              
318             This integer represents if there is a error or note.
319              
320             This is set to true if there was an error is set to a
321             integet greater than or equal to "1".
322              
323             =head3 error codes
324              
325             =head4 1
326              
327             No arguements passed.
328              
329             =head4 2
330              
331             No in directory specified.
332              
333             =head4 3
334              
335             No in directory specified.
336              
337             =head4 4
338              
339             The input directory does not exist or is not a directory.
340              
341             =head4 5
342              
343             The input directory is not readable.
344              
345             =head4 6
346              
347             The specified outpbut directory does not exist, is is not
348             a directory, or could not be created.
349              
350             =head4 7
351              
352             The output directory is not readable.
353              
354             =head4 8
355              
356             The output directory is not writable.
357              
358             =head4 9
359              
360             Failed to open the input directory.
361              
362             =head2 errored
363              
364             This contains a list of files or directories that could not be processed.
365              
366             =head1 AUTHOR
367              
368             Zane C. Bowers, C<< >>
369              
370             =head1 BUGS
371              
372             Please report any bugs or feature requests to C, or through
373             the web interface at L. I will be notified, and then you'll
374             automatically be notified of progress on your bug as I make changes.
375              
376              
377              
378              
379             =head1 SUPPORT
380              
381             You can find documentation for this module with the perldoc command.
382              
383             perldoc POD::Walker
384              
385              
386             You can also look for information at:
387              
388             =over 4
389              
390             =item * RT: CPAN's request tracker
391              
392             L
393              
394             =item * AnnoCPAN: Annotated CPAN documentation
395              
396             L
397              
398             =item * CPAN Ratings
399              
400             L
401              
402             =item * Search CPAN
403              
404             L
405              
406             =back
407              
408              
409             =head1 ACKNOWLEDGEMENTS
410              
411              
412             =head1 COPYRIGHT & LICENSE
413              
414             Copyright 2010 Zane C. Bowers, all rights reserved.
415              
416             This program is free software; you can redistribute it and/or modify it
417             under the same terms as Perl itself.
418              
419              
420             =cut
421              
422             1; # End of POD::Walker