3 use Config; # for config options in the makefile
4 use Getopt::Long; # for command-line parsing
12 installhtml - converts a collection of POD pages to HTML format.
16 installhtml [--help] [--podpath=<name>:...:<name>] [--podroot=<name>]
17 [--htmldir=<name>] [--htmlroot=<name>] [--norecurse] [--recurse]
18 [--splithead=<name>,...,<name>] [--splititem=<name>,...,<name>]
19 [--libpods=<name>,...,<name>] [--verbose]
23 I<installhtml> converts a collection of POD pages to a corresponding
24 collection of HTML pages. This is primarily used to convert the pod
25 pages found in the perl distribution.
35 =item B<--podpath> POD search path
37 The list of diretories to search for .pod and .pm files to be converted.
38 Default is `podroot/.'.
40 =item B<--podroot> POD search path base directory
42 The base directory to search for all .pod and .pm files to be converted.
43 Default is current directory.
45 =item B<--htmldir> HTML destination directory
47 The base directory which all HTML files will be written to. This should
48 be a path relative to the filesystem, not the resulting URL.
50 =item B<--htmlroot> URL base directory
52 The base directory which all resulting HTML files will be visible at in
53 a URL. The default is `/'.
55 =item B<--recurse> recurse on subdirectories
57 Whether or not to convert all .pm and .pod files found in subdirectories
58 too. Default is to not recurse.
60 =item B<--splithead> POD files to split on =head directive
62 Colon-seperated list of pod files to split by the =head directive. These
63 files should have names specified relative to podroot.
65 =item B<--splititem> POD files to split on =item directive
67 Colon-seperated list of all pod files to split by the =item directive.
68 I<installhtml> does not do the actual split, rather it invokes I<splitpod>
69 to do the dirty work. As with --splithead, these files should have names
70 specified relative to podroot.
72 =item B<--libpods> library PODs for LE<lt>E<gt> links
74 Colon-seperated list of "library" pod files. This is the same list that
75 will be passed to pod2html when any pod is converted.
77 =item B<--verbose> verbose output
85 The following command-line is an example of the one we use to convert
88 ./installhtml --podpath=lib:ext:pod:vms \
89 --podroot=/usr/src/perl \
90 --htmldir=/perl/nmanual \
91 --htmlroot=/perl/nmanual \
92 --splithead=pod/perlipc.pod \
93 --splititem=pod/perlfunc \
94 --libpods=perlfunc:perlguts:perlvar:perlrun:perlop \
100 Chris Hall E<lt>hallc@cs.colorado.eduE<gt>
106 $usage =<<END_OF_USAGE;
107 Usage: $0 --help --podpath=<name>:...:<name> --podroot=<name>
108 --htmldir=<name> --htmlroot=<name> --norecurse --recurse
109 --splithead=<name>,...,<name> --splititem=<name>,...,<name>
110 --libpods=<name>,...,<name> --verbose
112 --help - this message
113 --podpath - colon-separated list of directories containing .pod and
114 .pm files to be converted (. by default).
115 --podroot - filesystem base directory from which all relative paths in
116 podpath stem (default is .).
117 --htmldir - directory to store resulting html files in relative
118 to the filesystem (\$podroot/html by default).
119 --htmlroot - http-server base directory from which all relative paths
120 in podpath stem (default is /).
121 --libpods - comma-separated list of files to search for =item pod
122 directives in as targets of C<> and implicit links (empty
124 --norecurse - don't recurse on those subdirectories listed in podpath.
126 --recurse - recurse on those subdirectories listed in podpath
127 --splithead - comma-separated list of .pod or .pm files to split. will
128 split each file into several smaller files at every occurance
129 of a pod =head[1-6] directive.
130 --splititem - comma-separated list of .pod or .pm files to split using
132 --splitpod - where the program splitpod can be found (\$podroot/pod by
134 --verbose - self-explanatory.
139 @podpath = ( "." ); # colon-separated list of directories containing .pod
140 # and .pm files to be converted.
141 $podroot = "."; # assume the pods we want are here
142 $htmldir = ""; # nothing for now...
143 $htmlroot = "/"; # default value
144 $recurse = 0; # default behavior
145 @splithead = (); # don't split any files by default
146 @splititem = (); # don't split any files by default
147 $splitpod = ""; # nothing for now.
149 $verbose = 0; # whether or not to print debugging info
151 $pod2html = "pod/pod2html";
154 # parse the command-line
155 $result = GetOptions( qw(
168 usage("invalid parameters") unless $result;
169 parse_command_line();
172 # set these variables to appropriate values if the user didn't specify
174 $htmldir = "$htmlroot/html" unless $htmldir;
175 $splitpod = "$podroot/pod" unless $splitpod;
178 # make sure that the destination directory exists
179 (mkdir($htmldir, 0755) ||
180 die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
183 # the following array will eventually contain files that are to be
184 # ignored in the conversion process. these are files that have been
185 # process by splititem or splithead and should not be converted as a
190 # split pods. its important to do this before convert ANY pods because
191 # it may effect some of the links
192 @splitdirs = (); # files in these directories won't get an index
193 split_on_head($podroot, $htmldir, \@splitdirs, \@ignore, @splithead);
194 split_on_item($podroot, \@splitdirs, \@ignore, @splititem);
197 # convert the pod pages found in @poddirs
198 #warn "converting files\n" if $verbose;
199 #warn "\@ignore\t= @ignore\n" if $verbose;
200 foreach $dir (@podpath) {
201 installdir($dir, $recurse, $podroot, \@splitdirs, \@ignore);
205 # now go through and create master indices for each pod we split
206 foreach $dir (@splititem) {
207 print "creating index $htmldir/$dir.html\n" if $verbose;
208 create_index("$htmldir/$dir.html", "$htmldir/$dir");
211 foreach $dir (@splithead) {
212 $dir .= ".pod" unless $dir =~ /(\.pod|\.pm)$/;
213 # let pod2html create the file
214 runpod2html($dir, 1);
216 # now go through and truncate after the index
217 $dir =~ /^(.*?)(\.pod|\.pm)?$/sm;
218 $file = "$htmldir/$1";
219 print "creating index $file.html\n" if $verbose;
221 # read in everything until what would have been the first =head
222 # directive, patching the index as we go.
223 open(H, "<$file.html") ||
224 die "$0: error opening $file.html for input: $!\n";
229 s,HREF="#(.*)">,HREF="$file/$1.html">,g;
234 # now rewrite the file
235 open(H, ">$file.html") ||
236 die "$0: error opening $file.html for output: $!\n";
241 ##############################################################################
245 warn "$0: @_\n" if @_;
250 sub parse_command_line {
251 usage() if defined $opt_help;
252 $opt_help = ""; # make -w shut up
254 # list of directories
255 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
258 @splithead = split(",", $opt_splithead) if defined $opt_splithead;
259 @splititem = split(",", $opt_splititem) if defined $opt_splititem;
260 @libpods = split(",", $opt_libpods) if defined $opt_libpods;
262 $htmldir = $opt_htmldir if defined $opt_htmldir;
263 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
264 $podroot = $opt_podroot if defined $opt_podroot;
265 $splitpod = $opt_splitpod if defined $opt_splitpod;
267 $recurse = $opt_recurse if defined $opt_recurse;
268 $verbose = $opt_verbose if defined $opt_verbose;
273 my($html, $dir) = @_;
274 my(@files, @filedata, @index, $file);
276 # get the list of .html files in this directory
277 opendir(DIR, $dir) ||
278 die "$0: error opening directory $dir for reading: $!\n";
279 @files = sort(grep(/\.html$/, readdir(DIR)));
282 open(HTML, ">$html") ||
283 die "$0: error opening $html for output: $!\n";
285 # for each .html file in the directory, extract the index
286 # embedded in the file and throw it into the big index.
287 print HTML "<DL COMPACT>\n";
288 foreach $file (@files) {
291 open(IN, "<$dir/$file") ||
292 die "$0: error opening $dir/$file for input: $!\n";
296 # pull out the NAME section
297 ($name) = grep(/NAME=/, @filedata);
298 $name =~ m,/H1>\s(\S+)\s[\s-]*(.*?)\s*$,sm;
299 print HTML qq(<A HREF="$dir/$file">);
300 print HTML "<DT>$1</A><DD>$2\n" if defined $1;
301 # print HTML qq(<A HREF="$dir/$file">$1</A><BR>\n") if defined $1;
305 @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
308 s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$2/s;
311 print HTML "$_\n<P><HR><P>\n";
314 print HTML "</DL>\n";
321 my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
322 my($pod, $dirname, $filename);
324 # split the files specified in @splithead on =head[1-6] pod directives
325 print "splitting files by head.\n" if $verbose && $#splithead >= 0;
326 foreach $pod (@splithead) {
327 # figure out the directory name and filename
328 $pod =~ s,^([^/]*)$,/$1,;
329 $pod =~ m,(.*?)/(.*?)(\.pod)?$,;
331 $filename = "$2.pod";
333 # since we are splitting this file it shouldn't be converted.
334 push(@$ignore, "$podroot/$dirname/$filename");
337 splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
344 my($podroot, $splitdirs, $ignore, @splititem) = @_;
345 my($pwd, $dirname, $filename);
347 print "splitting files by item.\n" if $verbose && $#splititem >= 0;
349 foreach $pod (@splititem) {
350 # figure out the directory to split into
351 $pod =~ s,^([^/]*)$,/$1,;
352 $pod =~ m,(.*?)/(.*?)(\.pod)?$,;
354 $filename = "$2.pod";
356 # since we are splitting this file it shouldn't be converted.
357 push(@$ignore, "$podroot/$dirname.pod");
360 push(@$splitdirs, "$podroot/$dirname");
361 if (! -d "$podroot/$dirname") {
362 mkdir("$podroot/$dirname", 0755) ||
363 die "$0: error creating directory $podroot/$dirname: $!\n";
365 chdir("$podroot/$dirname") ||
366 die "$0: error changing to directory $podroot/$dirname: $!\n";
367 system("./splitpod", "../$filename");
374 # splitpod - splits a .pod file into several smaller .pod files
375 # where a new file is started each time a =head[1-6] pod directive
376 # is encountered in the input file.
379 my($pod, $poddir, $htmldir, $splitdirs) = @_;
380 my(@poddata, @filedata, @heads);
381 my($file, $i, $j, $prevsec, $section, $nextsec);
383 print "splitting $pod\n" if $verbose;
385 # read the file in paragraphs
387 open(SPLITIN, "<$pod") ||
388 die "$0: error opening $pod for input: $!\n";
389 @filedata = <SPLITIN>;
391 die "$0: error closing $pod: $!\n";
393 # restore the file internally by =head[1-6] sections
395 for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
396 $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
398 $poddata[$j] = "" unless defined $poddata[$j];
399 $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
403 # create list of =head[1-6] sections so that we can rewrite
404 # L<> links as necessary.
406 foreach $i (0..$#poddata) {
407 $heads{htmlize($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
410 # create a directory of a similar name and store all the
412 $pod =~ s,.*/(.*),$1,; # get the last part of the name
415 push(@$splitdirs, "$poddir/$dir");
416 mkdir("$poddir/$dir", 0755) ||
417 die "$0: could not create directory $poddir/$dir: $!\n"
418 unless -d "$poddir/$dir";
420 $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
424 # for each section of the file create a separate pod file
425 for ($i = 0; $i <= $#poddata; $i++) {
426 # determine the "prev" and "next" links
429 if ($i < $#poddata) {
430 $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
436 # determine an appropriate filename (this must correspond with
437 # what pod2html will try and guess)
438 # $poddata[$i] =~ /^\s*=head[1-6]\s+(.*)/;
439 $file = "$dir/" . htmlize($section) . ".pod";
441 # create the new .pod file
442 print "\tcreating $poddir/$file\n" if $verbose;
443 open(SPLITOUT, ">$poddir/$file") ||
444 die "$0: error opening $poddir/$file for output: $!\n";
445 $poddata[$i] =~ s,L<([^<>]*)>,
446 defined $heads{htmlize($1)} ? "L<$dir/$1>" : "L<$1>"
448 print SPLITOUT $poddata[$i]."\n\n";
449 print SPLITOUT "=over 4\n\n";
450 print SPLITOUT "=item *\n\nBack to L<$dir/\"$prevsec\">\n\n" if $prevsec;
451 print SPLITOUT "=item *\n\nForward to L<$dir/\"$nextsec\">\n\n" if $nextsec;
452 print SPLITOUT "=item *\n\nUp to L<$dir>\n\n";
453 print SPLITOUT "=back\n\n";
455 die "$0: error closing $poddir/$file: $!\n";
461 # installdir - takes care of converting the .pod and .pm files in the
462 # current directory to .html files and then installing those.
465 my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
466 my(@dirlist, @podlist, @pmlist, $doindex);
468 @dirlist = (); # directories to recurse on
469 @podlist = (); # .pod files to install
470 @pmlist = (); # .pm files to install
472 # should files in this directory get an index?
473 $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
475 opendir(DIR, "$podroot/$dir")
476 || die "$0: error opening directory $podroot/$dir: $!\n";
478 # find the directories to recurse on
479 @dirlist = map { "$dir/$_" }
480 grep(-d "$podroot/$dir/$_" && !/^\.{1,2}/, readdir(DIR)) if $recurse;
483 # find all the .pod files within the directory
484 @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
485 grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
488 # find all the .pm files within the directory
489 @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
490 grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
494 # recurse on all subdirectories we kept track of
495 foreach $dir (@dirlist) {
496 installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
499 # install all the pods we found
500 foreach $pod (@podlist) {
501 # check if we should ignore it.
502 next if grep($_ eq "$podroot/$pod.pod", @$ignore);
504 # check if a .pm files exists too
505 if (grep($_ eq "$pod.pm", @pmlist)) {
506 print "$0: Warning both `$podroot/$pod.pod' and "
507 . "`$podroot/$pod.pm' exist, using pod\n";
508 push(@ignore, "$pod.pm");
510 runpod2html("$pod.pod", $doindex);
513 # install all the .pm files we found
514 foreach $pm (@pmlist) {
515 # check if we should ignore it.
516 next if grep($_ eq "$pm.pm", @ignore);
518 runpod2html("$pm.pm", $doindex);
524 # runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
528 my($pod, $doindex) = @_;
529 my($html, $i, $dir, @dirs);
532 $html =~ s/\.(pod|pm)$/.html/g;
534 # make sure the destination directories exist
535 @dirs = split("/", $html);
537 for ($i = 0; $i < $#dirs; $i++) {
538 if (! -d "$dir$dirs[$i]") {
539 mkdir("$dir$dirs[$i]", 0755) ||
540 die "$0: error creating directory $dir$dirs[$i]: $!\n";
542 $dir .= "$dirs[$i]/";
546 print "$podroot/$pod => $htmldir/$html\n" if $verbose;
547 #system("./pod2html",
549 #Pod::Html'pod2html($pod2html,
550 "--htmlroot=$htmlroot",
551 "--podpath=".join(":", @podpath),
552 "--podroot=$podroot", "--netscape",
553 ($doindex ? "--index" : "--noindex"),
554 "--" . ($recurse ? "" : "no") . "recurse",
555 ($#libpods >= 0) ? "--libpods=" . join(":", @libpods) : "",
556 "--infile=$podroot/$pod", "--outfile=$htmldir/$html");
557 die "$0: error running $pod2html: $!\n" if $?;
560 sub htmlize { htmlify(0, @_) }