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