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