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