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