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>] [--ignore=<name>,...,<name>]
26 I<installhtml> converts a collection of POD pages to a corresponding
27 collection of HTML pages. This is primarily used to convert the pod
28 pages found in the perl distribution.
38 =item B<--podroot> POD search path base directory
40 The base directory to search for all .pod and .pm files to be converted.
41 Default is current directory.
43 =item B<--podpath> POD search path
45 The list of directories to search for .pod and .pm files to be converted.
46 Default is 'podroot/.'.
48 =item B<--recurse> recurse on subdirectories
50 Whether or not to convert all .pm and .pod files found in subdirectories
51 too. Default is to not recurse.
53 =item B<--htmldir> HTML destination directory
55 The base directory which all HTML files will be written to. This should
56 be a path relative to the filesystem, not the resulting URL.
58 =item B<--htmlroot> URL base directory
60 The base directory which all resulting HTML files will be visible at in
61 a URL. The default is '/'.
63 =item B<--splithead> POD files to split on =head directive
65 Comma-separated list of pod files to split by the =head directive. The
66 .pod suffix is optional. These files should have names specified
69 =item B<--splititem> POD files to split on =item directive
71 Comma-separated list of all pod files to split by the =item directive.
72 The .pod suffix is optional. I<installhtml> does not do the actual
73 split, rather it invokes I<splitpod> to do the dirty work. As with
74 --splithead, these files should have names specified relative to podroot.
76 =item B<--splitpod> Directory containing the splitpod program
78 The directory containing the splitpod program. The default is 'podroot/pod'.
80 =item B<--libpods> library PODs for LE<lt>E<gt> links
82 Comma-separated list of "library" pod files. This is the same list that
83 will be passed to pod2html when any pod is converted.
85 =item B<--ignore> files to be ignored
87 Comma-separated of files that shouldn't be installed, given relative
90 =item B<--verbose> verbose output
98 The following command-line is an example of the one we use to convert
101 ./installhtml --podpath=lib:ext:pod:vms \
102 --podroot=/usr/src/perl \
103 --htmldir=/perl/nmanual \
104 --htmlroot=/perl/nmanual \
105 --splithead=pod/perlipc \
106 --splititem=pod/perlfunc \
107 --libpods=perlfunc,perlguts,perlvar,perlrun,perlop \
113 Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
121 $usage =<<END_OF_USAGE;
122 Usage: $0 --help --podpath=<name>:...:<name> --podroot=<name>
123 --htmldir=<name> --htmlroot=<name> --norecurse --recurse
124 --splithead=<name>,...,<name> --splititem=<name>,...,<name>
125 --libpods=<name>,...,<name> --ignore=<name>,...,<name> --verbose
127 --help - this message
128 --podpath - colon-separated list of directories containing .pod and
129 .pm files to be converted (. by default).
130 --podroot - filesystem base directory from which all relative paths in
131 podpath stem (default is .).
132 --htmldir - directory to store resulting html files in relative
133 to the filesystem (\$podroot/html by default).
134 --htmlroot - http-server base directory from which all relative paths
135 in podpath stem (default is /).
136 --libpods - comma-separated list of files to search for =item pod
137 directives in as targets of C<> and implicit links (empty
139 --norecurse - don't recurse on those subdirectories listed in podpath.
141 --recurse - recurse on those subdirectories listed in podpath
142 --splithead - comma-separated list of .pod or .pm files to split. will
143 split each file into several smaller files at every occurrence
144 of a pod =head[1-6] directive.
145 --splititem - comma-separated list of .pod or .pm files to split using
147 --splitpod - directory where the program splitpod can be found
148 (\$podroot/pod by default).
149 --ignore - comma-separated list of files that shouldn't be installed.
150 --verbose - self-explanatory.
154 my (@libpods, @podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
155 @splititem, $splitpod, $verbose, $pod2html, @ignore);
158 @podpath = ( "." ); # colon-separated list of directories containing .pod
159 # and .pm files to be converted.
160 $podroot = "."; # assume the pods we want are here
161 $htmldir = ""; # nothing for now...
162 $htmlroot = "/"; # default value
163 $recurse = 0; # default behavior
164 @splithead = (); # don't split any files by default
165 @splititem = (); # don't split any files by default
166 $splitpod = ""; # nothing for now.
168 $verbose = 0; # whether or not to print debugging info
170 $pod2html = "pod/pod2html";
172 usage("") unless @ARGV;
174 # Overcome shell's p1,..,p8 limitation.
175 # See vms/descrip_mms.template -> descrip.mms for invokation.
176 if ( $^O eq 'VMS' ) { @ARGV = split(/\s+/,$ARGV[0]); }
178 use vars qw( %Options );
180 # parse the command-line
181 my $result = GetOptions( \%Options, qw(
195 usage("invalid parameters") unless $result;
196 parse_command_line();
199 # set these variables to appropriate values if the user didn't specify
201 $htmldir = "$htmlroot/html" unless $htmldir;
202 $splitpod = "$podroot/pod" unless $splitpod;
205 # make sure that the destination directory exists
206 (mkdir($htmldir, 0755) ||
207 die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
210 # the following array will eventually contain files that are to be
211 # ignored in the conversion process. these are files that have been
212 # process by splititem or splithead and should not be converted as a
216 # split pods. It's important to do this before convert ANY pods because
217 # it may affect some of the links
218 @splitdirs = (); # files in these directories won't get an index
219 split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
220 split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
223 # convert the pod pages found in @poddirs
224 #warn "converting files\n" if $verbose;
225 #warn "\@ignore\t= @ignore\n" if $verbose;
226 foreach my $dir (@podpath) {
227 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
231 # now go through and create master indices for each pod we split
232 foreach my $dir (@splititem) {
233 print "creating index $htmldir/$dir.html\n" if $verbose;
234 create_index("$htmldir/$dir.html", "$htmldir/$dir");
237 foreach my $dir (@splithead) {
238 (my $pod = $dir) =~ s,^.*/,,;
239 $dir .= ".pod" unless $dir =~ /(\.pod|\.pm)$/;
240 # let pod2html create the file
241 runpod2html($dir, 1);
243 # now go through and truncate after the index
244 $dir =~ /^(.*?)(\.pod|\.pm)?$/sm;
245 my $file = "$htmldir/$1";
246 print "creating index $file.html\n" if $verbose;
248 # read in everything until what would have been the first =head
249 # directive, patching the index as we go.
250 open(H, "<$file.html") ||
251 die "$0: error opening $file.html for input: $!\n";
255 last if /name="name"/i;
256 $_ =~ s{href="#(.*)">}{
257 my $url = "$pod/$1.html" ;
258 $url = Pod::Html::relativize_url( $url, "$file.html" )
259 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' );
266 # now rewrite the file
267 open(H, ">$file.html") ||
268 die "$0: error opening $file.html for output: $!\n";
269 print H "@data", "\n";
273 ##############################################################################
277 warn "$0: @_\n" if @_;
282 sub parse_command_line {
283 usage() if defined $Options{help};
284 $Options{help} = ""; # make -w shut up
286 # list of directories
287 @podpath = split(":", $Options{podpath}) if defined $Options{podpath};
290 @splithead = split(",", $Options{splithead}) if defined $Options{splithead};
291 @splititem = split(",", $Options{splititem}) if defined $Options{splititem};
292 @libpods = split(",", $Options{libpods}) if defined $Options{libpods};
294 $htmldir = $Options{htmldir} if defined $Options{htmldir};
295 $htmlroot = $Options{htmlroot} if defined $Options{htmlroot};
296 $podroot = $Options{podroot} if defined $Options{podroot};
297 $splitpod = $Options{splitpod} if defined $Options{splitpod};
299 $recurse = $Options{recurse} if defined $Options{recurse};
300 $verbose = $Options{verbose} if defined $Options{verbose};
302 @ignore = map "$podroot/$_", split(",", $Options{ignore}) if defined $Options{ignore};
307 my($html, $dir) = @_;
308 (my $pod = $dir) =~ s,^.*/,,;
309 my(@files, @filedata, @index, $file);
313 # get the list of .html files in this directory
314 opendir(DIR, $dir) ||
315 die "$0: error opening directory $dir for reading: $!\n";
316 @files = sort(grep(/\.html?$/, readdir(DIR)));
319 open(HTML, ">$html") ||
320 die "$0: error opening $html for output: $!\n";
322 # for each .html file in the directory, extract the index
323 # embedded in the file and throw it into the big index.
324 print HTML "<DL COMPACT>\n";
325 foreach $file (@files) {
328 open(IN, "<$dir/$file") ||
329 die "$0: error opening $dir/$file for input: $!\n";
333 # pull out the NAME section
335 ($name) = grep(/name="name"/i, @filedata);
336 ($lcp1,$lcp2) = ($name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,smi);
337 if (defined $lcp1 and $lcp1 =~ m,^<P>$,i) { # Uninteresting. Try again.
338 ($lcp1,$lcp2) = ($name =~ m,/H1>\s<P>\s*(\S+)\s[\s-]*(.*?)\s*$,smi);
340 my $url= "$pod/$file" ;
341 if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
342 $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
346 print HTML qq(<DT><A HREF="$url">);
347 print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
352 @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
355 s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
357 print HTML "$_\n<P><HR><P>\n";
360 print HTML "</DL>\n";
367 my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
368 my($pod, $dirname, $filename);
370 # split the files specified in @splithead on =head[1-6] pod directives
371 print "splitting files by head.\n" if $verbose && $#splithead >= 0;
372 foreach $pod (@splithead) {
373 # figure out the directory name and filename
374 $pod =~ s,^([^/]*)$,/$1,;
375 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
377 $filename = "$2.pod";
379 # since we are splitting this file it shouldn't be converted.
380 push(@$ignore, "$podroot/$dirname/$filename");
383 splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
390 my($podroot, $splitdirs, $ignore, @splititem) = @_;
391 my($pwd, $dirname, $filename);
393 print "splitting files by item.\n" if $verbose && $#splititem >= 0;
395 my $splitter = File::Spec->rel2abs("$splitpod/splitpod", $pwd);
396 my $perl = File::Spec->rel2abs($^X, $pwd);
397 foreach my $pod (@splititem) {
398 # figure out the directory to split into
399 $pod =~ s,^([^/]*)$,/$1,;
400 $pod =~ m,(.*)/(.*?)(\.pod)?$,;
402 $filename = "$2.pod";
404 # since we are splitting this file it shouldn't be converted.
405 push(@$ignore, "$podroot/$dirname.pod");
408 push(@$splitdirs, "$podroot/$dirname");
409 if (! -d "$podroot/$dirname") {
410 mkdir("$podroot/$dirname", 0755) ||
411 die "$0: error creating directory $podroot/$dirname: $!\n";
413 chdir("$podroot/$dirname") ||
414 die "$0: error changing to directory $podroot/$dirname: $!\n";
415 die "$splitter not found. Use '-splitpod dir' option.\n"
417 system($perl, $splitter, "../$filename") &&
418 warn "$0: error running '$splitter ../$filename'"
419 ." from $podroot/$dirname";
426 # splitpod - splits a .pod file into several smaller .pod files
427 # where a new file is started each time a =head[1-6] pod directive
428 # is encountered in the input file.
431 my($pod, $poddir, $htmldir, $splitdirs) = @_;
432 my(@poddata, @filedata, @heads);
433 my($file, $i, $j, $prevsec, $section, $nextsec);
435 print "splitting $pod\n" if $verbose;
437 # read the file in paragraphs
439 open(SPLITIN, "<$pod") ||
440 die "$0: error opening $pod for input: $!\n";
441 @filedata = <SPLITIN>;
443 die "$0: error closing $pod: $!\n";
445 # restore the file internally by =head[1-6] sections
447 for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
448 $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
450 $poddata[$j] = "" unless defined $poddata[$j];
451 $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
455 # create list of =head[1-6] sections so that we can rewrite
456 # L<> links as necessary.
458 foreach $i (0..$#poddata) {
459 $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
462 # create a directory of a similar name and store all the
464 $pod =~ s,.*/(.*),$1,; # get the last part of the name
467 push(@$splitdirs, "$poddir/$dir");
468 mkdir("$poddir/$dir", 0755) ||
469 die "$0: could not create directory $poddir/$dir: $!\n"
470 unless -d "$poddir/$dir";
472 $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
476 # for each section of the file create a separate pod file
477 for ($i = 0; $i <= $#poddata; $i++) {
478 # determine the "prev" and "next" links
481 if ($i < $#poddata) {
482 $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
488 # determine an appropriate filename (this must correspond with
489 # what pod2html will try and guess)
490 # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
491 $file = "$dir/" . anchorify($section) . ".pod";
493 # create the new .pod file
494 print "\tcreating $poddir/$file\n" if $verbose;
495 open(SPLITOUT, ">$poddir/$file") ||
496 die "$0: error opening $poddir/$file for output: $!\n";
497 $poddata[$i] =~ s,L<([^<>]*)>,
498 defined $heads{anchorify($1)} ? "L<$dir/$1>" : "L<$1>"
500 print SPLITOUT $poddata[$i]."\n\n";
501 print SPLITOUT "=over 4\n\n";
502 print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
503 print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
504 print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
505 print SPLITOUT "=back\n\n";
507 die "$0: error closing $poddir/$file: $!\n";
513 # installdir - takes care of converting the .pod and .pm files in the
514 # current directory to .html files and then installing those.
517 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
518 my(@dirlist, @podlist, @pmlist, $doindex);
520 @dirlist = (); # directories to recurse on
521 @podlist = (); # .pod files to install
522 @pmlist = (); # .pm files to install
524 # should files in this directory get an index?
525 $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
527 opendir(DIR, "$podroot/$dir")
528 || die "$0: error opening directory $podroot/$dir: $!\n";
530 # find the directories to recurse on
531 @dirlist = map { if ($^O eq 'VMS') {/^(.*)\.dir$/i; "$dir/$1";} else {"$dir/$_";}}
532 grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
535 # find all the .pod files within the directory
536 @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
537 grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
540 # find all the .pm files within the directory
541 @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
542 grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
546 # recurse on all subdirectories we kept track of
547 foreach $dir (@dirlist) {
548 installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
551 # install all the pods we found
552 foreach my $pod (@podlist) {
553 # check if we should ignore it.
554 next if $pod =~ m(/t/); # comes from a test file
555 next if grep($_ eq "$pod.pod", @$ignore);
557 # check if a .pm files exists too
558 if (grep($_ eq $pod, @pmlist)) {
559 print "$0: Warning both '$podroot/$pod.pod' and "
560 . "'$podroot/$pod.pm' exist, using pod\n";
561 push(@ignore, "$pod.pm");
563 runpod2html("$pod.pod", $doindex);
566 # install all the .pm files we found
567 foreach my $pm (@pmlist) {
568 # check if we should ignore it.
569 next if $pm =~ m(/t/); # comes from a test file
570 next if grep($_ eq "$pm.pm", @ignore);
572 runpod2html("$pm.pm", $doindex);
578 # runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
582 my($pod, $doindex) = @_;
583 my($html, $i, $dir, @dirs);
586 $html =~ s/\.(pod|pm)$/.html/g;
588 # make sure the destination directories exist
589 @dirs = split("/", $html);
591 for ($i = 0; $i < $#dirs; $i++) {
592 if (! -d "$dir$dirs[$i]") {
593 mkdir("$dir$dirs[$i]", 0755) ||
594 die "$0: error creating directory $dir$dirs[$i]: $!\n";
596 $dir .= "$dirs[$i]/";
600 print "$podroot/$pod => $htmldir/$html\n" if $verbose;
602 "--htmldir=$htmldir",
603 "--htmlroot=$htmlroot",
604 "--podpath=".join(":", @podpath),
605 "--podroot=$podroot", "--netscape",
607 ($doindex ? "--index" : "--noindex"),
608 "--" . ($recurse ? "" : "no") . "recurse",
609 ($#libpods >= 0) ? "--libpods=" . join(":", @libpods) : "",
610 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
611 die "$0: error running $pod2html: $!\n" if $?;