This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
better document version check aspect of "use"
[perl5.git] / installhtml
1 #!./perl -Ilib -w
2
3 # This file should really be extracted from a .PL file
4
5 use strict;
6 use Config;             # for config options in the makefile
7 use File::Spec::Functions qw(rel2abs no_upwards);
8 use Getopt::Long;       # for command-line parsing
9 use Cwd;
10 use Pod::Html 'anchorify';
11
12 =head1 NAME
13
14 installhtml - converts a collection of POD pages to HTML format.
15
16 =head1 SYNOPSIS
17
18   installhtml  [--help] [--podpath=<name>:...:<name>] [--podroot=<name>]
19        [--htmldir=<name>] [--htmlroot=<name>]  [--norecurse] [--recurse]
20        [--splithead=<name>,...,<name>]   [--splititem=<name>,...,<name>]
21        [--ignore=<name>,...,<name>]  [--verbose]
22
23 =head1 DESCRIPTION
24
25 I<installhtml> converts a collection of POD pages to a corresponding
26 collection of HTML pages.  This is used to convert the pod pages found in the
27 perl distribution.  (It is not intended as a general-purpose
28 converter/installer of POD pages in HTML format.  See L<Pod::Html>.)
29
30 =head1 OPTIONS
31
32 =over 4
33
34 =item B<--help> help
35
36 Displays the usage.
37
38 =item B<--podroot> POD search path base directory
39
40 The base directory to search for all .pod and .pm files to be converted.
41 Default is current directory.
42
43 =item B<--podpath> POD search path
44
45 The list of directories to search for .pod and .pm files to be converted.
46 Default is 'podroot/.'.
47
48 =item B<--recurse> recurse on subdirectories
49
50 Whether or not to convert all .pm and .pod files found in subdirectories
51 too.  Default is to not recurse.
52
53 =item B<--htmldir> HTML destination directory
54
55 The base directory which all HTML files will be written to.  This should
56 be a path relative to the filesystem, not the resulting URL.
57
58 =item B<--htmlroot> URL base directory
59
60 The base directory which all resulting HTML files will be visible at in
61 a URL.  The default is '/'.
62
63 =item B<--splithead> POD files to split on =head directive
64
65 Comma-separated list of pod files to split by the =head directive.  The
66 .pod suffix is optional. These files should have names specified
67 relative to podroot.
68
69 =item B<--splititem> POD files to split on =item directive
70
71 Comma-separated list of all pod files to split by the =item directive.  The
72 .pod suffix is optional.  I<installhtml> does not do the actual split, rather
73 it invokes I<splitpod>, a separate program in the Perl 5 core distribution,
74 to do the dirty work.  As with --splithead, these files should have names
75 specified relative to podroot.
76
77 =item B<--splitpod> Directory containing the splitpod program
78
79 The directory containing the splitpod program. The default is 'podroot/pod'.
80
81 =item B<--ignore> files to be ignored
82
83 Comma-separated of files that shouldn't be installed, given relative
84 to podroot.
85
86 =item B<--verbose> verbose output
87
88 Self-explanatory.
89
90 =back
91
92 =head1 EXAMPLE
93
94 The following command-line is an example of the one we use to convert
95 perl documentation:
96
97     ./installhtml --podpath=lib:ext:pod:vms   \
98                         --podroot=/usr/src/perl     \
99                         --htmldir=/perl/nmanual     \
100                         --htmlroot=/perl/nmanual    \
101                         --splithead=pod/perlipc     \
102                         --splititem=pod/perlfunc    \
103                         --recurse \
104                         --verbose
105
106 =head1 AUTHOR
107
108 Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
109
110 =cut
111
112 my $usage;
113
114 $usage =<<END_OF_USAGE;
115 Usage: $0 --help --podpath=<name>:...:<name> --podroot=<name>
116          --htmldir=<name> --htmlroot=<name> --norecurse --recurse
117          --splithead=<name>,...,<name> --splititem=<name>,...,<name>
118          --ignore=<name>,...,<name> --verbose
119
120     --help      - this message
121     --podpath   - colon-separated list of directories containing .pod and
122                   .pm files to be converted (. by default).
123     --podroot   - filesystem base directory from which all relative paths in
124                   podpath stem (default is .).
125     --htmldir   - directory to store resulting html files in relative
126                   to the filesystem (\$podroot/html by default).
127     --htmlroot  - http-server base directory from which all relative paths
128                   in podpath stem (default is /).
129     --norecurse - don't recurse on those subdirectories listed in podpath.
130                   (default behavior).
131     --recurse   - recurse on those subdirectories listed in podpath
132     --splithead - comma-separated list of .pod or .pm files to split.  will
133                   split each file into several smaller files at every occurrence
134                   of a pod =head[1-6] directive.
135     --splititem - comma-separated list of .pod or .pm files to split using
136                   splitpod.
137     --splitpod  - directory where the program splitpod can be found
138                   (\$podroot/pod by default).
139     --ignore    - comma-separated list of files that shouldn't be installed.
140     --verbose   - self-explanatory.
141
142 END_OF_USAGE
143
144 my (@podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
145     @splititem, $splitpod, $verbose, $pod2html, @ignore);
146
147 @podpath = ( "." );     # colon-separated list of directories containing .pod
148                         # and .pm files to be converted.
149 $podroot = ".";         # assume the pods we want are here
150 $htmldir = "";          # nothing for now...
151 $htmlroot = "/";        # default value
152 $recurse = 0;           # default behavior
153 @splithead = ();        # don't split any files by default
154 @splititem = ();        # don't split any files by default
155 $splitpod = "";         # nothing for now.
156
157 $verbose = 0;           # whether or not to print debugging info
158
159 $pod2html = "pod/pod2html";
160
161 usage("") unless @ARGV;
162
163 # Overcome shell's p1,..,p8 limitation.  
164 # See vms/descrip_mms.template -> descrip.mms for invocation.
165 if ( $^O eq 'VMS' ) { @ARGV = split(/\s+/,$ARGV[0]); }
166
167 our %Options;
168
169 # parse the command-line
170 my $result = GetOptions( \%Options, qw(
171         help
172         podpath=s
173         podroot=s
174         htmldir=s
175         htmlroot=s
176         ignore=s
177         recurse!
178         splithead=s
179         splititem=s
180         splitpod=s
181         verbose
182 ));
183 usage("invalid parameters") unless $result;
184 parse_command_line();
185
186
187 # set these variables to appropriate values if the user didn't specify
188 #  values for them.
189 $htmldir = "$htmlroot/html" unless $htmldir;
190 $splitpod = "$podroot/pod" unless $splitpod;
191
192
193 # make sure that the destination directory exists
194 (mkdir($htmldir, 0755) ||
195         die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
196
197
198 # the following array will eventually contain files that are to be
199 # ignored in the conversion process.  these are files that have been
200 # process by splititem or splithead and should not be converted as a
201 # result.
202 my @splitdirs;
203
204 # split pods. It's important to do this before convert ANY pods because
205 # it may affect some of the links
206 @splitdirs = ();    # files in these directories won't get an index
207 split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
208 split_on_item($podroot,           \@splitdirs, \@ignore, @splititem);
209
210
211 # convert the pod pages found in @poddirs
212 #warn "converting files\n" if $verbose;
213 #warn "\@ignore\t= @ignore\n" if $verbose;
214 foreach my $dir (@podpath) {
215     installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
216 }
217
218
219 # now go through and create master indices for each pod we split
220 foreach my $dir (@splititem) {
221     print "creating index $htmldir/$dir.html\n" if $verbose;
222     create_index("$htmldir/$dir.html", "$htmldir/$dir");
223 }
224
225 foreach my $dir (@splithead) {
226     (my $pod = $dir) =~ s,^.*/,,;
227     $dir .= ".pod" unless $dir =~ /(\.pod|\.pm)$/;
228     # let pod2html create the file
229     runpod2html($dir, 1);
230
231     # now go through and truncate after the index
232     $dir =~ /^(.*?)(\.pod|\.pm)?$/sm;
233     my $file = "$htmldir/$1";
234     print "creating index $file.html\n" if $verbose;
235
236     # read in everything until what would have been the first =head
237     # directive, patching the index as we go.
238     open(H, '<', "$file.html") ||
239         die "$0: error opening $file.html for input: $!\n";
240     $/ = "";
241     my @data = ();
242     while (<H>) {
243         last if /name="name"/i;
244         $_ =~ s{href="#(.*)">}{
245             my $url = "$pod/$1.html" ;
246             $url = Pod::Html::relativize_url( $url, "$file.html" )
247             if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' );
248             "href=\"$url\">" ;
249         }egi;
250         push @data, $_;
251     }
252     close(H);
253
254     # now rewrite the file
255     open(H, '>', "$file.html") ||
256         die "$0: error opening $file.html for output: $!\n";
257     print H "@data", "\n";
258     close(H);
259 }
260
261 ##############################################################################
262
263
264 sub usage {
265     warn "$0: @_\n" if @_;
266     die $usage;
267 }
268
269
270 sub parse_command_line {
271     usage() if defined $Options{help};
272     $Options{help} = "";                    # make -w shut up
273
274     # list of directories
275     @podpath   = split(":", $Options{podpath}) if defined $Options{podpath};
276
277     # lists of files
278     @splithead = split(",", $Options{splithead}) if defined $Options{splithead};
279     @splititem = split(",", $Options{splititem}) if defined $Options{splititem};
280
281     $htmldir  = $Options{htmldir}           if defined $Options{htmldir};
282     $htmlroot = $Options{htmlroot}          if defined $Options{htmlroot};
283     $podroot  = $Options{podroot}           if defined $Options{podroot};
284     $splitpod = $Options{splitpod}          if defined $Options{splitpod};
285
286     $recurse  = $Options{recurse}           if defined $Options{recurse};
287     $verbose  = $Options{verbose}           if defined $Options{verbose};
288
289     @ignore = map "$podroot/$_", split(",", $Options{ignore}) if defined $Options{ignore};
290 }
291
292
293 sub create_index {
294     my($html, $dir) = @_;
295     (my $pod = $dir) =~ s,^.*/,,;
296
297     # get the list of .html files in this directory
298     opendir(DIR, $dir) ||
299         die "$0: error opening directory $dir for reading: $!\n";
300     my @files = sort(grep(/\.html?$/, readdir(DIR)));
301     closedir(DIR);
302
303     open(HTML, '>', $html) ||
304         die "$0: error opening $html for output: $!\n";
305
306     # for each .html file in the directory, extract the index
307     #   embedded in the file and throw it into the big index.
308     print HTML "<DL COMPACT>\n";
309     foreach my $file (@files) {
310
311         my $filedata = do {
312             open(my $in, '<', "$dir/$file") ||
313                 die "$0: error opening $dir/$file for input: $!\n";
314             local $/ = undef;
315             <$in>;
316         };
317
318         # pull out the NAME section
319         my($lcp1, $lcp2) =
320             ($filedata =~
321                 m#<h1 id="NAME">NAME</h1>\s*<p>\s*(\S+)\s+-\s+(\S.*?\S)</p>#);
322         defined $lcp1 or die "$0: can't find NAME section in $dir/$file\n";
323
324         my $url= "$pod/$file" ;
325         if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
326             $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
327         }
328
329         print HTML qq(<DT><A HREF="$url">);
330         print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
331     }
332     print HTML "</DL>\n";
333
334     close(HTML);
335 }
336
337
338 sub split_on_head {
339     my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
340     my($pod, $dirname, $filename);
341
342     # split the files specified in @splithead on =head[1-6] pod directives
343     print "splitting files by head.\n" if $verbose && $#splithead >= 0;
344     foreach $pod (@splithead) {
345         # figure out the directory name and filename
346         $pod      =~ s,^([^/]*)$,/$1,;
347         $pod      =~ m,(.*)/(.*?)(\.pod)?$,;
348         $dirname  = $1;
349         $filename = "$2.pod";
350
351         # since we are splitting this file it shouldn't be converted.
352         push(@$ignore, "$podroot/$dirname/$filename");
353
354         # split the pod
355         splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
356             $splitdirs);
357     }
358 }
359
360
361 sub split_on_item {
362     my($podroot, $splitdirs, $ignore, @splititem) = @_;
363     my($pwd, $dirname, $filename);
364
365     print "splitting files by item.\n" if $verbose && $#splititem >= 0;
366     $pwd = getcwd();
367     my $splitter = rel2abs("$splitpod/splitpod", $pwd);
368     my $perl = rel2abs($^X, $pwd);
369     foreach my $pod (@splititem) {
370         # figure out the directory to split into
371         $pod      =~ s,^([^/]*)$,/$1,;
372         $pod      =~ m,(.*)/(.*?)(\.pod)?$,;
373         $dirname  = "$1/$2";
374         $filename = "$2.pod";
375
376         # since we are splitting this file it shouldn't be converted.
377         push(@$ignore, "$podroot/$dirname.pod");
378
379         # split the pod
380         push(@$splitdirs, "$podroot/$dirname");
381         if (! -d "$podroot/$dirname") {
382             mkdir("$podroot/$dirname", 0755) ||
383                     die "$0: error creating directory $podroot/$dirname: $!\n";
384         }
385         chdir("$podroot/$dirname") ||
386             die "$0: error changing to directory $podroot/$dirname: $!\n";
387         die "$splitter not found. Use '-splitpod dir' option.\n"
388             unless -f $splitter;
389         system($perl, $splitter, "../$filename") &&
390             warn "$0: error running '$splitter ../$filename'"
391                  ." from $podroot/$dirname";
392     }
393     chdir($pwd);
394 }
395
396
397 #
398 # splitpod - splits a .pod file into several smaller .pod files
399 #  where a new file is started each time a =head[1-6] pod directive
400 #  is encountered in the input file.
401 #
402 sub splitpod {
403     my($pod, $poddir, $htmldir, $splitdirs) = @_;
404     my(@poddata, @filedata, @heads);
405     my($file, $i, $j, $prevsec, $section, $nextsec);
406
407     print "splitting $pod\n" if $verbose;
408
409     # read the file in paragraphs
410     $/ = "";
411     open(SPLITIN, '<', $pod) ||
412         die "$0: error opening $pod for input: $!\n";
413     @filedata = <SPLITIN>;
414     close(SPLITIN) ||
415         die "$0: error closing $pod: $!\n";
416
417     # restore the file internally by =head[1-6] sections
418     @poddata = ();
419     for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
420         $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
421         if ($j >= 0) { 
422             $poddata[$j]  = "" unless defined $poddata[$j];
423             $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
424         }
425     }
426
427     # create list of =head[1-6] sections so that we can rewrite
428     #  L<> links as necessary.
429     my %heads = ();
430     foreach $i (0..$#poddata) {
431         $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
432     }
433
434     # create a directory of a similar name and store all the
435     #  files in there
436     $pod =~ s,.*/(.*),$1,;      # get the last part of the name
437     my $dir = $pod;
438     $dir =~ s/\.pod//g;
439     push(@$splitdirs, "$poddir/$dir");
440     mkdir("$poddir/$dir", 0755) ||
441         die "$0: could not create directory $poddir/$dir: $!\n"
442         unless -d "$poddir/$dir";
443
444     $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
445     $section    = "";
446     $nextsec    = $1;
447
448     # for each section of the file create a separate pod file
449     for ($i = 0; $i <= $#poddata; $i++) {
450         # determine the "prev" and "next" links
451         $prevsec = $section;
452         $section = $nextsec;
453         if ($i < $#poddata) {
454             $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
455             $nextsec       = $1;
456         } else {
457             $nextsec = "";
458         }
459
460         # determine an appropriate filename (this must correspond with
461         #  what pod2html will try and guess)
462         # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
463         $file = "$dir/" . anchorify($section) . ".pod";
464
465         # create the new .pod file
466         print "\tcreating $poddir/$file\n" if $verbose;
467         open(SPLITOUT, '>', "$poddir/$file") ||
468             die "$0: error opening $poddir/$file for output: $!\n";
469         $poddata[$i] =~ s,L<([^<>]*)>,
470                         defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
471                      ,ge;
472         print SPLITOUT $poddata[$i]."\n\n";
473         print SPLITOUT "=over 4\n\n";
474         print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
475         print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
476         print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
477         print SPLITOUT "=back\n\n";
478         close(SPLITOUT) ||
479             die "$0: error closing $poddir/$file: $!\n";
480     }
481 }
482
483
484 #
485 # installdir - takes care of converting the .pod and .pm files in the
486 #  current directory to .html files and then installing those.
487 #
488 sub installdir {
489     my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
490
491     my @dirlist; # directories to recurse on
492     my @podlist; # .pod files to install
493     my @pmlist;  # .pm files to install
494
495     # should files in this directory get an index?
496     my $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
497
498     opendir(DIR, "$podroot/$dir")
499         || die "$0: error opening directory $podroot/$dir: $!\n";
500
501     while(readdir DIR) {
502         no_upwards($_) or next;
503         my $is_dir = -d "$podroot/$dir/$_";
504         next if $is_dir and not $recurse;
505         my $target
506             = $is_dir    ? \@dirlist
507             : s/\.pod$// ? \@podlist
508             : s/\.pm$//  ? \@pmlist
509             : undef
510             ;
511         push @$target, "$dir/$_" if $target;
512     }
513
514     closedir(DIR);
515
516     if ($^O eq 'VMS') { s/\.dir$//i for @dirlist }
517
518     # recurse on all subdirectories we kept track of
519     foreach $dir (@dirlist) {
520         installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
521     }
522
523     # install all the pods we found
524     foreach my $pod (@podlist) {
525         # check if we should ignore it.
526         next if $pod =~ m(/t/); # comes from a test file
527         next if grep($_ eq "$pod.pod", @$ignore);
528
529         # check if a .pm files exists too
530         if (grep($_ eq $pod, @pmlist)) {
531             print  "$0: Warning both '$podroot/$pod.pod' and "
532                 . "'$podroot/$pod.pm' exist, using pod\n";
533             push(@ignore, "$pod.pm");
534         }
535         runpod2html("$pod.pod", $doindex);
536     }
537
538     # install all the .pm files we found
539     foreach my $pm (@pmlist) {
540         # check if we should ignore it.
541         next if $pm =~ m(/t/); # comes from a test file
542         next if grep($_ eq "$pm.pm", @ignore);
543
544         runpod2html("$pm.pm", $doindex);
545     }
546 }
547
548
549 #
550 # runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
551 #  file.
552 #
553 sub runpod2html {
554     my($pod, $doindex) = @_;
555     my($html, $i, $dir, @dirs);
556
557     $html = $pod;
558     $html =~ s/\.(pod|pm)$/.html/g;
559
560     # make sure the destination directories exist
561     @dirs = split("/", $html);
562     $dir  = "$htmldir/";
563     for ($i = 0; $i < $#dirs; $i++) {
564         if (! -d "$dir$dirs[$i]") {
565             mkdir("$dir$dirs[$i]", 0755) ||
566                 die "$0: error creating directory $dir$dirs[$i]: $!\n";
567         }
568         $dir .= "$dirs[$i]/";
569     }
570
571     # invoke pod2html
572     print "$podroot/$pod => $htmldir/$html\n" if $verbose;
573     Pod::Html::pod2html(
574         "--htmldir=$htmldir",
575         "--htmlroot=$htmlroot",
576         "--podpath=".join(":", @podpath),
577         "--podroot=$podroot",
578         "--header",
579         ($doindex ? "--index" : "--noindex"),
580         "--" . ($recurse ? "" : "no") . "recurse",
581         "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
582     die "$0: error running $pod2html: $!\n" if $?;
583 }