This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Decommission the symlink test.
[perl5.git] / installhtml
CommitLineData
90248788
TB
1#!./perl -w
2
3# This file should really be a extracted from a .PL
54310121 4
3e3baf6d
TB
5use lib 'lib'; # use source library if present
6
54310121 7use Config; # for config options in the makefile
8use Getopt::Long; # for command-line parsing
9use Cwd;
10use Pod::Html;
11
12umask 022;
13
14=head1 NAME
15
16installhtml - converts a collection of POD pages to HTML format.
17
18=head1 SYNOPSIS
19
20 installhtml [--help] [--podpath=<name>:...:<name>] [--podroot=<name>]
21 [--htmldir=<name>] [--htmlroot=<name>] [--norecurse] [--recurse]
22 [--splithead=<name>,...,<name>] [--splititem=<name>,...,<name>]
23 [--libpods=<name>,...,<name>] [--verbose]
24
25=head1 DESCRIPTION
26
27I<installhtml> converts a collection of POD pages to a corresponding
28collection of HTML pages. This is primarily used to convert the pod
29pages found in the perl distribution.
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 44=item B<--podpath> POD search path
45
e05c04ea 46The list of directories to search for .pod and .pm files to be converted.
54310121 47Default is `podroot/.'.
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 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
62a URL. The default is `/'.
63
54310121 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 69
70=item B<--splititem> POD files to split on =item directive
71
af19f77d 72Comma-separated list of all pod files to split by the =item directive.
3e3baf6d
TB
73The .pod suffix is optional. I<installhtml> does not do the actual
74split, rather it invokes I<splitpod> to do the dirty work. As with
75--splithead, these files should have names specified relative to podroot.
76
77=item B<--splitpod> Directory containing the splitpod program
78
79The directory containing the splitpod program. The default is `podroot/pod'.
54310121 80
81=item B<--libpods> library PODs for LE<lt>E<gt> links
82
af19f77d 83Comma-separated list of "library" pod files. This is the same list that
54310121 84will be passed to pod2html when any pod is converted.
85
86=item B<--verbose> verbose output
87
88Self-explanatory.
89
90=back
91
92=head1 EXAMPLE
93
94The following command-line is an example of the one we use to convert
95perl documentation:
96
97 ./installhtml --podpath=lib:ext:pod:vms \
84902520
TB
98 --podroot=/usr/src/perl \
99 --htmldir=/perl/nmanual \
100 --htmlroot=/perl/nmanual \
101 --splithead=pod/perlipc \
102 --splititem=pod/perlfunc \
af19f77d 103 --libpods=perlfunc,perlguts,perlvar,perlrun,perlop \
84902520
TB
104 --recurse \
105 --verbose
54310121 106
107=head1 AUTHOR
108
109Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
110
111=head1 TODO
112
113=cut
114
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>
119 --libpods=<name>,...,<name> --verbose
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
127 to the filesystem (\$podroot/html by default).
128 --htmlroot - http-server base directory from which all relative paths
129 in podpath stem (default is /).
130 --libpods - comma-separated list of files to search for =item pod
131 directives in as targets of C<> and implicit links (empty
132 by default).
133 --norecurse - don't recurse on those subdirectories listed in podpath.
134 (default behavior).
135 --recurse - recurse on those subdirectories listed in podpath
136 --splithead - comma-separated list of .pod or .pm files to split. will
e05c04ea 137 split each file into several smaller files at every occurrence
54310121 138 of a pod =head[1-6] directive.
139 --splititem - comma-separated list of .pod or .pm files to split using
140 splitpod.
3e3baf6d
TB
141 --splitpod - directory where the program splitpod can be found
142 (\$podroot/pod by default).
54310121 143 --verbose - self-explanatory.
144
145END_OF_USAGE
146
147@libpods = ();
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
158$verbose = 0; # whether or not to print debugging info
159
160$pod2html = "pod/pod2html";
161
3e3baf6d 162usage("") unless @ARGV;
54310121 163
164# parse the command-line
165$result = GetOptions( qw(
166 help
167 podpath=s
168 podroot=s
169 htmldir=s
170 htmlroot=s
171 libpods=s
172 recurse!
173 splithead=s
174 splititem=s
175 splitpod=s
176 verbose
177));
178usage("invalid parameters") unless $result;
179parse_command_line();
180
181
182# set these variables to appropriate values if the user didn't specify
183# values for them.
184$htmldir = "$htmlroot/html" unless $htmldir;
185$splitpod = "$podroot/pod" unless $splitpod;
186
187
188# make sure that the destination directory exists
189(mkdir($htmldir, 0755) ||
190 die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
191
192
193# the following array will eventually contain files that are to be
194# ignored in the conversion process. these are files that have been
195# process by splititem or splithead and should not be converted as a
196# result.
197@ignore = ();
198
199
200# split pods. its important to do this before convert ANY pods because
201# it may effect some of the links
202@splitdirs = (); # files in these directories won't get an index
203split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
3e3baf6d 204split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
54310121 205
206
207# convert the pod pages found in @poddirs
208#warn "converting files\n" if $verbose;
209#warn "\@ignore\t= @ignore\n" if $verbose;
210foreach $dir (@podpath) {
211 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
212}
213
214
215# now go through and create master indices for each pod we split
216foreach $dir (@splititem) {
217 print "creating index $htmldir/$dir.html\n" if $verbose;
218 create_index("$htmldir/$dir.html", "$htmldir/$dir");
219}
220
221foreach $dir (@splithead) {
222 $dir .= ".pod" unless $dir =~ /(\.pod|\.pm)$/;
223 # let pod2html create the file
224 runpod2html($dir, 1);
225
226 # now go through and truncate after the index
227 $dir =~ /^(.*?)(\.pod|\.pm)?$/sm;
228 $file = "$htmldir/$1";
229 print "creating index $file.html\n" if $verbose;
230
231 # read in everything until what would have been the first =head
232 # directive, patching the index as we go.
233 open(H, "<$file.html") ||
234 die "$0: error opening $file.html for input: $!\n";
235 $/ = "";
236 @data = ();
237 while (<H>) {
238 last if /NAME=/;
239 s,HREF="#(.*)">,HREF="$file/$1.html">,g;
240 push @data, $_;
241 }
242 close(H);
243
244 # now rewrite the file
245 open(H, ">$file.html") ||
246 die "$0: error opening $file.html for output: $!\n";
247 print H "@data\n";
248 close(H);
249}
250
251##############################################################################
252
253
254sub usage {
255 warn "$0: @_\n" if @_;
256 die $usage;
257}
258
259
260sub parse_command_line {
261 usage() if defined $opt_help;
262 $opt_help = ""; # make -w shut up
263
264 # list of directories
265 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
266
267 # lists of files
268 @splithead = split(",", $opt_splithead) if defined $opt_splithead;
269 @splititem = split(",", $opt_splititem) if defined $opt_splititem;
270 @libpods = split(",", $opt_libpods) if defined $opt_libpods;
271
272 $htmldir = $opt_htmldir if defined $opt_htmldir;
273 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
274 $podroot = $opt_podroot if defined $opt_podroot;
275 $splitpod = $opt_splitpod if defined $opt_splitpod;
276
277 $recurse = $opt_recurse if defined $opt_recurse;
278 $verbose = $opt_verbose if defined $opt_verbose;
279}
280
281
3e3baf6d
TB
282sub absolute_path {
283 my($cwd, $path) = @_;
284 return "$cwd/$path" unless $path =~ m:/:;
285 # add cwd if path is not already an absolute path
286 $path = "$cwd/$path" if (substr($path,0,1) ne '/');
287 return $path;
288}
289
290
54310121 291sub create_index {
292 my($html, $dir) = @_;
293 my(@files, @filedata, @index, $file);
af19f77d
LP
294 my($lcp1,$lcp2);
295
54310121 296
297 # get the list of .html files in this directory
298 opendir(DIR, $dir) ||
299 die "$0: error opening directory $dir for reading: $!\n";
39e571d4 300 @files = sort(grep(/\.html?$/, readdir(DIR)));
54310121 301 closedir(DIR);
302
303 open(HTML, ">$html") ||
304 die "$0: error opening $html for output: $!\n";
305
306 # for each .html file in the directory, extract the index
307 # embedded in the file and throw it into the big index.
308 print HTML "<DL COMPACT>\n";
309 foreach $file (@files) {
310 $/ = "";
311
312 open(IN, "<$dir/$file") ||
313 die "$0: error opening $dir/$file for input: $!\n";
314 @filedata = <IN>;
315 close(IN);
316
317 # pull out the NAME section
318 ($name) = grep(/NAME=/, @filedata);
af19f77d
LP
319 ($lcp1,$lcp2) = ($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,sm);
320 if (defined $lcp1 and $lcp1 eq '<P>') { # Uninteresting. Try again.
321 ($lcp1,$lcp2) = ($name =~ m,/H1>\s<P>\s(\S+)\s[\s-]*(.*?)\s*$,sm);
322 }
54310121 323 print HTML qq(<A HREF="$dir/$file">);
af19f77d
LP
324 print HTML "<DT>$lcp1</A><DD>$lcp2\n" if defined $lcp1;
325# print HTML qq(<A HREF="$dir/$file">$lcp1</A><BR>\n") if defined $lcp1;
54310121 326
327 next;
328
329 @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
330 @filedata);
331 for (@index) {
af19f77d 332 s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
54310121 333 s,#,$dir/$file#,g;
334 # print HTML "$_\n";
335 print HTML "$_\n<P><HR><P>\n";
336 }
337 }
338 print HTML "</DL>\n";
339
340 close(HTML);
341}
342
343
344sub split_on_head {
345 my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
346 my($pod, $dirname, $filename);
347
348 # split the files specified in @splithead on =head[1-6] pod directives
349 print "splitting files by head.\n" if $verbose && $#splithead >= 0;
350 foreach $pod (@splithead) {
351 # figure out the directory name and filename
352 $pod =~ s,^([^/]*)$,/$1,;
353 $pod =~ m,(.*?)/(.*?)(\.pod)?$,;
354 $dirname = $1;
355 $filename = "$2.pod";
356
357 # since we are splitting this file it shouldn't be converted.
358 push(@$ignore, "$podroot/$dirname/$filename");
359
360 # split the pod
361 splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
362 $splitdirs);
363 }
364}
365
366
367sub split_on_item {
368 my($podroot, $splitdirs, $ignore, @splititem) = @_;
369 my($pwd, $dirname, $filename);
370
371 print "splitting files by item.\n" if $verbose && $#splititem >= 0;
372 $pwd = getcwd();
3e3baf6d 373 my $splitter = absolute_path($pwd, "$splitpod/splitpod");
54310121 374 foreach $pod (@splititem) {
375 # figure out the directory to split into
376 $pod =~ s,^([^/]*)$,/$1,;
377 $pod =~ m,(.*?)/(.*?)(\.pod)?$,;
378 $dirname = "$1/$2";
379 $filename = "$2.pod";
380
381 # since we are splitting this file it shouldn't be converted.
382 push(@$ignore, "$podroot/$dirname.pod");
383
384 # split the pod
385 push(@$splitdirs, "$podroot/$dirname");
386 if (! -d "$podroot/$dirname") {
387 mkdir("$podroot/$dirname", 0755) ||
388 die "$0: error creating directory $podroot/$dirname: $!\n";
389 }
390 chdir("$podroot/$dirname") ||
391 die "$0: error changing to directory $podroot/$dirname: $!\n";
3e3baf6d
TB
392 die "$splitter not found. Use '-splitpod dir' option.\n"
393 unless -f $splitter;
394 system("perl", $splitter, "../$filename") &&
395 warn "$0: error running '$splitter ../$filename'"
e05c04ea 396 ." from $podroot/$dirname";
54310121 397 }
398 chdir($pwd);
399}
400
401
402#
403# splitpod - splits a .pod file into several smaller .pod files
404# where a new file is started each time a =head[1-6] pod directive
405# is encountered in the input file.
406#
407sub splitpod {
408 my($pod, $poddir, $htmldir, $splitdirs) = @_;
409 my(@poddata, @filedata, @heads);
410 my($file, $i, $j, $prevsec, $section, $nextsec);
411
412 print "splitting $pod\n" if $verbose;
413
414 # read the file in paragraphs
415 $/ = "";
416 open(SPLITIN, "<$pod") ||
417 die "$0: error opening $pod for input: $!\n";
418 @filedata = <SPLITIN>;
419 close(SPLITIN) ||
420 die "$0: error closing $pod: $!\n";
421
422 # restore the file internally by =head[1-6] sections
423 @poddata = ();
424 for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
425 $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
426 if ($j >= 0) {
427 $poddata[$j] = "" unless defined $poddata[$j];
428 $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
429 }
430 }
431
432 # create list of =head[1-6] sections so that we can rewrite
433 # L<> links as necessary.
434 %heads = ();
435 foreach $i (0..$#poddata) {
436 $heads{htmlize($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
437 }
438
439 # create a directory of a similar name and store all the
440 # files in there
441 $pod =~ s,.*/(.*),$1,; # get the last part of the name
442 $dir = $pod;
443 $dir =~ s/\.pod//g;
444 push(@$splitdirs, "$poddir/$dir");
445 mkdir("$poddir/$dir", 0755) ||
446 die "$0: could not create directory $poddir/$dir: $!\n"
447 unless -d "$poddir/$dir";
448
449 $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
450 $section = "";
451 $nextsec = $1;
452
453 # for each section of the file create a separate pod file
454 for ($i = 0; $i <= $#poddata; $i++) {
455 # determine the "prev" and "next" links
456 $prevsec = $section;
457 $section = $nextsec;
458 if ($i < $#poddata) {
459 $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
460 $nextsec = $1;
461 } else {
462 $nextsec = "";
463 }
464
465 # determine an appropriate filename (this must correspond with
466 # what pod2html will try and guess)
467 # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
468 $file = "$dir/" . htmlize($section) . ".pod";
469
470 # create the new .pod file
471 print "\tcreating $poddir/$file\n" if $verbose;
472 open(SPLITOUT, ">$poddir/$file") ||
473 die "$0: error opening $poddir/$file for output: $!\n";
474 $poddata[$i] =~ s,L<([^<>]*)>,
475 defined $heads{htmlize($1)} ? "L<$dir/$1>" : "L<$1>"
476 ,ge;
477 print SPLITOUT $poddata[$i]."\n\n";
478 print SPLITOUT "=over 4\n\n";
479 print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
480 print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
481 print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
482 print SPLITOUT "=back\n\n";
483 close(SPLITOUT) ||
484 die "$0: error closing $poddir/$file: $!\n";
485 }
486}
487
488
489#
490# installdir - takes care of converting the .pod and .pm files in the
491# current directory to .html files and then installing those.
492#
493sub installdir {
494 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
495 my(@dirlist, @podlist, @pmlist, $doindex);
496
497 @dirlist = (); # directories to recurse on
498 @podlist = (); # .pod files to install
499 @pmlist = (); # .pm files to install
500
501 # should files in this directory get an index?
502 $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
503
504 opendir(DIR, "$podroot/$dir")
505 || die "$0: error opening directory $podroot/$dir: $!\n";
506
507 # find the directories to recurse on
508 @dirlist = map { "$dir/$_" }
509 grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
510 rewinddir(DIR);
511
512 # find all the .pod files within the directory
513 @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
514 grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
515 rewinddir(DIR);
516
517 # find all the .pm files within the directory
518 @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
519 grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
520
521 closedir(DIR);
522
523 # recurse on all subdirectories we kept track of
524 foreach $dir (@dirlist) {
525 installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
526 }
527
528 # install all the pods we found
529 foreach $pod (@podlist) {
530 # check if we should ignore it.
531 next if grep($_ eq "$podroot/$pod.pod", @$ignore);
532
533 # check if a .pm files exists too
534 if (grep($_ eq "$pod.pm", @pmlist)) {
535 print "$0: Warning both `$podroot/$pod.pod' and "
536 . "`$podroot/$pod.pm' exist, using pod\n";
537 push(@ignore, "$pod.pm");
538 }
539 runpod2html("$pod.pod", $doindex);
540 }
541
542 # install all the .pm files we found
543 foreach $pm (@pmlist) {
544 # check if we should ignore it.
545 next if grep($_ eq "$pm.pm", @ignore);
546
547 runpod2html("$pm.pm", $doindex);
548 }
549}
550
551
552#
553# runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
554# file.
555#
556sub runpod2html {
557 my($pod, $doindex) = @_;
558 my($html, $i, $dir, @dirs);
559
560 $html = $pod;
561 $html =~ s/\.(pod|pm)$/.html/g;
562
563 # make sure the destination directories exist
564 @dirs = split("/", $html);
565 $dir = "$htmldir/";
566 for ($i = 0; $i < $#dirs; $i++) {
567 if (! -d "$dir$dirs[$i]") {
568 mkdir("$dir$dirs[$i]", 0755) ||
569 die "$0: error creating directory $dir$dirs[$i]: $!\n";
570 }
571 $dir .= "$dirs[$i]/";
572 }
573
574 # invoke pod2html
575 print "$podroot/$pod => $htmldir/$html\n" if $verbose;
576#system("./pod2html",
577 Pod::Html'pod2html(
578 #Pod::Html'pod2html($pod2html,
579 "--htmlroot=$htmlroot",
580 "--podpath=".join(":", @podpath),
581 "--podroot=$podroot", "--netscape",
582 ($doindex ? "--index" : "--noindex"),
583 "--" . ($recurse ? "" : "no") . "recurse",
584 ($#libpods >= 0) ? "--libpods=" . join(":", @libpods) : "",
585 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
586 die "$0: error running $pod2html: $!\n" if $?;
587}
588
589sub htmlize { htmlify(0, @_) }