This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Porting/Maintainers.pl - Consistent quotes
[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
26collection of HTML pages. This is primarily used to convert the pod
27pages found in the perl distribution.
28
29=head1 OPTIONS
30
31=over 4
32
33=item B<--help> help
34
35Displays the usage.
36
3e3baf6d
TB
37=item B<--podroot> POD search path base directory
38
39The base directory to search for all .pod and .pm files to be converted.
40Default is current directory.
41
54310121 42=item B<--podpath> POD search path
43
e05c04ea 44The list of directories to search for .pod and .pm files to be converted.
e1b7525f 45Default is 'podroot/.'.
54310121 46
3e3baf6d 47=item B<--recurse> recurse on subdirectories
54310121 48
3e3baf6d
TB
49Whether or not to convert all .pm and .pod files found in subdirectories
50too. Default is to not recurse.
54310121 51
52=item B<--htmldir> HTML destination directory
53
54The base directory which all HTML files will be written to. This should
55be a path relative to the filesystem, not the resulting URL.
56
57=item B<--htmlroot> URL base directory
58
59The base directory which all resulting HTML files will be visible at in
e1b7525f 60a URL. The default is '/'.
54310121 61
54310121 62=item B<--splithead> POD files to split on =head directive
63
af19f77d 64Comma-separated list of pod files to split by the =head directive. The
3e3baf6d
TB
65.pod suffix is optional. These files should have names specified
66relative to podroot.
54310121 67
68=item B<--splititem> POD files to split on =item directive
69
af19f77d 70Comma-separated list of all pod files to split by the =item directive.
3e3baf6d
TB
71The .pod suffix is optional. I<installhtml> does not do the actual
72split, 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
e1b7525f 77The directory containing the splitpod program. The default is 'podroot/pod'.
54310121 78
47ec63d0
RGS
79=item B<--ignore> files to be ignored
80
81Comma-separated of files that shouldn't be installed, given relative
82to podroot.
83
54310121 84=item B<--verbose> verbose output
85
86Self-explanatory.
87
88=back
89
90=head1 EXAMPLE
91
92The following command-line is an example of the one we use to convert
93perl documentation:
94
95 ./installhtml --podpath=lib:ext:pod:vms \
84902520
TB
96 --podroot=/usr/src/perl \
97 --htmldir=/perl/nmanual \
98 --htmlroot=/perl/nmanual \
99 --splithead=pod/perlipc \
100 --splititem=pod/perlfunc \
84902520
TB
101 --recurse \
102 --verbose
54310121 103
104=head1 AUTHOR
105
106Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
107
54310121 108=cut
109
3d8876fc
MS
110my $usage;
111
54310121 112$usage =<<END_OF_USAGE;
113Usage: $0 --help --podpath=<name>:...:<name> --podroot=<name>
114 --htmldir=<name> --htmlroot=<name> --norecurse --recurse
115 --splithead=<name>,...,<name> --splititem=<name>,...,<name>
6c6fc3be 116 --ignore=<name>,...,<name> --verbose
54310121 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
47ec63d0 124 to the filesystem (\$podroot/html by default).
54310121 125 --htmlroot - http-server base directory from which all relative paths
126 in podpath stem (default is /).
54310121 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
e05c04ea 131 split each file into several smaller files at every occurrence
54310121 132 of a pod =head[1-6] directive.
133 --splititem - comma-separated list of .pod or .pm files to split using
134 splitpod.
3e3baf6d
TB
135 --splitpod - directory where the program splitpod can be found
136 (\$podroot/pod by default).
47ec63d0 137 --ignore - comma-separated list of files that shouldn't be installed.
54310121 138 --verbose - self-explanatory.
139
140END_OF_USAGE
141
6c6fc3be 142my (@podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
47ec63d0 143 @splititem, $splitpod, $verbose, $pod2html, @ignore);
3d8876fc 144
54310121 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
47ec63d0 155$verbose = 0; # whether or not to print debugging info
54310121 156
157$pod2html = "pod/pod2html";
158
3e3baf6d 159usage("") unless @ARGV;
54310121 160
c645ec3f 161# Overcome shell's p1,..,p8 limitation.
af1e27ef 162# See vms/descrip_mms.template -> descrip.mms for invocation.
c645ec3f
GS
163if ( $^O eq 'VMS' ) { @ARGV = split(/\s+/,$ARGV[0]); }
164
65b03822 165use vars qw( %Options );
3d8876fc 166
54310121 167# parse the command-line
65b03822 168my $result = GetOptions( \%Options, qw(
54310121 169 help
170 podpath=s
171 podroot=s
172 htmldir=s
173 htmlroot=s
47ec63d0 174 ignore=s
54310121 175 recurse!
176 splithead=s
177 splititem=s
178 splitpod=s
179 verbose
180));
181usage("invalid parameters") unless $result;
182parse_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.
3d8876fc 200my @splitdirs;
54310121 201
47ec63d0
RGS
202# split pods. It's important to do this before convert ANY pods because
203# it may affect some of the links
54310121 204@splitdirs = (); # files in these directories won't get an index
205split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
3e3baf6d 206split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
54310121 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;
3d8876fc 212foreach my $dir (@podpath) {
54310121 213 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
214}
215
216
217# now go through and create master indices for each pod we split
3d8876fc 218foreach my $dir (@splititem) {
54310121 219 print "creating index $htmldir/$dir.html\n" if $verbose;
220 create_index("$htmldir/$dir.html", "$htmldir/$dir");
221}
222
3d8876fc 223foreach my $dir (@splithead) {
776d31fe 224 (my $pod = $dir) =~ s,^.*/,,;
54310121 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;
3d8876fc 231 my $file = "$htmldir/$1";
54310121 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 $/ = "";
3d8876fc 239 my @data = ();
54310121 240 while (<H>) {
776d31fe
SH
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;
54310121 248 push @data, $_;
776d31fe 249 }
54310121 250 close(H);
251
47ec63d0 252 # now rewrite the file
54310121 253 open(H, ">$file.html") ||
254 die "$0: error opening $file.html for output: $!\n";
3d8876fc 255 print H "@data", "\n";
54310121 256 close(H);
257}
258
259##############################################################################
260
261
262sub usage {
263 warn "$0: @_\n" if @_;
264 die $usage;
265}
266
267
268sub parse_command_line {
65b03822 269 usage() if defined $Options{help};
270 $Options{help} = ""; # make -w shut up
54310121 271
272 # list of directories
65b03822 273 @podpath = split(":", $Options{podpath}) if defined $Options{podpath};
54310121 274
275 # lists of files
65b03822 276 @splithead = split(",", $Options{splithead}) if defined $Options{splithead};
277 @splititem = split(",", $Options{splititem}) if defined $Options{splititem};
54310121 278
65b03822 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};
54310121 283
65b03822 284 $recurse = $Options{recurse} if defined $Options{recurse};
285 $verbose = $Options{verbose} if defined $Options{verbose};
47ec63d0
RGS
286
287 @ignore = map "$podroot/$_", split(",", $Options{ignore}) if defined $Options{ignore};
54310121 288}
289
290
291sub create_index {
292 my($html, $dir) = @_;
df00c672 293 (my $pod = $dir) =~ s,^.*/,,;
54310121 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";
e0f13893 298 my @files = sort(grep(/\.html?$/, readdir(DIR)));
54310121 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";
e0f13893 307 foreach my $file (@files) {
54310121 308
e0f13893
Z
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 };
54310121 315
316 # pull out the NAME section
e0f13893
Z
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
df00c672 322 my $url= "$pod/$file" ;
65b03822 323 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
df00c672 324 $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
29f227c9
BS
325 }
326
e0f13893
Z
327 print HTML qq(<DT><A HREF="$url">);
328 print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
54310121 329 }
330 print HTML "</DL>\n";
331
332 close(HTML);
333}
334
335
336sub 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,;
df00c672 345 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 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
359sub 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();
b1124ff0
AP
365 my $splitter = rel2abs("$splitpod/splitpod", $pwd);
366 my $perl = rel2abs($^X, $pwd);
3d8876fc 367 foreach my $pod (@splititem) {
54310121 368 # figure out the directory to split into
369 $pod =~ s,^([^/]*)$,/$1,;
df00c672 370 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 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";
3e3baf6d
TB
385 die "$splitter not found. Use '-splitpod dir' option.\n"
386 unless -f $splitter;
ab9905e6 387 system($perl, $splitter, "../$filename") &&
3e3baf6d 388 warn "$0: error running '$splitter ../$filename'"
e05c04ea 389 ." from $podroot/$dirname";
54310121 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#
400sub 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.
3d8876fc 427 my %heads = ();
54310121 428 foreach $i (0..$#poddata) {
776d31fe 429 $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
54310121 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
3d8876fc 435 my $dir = $pod;
54310121 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+(.*)/;
776d31fe 461 $file = "$dir/" . anchorify($section) . ".pod";
54310121 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<([^<>]*)>,
776d31fe
SH
468 defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
469 ,ge;
54310121 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#
486sub installdir {
487 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
54310121 488
8de3880f
AP
489 my @dirlist; # directories to recurse on
490 my @podlist; # .pod files to install
491 my @pmlist; # .pm files to install
54310121 492
493 # should files in this directory get an index?
8de3880f 494 my $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
54310121 495
496 opendir(DIR, "$podroot/$dir")
497 || die "$0: error opening directory $podroot/$dir: $!\n";
498
83c03aa3
AP
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 }
54310121 511
512 closedir(DIR);
513
83c03aa3
AP
514 if ($^O eq 'VMS') { s/\.dir$//i for @dirlist }
515
54310121 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
3d8876fc 522 foreach my $pod (@podlist) {
54310121 523 # check if we should ignore it.
47ec63d0
RGS
524 next if $pod =~ m(/t/); # comes from a test file
525 next if grep($_ eq "$pod.pod", @$ignore);
54310121 526
527 # check if a .pm files exists too
df00c672 528 if (grep($_ eq $pod, @pmlist)) {
e1b7525f
YO
529 print "$0: Warning both '$podroot/$pod.pod' and "
530 . "'$podroot/$pod.pm' exist, using pod\n";
54310121 531 push(@ignore, "$pod.pm");
532 }
533 runpod2html("$pod.pod", $doindex);
534 }
535
536 # install all the .pm files we found
3d8876fc 537 foreach my $pm (@pmlist) {
54310121 538 # check if we should ignore it.
47ec63d0 539 next if $pm =~ m(/t/); # comes from a test file
54310121 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#
551sub 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;
df00c672 571 Pod::Html::pod2html(
5a039dd3 572 "--htmldir=$htmldir",
54310121 573 "--htmlroot=$htmlroot",
574 "--podpath=".join(":", @podpath),
6c6fc3be 575 "--podroot=$podroot",
7c82de66 576 "--header",
54310121 577 ($doindex ? "--index" : "--noindex"),
578 "--" . ($recurse ? "" : "no") . "recurse",
54310121 579 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
580 die "$0: error running $pod2html: $!\n" if $?;
581}