home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / lib / Pod / Find.pm < prev    next >
Text File  |  2000-03-16  |  8KB  |  279 lines

  1. #############################################################################  
  2. # Pod/Find.pm -- finds files containing POD documentation
  3. #
  4. # Author: Marek Rouchal <marek@saftsack.fs.uni-bayreuth.de>
  5. # Copyright (C) 1999-2000 by Marek Rouchal (and borrowing code
  6. # from Nick Ing-Simmon's PodToHtml). All rights reserved.
  7. # This file is part of "PodParser". Pod::Find is free software;
  8. # you can redistribute it and/or modify it under the same terms
  9. # as Perl itself.
  10. #############################################################################
  11.  
  12. package Pod::Find;
  13.  
  14. use vars qw($VERSION);
  15. $VERSION = 0.12;   ## Current version of this package
  16. require  5.005;    ## requires this Perl version or later
  17.  
  18. #############################################################################
  19.  
  20. =head1 NAME
  21.  
  22. Pod::Find - find POD documents in directory trees
  23.  
  24. =head1 SYNOPSIS
  25.  
  26.   use Pod::Find qw(pod_find simplify_name);
  27.   my %pods = pod_find({ -verbose => 1, -inc => 1 });
  28.   foreach(keys %pods) {
  29.      print "found library POD `$pods{$_}' in $_\n";
  30.   }
  31.  
  32.   print "podname=",simplify_name('a/b/c/mymodule.pod'),"\n";
  33.  
  34. =head1 DESCRIPTION
  35.  
  36. B<Pod::Find> provides a function B<pod_find> that searches for POD
  37. documents in a given set of files and directories. It returns a hash
  38. with the file names as keys and the POD name as value. The POD name
  39. is derived from the file name and its position in the directory tree.
  40.  
  41. E.g. when searching in F<$HOME/perl5lib>, the file
  42. F<$HOME/perl5lib/MyModule.pm> would get the POD name I<MyModule>,
  43. whereas F<$HOME/perl5lib/Myclass/Subclass.pm> would be
  44. I<Myclass::Subclass>. The name information can be used for POD
  45. translators.
  46.  
  47. Only text files containing at least one valid POD command are found.
  48.  
  49. A warning is printed if more than one POD file with the same POD name
  50. is found, e.g. F<CPAN.pm> in different directories. This usually
  51. indicates duplicate occurrences of modules in the I<@INC> search path.
  52.  
  53. The function B<simplify_name> is equivalent to B<basename>, but also
  54. strips Perl-like extensions (.pm, .pl, .pod) and extensions like
  55. F<.bat>, F<.cmd> on Win32 and OS/2, respectively.
  56.  
  57. Note that neither B<pod_find> nor B<simplify_name> are exported by
  58. default so be sure to specify them in the B<use> statement if you need
  59. them:
  60.  
  61.   use Pod::Find qw(pod_find simplify_name);
  62.  
  63. =head1 OPTIONS
  64.  
  65. The first argument for B<pod_find> may be a hash reference with options.
  66. The rest are either directories that are searched recursively or files.
  67. The POD names of files are the plain basenames with any Perl-like extension
  68. (.pm, .pl, .pod) stripped.
  69.  
  70. =over 4
  71.  
  72. =item B<-verbose>
  73.  
  74. Print progress information while scanning.
  75.  
  76. =item B<-perl>
  77.  
  78. Apply Perl-specific heuristics to find the correct PODs. This includes
  79. stripping Perl-like extensions, omitting subdirectories that are numeric
  80. but do I<not> match the current Perl interpreter's version id, suppressing
  81. F<site_perl> as a module hierarchy name etc.
  82.  
  83. =item B<-script>
  84.  
  85. Search for PODs in the current Perl interpreter's installation 
  86. B<scriptdir>. This is taken from the local L<Config|Config> module.
  87.  
  88. =item B<-inc>
  89.  
  90. Search for PODs in the current Perl interpreter's I<@INC> paths. This
  91. automatically considers paths specified in the C<PERL5LIB> environment.
  92.  
  93. =back
  94.  
  95. =head1 AUTHOR
  96.  
  97. Marek Rouchal E<lt>marek@saftsack.fs.uni-bayreuth.deE<gt>,
  98. heavily borrowing code from Nick Ing-Simmons' PodToHtml.
  99.  
  100. =head1 SEE ALSO
  101.  
  102. L<Pod::Parser>, L<Pod::Checker>
  103.  
  104. =cut
  105.  
  106. use strict;
  107. #use diagnostics;
  108. use Exporter;
  109. use File::Spec;
  110. use File::Find;
  111. use Cwd;
  112.  
  113. use vars qw(@ISA @EXPORT_OK $VERSION);
  114. @ISA = qw(Exporter);
  115. @EXPORT_OK = qw(&pod_find &simplify_name);
  116.  
  117. # package global variables
  118. my $SIMPLIFY_RX;
  119.  
  120. # return a hash of the POD files found
  121. # first argument may be a hashref (options),
  122. # rest is a list of directories to search recursively
  123. sub pod_find
  124. {
  125.     my %opts;
  126.     if(ref $_[0]) {
  127.         %opts = %{shift()};
  128.     }
  129.  
  130.     $opts{-verbose} ||= 0;
  131.     $opts{-perl}    ||= 0;
  132.  
  133.     my (@search) = @_;
  134.  
  135.     if($opts{-script}) {
  136.         require Config;
  137.         push(@search, $Config::Config{scriptdir});
  138.         $opts{-perl} = 1;
  139.     }
  140.  
  141.     if($opts{-inc}) {
  142.         push(@search, grep($_ ne '.',@INC));
  143.         $opts{-perl} = 1;
  144.     }
  145.  
  146.     if($opts{-perl}) {
  147.         require Config;
  148.         # this code simplifies the POD name for Perl modules:
  149.         # * remove "site_perl"
  150.         # * remove e.g. "i586-linux" (from 'archname')
  151.         # * remove e.g. 5.00503
  152.         # * remove pod/ if followed by *.pod (e.g. in pod/perlfunc.pod)
  153.         $SIMPLIFY_RX =
  154.           qq!^(?i:site_perl/|\Q$Config::Config{archname}\E/|\\d+\\.\\d+([_.]?\\d+)?/|pod/(?=.*?\\.pod\\z))*!;
  155.  
  156.     }
  157.  
  158.     my %dirs_visited;
  159.     my %pods;
  160.     my %names;
  161.     my $pwd = cwd();
  162.  
  163.     foreach my $try (@search) {
  164.         unless(File::Spec->file_name_is_absolute($try)) {
  165.             # make path absolute
  166.             $try = File::Spec->catfile($pwd,$try);
  167.         }
  168.         # simplify path
  169.         $try = File::Spec->canonpath($try);
  170.         my $name;
  171.         if(-f $try) {
  172.             if($name = _check_and_extract_name($try, $opts{-verbose})) {
  173.                 _check_for_duplicates($try, $name, \%names, \%pods);
  174.             }
  175.             next;
  176.         }
  177.         my $root_rx = qq!^\Q$try\E/!;
  178.         File::Find::find( sub {
  179.             my $item = $File::Find::name;
  180.             if(-d) {
  181.                 if($dirs_visited{$item}) {
  182.                     warn "Directory '$item' already seen, skipping.\n"
  183.                         if($opts{-verbose});
  184.                     $File::Find::prune = 1;
  185.                     return;
  186.                 }
  187.                 else {
  188.                     $dirs_visited{$item} = 1;
  189.                 }
  190.                 if($opts{-perl} && /^(\d+\.[\d_]+)\z/s && eval "$1" != $]) {
  191.                     $File::Find::prune = 1;
  192.                     warn "Perl $] version mismatch on $_, skipping.\n"
  193.                         if($opts{-verbose});
  194.                 }
  195.                 return;
  196.             }
  197.             if($name = _check_and_extract_name($item, $opts{-verbose}, $root_rx)) {
  198.                 _check_for_duplicates($item, $name, \%names, \%pods);
  199.             }
  200.         }, $try); # end of File::Find::find
  201.     }
  202.     chdir $pwd;
  203.     %pods;
  204. }
  205.  
  206. sub _check_for_duplicates {
  207.     my ($file, $name, $names_ref, $pods_ref) = @_;
  208.     if($$names_ref{$name}) {
  209.         warn "Duplicate POD found (shadowing?): $name ($file)\n";
  210.         warn "    Already seen in ",
  211.             join(' ', grep($$pods_ref{$_} eq $name, keys %$pods_ref)),"\n";
  212.     }
  213.     else {
  214.         $$names_ref{$name} = 1;
  215.     }
  216.     $$pods_ref{$file} = $name;
  217. }
  218.  
  219. sub _check_and_extract_name {
  220.     my ($file, $verbose, $root_rx) = @_;
  221.  
  222.     # check extension or executable flag
  223.     # this involves testing the .bat extension on Win32!
  224.     unless($file =~ /\.(pod|pm|plx?)\z/i || (-f $file && -x _ && -T _)) {
  225.         return undef;
  226.     }
  227.  
  228.     # check for one line of POD
  229.     unless(open(POD,"<$file")) {
  230.         warn "Error: $file is unreadable: $!\n";
  231.         return undef;
  232.     }
  233.     local $/ = undef;
  234.     my $pod = <POD>;
  235.     close(POD);
  236.     unless($pod =~ /\n=(head\d|pod|over|item)\b/) {
  237.         warn "No POD in $file, skipping.\n"
  238.             if($verbose);
  239.         return;
  240.     }
  241.     undef $pod;
  242.  
  243.     # strip non-significant path components
  244.     # _TODO_ what happens on e.g. Win32?
  245.     my $name = $file;
  246.     if(defined $root_rx) {
  247.         $name =~ s!$root_rx!!s;
  248.         $name =~ s!$SIMPLIFY_RX!!os if(defined $SIMPLIFY_RX);
  249.     }
  250.     else {
  251.         $name =~ s:^.*/::s;
  252.     }
  253.     _simplify($name);
  254.     $name =~ s!/+!::!g; #/
  255.     $name;
  256. }
  257.  
  258. # basic simplification of the POD name:
  259. # basename & strip extension
  260. sub simplify_name {
  261.     my ($str) = @_;
  262.     # remove all path components
  263.     $str =~ s:^.*/::s;
  264.     _simplify($str);
  265.     $str;
  266. }
  267.  
  268. # internal sub only
  269. sub _simplify {
  270.     # strip Perl's own extensions
  271.     $_[0] =~ s/\.(pod|pm|plx?)\z//i;
  272.     # strip meaningless extensions on Win32 and OS/2
  273.     $_[0] =~ s/\.(bat|exe|cmd)\z//i if($^O =~ /win|os2/i);
  274. }
  275.  
  276. 1;
  277.  
  278.