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