This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Start rationalizing Unix-to-VMS file spec conversion code.
[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          [--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     my(@files, @filedata, @index, $file);
295     my($lcp1,$lcp2);
296
297
298     # get the list of .html files in this directory
299     opendir(DIR, $dir) ||
300         die "$0: error opening directory $dir for reading: $!\n";
301     @files = sort(grep(/\.html?$/, readdir(DIR)));
302     closedir(DIR);
303
304     open(HTML, ">$html") ||
305         die "$0: error opening $html for output: $!\n";
306
307     # for each .html file in the directory, extract the index
308     #   embedded in the file and throw it into the big index.
309     print HTML "<DL COMPACT>\n";
310     foreach $file (@files) {
311         $/ = "";
312
313         open(IN, "<$dir/$file") ||
314             die "$0: error opening $dir/$file for input: $!\n";
315         @filedata = <IN>;
316         close(IN);
317
318         # pull out the NAME section
319         my $name;
320         ($name) = grep(/name="name"/i, @filedata);
321         ($lcp1,$lcp2) = ($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,smi);
322         if (defined $lcp1 and $lcp1 =~ m,^<P>$,i) { # Uninteresting.  Try again.
323             ($lcp1,$lcp2) = ($name =~ m,/H1>\s<P>\s*(\S+)\s[\s-]*(.*?)\s*$,smi);
324         }
325         my $url= "$pod/$file" ;
326         if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
327             $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
328         }
329
330         if (defined $lcp1) {
331             print HTML qq(<DT><A HREF="$url">);
332             print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
333         }
334
335         next;
336
337         @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
338                     @filedata);
339         for (@index) {
340             s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
341             s,#,$dir/$file#,g;
342             print HTML "$_\n<P><HR><P>\n";
343         }
344     }
345     print HTML "</DL>\n";
346
347     close(HTML);
348 }
349
350
351 sub split_on_head {
352     my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
353     my($pod, $dirname, $filename);
354
355     # split the files specified in @splithead on =head[1-6] pod directives
356     print "splitting files by head.\n" if $verbose && $#splithead >= 0;
357     foreach $pod (@splithead) {
358         # figure out the directory name and filename
359         $pod      =~ s,^([^/]*)$,/$1,;
360         $pod      =~ m,(.*)/(.*?)(\.pod)?$,;
361         $dirname  = $1;
362         $filename = "$2.pod";
363
364         # since we are splitting this file it shouldn't be converted.
365         push(@$ignore, "$podroot/$dirname/$filename");
366
367         # split the pod
368         splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
369             $splitdirs);
370     }
371 }
372
373
374 sub split_on_item {
375     my($podroot, $splitdirs, $ignore, @splititem) = @_;
376     my($pwd, $dirname, $filename);
377
378     print "splitting files by item.\n" if $verbose && $#splititem >= 0;
379     $pwd = getcwd();
380     my $splitter = File::Spec->rel2abs("$splitpod/splitpod", $pwd);
381     my $perl = File::Spec->rel2abs($^X, $pwd);
382     foreach my $pod (@splititem) {
383         # figure out the directory to split into
384         $pod      =~ s,^([^/]*)$,/$1,;
385         $pod      =~ m,(.*)/(.*?)(\.pod)?$,;
386         $dirname  = "$1/$2";
387         $filename = "$2.pod";
388
389         # since we are splitting this file it shouldn't be converted.
390         push(@$ignore, "$podroot/$dirname.pod");
391
392         # split the pod
393         push(@$splitdirs, "$podroot/$dirname");
394         if (! -d "$podroot/$dirname") {
395             mkdir("$podroot/$dirname", 0755) ||
396                     die "$0: error creating directory $podroot/$dirname: $!\n";
397         }
398         chdir("$podroot/$dirname") ||
399             die "$0: error changing to directory $podroot/$dirname: $!\n";
400         die "$splitter not found. Use '-splitpod dir' option.\n"
401             unless -f $splitter;
402         system($perl, $splitter, "../$filename") &&
403             warn "$0: error running '$splitter ../$filename'"
404                  ." from $podroot/$dirname";
405     }
406     chdir($pwd);
407 }
408
409
410 #
411 # splitpod - splits a .pod file into several smaller .pod files
412 #  where a new file is started each time a =head[1-6] pod directive
413 #  is encountered in the input file.
414 #
415 sub splitpod {
416     my($pod, $poddir, $htmldir, $splitdirs) = @_;
417     my(@poddata, @filedata, @heads);
418     my($file, $i, $j, $prevsec, $section, $nextsec);
419
420     print "splitting $pod\n" if $verbose;
421
422     # read the file in paragraphs
423     $/ = "";
424     open(SPLITIN, "<$pod") ||
425         die "$0: error opening $pod for input: $!\n";
426     @filedata = <SPLITIN>;
427     close(SPLITIN) ||
428         die "$0: error closing $pod: $!\n";
429
430     # restore the file internally by =head[1-6] sections
431     @poddata = ();
432     for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
433         $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
434         if ($j >= 0) { 
435             $poddata[$j]  = "" unless defined $poddata[$j];
436             $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
437         }
438     }
439
440     # create list of =head[1-6] sections so that we can rewrite
441     #  L<> links as necessary.
442     my %heads = ();
443     foreach $i (0..$#poddata) {
444         $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
445     }
446
447     # create a directory of a similar name and store all the
448     #  files in there
449     $pod =~ s,.*/(.*),$1,;      # get the last part of the name
450     my $dir = $pod;
451     $dir =~ s/\.pod//g;
452     push(@$splitdirs, "$poddir/$dir");
453     mkdir("$poddir/$dir", 0755) ||
454         die "$0: could not create directory $poddir/$dir: $!\n"
455         unless -d "$poddir/$dir";
456
457     $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
458     $section    = "";
459     $nextsec    = $1;
460
461     # for each section of the file create a separate pod file
462     for ($i = 0; $i <= $#poddata; $i++) {
463         # determine the "prev" and "next" links
464         $prevsec = $section;
465         $section = $nextsec;
466         if ($i < $#poddata) {
467             $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
468             $nextsec       = $1;
469         } else {
470             $nextsec = "";
471         }
472
473         # determine an appropriate filename (this must correspond with
474         #  what pod2html will try and guess)
475         # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
476         $file = "$dir/" . anchorify($section) . ".pod";
477
478         # create the new .pod file
479         print "\tcreating $poddir/$file\n" if $verbose;
480         open(SPLITOUT, ">$poddir/$file") ||
481             die "$0: error opening $poddir/$file for output: $!\n";
482         $poddata[$i] =~ s,L<([^<>]*)>,
483                         defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
484                      ,ge;
485         print SPLITOUT $poddata[$i]."\n\n";
486         print SPLITOUT "=over 4\n\n";
487         print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
488         print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
489         print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
490         print SPLITOUT "=back\n\n";
491         close(SPLITOUT) ||
492             die "$0: error closing $poddir/$file: $!\n";
493     }
494 }
495
496
497 #
498 # installdir - takes care of converting the .pod and .pm files in the
499 #  current directory to .html files and then installing those.
500 #
501 sub installdir {
502     my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
503     my(@dirlist, @podlist, @pmlist, $doindex);
504
505     @dirlist = ();      # directories to recurse on
506     @podlist = ();      # .pod files to install
507     @pmlist  = ();      # .pm files to install
508
509     # should files in this directory get an index?
510     $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
511
512     opendir(DIR, "$podroot/$dir")
513         || die "$0: error opening directory $podroot/$dir: $!\n";
514
515     # find the directories to recurse on
516     @dirlist = map { if ($^O eq 'VMS') {/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}}
517         grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
518     rewinddir(DIR);
519
520     # find all the .pod files within the directory
521     @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
522         grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
523     rewinddir(DIR);
524
525     # find all the .pm files within the directory
526     @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
527         grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
528
529     closedir(DIR);
530
531     # recurse on all subdirectories we kept track of
532     foreach $dir (@dirlist) {
533         installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
534     }
535
536     # install all the pods we found
537     foreach my $pod (@podlist) {
538         # check if we should ignore it.
539         next if $pod =~ m(/t/); # comes from a test file
540         next if grep($_ eq "$pod.pod", @$ignore);
541
542         # check if a .pm files exists too
543         if (grep($_ eq $pod, @pmlist)) {
544             print  "$0: Warning both '$podroot/$pod.pod' and "
545                 . "'$podroot/$pod.pm' exist, using pod\n";
546             push(@ignore, "$pod.pm");
547         }
548         runpod2html("$pod.pod", $doindex);
549     }
550
551     # install all the .pm files we found
552     foreach my $pm (@pmlist) {
553         # check if we should ignore it.
554         next if $pm =~ m(/t/); # comes from a test file
555         next if grep($_ eq "$pm.pm", @ignore);
556
557         runpod2html("$pm.pm", $doindex);
558     }
559 }
560
561
562 #
563 # runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
564 #  file.
565 #
566 sub runpod2html {
567     my($pod, $doindex) = @_;
568     my($html, $i, $dir, @dirs);
569
570     $html = $pod;
571     $html =~ s/\.(pod|pm)$/.html/g;
572
573     # make sure the destination directories exist
574     @dirs = split("/", $html);
575     $dir  = "$htmldir/";
576     for ($i = 0; $i < $#dirs; $i++) {
577         if (! -d "$dir$dirs[$i]") {
578             mkdir("$dir$dirs[$i]", 0755) ||
579                 die "$0: error creating directory $dir$dirs[$i]: $!\n";
580         }
581         $dir .= "$dirs[$i]/";
582     }
583
584     # invoke pod2html
585     print "$podroot/$pod => $htmldir/$html\n" if $verbose;
586     Pod::Html::pod2html(
587         "--htmldir=$htmldir",
588         "--htmlroot=$htmlroot",
589         "--podpath=".join(":", @podpath),
590         "--podroot=$podroot",
591         "--header",
592         ($doindex ? "--index" : "--noindex"),
593         "--" . ($recurse ? "" : "no") . "recurse",
594         "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
595     die "$0: error running $pod2html: $!\n" if $?;
596 }