3 # This file should really be extracted from a .PL file
6 use Config; # for config options in the makefile
8 use Getopt::Long; # for command-line parsing
10 use Pod::Html 'anchorify';
14 installhtml - converts a collection of POD pages to HTML format.
18 installhtml [--help] [--podpath=<name>:...:<name>] [--podroot=<name>]
19 [--htmldir=<name>] [--htmlroot=<name>] [--norecurse] [--recurse]
20 [--splithead=<name>,...,<name>] [--splititem=<name>,...,<name>]
21 [--libpods=<name>,...,<name>] [--verbose]
25 I<installhtml> converts a collection of POD pages to a corresponding
26 collection of HTML pages. This is primarily used to convert the pod
27 pages found in the perl distribution.
37 =item B<--podroot> POD search path base directory
39 The base directory to search for all .pod and .pm files to be converted.
40 Default is current directory.
42 =item B<--podpath> POD search path
44 The list of directories to search for .pod and .pm files to be converted.
45 Default is `podroot/.'.
47 =item B<--recurse> recurse on subdirectories
49 Whether or not to convert all .pm and .pod files found in subdirectories
50 too. Default is to not recurse.
52 =item B<--htmldir> HTML destination directory
54 The base directory which all HTML files will be written to. This should
55 be a path relative to the filesystem, not the resulting URL.
57 =item B<--htmlroot> URL base directory
59 The base directory which all resulting HTML files will be visible at in
60 a URL. The default is `/'.
62 =item B<--splithead> POD files to split on =head directive
64 Comma-separated list of pod files to split by the =head directive. The
65 .pod suffix is optional. These files should have names specified
68 =item B<--splititem> POD files to split on =item directive
70 Comma-separated list of all pod files to split by the =item directive.
71 The .pod suffix is optional. I<installhtml> does not do the actual
72 split, rather it invokes I<splitpod> to do the dirty work. As with
73 --splithead, these files should have names specified relative to podroot.
75 =item B<--splitpod> Directory containing the splitpod program
77 The directory containing the splitpod program. The default is `podroot/pod'.
79 =item B<--libpods> library PODs for LE<lt>E<gt> links
81 Comma-separated list of "library" pod files. This is the same list that
82 will be passed to pod2html when any pod is converted.
84 =item B<--verbose> verbose output
92 The following command-line is an example of the one we use to convert
95 ./installhtml --podpath=lib:ext:pod:vms \
96 --podroot=/usr/src/perl \
97 --htmldir=/perl/nmanual \
98 --htmlroot=/perl/nmanual \
99 --splithead=pod/perlipc \
100 --splititem=pod/perlfunc \
101 --libpods=perlfunc,perlguts,perlvar,perlrun,perlop \
107 Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
115 $usage =<<END_OF_USAGE;
116 Usage: $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
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
133 --norecurse - don't recurse on those subdirectories listed in podpath.
135 --recurse - recurse on those subdirectories listed in podpath
136 --splithead - comma-separated list of .pod or .pm files to split. will
137 split each file into several smaller files at every occurrence
138 of a pod =head[1-6] directive.
139 --splititem - comma-separated list of .pod or .pm files to split using
141 --splitpod - directory where the program splitpod can be found
142 (\$podroot/pod by default).
143 --verbose - self-explanatory.
147 my (@libpods, @podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
148 @splititem, $splitpod, $verbose, $pod2html);
151 @podpath = ( "." ); # colon-separated list of directories containing .pod
152 # and .pm files to be converted.
153 $podroot = "."; # assume the pods we want are here
154 $htmldir = ""; # nothing for now...
155 $htmlroot = "/"; # default value
156 $recurse = 0; # default behavior
157 @splithead = (); # don't split any files by default
158 @splititem = (); # don't split any files by default
159 $splitpod = ""; # nothing for now.
161 $verbose = 0; # whether or not to print debugging info
163 $pod2html = "pod/pod2html";
165 usage("") unless @ARGV;
167 # Overcome shell's p1,..,p8 limitation.
168 # See vms/descrip_mms.template -> descrip.mms for invokation.
169 if ( $^O eq 'VMS' ) { @ARGV = split(/\s+/,$ARGV[0]); }
171 use vars qw( %Options );
173 # parse the command-line
174 my $result = GetOptions( \%Options, qw(
187 usage("invalid parameters") unless $result;
188 parse_command_line();
191 # set these variables to appropriate values if the user didn't specify
193 $htmldir = "$htmlroot/html" unless $htmldir;
194 $splitpod = "$podroot/pod" unless $splitpod;
197 # make sure that the destination directory exists
198 (mkdir($htmldir, 0755) ||
199 die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
202 # the following array will eventually contain files that are to be
203 # ignored in the conversion process. these are files that have been
204 # process by splititem or splithead and should not be converted as a
209 # split pods. its important to do this before convert ANY pods because
210 # it may effect some of the links
211 @splitdirs = (); # files in these directories won't get an index
212 split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
213 split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
216 # convert the pod pages found in @poddirs
217 #warn "converting files\n" if $verbose;
218 #warn "\@ignore\t= @ignore\n" if $verbose;
219 foreach my $dir (@podpath) {
220 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
224 # now go through and create master indices for each pod we split
225 foreach my $dir (@splititem) {
226 print "creating index $htmldir/$dir.html\n" if $verbose;
227 create_index("$htmldir/$dir.html", "$htmldir/$dir");
230 foreach my $dir (@splithead) {
231 (my $pod = $dir) =~ s,^.*/,,;
232 $dir .= ".pod" unless $dir =~ /(\.pod|\.pm)$/;
233 # let pod2html create the file
234 runpod2html($dir, 1);
236 # now go through and truncate after the index
237 $dir =~ /^(.*?)(\.pod|\.pm)?$/sm;
238 my $file = "$htmldir/$1";
239 print "creating index $file.html\n" if $verbose;
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";
248 last if /name="name"/i;
249 $_ =~ s{href="#(.*)">}{
250 my $url = "$pod/$1.html" ;
251 $url = Pod::Html::relativize_url( $url, "$file.html" )
252 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' );
259 # now rewrite the file
260 open(H, ">$file.html") ||
261 die "$0: error opening $file.html for output: $!\n";
262 print H "@data", "\n";
266 ##############################################################################
270 warn "$0: @_\n" if @_;
275 sub parse_command_line {
276 usage() if defined $Options{help};
277 $Options{help} = ""; # make -w shut up
279 # list of directories
280 @podpath = split(":", $Options{podpath}) if defined $Options{podpath};
283 @splithead = split(",", $Options{splithead}) if defined $Options{splithead};
284 @splititem = split(",", $Options{splititem}) if defined $Options{splititem};
285 @libpods = split(",", $Options{libpods}) if defined $Options{libpods};
287 $htmldir = $Options{htmldir} if defined $Options{htmldir};
288 $htmlroot = $Options{htmlroot} if defined $Options{htmlroot};
289 $podroot = $Options{podroot} if defined $Options{podroot};
290 $splitpod = $Options{splitpod} if defined $Options{splitpod};
292 $recurse = $Options{recurse} if defined $Options{recurse};
293 $verbose = $Options{verbose} if defined $Options{verbose};
298 my($html, $dir) = @_;
299 (my $pod = $dir) =~ s,^.*/,,;
300 my(@files, @filedata, @index, $file);
304 # get the list of .html files in this directory
305 opendir(DIR, $dir) ||
306 die "$0: error opening directory $dir for reading: $!\n";
307 @files = sort(grep(/\.html?$/, readdir(DIR)));
310 open(HTML, ">$html") ||
311 die "$0: error opening $html for output: $!\n";
313 # for each .html file in the directory, extract the index
314 # embedded in the file and throw it into the big index.
315 print HTML "<DL COMPACT>\n";
316 foreach $file (@files) {
319 open(IN, "<$dir/$file") ||
320 die "$0: error opening $dir/$file for input: $!\n";
324 # pull out the NAME section
326 ($name) = grep(/name="name"/i, @filedata);
327 ($lcp1,$lcp2) = ($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,smi);
328 if (defined $lcp1 and $lcp1 =~ m,^<P>$,i) { # Uninteresting. Try again.
329 ($lcp1,$lcp2) = ($name =~ m,/H1>\s<P>\s*(\S+)\s[\s-]*(.*?)\s*$,smi);
331 my $url= "$pod/$file" ;
332 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
333 $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
337 print HTML qq(<DT><A HREF="$url">);
338 print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
343 @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
346 s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
348 print HTML "$_\n<P><HR><P>\n";
351 print HTML "</DL>\n";
358 my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
359 my($pod, $dirname, $filename);
361 # split the files specified in @splithead on =head[1-6] pod directives
362 print "splitting files by head.\n" if $verbose && $#splithead >= 0;
363 foreach $pod (@splithead) {
364 # figure out the directory name and filename
365 $pod =~ s,^([^/]*)$,/$1,;
366 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
368 $filename = "$2.pod";
370 # since we are splitting this file it shouldn't be converted.
371 push(@$ignore, "$podroot/$dirname/$filename");
374 splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
381 my($podroot, $splitdirs, $ignore, @splititem) = @_;
382 my($pwd, $dirname, $filename);
384 print "splitting files by item.\n" if $verbose && $#splititem >= 0;
386 my $splitter = File::Spec->rel2abs("$splitpod/splitpod", $pwd);
387 my $perl = File::Spec->rel2abs($^X, $pwd);
388 foreach my $pod (@splititem) {
389 # figure out the directory to split into
390 $pod =~ s,^([^/]*)$,/$1,;
391 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
393 $filename = "$2.pod";
395 # since we are splitting this file it shouldn't be converted.
396 push(@$ignore, "$podroot/$dirname.pod");
399 push(@$splitdirs, "$podroot/$dirname");
400 if (! -d "$podroot/$dirname") {
401 mkdir("$podroot/$dirname", 0755) ||
402 die "$0: error creating directory $podroot/$dirname: $!\n";
404 chdir("$podroot/$dirname") ||
405 die "$0: error changing to directory $podroot/$dirname: $!\n";
406 die "$splitter not found. Use '-splitpod dir' option.\n"
408 system($perl, $splitter, "../$filename") &&
409 warn "$0: error running '$splitter ../$filename'"
410 ." from $podroot/$dirname";
417 # splitpod - splits a .pod file into several smaller .pod files
418 # where a new file is started each time a =head[1-6] pod directive
419 # is encountered in the input file.
422 my($pod, $poddir, $htmldir, $splitdirs) = @_;
423 my(@poddata, @filedata, @heads);
424 my($file, $i, $j, $prevsec, $section, $nextsec);
426 print "splitting $pod\n" if $verbose;
428 # read the file in paragraphs
430 open(SPLITIN, "<$pod") ||
431 die "$0: error opening $pod for input: $!\n";
432 @filedata = <SPLITIN>;
434 die "$0: error closing $pod: $!\n";
436 # restore the file internally by =head[1-6] sections
438 for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
439 $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
441 $poddata[$j] = "" unless defined $poddata[$j];
442 $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
446 # create list of =head[1-6] sections so that we can rewrite
447 # L<> links as necessary.
449 foreach $i (0..$#poddata) {
450 $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
453 # create a directory of a similar name and store all the
455 $pod =~ s,.*/(.*),$1,; # get the last part of the name
458 push(@$splitdirs, "$poddir/$dir");
459 mkdir("$poddir/$dir", 0755) ||
460 die "$0: could not create directory $poddir/$dir: $!\n"
461 unless -d "$poddir/$dir";
463 $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
467 # for each section of the file create a separate pod file
468 for ($i = 0; $i <= $#poddata; $i++) {
469 # determine the "prev" and "next" links
472 if ($i < $#poddata) {
473 $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
479 # determine an appropriate filename (this must correspond with
480 # what pod2html will try and guess)
481 # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
482 $file = "$dir/" . anchorify($section) . ".pod";
484 # create the new .pod file
485 print "\tcreating $poddir/$file\n" if $verbose;
486 open(SPLITOUT, ">$poddir/$file") ||
487 die "$0: error opening $poddir/$file for output: $!\n";
488 $poddata[$i] =~ s,L<([^<>]*)>,
489 defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
491 print SPLITOUT $poddata[$i]."\n\n";
492 print SPLITOUT "=over 4\n\n";
493 print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
494 print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
495 print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
496 print SPLITOUT "=back\n\n";
498 die "$0: error closing $poddir/$file: $!\n";
504 # installdir - takes care of converting the .pod and .pm files in the
505 # current directory to .html files and then installing those.
508 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
509 my(@dirlist, @podlist, @pmlist, $doindex);
511 @dirlist = (); # directories to recurse on
512 @podlist = (); # .pod files to install
513 @pmlist = (); # .pm files to install
515 # should files in this directory get an index?
516 $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
518 opendir(DIR, "$podroot/$dir")
519 || die "$0: error opening directory $podroot/$dir: $!\n";
521 # find the directories to recurse on
522 @dirlist = map { if ($^O eq 'VMS') {/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}}
523 grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
526 # find all the .pod files within the directory
527 @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
528 grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
531 # find all the .pm files within the directory
532 @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
533 grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
537 # recurse on all subdirectories we kept track of
538 foreach $dir (@dirlist) {
539 installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
542 # install all the pods we found
543 foreach my $pod (@podlist) {
544 # check if we should ignore it.
545 next if grep($_ eq "$podroot/$pod.pod", @$ignore);
547 # check if a .pm files exists too
548 if (grep($_ eq $pod, @pmlist)) {
549 print "$0: Warning both `$podroot/$pod.pod' and "
550 . "`$podroot/$pod.pm' exist, using pod\n";
551 push(@ignore, "$pod.pm");
553 runpod2html("$pod.pod", $doindex);
556 # install all the .pm files we found
557 foreach my $pm (@pmlist) {
558 # check if we should ignore it.
559 next if grep($_ eq "$pm.pm", @ignore);
561 runpod2html("$pm.pm", $doindex);
567 # runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
571 my($pod, $doindex) = @_;
572 my($html, $i, $dir, @dirs);
575 $html =~ s/\.(pod|pm)$/.html/g;
577 # make sure the destination directories exist
578 @dirs = split("/", $html);
580 for ($i = 0; $i < $#dirs; $i++) {
581 if (! -d "$dir$dirs[$i]") {
582 mkdir("$dir$dirs[$i]", 0755) ||
583 die "$0: error creating directory $dir$dirs[$i]: $!\n";
585 $dir .= "$dirs[$i]/";
589 print "$podroot/$pod => $htmldir/$html\n" if $verbose;
591 "--htmldir=$htmldir",
592 "--htmlroot=$htmlroot",
593 "--podpath=".join(":", @podpath),
594 "--podroot=$podroot", "--netscape",
596 ($doindex ? "--index" : "--noindex"),
597 "--" . ($recurse ? "" : "no") . "recurse",
598 ($#libpods >= 0) ? "--libpods=" . join(":", @libpods) : "",
599 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
600 die "$0: error running $pod2html: $!\n" if $?;