This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
remove perl525*delta.pod
[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 6use Config; # for config options in the makefile
83c03aa3 7use File::Spec::Functions qw(rel2abs no_upwards);
54310121 8use Getopt::Long; # for command-line parsing
9use Cwd;
776d31fe 10use Pod::Html 'anchorify';
54310121 11
54310121 12=head1 NAME
13
14installhtml - converts a collection of POD pages to HTML format.
15
16=head1 SYNOPSIS
17
555bd962
BG
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]
54310121 22
23=head1 DESCRIPTION
24
25I<installhtml> converts a collection of POD pages to a corresponding
4446c207
JK
26collection of HTML pages. This is used to convert the pod pages found in the
27perl distribution. (It is not intended as a general-purpose
28converter/installer of POD pages in HTML format. See L<Pod::Html>.)
54310121 29
30=head1 OPTIONS
31
32=over 4
33
34=item B<--help> help
35
36Displays the usage.
37
3e3baf6d
TB
38=item B<--podroot> POD search path base directory
39
40The base directory to search for all .pod and .pm files to be converted.
41Default is current directory.
42
54310121 43=item B<--podpath> POD search path
44
e05c04ea 45The list of directories to search for .pod and .pm files to be converted.
e1b7525f 46Default is 'podroot/.'.
54310121 47
3e3baf6d 48=item B<--recurse> recurse on subdirectories
54310121 49
3e3baf6d
TB
50Whether or not to convert all .pm and .pod files found in subdirectories
51too. Default is to not recurse.
54310121 52
53=item B<--htmldir> HTML destination directory
54
55The base directory which all HTML files will be written to. This should
56be a path relative to the filesystem, not the resulting URL.
57
58=item B<--htmlroot> URL base directory
59
60The base directory which all resulting HTML files will be visible at in
e1b7525f 61a URL. The default is '/'.
54310121 62
54310121 63=item B<--splithead> POD files to split on =head directive
64
af19f77d 65Comma-separated list of pod files to split by the =head directive. The
3e3baf6d
TB
66.pod suffix is optional. These files should have names specified
67relative to podroot.
54310121 68
69=item B<--splititem> POD files to split on =item directive
70
4446c207
JK
71Comma-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
73it invokes I<splitpod>, a separate program in the Perl 5 core distribution,
74to do the dirty work. As with --splithead, these files should have names
75specified relative to podroot.
3e3baf6d
TB
76
77=item B<--splitpod> Directory containing the splitpod program
78
e1b7525f 79The directory containing the splitpod program. The default is 'podroot/pod'.
54310121 80
47ec63d0
RGS
81=item B<--ignore> files to be ignored
82
83Comma-separated of files that shouldn't be installed, given relative
84to podroot.
85
54310121 86=item B<--verbose> verbose output
87
88Self-explanatory.
89
90=back
91
92=head1 EXAMPLE
93
94The following command-line is an example of the one we use to convert
95perl documentation:
96
97 ./installhtml --podpath=lib:ext:pod:vms \
84902520
TB
98 --podroot=/usr/src/perl \
99 --htmldir=/perl/nmanual \
100 --htmlroot=/perl/nmanual \
101 --splithead=pod/perlipc \
102 --splititem=pod/perlfunc \
84902520
TB
103 --recurse \
104 --verbose
54310121 105
106=head1 AUTHOR
107
108Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
109
54310121 110=cut
111
3d8876fc
MS
112my $usage;
113
54310121 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>
6c6fc3be 118 --ignore=<name>,...,<name> --verbose
54310121 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
47ec63d0 126 to the filesystem (\$podroot/html by default).
54310121 127 --htmlroot - http-server base directory from which all relative paths
128 in podpath stem (default is /).
54310121 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
e05c04ea 133 split each file into several smaller files at every occurrence
54310121 134 of a pod =head[1-6] directive.
135 --splititem - comma-separated list of .pod or .pm files to split using
136 splitpod.
3e3baf6d
TB
137 --splitpod - directory where the program splitpod can be found
138 (\$podroot/pod by default).
47ec63d0 139 --ignore - comma-separated list of files that shouldn't be installed.
54310121 140 --verbose - self-explanatory.
141
142END_OF_USAGE
143
6c6fc3be 144my (@podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
47ec63d0 145 @splititem, $splitpod, $verbose, $pod2html, @ignore);
3d8876fc 146
54310121 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
47ec63d0 157$verbose = 0; # whether or not to print debugging info
54310121 158
159$pod2html = "pod/pod2html";
160
3e3baf6d 161usage("") unless @ARGV;
54310121 162
c645ec3f 163# Overcome shell's p1,..,p8 limitation.
af1e27ef 164# See vms/descrip_mms.template -> descrip.mms for invocation.
c645ec3f
GS
165if ( $^O eq 'VMS' ) { @ARGV = split(/\s+/,$ARGV[0]); }
166
65b03822 167use vars qw( %Options );
3d8876fc 168
54310121 169# parse the command-line
65b03822 170my $result = GetOptions( \%Options, qw(
54310121 171 help
172 podpath=s
173 podroot=s
174 htmldir=s
175 htmlroot=s
47ec63d0 176 ignore=s
54310121 177 recurse!
178 splithead=s
179 splititem=s
180 splitpod=s
181 verbose
182));
183usage("invalid parameters") unless $result;
184parse_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.
3d8876fc 202my @splitdirs;
54310121 203
47ec63d0
RGS
204# split pods. It's important to do this before convert ANY pods because
205# it may affect some of the links
54310121 206@splitdirs = (); # files in these directories won't get an index
207split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
3e3baf6d 208split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
54310121 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;
3d8876fc 214foreach my $dir (@podpath) {
54310121 215 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
216}
217
218
219# now go through and create master indices for each pod we split
3d8876fc 220foreach my $dir (@splititem) {
54310121 221 print "creating index $htmldir/$dir.html\n" if $verbose;
222 create_index("$htmldir/$dir.html", "$htmldir/$dir");
223}
224
3d8876fc 225foreach my $dir (@splithead) {
776d31fe 226 (my $pod = $dir) =~ s,^.*/,,;
54310121 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;
3d8876fc 233 my $file = "$htmldir/$1";
54310121 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.
1ae6ead9 238 open(H, '<', "$file.html") ||
54310121 239 die "$0: error opening $file.html for input: $!\n";
240 $/ = "";
3d8876fc 241 my @data = ();
54310121 242 while (<H>) {
776d31fe
SH
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;
54310121 250 push @data, $_;
776d31fe 251 }
54310121 252 close(H);
253
47ec63d0 254 # now rewrite the file
1ae6ead9 255 open(H, '>', "$file.html") ||
54310121 256 die "$0: error opening $file.html for output: $!\n";
3d8876fc 257 print H "@data", "\n";
54310121 258 close(H);
259}
260
261##############################################################################
262
263
264sub usage {
265 warn "$0: @_\n" if @_;
266 die $usage;
267}
268
269
270sub parse_command_line {
65b03822 271 usage() if defined $Options{help};
272 $Options{help} = ""; # make -w shut up
54310121 273
274 # list of directories
65b03822 275 @podpath = split(":", $Options{podpath}) if defined $Options{podpath};
54310121 276
277 # lists of files
65b03822 278 @splithead = split(",", $Options{splithead}) if defined $Options{splithead};
279 @splititem = split(",", $Options{splititem}) if defined $Options{splititem};
54310121 280
65b03822 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};
54310121 285
65b03822 286 $recurse = $Options{recurse} if defined $Options{recurse};
287 $verbose = $Options{verbose} if defined $Options{verbose};
47ec63d0
RGS
288
289 @ignore = map "$podroot/$_", split(",", $Options{ignore}) if defined $Options{ignore};
54310121 290}
291
292
293sub create_index {
294 my($html, $dir) = @_;
df00c672 295 (my $pod = $dir) =~ s,^.*/,,;
54310121 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";
e0f13893 300 my @files = sort(grep(/\.html?$/, readdir(DIR)));
54310121 301 closedir(DIR);
302
1ae6ead9 303 open(HTML, '>', $html) ||
54310121 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";
e0f13893 309 foreach my $file (@files) {
54310121 310
e0f13893 311 my $filedata = do {
1ae6ead9 312 open(my $in, '<', "$dir/$file") ||
e0f13893
Z
313 die "$0: error opening $dir/$file for input: $!\n";
314 local $/ = undef;
315 <$in>;
316 };
54310121 317
318 # pull out the NAME section
e0f13893
Z
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
df00c672 324 my $url= "$pod/$file" ;
65b03822 325 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
df00c672 326 $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
29f227c9
BS
327 }
328
e0f13893
Z
329 print HTML qq(<DT><A HREF="$url">);
330 print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
54310121 331 }
332 print HTML "</DL>\n";
333
334 close(HTML);
335}
336
337
338sub 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,;
df00c672 347 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 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
361sub 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();
b1124ff0
AP
367 my $splitter = rel2abs("$splitpod/splitpod", $pwd);
368 my $perl = rel2abs($^X, $pwd);
3d8876fc 369 foreach my $pod (@splititem) {
54310121 370 # figure out the directory to split into
371 $pod =~ s,^([^/]*)$,/$1,;
df00c672 372 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 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";
3e3baf6d
TB
387 die "$splitter not found. Use '-splitpod dir' option.\n"
388 unless -f $splitter;
ab9905e6 389 system($perl, $splitter, "../$filename") &&
3e3baf6d 390 warn "$0: error running '$splitter ../$filename'"
e05c04ea 391 ." from $podroot/$dirname";
54310121 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#
402sub 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 $/ = "";
1ae6ead9 411 open(SPLITIN, '<', $pod) ||
54310121 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.
3d8876fc 429 my %heads = ();
54310121 430 foreach $i (0..$#poddata) {
776d31fe 431 $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
54310121 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
3d8876fc 437 my $dir = $pod;
54310121 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+(.*)/;
776d31fe 463 $file = "$dir/" . anchorify($section) . ".pod";
54310121 464
465 # create the new .pod file
466 print "\tcreating $poddir/$file\n" if $verbose;
1ae6ead9 467 open(SPLITOUT, '>', "$poddir/$file") ||
54310121 468 die "$0: error opening $poddir/$file for output: $!\n";
469 $poddata[$i] =~ s,L<([^<>]*)>,
776d31fe
SH
470 defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
471 ,ge;
54310121 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#
488sub installdir {
489 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
54310121 490
8de3880f
AP
491 my @dirlist; # directories to recurse on
492 my @podlist; # .pod files to install
493 my @pmlist; # .pm files to install
54310121 494
495 # should files in this directory get an index?
8de3880f 496 my $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
54310121 497
498 opendir(DIR, "$podroot/$dir")
499 || die "$0: error opening directory $podroot/$dir: $!\n";
500
83c03aa3
AP
501 while(readdir DIR) {
502 no_upwards($_) or next;
503 my $is_dir = -d "$podroot/$dir/$_";
504 next if $is_dir and not $recurse;
7e7b3f79
AP
505 my $target
506 = $is_dir ? \@dirlist
507 : s/\.pod$// ? \@podlist
508 : s/\.pm$// ? \@pmlist
509 : undef
510 ;
83c03aa3
AP
511 push @$target, "$dir/$_" if $target;
512 }
54310121 513
514 closedir(DIR);
515
83c03aa3
AP
516 if ($^O eq 'VMS') { s/\.dir$//i for @dirlist }
517
54310121 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
3d8876fc 524 foreach my $pod (@podlist) {
54310121 525 # check if we should ignore it.
47ec63d0
RGS
526 next if $pod =~ m(/t/); # comes from a test file
527 next if grep($_ eq "$pod.pod", @$ignore);
54310121 528
529 # check if a .pm files exists too
df00c672 530 if (grep($_ eq $pod, @pmlist)) {
e1b7525f
YO
531 print "$0: Warning both '$podroot/$pod.pod' and "
532 . "'$podroot/$pod.pm' exist, using pod\n";
54310121 533 push(@ignore, "$pod.pm");
534 }
535 runpod2html("$pod.pod", $doindex);
536 }
537
538 # install all the .pm files we found
3d8876fc 539 foreach my $pm (@pmlist) {
54310121 540 # check if we should ignore it.
47ec63d0 541 next if $pm =~ m(/t/); # comes from a test file
54310121 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#
553sub 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;
df00c672 573 Pod::Html::pod2html(
5a039dd3 574 "--htmldir=$htmldir",
54310121 575 "--htmlroot=$htmlroot",
576 "--podpath=".join(":", @podpath),
6c6fc3be 577 "--podroot=$podroot",
7c82de66 578 "--header",
54310121 579 ($doindex ? "--index" : "--noindex"),
580 "--" . ($recurse ? "" : "no") . "recurse",
54310121 581 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
582 die "$0: error running $pod2html: $!\n" if $?;
583}