This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Partial rewrite of perlobj.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
7use Getopt::Long; # for command-line parsing
8use Cwd;
9use Pod::Html;
10
54310121 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 41=item B<--podpath> POD search path
42
e05c04ea 43The list of directories to search for .pod and .pm files to be converted.
54310121 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 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 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 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 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 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 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 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 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 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 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
3d8876fc
MS
170use vars qw($opt_htmldir $opt_htmlroot $opt_podroot $opt_splitpod
171 $opt_verbose $opt_help $opt_podpath $opt_splithead $opt_splititem
172 $opt_libpods $opt_recurse);
173
54310121 174# parse the command-line
3d8876fc 175my $result = GetOptions( qw(
54310121 176 help
177 podpath=s
178 podroot=s
179 htmldir=s
180 htmlroot=s
181 libpods=s
182 recurse!
183 splithead=s
184 splititem=s
185 splitpod=s
186 verbose
187));
188usage("invalid parameters") unless $result;
189parse_command_line();
190
191
192# set these variables to appropriate values if the user didn't specify
193# values for them.
194$htmldir = "$htmlroot/html" unless $htmldir;
195$splitpod = "$podroot/pod" unless $splitpod;
196
197
198# make sure that the destination directory exists
199(mkdir($htmldir, 0755) ||
200 die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
201
202
203# the following array will eventually contain files that are to be
204# ignored in the conversion process. these are files that have been
205# process by splititem or splithead and should not be converted as a
206# result.
3d8876fc
MS
207my @ignore = ();
208my @splitdirs;
54310121 209
210# split pods. its important to do this before convert ANY pods because
211# it may effect some of the links
212@splitdirs = (); # files in these directories won't get an index
213split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
3e3baf6d 214split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
54310121 215
216
217# convert the pod pages found in @poddirs
218#warn "converting files\n" if $verbose;
219#warn "\@ignore\t= @ignore\n" if $verbose;
3d8876fc 220foreach my $dir (@podpath) {
54310121 221 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
222}
223
224
225# now go through and create master indices for each pod we split
3d8876fc 226foreach my $dir (@splititem) {
54310121 227 print "creating index $htmldir/$dir.html\n" if $verbose;
228 create_index("$htmldir/$dir.html", "$htmldir/$dir");
229}
230
3d8876fc 231foreach my $dir (@splithead) {
54310121 232 $dir .= ".pod" unless $dir =~ /(\.pod|\.pm)$/;
233 # let pod2html create the file
234 runpod2html($dir, 1);
235
236 # now go through and truncate after the index
237 $dir =~ /^(.*?)(\.pod|\.pm)?$/sm;
3d8876fc 238 my $file = "$htmldir/$1";
54310121 239 print "creating index $file.html\n" if $verbose;
240
241 # read in everything until what would have been the first =head
242 # directive, patching the index as we go.
243 open(H, "<$file.html") ||
244 die "$0: error opening $file.html for input: $!\n";
245 $/ = "";
3d8876fc 246 my @data = ();
54310121 247 while (<H>) {
248 last if /NAME=/;
29f227c9
BS
249 $_ =~ s{HREF="#(.*)">}{
250 my $url = "$file/$1.html" ;
251 $url = Pod::Html::relativize_url( $url, "$file.html" )
252 if ( ! defined $opt_htmlroot || $opt_htmlroot eq '' ) ;
253 "HREF=\"$url\">" ;
254 }eg;
54310121 255 push @data, $_;
256 }
257 close(H);
258
259 # now rewrite the file
260 open(H, ">$file.html") ||
261 die "$0: error opening $file.html for output: $!\n";
3d8876fc 262 print H "@data", "\n";
54310121 263 close(H);
264}
265
266##############################################################################
267
268
269sub usage {
270 warn "$0: @_\n" if @_;
271 die $usage;
272}
273
274
275sub parse_command_line {
276 usage() if defined $opt_help;
277 $opt_help = ""; # make -w shut up
278
279 # list of directories
280 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
281
282 # lists of files
283 @splithead = split(",", $opt_splithead) if defined $opt_splithead;
284 @splititem = split(",", $opt_splititem) if defined $opt_splititem;
285 @libpods = split(",", $opt_libpods) if defined $opt_libpods;
286
287 $htmldir = $opt_htmldir if defined $opt_htmldir;
288 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
289 $podroot = $opt_podroot if defined $opt_podroot;
290 $splitpod = $opt_splitpod if defined $opt_splitpod;
291
292 $recurse = $opt_recurse if defined $opt_recurse;
293 $verbose = $opt_verbose if defined $opt_verbose;
294}
295
296
3e3baf6d
TB
297sub absolute_path {
298 my($cwd, $path) = @_;
299 return "$cwd/$path" unless $path =~ m:/:;
300 # add cwd if path is not already an absolute path
301 $path = "$cwd/$path" if (substr($path,0,1) ne '/');
302 return $path;
303}
304
305
54310121 306sub create_index {
307 my($html, $dir) = @_;
308 my(@files, @filedata, @index, $file);
af19f77d
LP
309 my($lcp1,$lcp2);
310
54310121 311
312 # get the list of .html files in this directory
313 opendir(DIR, $dir) ||
314 die "$0: error opening directory $dir for reading: $!\n";
39e571d4 315 @files = sort(grep(/\.html?$/, readdir(DIR)));
54310121 316 closedir(DIR);
317
318 open(HTML, ">$html") ||
319 die "$0: error opening $html for output: $!\n";
320
321 # for each .html file in the directory, extract the index
322 # embedded in the file and throw it into the big index.
323 print HTML "<DL COMPACT>\n";
324 foreach $file (@files) {
325 $/ = "";
326
327 open(IN, "<$dir/$file") ||
328 die "$0: error opening $dir/$file for input: $!\n";
329 @filedata = <IN>;
330 close(IN);
331
332 # pull out the NAME section
3d8876fc 333 my $name;
54310121 334 ($name) = grep(/NAME=/, @filedata);
af19f77d
LP
335 ($lcp1,$lcp2) = ($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,sm);
336 if (defined $lcp1 and $lcp1 eq '<P>') { # Uninteresting. Try again.
337 ($lcp1,$lcp2) = ($name =~ m,/H1>\s<P>\s(\S+)\s[\s-]*(.*?)\s*$,sm);
338 }
29f227c9
BS
339 my $url= "$dir/$file" ;
340 if ( ! defined $opt_htmlroot || $opt_htmlroot eq '' ) {
341 $url = Pod::Html::relativize_url( "$dir/$file", $html ) ;
342 }
343
344 print HTML qq(<A HREF="$url">);
af19f77d 345 print HTML "<DT>$lcp1</A><DD>$lcp2\n" if defined $lcp1;
29f227c9 346# print HTML qq(<A HREF="$url">$lcp1</A><BR>\n") if defined $lcp1;
54310121 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;
355 # print HTML "$_\n";
356 print HTML "$_\n<P><HR><P>\n";
357 }
358 }
359 print HTML "</DL>\n";
360
361 close(HTML);
362}
363
364
365sub split_on_head {
366 my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
367 my($pod, $dirname, $filename);
368
369 # split the files specified in @splithead on =head[1-6] pod directives
370 print "splitting files by head.\n" if $verbose && $#splithead >= 0;
371 foreach $pod (@splithead) {
372 # figure out the directory name and filename
373 $pod =~ s,^([^/]*)$,/$1,;
374 $pod =~ m,(.*?)/(.*?)(\.pod)?$,;
375 $dirname = $1;
376 $filename = "$2.pod";
377
378 # since we are splitting this file it shouldn't be converted.
379 push(@$ignore, "$podroot/$dirname/$filename");
380
381 # split the pod
382 splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
383 $splitdirs);
384 }
385}
386
387
388sub split_on_item {
389 my($podroot, $splitdirs, $ignore, @splititem) = @_;
390 my($pwd, $dirname, $filename);
391
392 print "splitting files by item.\n" if $verbose && $#splititem >= 0;
393 $pwd = getcwd();
3e3baf6d 394 my $splitter = absolute_path($pwd, "$splitpod/splitpod");
3d8876fc 395 foreach my $pod (@splititem) {
54310121 396 # figure out the directory to split into
397 $pod =~ s,^([^/]*)$,/$1,;
398 $pod =~ m,(.*?)/(.*?)(\.pod)?$,;
399 $dirname = "$1/$2";
400 $filename = "$2.pod";
401
402 # since we are splitting this file it shouldn't be converted.
403 push(@$ignore, "$podroot/$dirname.pod");
404
405 # split the pod
406 push(@$splitdirs, "$podroot/$dirname");
407 if (! -d "$podroot/$dirname") {
408 mkdir("$podroot/$dirname", 0755) ||
409 die "$0: error creating directory $podroot/$dirname: $!\n";
410 }
411 chdir("$podroot/$dirname") ||
412 die "$0: error changing to directory $podroot/$dirname: $!\n";
3e3baf6d
TB
413 die "$splitter not found. Use '-splitpod dir' option.\n"
414 unless -f $splitter;
415 system("perl", $splitter, "../$filename") &&
416 warn "$0: error running '$splitter ../$filename'"
e05c04ea 417 ." from $podroot/$dirname";
54310121 418 }
419 chdir($pwd);
420}
421
422
423#
424# splitpod - splits a .pod file into several smaller .pod files
425# where a new file is started each time a =head[1-6] pod directive
426# is encountered in the input file.
427#
428sub splitpod {
429 my($pod, $poddir, $htmldir, $splitdirs) = @_;
430 my(@poddata, @filedata, @heads);
431 my($file, $i, $j, $prevsec, $section, $nextsec);
432
433 print "splitting $pod\n" if $verbose;
434
435 # read the file in paragraphs
436 $/ = "";
437 open(SPLITIN, "<$pod") ||
438 die "$0: error opening $pod for input: $!\n";
439 @filedata = <SPLITIN>;
440 close(SPLITIN) ||
441 die "$0: error closing $pod: $!\n";
442
443 # restore the file internally by =head[1-6] sections
444 @poddata = ();
445 for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
446 $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
447 if ($j >= 0) {
448 $poddata[$j] = "" unless defined $poddata[$j];
449 $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
450 }
451 }
452
453 # create list of =head[1-6] sections so that we can rewrite
454 # L<> links as necessary.
3d8876fc 455 my %heads = ();
54310121 456 foreach $i (0..$#poddata) {
457 $heads{htmlize($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
458 }
459
460 # create a directory of a similar name and store all the
461 # files in there
462 $pod =~ s,.*/(.*),$1,; # get the last part of the name
3d8876fc 463 my $dir = $pod;
54310121 464 $dir =~ s/\.pod//g;
465 push(@$splitdirs, "$poddir/$dir");
466 mkdir("$poddir/$dir", 0755) ||
467 die "$0: could not create directory $poddir/$dir: $!\n"
468 unless -d "$poddir/$dir";
469
470 $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
471 $section = "";
472 $nextsec = $1;
473
474 # for each section of the file create a separate pod file
475 for ($i = 0; $i <= $#poddata; $i++) {
476 # determine the "prev" and "next" links
477 $prevsec = $section;
478 $section = $nextsec;
479 if ($i < $#poddata) {
480 $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
481 $nextsec = $1;
482 } else {
483 $nextsec = "";
484 }
485
486 # determine an appropriate filename (this must correspond with
487 # what pod2html will try and guess)
488 # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
489 $file = "$dir/" . htmlize($section) . ".pod";
490
491 # create the new .pod file
492 print "\tcreating $poddir/$file\n" if $verbose;
493 open(SPLITOUT, ">$poddir/$file") ||
494 die "$0: error opening $poddir/$file for output: $!\n";
495 $poddata[$i] =~ s,L<([^<>]*)>,
496 defined $heads{htmlize($1)} ? "L<$dir/$1>" : "L<$1>"
497 ,ge;
498 print SPLITOUT $poddata[$i]."\n\n";
499 print SPLITOUT "=over 4\n\n";
500 print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
501 print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
502 print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
503 print SPLITOUT "=back\n\n";
504 close(SPLITOUT) ||
505 die "$0: error closing $poddir/$file: $!\n";
506 }
507}
508
509
510#
511# installdir - takes care of converting the .pod and .pm files in the
512# current directory to .html files and then installing those.
513#
514sub installdir {
515 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
516 my(@dirlist, @podlist, @pmlist, $doindex);
517
518 @dirlist = (); # directories to recurse on
519 @podlist = (); # .pod files to install
520 @pmlist = (); # .pm files to install
521
522 # should files in this directory get an index?
523 $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
524
525 opendir(DIR, "$podroot/$dir")
526 || die "$0: error opening directory $podroot/$dir: $!\n";
527
528 # find the directories to recurse on
fe4c6be1 529 @dirlist = map { if ($^O eq 'VMS') {/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}}
54310121 530 grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
531 rewinddir(DIR);
532
533 # find all the .pod files within the directory
534 @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
535 grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
536 rewinddir(DIR);
537
538 # find all the .pm files within the directory
539 @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
540 grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
541
542 closedir(DIR);
543
544 # recurse on all subdirectories we kept track of
545 foreach $dir (@dirlist) {
546 installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
547 }
548
549 # install all the pods we found
3d8876fc 550 foreach my $pod (@podlist) {
54310121 551 # check if we should ignore it.
552 next if grep($_ eq "$podroot/$pod.pod", @$ignore);
553
554 # check if a .pm files exists too
555 if (grep($_ eq "$pod.pm", @pmlist)) {
556 print "$0: Warning both `$podroot/$pod.pod' and "
557 . "`$podroot/$pod.pm' exist, using pod\n";
558 push(@ignore, "$pod.pm");
559 }
560 runpod2html("$pod.pod", $doindex);
561 }
562
563 # install all the .pm files we found
3d8876fc 564 foreach my $pm (@pmlist) {
54310121 565 # check if we should ignore it.
566 next if grep($_ eq "$pm.pm", @ignore);
567
568 runpod2html("$pm.pm", $doindex);
569 }
570}
571
572
573#
574# runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
575# file.
576#
577sub runpod2html {
578 my($pod, $doindex) = @_;
579 my($html, $i, $dir, @dirs);
580
581 $html = $pod;
582 $html =~ s/\.(pod|pm)$/.html/g;
583
584 # make sure the destination directories exist
585 @dirs = split("/", $html);
586 $dir = "$htmldir/";
587 for ($i = 0; $i < $#dirs; $i++) {
588 if (! -d "$dir$dirs[$i]") {
589 mkdir("$dir$dirs[$i]", 0755) ||
590 die "$0: error creating directory $dir$dirs[$i]: $!\n";
591 }
592 $dir .= "$dirs[$i]/";
593 }
594
595 # invoke pod2html
596 print "$podroot/$pod => $htmldir/$html\n" if $verbose;
597#system("./pod2html",
598 Pod::Html'pod2html(
599 #Pod::Html'pod2html($pod2html,
5a039dd3 600 "--htmldir=$htmldir",
54310121 601 "--htmlroot=$htmlroot",
602 "--podpath=".join(":", @podpath),
603 "--podroot=$podroot", "--netscape",
7c82de66 604 "--header",
54310121 605 ($doindex ? "--index" : "--noindex"),
606 "--" . ($recurse ? "" : "no") . "recurse",
607 ($#libpods >= 0) ? "--libpods=" . join(":", @libpods) : "",
608 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
609 die "$0: error running $pod2html: $!\n" if $?;
610}
611
612sub htmlize { htmlify(0, @_) }