4 use File::Basename qw(&basename &dirname);
6 # List explicitly here the variables you want Configure to
7 # generate. Metaconfig only looks for shell variables, so you
8 # have to mention them as if they were shell variables, not
9 # %Config entries. Thus you write
11 # to ensure Configure will look for $Config{startperl}.
13 # This forces PL files to create target in same directory as PL file.
14 # This is so that make depend always knows where to find PL derivatives.
16 $file = basename($0, '.PL');
17 $file .= '.com' if $^O eq 'VMS';
19 open OUT,">$file" or die "Can't create $file: $!";
21 print "Extracting $file (with variable substitutions)\n";
23 # In this section, perl variables will be expanded during extraction.
24 # You can use $Config{...} to use Configure variables.
26 print OUT <<"!GROK!THIS!";
28 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
29 if \$running_under_some_shell;
32 # In the following, perl variables are not expanded during extraction.
34 print OUT <<'!NO!SUBS!';
37 # pod2html - convert pod format to html
39 # usage: pod2html [podfiles]
40 # Will read the cwd and parse all files with .pod extension
41 # if no arguments are given on the command line.
43 # Many helps, suggestions, and fixes from the perl5 porters, and all over.
44 # Bill Middleton - wjm@metronet.com
46 # Please send patches/fixes/features to me
55 ################################################################################
56 # Invoke with various levels of debugging possible
57 ################################################################################
58 while ($ARGV[0] =~ /^-d(.*)/) {
60 $Debug{ lc($1 || shift) }++;
63 # ck for podnames on command line
68 ################################################################################
69 # CONFIGURE - change the following to suit your OS and taste
70 ################################################################################
71 # The beginning of the url for the anchors to the other sections.
72 # Edit $type to suit. It's configured for relative url's now.
73 # Other possibilities are:
74 # $type = '<A HREF="file://localhost/usr/local/htmldir/'; # file url
75 # $type = '<A HREF="http://www.bozo.com/perl/manual/html/' # server
79 ################################################################################
80 # location of all podfiles unless on command line
81 # $installprivlib='HD:usr:local:lib:perl5'; # uncomment for Mac
82 # $installprivlib='C:\usr\local\lib\perl5'; # uncomment for DOS (I hope)
83 # $installprivlib='/usr/local/lib/perl5'; # Unix
84 $installprivlib="./"; # Standard perl pod directory for intallation
86 ################################################################################
87 # Where to write out the html files
88 # $installhtmldir='HD:usr:local:lib:perl5:html'; # uncomment for Mac
89 # $installhtmldir='C:\usr\local\lib\perl5\html'; # uncomment for DOS (I hope)
90 $installhtmldir = './';
94 if(!(-d $installhtmldir)){
95 print "Installation directory $installhtmldir does not exist, using cwd\n";
96 print "Hit ^C now to edit this script and configure installhtmldir\n";
97 $installhtmldir = '.';
100 ################################################################################
101 # the html extension, change to htm for DOS
105 ################################################################################
106 # arbitrary name for this group of pods
110 ################################################################################
111 # look in these pods for links to things not found within the current pod
112 # be careful tho, namespace collisions cause stupid links
114 @inclusions = qw[ perlfunc perlvar perlrun perlop ];
116 ################################################################################
117 # Directory path separator
118 # $sep= ":"; # uncomment for Mac
119 # $sep= "\"; # uncomment for DOS
123 ################################################################################
124 # Create 8.3 html files if this equals 1
128 ################################################################################
129 # Create maximum 32 character html files if this equals 1
132 ################################################################################
134 # Beyond here be dragons. :-)
135 ################################################################################
137 $A = {}; # The beginning of all things
140 find($installprivlib);
141 splice(@Pods,$#Pods+1,0,@modpods);;
144 @Pods or die "aak, expected pods";
145 open(INDEX,">".$installhtmldir.$sep."index.".$htmlext) or
146 (die "cant open index.$htmlext");
147 print INDEX "\n<HTML>\n<HEAD>\n<TITLE>Index of all pods for $package</TITLE></HEAD>\n<BODY>\n";
148 print INDEX "<H1>Index of all pods for $package</H1>\n<hr><UL>\n";
149 # loop twice through the pods, first to learn the links, then to produce html
151 print STDERR "Scanning pods...\n" unless $count;
153 foreach $podfh ( @Pods ) {
156 $refname =~ s/\Q$installprivlib${sep}\E?//;
157 $refname =~ s/${sep}/::/g;
158 $refname =~ s/\.p(m|od)$//;
159 $refname =~ s/^pod:://;
160 $savename = $refname;
161 $refname =~ s/::/_/g;
162 if($DOSify && !$count){ # shorten the name for DOS
163 (length($refname) > 8) and ( $refname = substr($refname,0,8));
164 while(defined($DosNames{$refname})){
165 @refname=split(//,$refname);
167 ($refname[$#refname] eq "z") and ($refname[$#refname] = "a");
168 $refname[$#refname]++;
169 $refname=join('',@refname);
170 $refname =~ s/\W/_/g;
172 $DosNames{$refname} = 1;
173 $Podnames{$savename} = $refname . ".$htmlext";
175 elsif(!$DOSify and !$count){
176 $Podnames{$savename} = $refname . ".$htmlext";
179 Debug("files", "opening 2 $podfh" );
180 print "Creating $Podnames{$savename} from $podfh\n" if $count;
181 $RS = "\n="; # grok pods by item (Nonstandard but effecient)
182 open($podfh,"<".$podfh) || die "can't open $podfh: $ERRNO";
186 ($all[0] =~ s/^=//) || pop(@all);
187 for ($i=0;$i <= $#all;$i++){ splice(@all,$i+1,1) unless
188 (($all[$i] =~ s/=$//) && ($all[$i+1] !~ /^cut/)) ; # whoa..
191 unless (grep(/NAME/,@all)){
192 print STDERR "NAME header not found in $podfh, skipping\n";
193 #delete($Podnames{$savename});
197 next unless length($Podnames{$savename});
198 open(HTML,">".$installhtmldir.$sep.$Podnames{$savename}) or
199 (die "can't create $Podnames{$savename}: $ERRNO");
200 print HTML "<HTML><HEAD>";
201 print HTML "<TITLE>$refname</TITLE>\n</HEAD>\n<BODY>";
204 for ($i = 0; $i <= $#all; $i++) { # decide what to do with each chunk
205 $all[$i] =~ /^(\w+)\s*(.*)\n?([^\0]*)$/ ;
206 ($cmd, $title, $rest) = ($1,$2,$3);
207 if(length($cmd)){$cutting =0;}
209 if(($title =~ /NAME/) and ($didindex == 0) and $count){
210 print INDEX "<LI><A HREF=\"$Podnames{$savename}\">$rest</A>\n";
213 if ($cmd eq "item") {
214 if ($count ) { # producing html
215 do_list("over",$all[$i],\$in_list,\$depth) unless $depth;
216 do_item($title,$rest,$in_list);
220 scan_thing("item",$title,$pod);
223 elsif ($cmd =~ /^head([12])/) {
225 if ($count) { # producing html
226 do_hdr($num,$title,$rest,$depth);
230 scan_thing($cmd,$title,$pod); # skip head1
233 elsif ($cmd =~ /^over/) {
234 $count and $depth and do_list("over",$all[$i+1],\$in_list,\$depth);
236 elsif ($cmd =~ /^back/) {
237 if ($count) { # producing html
238 ($depth) or next; # just skip it
239 do_list("back",$all[$i+1],\$in_list,\$depth);
240 do_rest("$title$rest");
243 elsif ($cmd =~ /^cut/) {
246 elsif ($cmd =~ /^for/) { # experimental pragma html
247 if ($count) { # producing html
248 if ($title =~ s/^html//) {
250 do_rest("$title$rest");
254 elsif ($cmd =~ /^begin/) { # experimental pragma html
255 if ($count) { # producing html
256 if ($title =~ s/^html//) {
257 print HTML $title,"\n",$rest;
259 elsif ($title =~ /^end/) {
264 elsif ($Debug{"misc"}) {
265 warn("unrecognized header: $cmd");
268 # close open lists without '=back' stmts
269 if ($count) { # producing html
271 do_list("back",$all[$i+1],\$in_list,\$depth);
273 print HTML "\n</BODY>\n</HTML>\n";
277 print INDEX "\n</UL></BODY>\n</HTML>\n";
279 sub do_list{ # setup a list type, depending on some grok logic
280 my($which,$next_one,$list_type,$depth) = @_;
282 if ($which eq "over") {
283 unless ($next_one =~ /^item\s+(.*)/) {
284 warn "Bad list, $1\n" if $Debug{"misc"};
288 if ($key =~ /^1\.?/) {
290 } elsif ($key =~ /\*\s*$/) {
292 } elsif ($key =~ /\*?\s*\w/) {
295 warn "unknown list type for item $key" if $Debug{"misc"};
299 print HTML qq{<$$list_type>};
302 elsif ($which eq "back") {
303 print HTML qq{\n</$$list_type>\n};
308 sub do_hdr{ # headers
309 my($num,$title,$rest,$depth) = @_;
310 my($savename,$restofname);
311 print HTML qq{<p><hr>\n} if $num == 1;
312 ($savename = $title) =~ s/^(\w+)([\s,]+.*)/$1/;
314 (defined($Podnames{$savename})) ? ($savename = $savename) : ($savename = 0);
315 process_thing(\$title,"NAME");
316 print HTML qq{\n<H$num> };
318 print HTML "<A HREF=\"$Podnames{$savename}\">$savename$restofname</A>";
323 print HTML qq{</H$num>\n};
327 sub do_item{ # list items
328 my($title,$rest,$list_type) = @_;
330 $bullet_only = ($title eq '*' and $list_type eq 'UL') ? 1 : 0;
333 (defined($Podnames{$savename})) ? ($savename = $savename) : ($savename = 0);
334 process_thing(\$title,"NAME");
335 if ($list_type eq "DL") {
336 print HTML qq{\n<DT>\n};
338 print HTML "<A HREF=\"$Podnames{$savename}\">$savename $rest</A>\n</DT>";
342 (print HTML qq{\n<STRONG>\n}) unless ($title =~ /STRONG/);
344 if($title !~ /STRONG/){
345 print HTML "\n</STRONG></DT>\n";
347 print HTML "</DT>\n";
350 print HTML qq{<DD>\n};
353 print HTML qq{\n<LI>};
354 unless ($bullet_only or $list_type eq "OL") {
356 print HTML "<A HREF=\"$savename.$htmlext\">$savename</A>";
359 print HTML $title,"\n";
366 sub do_rest{ # the rest of the chunk handled here
368 my(@lines,$p,$q,$line,,@paras,$inpre);
369 @paras = split(/\n\n\n*/,$rest);
370 for ($p = 0; $p <= $#paras; $p++) {
371 $paras[$p] =~ s/^\n//mg;
372 @lines = split(/\n/,$paras[$p]);
373 if ($in_html) { # handle =for html paragraphs
374 print HTML $paras[0];
378 elsif ($lines[0] =~ /^\s+\w*\t.*/) { # listing or unordered list
380 foreach $line (@lines) {
381 ($line =~ /^\s+(\w*)\t(.*)/) && (($key,$rem) = ($1,$2));
382 print HTML defined($Podnames{$key})
383 ? "<LI>$type$Podnames{$key}\">$key<\/A>\t$rem</LI>\n"
384 : "<LI>$line</LI>\n";
386 print HTML qq{</UL>\n};
388 elsif ($lines[0] =~ /^\s/) { # preformatted code
389 if ($paras[$p] =~/>>|<</) {
390 print HTML qq{\n<PRE>\n};
393 else { # Still cant beat XMP. Yes, I know
394 print HTML qq{\n<XMP>\n}; # it's been obsoleted... suggestions?
397 while (defined($paras[$p])) {
398 @lines = split(/\n/,$paras[$p]);
399 foreach $q (@lines) { # mind your p's and q's here :-)
400 if ($paras[$p] =~ />>|<</) {
402 process_thing(\$q,"HTML");
405 print HTML qq{\n</XMP>\n};
406 print HTML qq{<PRE>\n};
408 process_thing(\$q,"HTML");
411 1 while $q =~ s/\t+/' 'x (length($&) * 8 - length($`) % 8)/e;
414 last if $paras[$p+1] !~ /^\s/;
417 print HTML ($inpre==1) ? (qq{\n</PRE>\n}) : (qq{\n</XMP>\n});
420 @lines = split(/\n/,$paras[$p]);
421 foreach $line (@lines) {
422 process_thing(\$line,"HTML");
423 $line =~ s/STRONG([^>])/STRONG>$1/; # lame attempt to fix strong
424 print HTML qq{$line\n};
431 sub process_thing{ # process a chunk, order important
432 my($thing,$htype) = @_;
434 find_refs($thing,$htype);
435 post_escapes($thing);
438 sub scan_thing{ # scan a chunk for later references
439 my($cmd,$title,$pod) = @_;
443 # remove any formatting information for the headers
444 s/[SFCBI]<(.*?)>/$1/g;
445 # the "don't format me" thing
447 if ($cmd eq "item") {
448 /^\*/ and return; # skip bullets
449 /^\d+\./ and return; # skip numbers
452 return if defined $A->{$pod}->{"Items"}->{$_};
453 $A->{$pod}->{"Items"}->{$_} = gensym($pod, $_);
454 $A->{$pod}->{"Items"}->{(split(' ',$_))[0]}=$A->{$pod}->{"Items"}->{$_};
455 Debug("items", "item $_");
456 if (!/^-\w$/ && /([%\$\@\w]+)/ && $1 ne $_
457 && !defined($A->{$pod}->{"Items"}->{$_}) && ($_ ne $1))
459 $A->{$pod}->{"Items"}->{$1} = $A->{$pod}->{"Items"}->{$_};
460 Debug("items", "item $1 REF TO $_");
462 if ( m{^(tr|y|s|m|q[qwx])/.*[^/]} ) {
464 $pf .= "/" if $1 eq "tr" || $1 eq "y" || $1 eq "s";
466 $A->{$pod}->{"Items"}->{$pf} = $A->{$pod}->{"Items"}->{$_};
467 Debug("items", "item $pf REF TO $_");
471 elsif ($cmd =~ /^head[12]/) {
472 return if defined($A->{$pod}->{"Headers"}->{$_});
473 $A->{$pod}->{"Headers"}->{$_} = gensym($pod, $_);
474 Debug("headers", "header $_");
477 warn "unrecognized header: $cmd" if $Debug;
483 my($char, $bigkey, $lilkey,$htype) = @_;
484 my($key,$ref,$podname);
485 for $podname ($pod,@inclusions) {
486 for $ref ( "Items", "Headers" ) {
487 if (defined $A->{$podname}->{$ref}->{$bigkey}) {
488 $value = $A->{$podname}->{$ref}->{$key = $bigkey};
489 Debug("subs", "bigkey is $bigkey, value is $value\n");
491 elsif (defined $A->{$podname}->{$ref}->{$lilkey}) {
492 $value = $A->{$podname}->{$ref}->{$key = $lilkey};
493 return "" if $lilkey eq '';
494 Debug("subs", "lilkey is $lilkey, value is $value\n");
498 ($pod2, $num) = $value =~ /^(.*)_(\S+_\d+)$/;
499 if ($htype eq "NAME") {
500 return "\n<A NAME=\"".$value."\">\n$bigkey</A>\n"
504 return "\n$type$Podnames{$pod2}\#".$value."\">$bigkey<\/A>\n";
508 if ($char =~ /[IF]/) {
509 return "<EM>$bigkey</EM>";
510 } elsif ($char =~ /C/) {
511 return "<CODE>$bigkey</CODE>";
513 if($bigkey =~ /STRONG/){
517 return "<STRONG>$bigkey</STRONG>";
523 my($thing,$htype) = @_;
525 # LREF: a manpage(3f) we don't know about
527 #s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))>:the I<$1>$2 manpage:g;
528 s@(\S+?://\S*[^.,;!?\s])@noremap(qq{<A HREF="$1">$1</A>})@ge;
529 s,([a-z0-9_.-]+\@([a-z0-9_-]+\.)+([a-z0-9_-]+)),noremap(qq{<A HREF="MAILTO:$1">$1</A>}),gie;
530 s/L<([^>]*)>/lrefs($1,$htype)/ge;
531 s/([CIBF])<(\W*?(-?\w*).*?)>/picrefs($1, $2, $3, $htype)/ge;
532 s/(S)<([^\/]\W*?(-?\w*).*?)>/picrefs($1, $2, $3, $htype)/ge;
533 s/((\w+)\(\))/picrefs("I", $1, $2,$htype)/ge;
534 s/([\$\@%](?!&[gl]t)([\w:]+|\W\b))/varrefs($1,$htype)/ge;
536 if ($$thing eq $orig && $htype eq "NAME") {
537 $$thing = picrefs("I", $$thing, "", $htype);
543 my($page, $item) = split(m#/#, $_[0], 2);
546 my($section) = $page =~ /\((.*)\)/;
548 if ($page =~ /^[A-Z]/ && $item) {
550 $item = "$page/$item";
552 } elsif (!$item && $page =~ /[^a-z\-]/ && $page !~ /^\$.$/) {
559 if (!defined $section && defined $Podnames{$page}) {
560 return "\n$type$Podnames{$page}\">\nthe <EM>$page</EM> manpage<\/A>\n";
562 (warn "Bizarre entry $page/$item") if $Debug;
563 return "the <EM>$_[0]</EM> manpage\n";
567 if ($item =~ s/"(.*)"/$1/ || ($item =~ /[^\w\/\-]/ && $item !~ /^\$.$/)) {
568 $text = "<EM>$item</EM>";
571 $text = "<EM>$item</EM>";
574 for $podname ($pod, @inclusions) {
576 if ($ref eq "Items") {
577 if (defined($value = $A->{$podname}->{$ref}->{$item})) {
578 ($pod2,$num) = split(/_/,$value,2); # break here
579 return (($pod eq $pod2) && ($htype eq "NAME"))
580 ? "\n<A NAME=\"".$value."\">\n$text</A>\n"
581 : "\n$type$Podnames{$pod2}\#".$value."\">$text<\/A>\n";
584 elsif ($ref eq "Headers") {
585 if (defined($value = $A->{$podname}->{$ref}->{$item})) {
586 ($pod2,$num) = split(/_/,$value,2); # break here
587 return (($pod eq $pod2) && ($htype eq "NAME"))
588 ? "\n<A NAME=\"".$value."\">\n$text</A>\n"
589 : "\n$type$Podnames{$pod2}\#".$value."\">$text<\/A>\n";
593 warn "No $ref reference for $item (@_)" if $Debug;
598 my ($var,$htype) = @_;
599 for $podname ($pod,@inclusions) {
600 if ($value = $A->{$podname}->{"Items"}->{$var}) {
601 ($pod2,$num) = split(/_/,$value,2);
602 Debug("vars", "way cool -- var ref on $var");
603 return (($pod eq $pod2) && ($htype eq "NAME")) # INHERIT $_, $pod
604 ? "\n<A NAME=\"".$value."\">\n$var</A>\n"
605 : "\n$type$Podnames{$pod2}\#".$value."\">$var<\/A>\n";
608 Debug( "vars", "bummer, $var not a var");
609 if($var =~ /STRONG/){
613 return "<STRONG>$var</STRONG>";
618 my ($podname, $key) = @_;
620 ($key = lc($key)) =~ tr/a-z/_/cs;
621 my $name = "${podname}_${key}_0";
623 while ($sawsym{$name}++) {
624 $name =~ s/_?(\d+)$/'_' . ($1 + 1)/e;
629 sub pre_escapes { # twiddle these, and stay up late :-)
632 s/([\200-\377])/noremap("&#".ord($1).";")/ge;
634 s/&/noremap("&")/ge;
635 s/<</noremap("<<")/eg;
636 s/([^ESIBLCF])</$1\<\;/g;
637 s/E<(\d+)>/\&#$1\;/g; # embedded numeric special
638 s/E<([^\/][^<>]*)>/\&$1\;/g; # embedded special
641 sub noremap { # adding translator for hibit chars soon
643 $hide =~ tr/\000-\177/\200-\377/;
651 s/([^GM])>>/$1\>\;\>\;/g;
652 s/([^D][^"MGA])>/$1\>\;/g;
653 tr/\200-\377/\000-\177/;
659 print STDERR @_,"\n" if $Debug{$level};
664 print STDERR "TABLE DUMP $t\n";
665 foreach $k (sort keys %$t) {
666 printf STDERR "%-20s <%s>\n", $t->{$k}, $k;
678 if ($name =~ /\.p(m|od)$/){
679 push(@modpods, $name) if ($name =~ /\.p(m|od)$/);
686 close OUT or die "Can't close $file: $!";
687 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
688 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';