(?p{}) has been deprecated for a long time.
[p5sagit/p5-mst-13.2.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
cac3fadd 7use File::Spec;
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
18 installhtml [--help] [--podpath=<name>:...:<name>] [--podroot=<name>]
19 [--htmldir=<name>] [--htmlroot=<name>] [--norecurse] [--recurse]
20 [--splithead=<name>,...,<name>] [--splititem=<name>,...,<name>]
47ec63d0 21 [--libpods=<name>,...,<name>] [--ignore=<name>,...,<name>]
22 [--verbose]
54310121 23
24=head1 DESCRIPTION
25
26I<installhtml> converts a collection of POD pages to a corresponding
27collection of HTML pages. This is primarily used to convert the pod
28pages found in the perl distribution.
29
30=head1 OPTIONS
31
32=over 4
33
34=item B<--help> help
35
36Displays the usage.
37
3e3baf6d 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.
54310121 46Default is `podroot/.'.
47
3e3baf6d 48=item B<--recurse> recurse on subdirectories
54310121 49
3e3baf6d 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
61a URL. The default is `/'.
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 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
af19f77d 71Comma-separated list of all pod files to split by the =item directive.
3e3baf6d 72The .pod suffix is optional. I<installhtml> does not do the actual
73split, 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
78The directory containing the splitpod program. The default is `podroot/pod'.
54310121 79
80=item B<--libpods> library PODs for LE<lt>E<gt> links
81
af19f77d 82Comma-separated list of "library" pod files. This is the same list that
54310121 83will be passed to pod2html when any pod is converted.
84
47ec63d0 85=item B<--ignore> files to be ignored
86
87Comma-separated of files that shouldn't be installed, given relative
88to podroot.
89
54310121 90=item B<--verbose> verbose output
91
92Self-explanatory.
93
94=back
95
96=head1 EXAMPLE
97
98The following command-line is an example of the one we use to convert
99perl documentation:
100
101 ./installhtml --podpath=lib:ext:pod:vms \
84902520 102 --podroot=/usr/src/perl \
103 --htmldir=/perl/nmanual \
104 --htmlroot=/perl/nmanual \
105 --splithead=pod/perlipc \
106 --splititem=pod/perlfunc \
af19f77d 107 --libpods=perlfunc,perlguts,perlvar,perlrun,perlop \
84902520 108 --recurse \
109 --verbose
54310121 110
111=head1 AUTHOR
112
113Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
114
115=head1 TODO
116
117=cut
118
3d8876fc 119my $usage;
120
54310121 121$usage =<<END_OF_USAGE;
122Usage: $0 --help --podpath=<name>:...:<name> --podroot=<name>
123 --htmldir=<name> --htmlroot=<name> --norecurse --recurse
124 --splithead=<name>,...,<name> --splititem=<name>,...,<name>
47ec63d0 125 --libpods=<name>,...,<name> --ignore=<name>,...,<name> --verbose
54310121 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
47ec63d0 133 to the filesystem (\$podroot/html by default).
54310121 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
e05c04ea 143 split each file into several smaller files at every occurrence
54310121 144 of a pod =head[1-6] directive.
145 --splititem - comma-separated list of .pod or .pm files to split using
146 splitpod.
3e3baf6d 147 --splitpod - directory where the program splitpod can be found
148 (\$podroot/pod by default).
47ec63d0 149 --ignore - comma-separated list of files that shouldn't be installed.
54310121 150 --verbose - self-explanatory.
151
152END_OF_USAGE
153
3d8876fc 154my (@libpods, @podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
47ec63d0 155 @splititem, $splitpod, $verbose, $pod2html, @ignore);
3d8876fc 156
54310121 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
47ec63d0 168$verbose = 0; # whether or not to print debugging info
54310121 169
170$pod2html = "pod/pod2html";
171
3e3baf6d 172usage("") unless @ARGV;
54310121 173
c645ec3f 174# Overcome shell's p1,..,p8 limitation.
175# See vms/descrip_mms.template -> descrip.mms for invokation.
176if ( $^O eq 'VMS' ) { @ARGV = split(/\s+/,$ARGV[0]); }
177
65b03822 178use vars qw( %Options );
3d8876fc 179
54310121 180# parse the command-line
65b03822 181my $result = GetOptions( \%Options, qw(
54310121 182 help
183 podpath=s
184 podroot=s
185 htmldir=s
186 htmlroot=s
187 libpods=s
47ec63d0 188 ignore=s
54310121 189 recurse!
190 splithead=s
191 splititem=s
192 splitpod=s
193 verbose
194));
195usage("invalid parameters") unless $result;
196parse_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.
3d8876fc 214my @splitdirs;
54310121 215
47ec63d0 216# split pods. It's important to do this before convert ANY pods because
217# it may affect some of the links
54310121 218@splitdirs = (); # files in these directories won't get an index
219split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
3e3baf6d 220split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
54310121 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;
3d8876fc 226foreach my $dir (@podpath) {
54310121 227 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
228}
229
230
231# now go through and create master indices for each pod we split
3d8876fc 232foreach my $dir (@splititem) {
54310121 233 print "creating index $htmldir/$dir.html\n" if $verbose;
234 create_index("$htmldir/$dir.html", "$htmldir/$dir");
235}
236
3d8876fc 237foreach my $dir (@splithead) {
776d31fe 238 (my $pod = $dir) =~ s,^.*/,,;
54310121 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;
3d8876fc 245 my $file = "$htmldir/$1";
54310121 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 $/ = "";
3d8876fc 253 my @data = ();
54310121 254 while (<H>) {
776d31fe 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;
54310121 262 push @data, $_;
776d31fe 263 }
54310121 264 close(H);
265
47ec63d0 266 # now rewrite the file
54310121 267 open(H, ">$file.html") ||
268 die "$0: error opening $file.html for output: $!\n";
3d8876fc 269 print H "@data", "\n";
54310121 270 close(H);
271}
272
273##############################################################################
274
275
276sub usage {
277 warn "$0: @_\n" if @_;
278 die $usage;
279}
280
281
282sub parse_command_line {
65b03822 283 usage() if defined $Options{help};
284 $Options{help} = ""; # make -w shut up
54310121 285
286 # list of directories
65b03822 287 @podpath = split(":", $Options{podpath}) if defined $Options{podpath};
54310121 288
289 # lists of files
65b03822 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};
54310121 293
65b03822 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};
54310121 298
65b03822 299 $recurse = $Options{recurse} if defined $Options{recurse};
300 $verbose = $Options{verbose} if defined $Options{verbose};
47ec63d0 301
302 @ignore = map "$podroot/$_", split(",", $Options{ignore}) if defined $Options{ignore};
54310121 303}
304
305
306sub create_index {
307 my($html, $dir) = @_;
df00c672 308 (my $pod = $dir) =~ s,^.*/,,;
54310121 309 my(@files, @filedata, @index, $file);
af19f77d 310 my($lcp1,$lcp2);
311
54310121 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";
39e571d4 316 @files = sort(grep(/\.html?$/, readdir(DIR)));
54310121 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
df00c672 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);
af19f77d 339 }
df00c672 340 my $url= "$pod/$file" ;
65b03822 341 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
df00c672 342 $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
29f227c9 343 }
344
df00c672 345 if (defined $lcp1) {
346 print HTML qq(<DT><A HREF="$url">);
347 print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
348 }
54310121 349
350 next;
351
352 @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
353 @filedata);
354 for (@index) {
af19f77d 355 s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
54310121 356 s,#,$dir/$file#,g;
54310121 357 print HTML "$_\n<P><HR><P>\n";
358 }
359 }
360 print HTML "</DL>\n";
361
362 close(HTML);
363}
364
365
366sub 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,;
df00c672 375 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 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
389sub 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();
cac3fadd 395 my $splitter = File::Spec->rel2abs("$splitpod/splitpod", $pwd);
396 my $perl = File::Spec->rel2abs($^X, $pwd);
3d8876fc 397 foreach my $pod (@splititem) {
54310121 398 # figure out the directory to split into
399 $pod =~ s,^([^/]*)$,/$1,;
df00c672 400 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 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";
3e3baf6d 415 die "$splitter not found. Use '-splitpod dir' option.\n"
416 unless -f $splitter;
ab9905e6 417 system($perl, $splitter, "../$filename") &&
3e3baf6d 418 warn "$0: error running '$splitter ../$filename'"
e05c04ea 419 ." from $podroot/$dirname";
54310121 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#
430sub 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.
3d8876fc 457 my %heads = ();
54310121 458 foreach $i (0..$#poddata) {
776d31fe 459 $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
54310121 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
3d8876fc 465 my $dir = $pod;
54310121 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+(.*)/;
776d31fe 491 $file = "$dir/" . anchorify($section) . ".pod";
54310121 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<([^<>]*)>,
776d31fe 498 defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
499 ,ge;
54310121 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#
516sub 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
fe4c6be1 531 @dirlist = map { if ($^O eq 'VMS') {/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}}
54310121 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
3d8876fc 552 foreach my $pod (@podlist) {
54310121 553 # check if we should ignore it.
47ec63d0 554 next if $pod =~ m(/t/); # comes from a test file
555 next if grep($_ eq "$pod.pod", @$ignore);
54310121 556
557 # check if a .pm files exists too
df00c672 558 if (grep($_ eq $pod, @pmlist)) {
54310121 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
3d8876fc 567 foreach my $pm (@pmlist) {
54310121 568 # check if we should ignore it.
47ec63d0 569 next if $pm =~ m(/t/); # comes from a test file
54310121 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#
581sub 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;
df00c672 601 Pod::Html::pod2html(
5a039dd3 602 "--htmldir=$htmldir",
54310121 603 "--htmlroot=$htmlroot",
604 "--podpath=".join(":", @podpath),
605 "--podroot=$podroot", "--netscape",
7c82de66 606 "--header",
54310121 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}