This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Safe.pm: Make sure SWASHNEW is properly loaded
[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
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>]
6c6fc3be 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 my(@files, @filedata, @index, $file);
af19f77d
LP
295 my($lcp1,$lcp2);
296
54310121 297
298 # get the list of .html files in this directory
299 opendir(DIR, $dir) ||
300 die "$0: error opening directory $dir for reading: $!\n";
39e571d4 301 @files = sort(grep(/\.html?$/, readdir(DIR)));
54310121 302 closedir(DIR);
303
304 open(HTML, ">$html") ||
305 die "$0: error opening $html for output: $!\n";
306
307 # for each .html file in the directory, extract the index
308 # embedded in the file and throw it into the big index.
309 print HTML "<DL COMPACT>\n";
310 foreach $file (@files) {
311 $/ = "";
312
313 open(IN, "<$dir/$file") ||
314 die "$0: error opening $dir/$file for input: $!\n";
315 @filedata = <IN>;
316 close(IN);
317
318 # pull out the NAME section
df00c672
SH
319 my $name;
320 ($name) = grep(/name="name"/i, @filedata);
321 ($lcp1,$lcp2) = ($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,smi);
322 if (defined $lcp1 and $lcp1 =~ m,^<P>$,i) { # Uninteresting. Try again.
323 ($lcp1,$lcp2) = ($name =~ m,/H1>\s<P>\s*(\S+)\s[\s-]*(.*?)\s*$,smi);
af19f77d 324 }
df00c672 325 my $url= "$pod/$file" ;
65b03822 326 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
df00c672 327 $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
29f227c9
BS
328 }
329
df00c672
SH
330 if (defined $lcp1) {
331 print HTML qq(<DT><A HREF="$url">);
332 print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
333 }
54310121 334
335 next;
336
337 @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
338 @filedata);
339 for (@index) {
af19f77d 340 s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
54310121 341 s,#,$dir/$file#,g;
54310121 342 print HTML "$_\n<P><HR><P>\n";
343 }
344 }
345 print HTML "</DL>\n";
346
347 close(HTML);
348}
349
350
351sub split_on_head {
352 my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
353 my($pod, $dirname, $filename);
354
355 # split the files specified in @splithead on =head[1-6] pod directives
356 print "splitting files by head.\n" if $verbose && $#splithead >= 0;
357 foreach $pod (@splithead) {
358 # figure out the directory name and filename
359 $pod =~ s,^([^/]*)$,/$1,;
df00c672 360 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 361 $dirname = $1;
362 $filename = "$2.pod";
363
364 # since we are splitting this file it shouldn't be converted.
365 push(@$ignore, "$podroot/$dirname/$filename");
366
367 # split the pod
368 splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
369 $splitdirs);
370 }
371}
372
373
374sub split_on_item {
375 my($podroot, $splitdirs, $ignore, @splititem) = @_;
376 my($pwd, $dirname, $filename);
377
378 print "splitting files by item.\n" if $verbose && $#splititem >= 0;
379 $pwd = getcwd();
cac3fadd
SH
380 my $splitter = File::Spec->rel2abs("$splitpod/splitpod", $pwd);
381 my $perl = File::Spec->rel2abs($^X, $pwd);
3d8876fc 382 foreach my $pod (@splititem) {
54310121 383 # figure out the directory to split into
384 $pod =~ s,^([^/]*)$,/$1,;
df00c672 385 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
54310121 386 $dirname = "$1/$2";
387 $filename = "$2.pod";
388
389 # since we are splitting this file it shouldn't be converted.
390 push(@$ignore, "$podroot/$dirname.pod");
391
392 # split the pod
393 push(@$splitdirs, "$podroot/$dirname");
394 if (! -d "$podroot/$dirname") {
395 mkdir("$podroot/$dirname", 0755) ||
396 die "$0: error creating directory $podroot/$dirname: $!\n";
397 }
398 chdir("$podroot/$dirname") ||
399 die "$0: error changing to directory $podroot/$dirname: $!\n";
3e3baf6d
TB
400 die "$splitter not found. Use '-splitpod dir' option.\n"
401 unless -f $splitter;
ab9905e6 402 system($perl, $splitter, "../$filename") &&
3e3baf6d 403 warn "$0: error running '$splitter ../$filename'"
e05c04ea 404 ." from $podroot/$dirname";
54310121 405 }
406 chdir($pwd);
407}
408
409
410#
411# splitpod - splits a .pod file into several smaller .pod files
412# where a new file is started each time a =head[1-6] pod directive
413# is encountered in the input file.
414#
415sub splitpod {
416 my($pod, $poddir, $htmldir, $splitdirs) = @_;
417 my(@poddata, @filedata, @heads);
418 my($file, $i, $j, $prevsec, $section, $nextsec);
419
420 print "splitting $pod\n" if $verbose;
421
422 # read the file in paragraphs
423 $/ = "";
424 open(SPLITIN, "<$pod") ||
425 die "$0: error opening $pod for input: $!\n";
426 @filedata = <SPLITIN>;
427 close(SPLITIN) ||
428 die "$0: error closing $pod: $!\n";
429
430 # restore the file internally by =head[1-6] sections
431 @poddata = ();
432 for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
433 $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
434 if ($j >= 0) {
435 $poddata[$j] = "" unless defined $poddata[$j];
436 $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
437 }
438 }
439
440 # create list of =head[1-6] sections so that we can rewrite
441 # L<> links as necessary.
3d8876fc 442 my %heads = ();
54310121 443 foreach $i (0..$#poddata) {
776d31fe 444 $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
54310121 445 }
446
447 # create a directory of a similar name and store all the
448 # files in there
449 $pod =~ s,.*/(.*),$1,; # get the last part of the name
3d8876fc 450 my $dir = $pod;
54310121 451 $dir =~ s/\.pod//g;
452 push(@$splitdirs, "$poddir/$dir");
453 mkdir("$poddir/$dir", 0755) ||
454 die "$0: could not create directory $poddir/$dir: $!\n"
455 unless -d "$poddir/$dir";
456
457 $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
458 $section = "";
459 $nextsec = $1;
460
461 # for each section of the file create a separate pod file
462 for ($i = 0; $i <= $#poddata; $i++) {
463 # determine the "prev" and "next" links
464 $prevsec = $section;
465 $section = $nextsec;
466 if ($i < $#poddata) {
467 $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
468 $nextsec = $1;
469 } else {
470 $nextsec = "";
471 }
472
473 # determine an appropriate filename (this must correspond with
474 # what pod2html will try and guess)
475 # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
776d31fe 476 $file = "$dir/" . anchorify($section) . ".pod";
54310121 477
478 # create the new .pod file
479 print "\tcreating $poddir/$file\n" if $verbose;
480 open(SPLITOUT, ">$poddir/$file") ||
481 die "$0: error opening $poddir/$file for output: $!\n";
482 $poddata[$i] =~ s,L<([^<>]*)>,
776d31fe
SH
483 defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
484 ,ge;
54310121 485 print SPLITOUT $poddata[$i]."\n\n";
486 print SPLITOUT "=over 4\n\n";
487 print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
488 print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
489 print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
490 print SPLITOUT "=back\n\n";
491 close(SPLITOUT) ||
492 die "$0: error closing $poddir/$file: $!\n";
493 }
494}
495
496
497#
498# installdir - takes care of converting the .pod and .pm files in the
499# current directory to .html files and then installing those.
500#
501sub installdir {
502 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
503 my(@dirlist, @podlist, @pmlist, $doindex);
504
505 @dirlist = (); # directories to recurse on
506 @podlist = (); # .pod files to install
507 @pmlist = (); # .pm files to install
508
509 # should files in this directory get an index?
510 $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
511
512 opendir(DIR, "$podroot/$dir")
513 || die "$0: error opening directory $podroot/$dir: $!\n";
514
515 # find the directories to recurse on
fe4c6be1 516 @dirlist = map { if ($^O eq 'VMS') {/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}}
54310121 517 grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
518 rewinddir(DIR);
519
520 # find all the .pod files within the directory
521 @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
522 grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
523 rewinddir(DIR);
524
525 # find all the .pm files within the directory
526 @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
527 grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
528
529 closedir(DIR);
530
531 # recurse on all subdirectories we kept track of
532 foreach $dir (@dirlist) {
533 installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
534 }
535
536 # install all the pods we found
3d8876fc 537 foreach my $pod (@podlist) {
54310121 538 # check if we should ignore it.
47ec63d0
RGS
539 next if $pod =~ m(/t/); # comes from a test file
540 next if grep($_ eq "$pod.pod", @$ignore);
54310121 541
542 # check if a .pm files exists too
df00c672 543 if (grep($_ eq $pod, @pmlist)) {
e1b7525f
YO
544 print "$0: Warning both '$podroot/$pod.pod' and "
545 . "'$podroot/$pod.pm' exist, using pod\n";
54310121 546 push(@ignore, "$pod.pm");
547 }
548 runpod2html("$pod.pod", $doindex);
549 }
550
551 # install all the .pm files we found
3d8876fc 552 foreach my $pm (@pmlist) {
54310121 553 # check if we should ignore it.
47ec63d0 554 next if $pm =~ m(/t/); # comes from a test file
54310121 555 next if grep($_ eq "$pm.pm", @ignore);
556
557 runpod2html("$pm.pm", $doindex);
558 }
559}
560
561
562#
563# runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
564# file.
565#
566sub runpod2html {
567 my($pod, $doindex) = @_;
568 my($html, $i, $dir, @dirs);
569
570 $html = $pod;
571 $html =~ s/\.(pod|pm)$/.html/g;
572
573 # make sure the destination directories exist
574 @dirs = split("/", $html);
575 $dir = "$htmldir/";
576 for ($i = 0; $i < $#dirs; $i++) {
577 if (! -d "$dir$dirs[$i]") {
578 mkdir("$dir$dirs[$i]", 0755) ||
579 die "$0: error creating directory $dir$dirs[$i]: $!\n";
580 }
581 $dir .= "$dirs[$i]/";
582 }
583
584 # invoke pod2html
585 print "$podroot/$pod => $htmldir/$html\n" if $verbose;
df00c672 586 Pod::Html::pod2html(
5a039dd3 587 "--htmldir=$htmldir",
54310121 588 "--htmlroot=$htmlroot",
589 "--podpath=".join(":", @podpath),
6c6fc3be 590 "--podroot=$podroot",
7c82de66 591 "--header",
54310121 592 ($doindex ? "--index" : "--noindex"),
593 "--" . ($recurse ? "" : "no") . "recurse",
54310121 594 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
595 die "$0: error running $pod2html: $!\n" if $?;
596}