ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/staticperl/perl/installhtml
Revision: 1.1
Committed: Thu Jun 30 14:26:41 2005 UTC (21 years, 3 months ago) by root
Branch: MAIN
CVS Tags: PERL-5-8-7, HEAD
Branch point for: PERL
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!./perl -Ilib -w
2    
3     # This file should really be extracted from a .PL file
4    
5     use strict;
6     use Config; # for config options in the makefile
7     use File::Spec;
8     use Getopt::Long; # for command-line parsing
9     use Cwd;
10     use Pod::Html 'anchorify';
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     Comma-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     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.
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     Comma-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     my $usage;
114    
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
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
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
140     splitpod.
141     --splitpod - directory where the program splitpod can be found
142     (\$podroot/pod by default).
143     --verbose - self-explanatory.
144    
145     END_OF_USAGE
146    
147     my (@libpods, @podpath, $podroot, $htmldir, $htmlroot, $recurse, @splithead,
148     @splititem, $splitpod, $verbose, $pod2html);
149    
150     @libpods = ();
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.
160    
161     $verbose = 0; # whether or not to print debugging info
162    
163     $pod2html = "pod/pod2html";
164    
165     usage("") unless @ARGV;
166    
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]); }
170    
171     use vars qw( %Options );
172    
173     # parse the command-line
174     my $result = GetOptions( \%Options, qw(
175     help
176     podpath=s
177     podroot=s
178     htmldir=s
179     htmlroot=s
180     libpods=s
181     recurse!
182     splithead=s
183     splititem=s
184     splitpod=s
185     verbose
186     ));
187     usage("invalid parameters") unless $result;
188     parse_command_line();
189    
190    
191     # set these variables to appropriate values if the user didn't specify
192     # values for them.
193     $htmldir = "$htmlroot/html" unless $htmldir;
194     $splitpod = "$podroot/pod" unless $splitpod;
195    
196    
197     # make sure that the destination directory exists
198     (mkdir($htmldir, 0755) ||
199     die "$0: cannot make directory $htmldir: $!\n") if ! -d $htmldir;
200    
201    
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
205     # result.
206     my @ignore = ();
207     my @splitdirs;
208    
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);
214    
215    
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);
221     }
222    
223    
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");
228     }
229    
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);
235    
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;
240    
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";
245     $/ = "";
246     my @data = ();
247     while (<H>) {
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 '' );
253     "href=\"$url\">" ;
254     }egi;
255     push @data, $_;
256     }
257     close(H);
258    
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";
263     close(H);
264     }
265    
266     ##############################################################################
267    
268    
269     sub usage {
270     warn "$0: @_\n" if @_;
271     die $usage;
272     }
273    
274    
275     sub parse_command_line {
276     usage() if defined $Options{help};
277     $Options{help} = ""; # make -w shut up
278    
279     # list of directories
280     @podpath = split(":", $Options{podpath}) if defined $Options{podpath};
281    
282     # lists of files
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};
286    
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};
291    
292     $recurse = $Options{recurse} if defined $Options{recurse};
293     $verbose = $Options{verbose} if defined $Options{verbose};
294     }
295    
296    
297     sub create_index {
298     my($html, $dir) = @_;
299     (my $pod = $dir) =~ s,^.*/,,;
300     my(@files, @filedata, @index, $file);
301     my($lcp1,$lcp2);
302    
303    
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)));
308     closedir(DIR);
309    
310     open(HTML, ">$html") ||
311     die "$0: error opening $html for output: $!\n";
312    
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) {
317     $/ = "";
318    
319     open(IN, "<$dir/$file") ||
320     die "$0: error opening $dir/$file for input: $!\n";
321     @filedata = <IN>;
322     close(IN);
323    
324     # pull out the NAME section
325     my $name;
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);
330     }
331     my $url= "$pod/$file" ;
332     if ( ! defined $Options{htmlroot} || $Options{htmlroot} eq '' ) {
333     $url = Pod::Html::relativize_url( "$pod/$file", $html ) ;
334     }
335    
336     if (defined $lcp1) {
337     print HTML qq(<DT><A HREF="$url">);
338     print HTML "$lcp1</A></DT><DD>$lcp2</DD>\n";
339     }
340    
341     next;
342    
343     @index = grep(/<!-- INDEX BEGIN -->.*<!-- INDEX END -->/s,
344     @filedata);
345     for (@index) {
346     s/<!-- INDEX BEGIN -->(\s*<!--)(.*)(-->\s*)<!-- INDEX END -->/$lcp2/s;
347     s,#,$dir/$file#,g;
348     print HTML "$_\n<P><HR><P>\n";
349     }
350     }
351     print HTML "</DL>\n";
352    
353     close(HTML);
354     }
355    
356    
357     sub split_on_head {
358     my($podroot, $htmldir, $splitdirs, $ignore, @splithead) = @_;
359     my($pod, $dirname, $filename);
360    
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)?$,;
367     $dirname = $1;
368     $filename = "$2.pod";
369    
370     # since we are splitting this file it shouldn't be converted.
371     push(@$ignore, "$podroot/$dirname/$filename");
372    
373     # split the pod
374     splitpod("$podroot/$dirname/$filename", "$podroot/$dirname", $htmldir,
375     $splitdirs);
376     }
377     }
378    
379    
380     sub split_on_item {
381     my($podroot, $splitdirs, $ignore, @splititem) = @_;
382     my($pwd, $dirname, $filename);
383    
384     print "splitting files by item.\n" if $verbose && $#splititem >= 0;
385     $pwd = getcwd();
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)?$,;
392     $dirname = "$1/$2";
393     $filename = "$2.pod";
394    
395     # since we are splitting this file it shouldn't be converted.
396     push(@$ignore, "$podroot/$dirname.pod");
397    
398     # split the 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";
403     }
404     chdir("$podroot/$dirname") ||
405     die "$0: error changing to directory $podroot/$dirname: $!\n";
406     die "$splitter not found. Use '-splitpod dir' option.\n"
407     unless -f $splitter;
408     system($perl, $splitter, "../$filename") &&
409     warn "$0: error running '$splitter ../$filename'"
410     ." from $podroot/$dirname";
411     }
412     chdir($pwd);
413     }
414    
415    
416     #
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.
420     #
421     sub splitpod {
422     my($pod, $poddir, $htmldir, $splitdirs) = @_;
423     my(@poddata, @filedata, @heads);
424     my($file, $i, $j, $prevsec, $section, $nextsec);
425    
426     print "splitting $pod\n" if $verbose;
427    
428     # read the file in paragraphs
429     $/ = "";
430     open(SPLITIN, "<$pod") ||
431     die "$0: error opening $pod for input: $!\n";
432     @filedata = <SPLITIN>;
433     close(SPLITIN) ||
434     die "$0: error closing $pod: $!\n";
435    
436     # restore the file internally by =head[1-6] sections
437     @poddata = ();
438     for ($i = 0, $j = -1; $i <= $#filedata; $i++) {
439     $j++ if ($filedata[$i] =~ /^\s*=head[1-6]/);
440     if ($j >= 0) {
441     $poddata[$j] = "" unless defined $poddata[$j];
442     $poddata[$j] .= "\n$filedata[$i]" if $j >= 0;
443     }
444     }
445    
446     # create list of =head[1-6] sections so that we can rewrite
447     # L<> links as necessary.
448     my %heads = ();
449     foreach $i (0..$#poddata) {
450     $heads{anchorify($1)} = 1 if $poddata[$i] =~ /=head[1-6]\s+(.*)/;
451     }
452    
453     # create a directory of a similar name and store all the
454     # files in there
455     $pod =~ s,.*/(.*),$1,; # get the last part of the name
456     my $dir = $pod;
457     $dir =~ s/\.pod//g;
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";
462    
463     $poddata[0] =~ /^\s*=head[1-6]\s+(.*)/;
464     $section = "";
465     $nextsec = $1;
466    
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
470     $prevsec = $section;
471     $section = $nextsec;
472     if ($i < $#poddata) {
473     $poddata[$i+1] =~ /^\s*=head[1-6]\s+(.*)/;
474     $nextsec = $1;
475     } else {
476     $nextsec = "";
477     }
478    
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";
483    
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>"
490     ,ge;
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";
497     close(SPLITOUT) ||
498     die "$0: error closing $poddir/$file: $!\n";
499     }
500     }
501    
502    
503     #
504     # installdir - takes care of converting the .pod and .pm files in the
505     # current directory to .html files and then installing those.
506     #
507     sub installdir {
508     my($dir, $recurse, $podroot, $splitdirs, $ignore) = @_;
509     my(@dirlist, @podlist, @pmlist, $doindex);
510    
511     @dirlist = (); # directories to recurse on
512     @podlist = (); # .pod files to install
513     @pmlist = (); # .pm files to install
514    
515     # should files in this directory get an index?
516     $doindex = (grep($_ eq "$podroot/$dir", @$splitdirs) ? 0 : 1);
517    
518     opendir(DIR, "$podroot/$dir")
519     || die "$0: error opening directory $podroot/$dir: $!\n";
520    
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;
524     rewinddir(DIR);
525    
526     # find all the .pod files within the directory
527     @podlist = map { /^(.*)\.pod$/; "$dir/$1" }
528     grep(! -d "$podroot/$dir/$_" && /\.pod$/, readdir(DIR));
529     rewinddir(DIR);
530    
531     # find all the .pm files within the directory
532     @pmlist = map { /^(.*)\.pm$/; "$dir/$1" }
533     grep(! -d "$podroot/$dir/$_" && /\.pm$/, readdir(DIR));
534    
535     closedir(DIR);
536    
537     # recurse on all subdirectories we kept track of
538     foreach $dir (@dirlist) {
539     installdir($dir, $recurse, $podroot, $splitdirs, $ignore);
540     }
541    
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);
546    
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");
552     }
553     runpod2html("$pod.pod", $doindex);
554     }
555    
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);
560    
561     runpod2html("$pm.pm", $doindex);
562     }
563     }
564    
565    
566     #
567     # runpod2html - invokes pod2html to convert a .pod or .pm file to a .html
568     # file.
569     #
570     sub runpod2html {
571     my($pod, $doindex) = @_;
572     my($html, $i, $dir, @dirs);
573    
574     $html = $pod;
575     $html =~ s/\.(pod|pm)$/.html/g;
576    
577     # make sure the destination directories exist
578     @dirs = split("/", $html);
579     $dir = "$htmldir/";
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";
584     }
585     $dir .= "$dirs[$i]/";
586     }
587    
588     # invoke pod2html
589     print "$podroot/$pod => $htmldir/$html\n" if $verbose;
590     Pod::Html::pod2html(
591     "--htmldir=$htmldir",
592     "--htmlroot=$htmlroot",
593     "--podpath=".join(":", @podpath),
594     "--podroot=$podroot", "--netscape",
595     "--header",
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 $?;
601     }