home *** CD-ROM | disk | FTP | other *** search
/ Tutto per Internet / Internet.iso / soft95 / Html / PerlW32 / pod2htm.bat < prev    next >
Encoding:
DOS Batch File  |  1996-01-31  |  12.1 KB  |  474 lines

  1. @rem = '--*-Perl-*--';
  2. @rem = '
  3. @echo off
  4. perl -S %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. @rem ';
  7. #!/ugrad0/csss/pub/bin/hp700/perl
  8. eval 'exec perl -S $0 ${1+"$@"}'
  9.     if $running_under_some_shell;
  10. #
  11. # pod2html - convert pod format to html
  12. # usage: pod2html [podfiles]
  13. # will read the cwd and parse all files with .pod extension
  14. # if no arguments are given on the command line.
  15. #
  16. *RS = */;
  17. *ERRNO = *!;
  18.  
  19. use Carp;
  20.  
  21. $gensym = 0;
  22.  
  23. while ($ARGV[0] =~ /^-d(.*)/) {
  24.     shift;
  25.     $Debug{ lc($1 || shift) }++;
  26. }
  27.  
  28. # look in these pods for things not found within the current pod
  29. @inclusions = qw[
  30.      perlfunc perlvar perlrun perlop 
  31. ];
  32.  
  33. # ck for podnames on command line
  34. while ($ARGV[0]) {
  35.     push(@Pods,shift);
  36. }
  37. $A={};
  38.  
  39. # location of pods
  40. $dir="."; 
  41.  
  42. # The beginning of the url for the anchors to the other sections.
  43. # Edit $type to suit.  It's configured for relative url's now.
  44. $type='<A HREF="';        
  45. $debug = 0;
  46.  
  47. unless(@Pods){
  48.     opendir(DIR,$dir)  or  die "Can't opendir $dir: $ERRNO";
  49.     @Pods = grep(/\.pod$/,readdir(DIR));
  50.     closedir(DIR) or die "Can't closedir $dir: $ERRNO";
  51. }
  52. @Pods or die "expected pods";
  53.  
  54. # loop twice through the pods, first to learn the links, then to produce html
  55. for $count (0,1){
  56.     (print "Scanning pods...\n") unless $count;
  57.     foreach $podfh ( @Pods ) {
  58.     ($pod = $podfh) =~ s/\.pod$//;
  59.     Debug("files", "opening 2 $podfh" );
  60.     (print "Creating $pod.htm from $podfh\n") if $count;
  61.     $RS = "\n=";
  62.     open($podfh,"<".$podfh)  || die "can't open $podfh: $ERRNO";
  63.     @all=<$podfh>;
  64.     close($podfh);
  65.     $RS = "\n";
  66.     $all[0]=~s/^=//;
  67.     for(@all){s/=$//;}
  68.     $Podnames{$pod} = 1;
  69.     $in_list=0;
  70.     $html=$pod.".htm";
  71.     if($count){
  72.         open(HTML,">$html") || die "can't create $html: $ERRNO";
  73.         print HTML <<'HTML__EOQ', <<"HTML__EOQQ";
  74.         <!-- \$RCSfile\$\$Revision\$\$Date\$ -->
  75.         <!-- \$Log\$ -->
  76.         <HTML>
  77. HTML__EOQ
  78.         <TITLE>\U$pod\E</TITLE>
  79. HTML__EOQQ
  80.     }
  81.  
  82.     for($i=0;$i<=$#all;$i++){
  83.  
  84.         $all[$i] =~ /^(\w+)\s*(.*)\n?([^\0]*)$/ ;
  85.         ($cmd, $title, $rest) = ($1,$2,$3);
  86.         if ($cmd eq "item") {
  87.         if($count ){
  88.             ($depth) or do_list("over",$all[$i],\$in_list,\$depth);
  89.             do_item($title,$rest,$in_list);
  90.         }
  91.         else{
  92.             # scan item
  93.             scan_thing("item",$title,$pod);
  94.         }
  95.         }
  96.         elsif ($cmd =~ /^head([12])/){
  97.         $num=$1;
  98.         if($count){
  99.             do_hdr($num,$title,$rest,$depth);
  100.         }
  101.         else{
  102.             # header scan
  103.             scan_thing($cmd,$title,$pod); # skip head1
  104.         }
  105.         }
  106.         elsif ($cmd =~ /^over/) {
  107.         $count and $depth and do_list("over",$all[$i+1],\$in_list,\$depth);
  108.         }
  109.         elsif ($cmd =~ /^back/) {
  110.         if($count){
  111.             ($depth) or next; # just skip it
  112.             do_list("back",$all[$i+1],\$in_list,\$depth);
  113.             do_rest("$title.$rest");
  114.         }
  115.         }
  116.         elsif ($cmd =~ /^cut/) {
  117.         next;
  118.         }
  119.         elsif($Debug){
  120.         (warn "unrecognized header: $cmd") if $Debug;
  121.         }
  122.     }
  123.         # close open lists without '=back' stmts
  124.     if($count){
  125.         while($depth){
  126.          do_list("back",$all[$i+1],\$in_list,\$depth);
  127.         }
  128.         print HTML "\n</HTML>\n";
  129.     }
  130.     }
  131. }
  132.  
  133. sub do_list{
  134.     my($which,$next_one,$list_type,$depth)=@_;
  135.     my($key);
  136.     if($which eq "over"){
  137.     ($next_one =~ /^item\s+(.*)/ ) or (warn "Bad list, $1\n") if $Debug;
  138.     $key=$1;
  139.     if($key =~ /^1\.?/){
  140.     $$list_type = "OL";
  141.     }
  142.     elsif($key =~ /\*\s*$/){
  143.     $$list_type="UL";
  144.     }
  145.     elsif($key =~ /\*?\s*\w/){
  146.     $$list_type="DL";
  147.     }
  148.     else{
  149.     (warn "unknown list type for item $key") if $Debug;
  150.     }
  151.     print HTML qq{\n};
  152.     print HTML qq{<$$list_type>};
  153.     $$depth++;
  154.     }
  155.     elsif($which eq "back"){
  156.     print HTML qq{\n</$$list_type>\n};
  157.     $$depth--;
  158.     }
  159. }
  160.  
  161. sub do_hdr{
  162.     my($num,$title,$rest,$depth)=@_;
  163.     ($num == 1) and print HTML qq{<p><hr>\n};
  164.     process_thing(\$title,"NAME");
  165.     print HTML qq{\n<H$num> };
  166.     print HTML $title; 
  167.     print HTML qq{</H$num>\n};
  168.     do_rest($rest);
  169. }
  170.  
  171. sub do_item{
  172.     my($title,$rest,$list_type)=@_;
  173.     process_thing(\$title,"NAME");
  174.     if($list_type eq "DL"){
  175.     print HTML qq{\n<DT><STRONG>\n};
  176.     print HTML $title; 
  177.     print HTML qq{\n</STRONG></DT>\n};
  178.     print HTML qq{<DD>\n};
  179.     }
  180.     else{
  181.     print HTML qq{\n<LI>};
  182.     ($list_type ne "OL") && (print HTML $title,"\n");
  183.     }
  184.     do_rest($rest);
  185.     print HTML ($list_type eq "DL" )? qq{</DD>} : qq{</LI>};
  186. }
  187.  
  188. sub do_rest{
  189.     my($rest)=@_;
  190.     my(@lines,$p,$q,$line,,@paras,$inpre);
  191.     @paras=split(/\n\n+/,$rest);
  192.     for($p=0;$p<=$#paras;$p++){
  193.     @lines=split(/\n/,$paras[$p]);
  194.     if($lines[0] =~ /^\s+\w*\t.*/){  # listing or unordered list
  195.         print HTML qq{<UL>};
  196.         foreach $line (@lines){ 
  197.         ($line =~ /^\s+(\w*)\t(.*)/) && (($key,$rem) = ($1,$2));
  198.         print HTML defined($Podnames{$key}) ?
  199.             "<LI>$type$key.htm\">$key<\/A>\t$rem</LI>\n" : 
  200.             "<LI>$line</LI>\n";
  201.         }
  202.         print HTML qq{</UL>\n};
  203.     }
  204.     elsif($lines[0] =~ /^\s/){       # preformatted code
  205.         if($paras[$p] =~/>>|<</){
  206.         print HTML qq{\n<PRE>\n};
  207.         $inpre=1;
  208.         }
  209.         else{
  210.         print HTML qq{\n<XMP>\n};
  211.         $inpre=0;
  212.         }
  213. inner:
  214.         while(defined($paras[$p])){
  215.             @lines=split(/\n/,$paras[$p]);
  216.         foreach $q (@lines){
  217.             if($paras[$p]=~/>>|<</){
  218.             if($inpre){
  219.                 process_thing(\$q,"HTML");
  220.             }
  221.             else {
  222.                 print HTML qq{\n</XMP>\n};
  223.                 print HTML qq{<PRE>\n};
  224.                 $inpre=1;
  225.                 process_thing(\$q,"HTML");
  226.             }
  227.             }
  228.             while($q =~  s/\t+/' 'x (length($&) * 8 - length($`) % 8)/e){
  229.             1;
  230.             }
  231.             print HTML  $q,"\n";
  232.         }
  233.         last if $paras[$p+1] !~ /^\s/;
  234.         $p++;
  235.         }
  236.         print HTML ($inpre==1) ? (qq{\n</PRE>\n}) : (qq{\n</XMP>\n});
  237.     }
  238.     else{                             # other text
  239.         @lines=split(/\n/,$paras[$p]);
  240.         foreach $line (@lines){
  241.                 process_thing(\$line,"HTML");
  242.         print HTML qq{$line\n};
  243.         }
  244.     }
  245.     print HTML qq{<p>};
  246.     }
  247. }
  248.  
  249. sub process_thing{
  250.     my($thing,$htype)=@_;
  251.     pre_escapes($thing);
  252.     find_refs($thing,$htype);
  253.     post_escapes($thing);
  254. }
  255.  
  256. sub scan_thing{
  257.     my($cmd,$title,$pod)=@_;
  258.     $_=$title;
  259.     s/\n$//;
  260.     s/E<(.*?)>/&$1;/g;
  261.     # remove any formatting information for the headers
  262.     s/[SFCBI]<(.*?)>/$1/g;         
  263.     # the "don't format me" thing
  264.     s/Z<>//g;
  265.     if ($cmd eq "item") {
  266.  
  267.         if (/^\*/)     {  return }     # skip bullets
  268.         if (/^\d+\./)     {  return }     # skip numbers
  269.         s/(-[a-z]).*/$1/i;
  270.     trim($_);
  271.         return if defined $A->{$pod}->{"Items"}->{$_};
  272.         $A->{$pod}->{"Items"}->{$_} = gensym($pod, $_);
  273.         $A->{$pod}->{"Items"}->{(split(' ',$_))[0]}=$A->{$pod}->{"Items"}->{$_};
  274.         Debug("items", "item $_");
  275.         if (!/^-\w$/ && /([%\$\@\w]+)/ && $1 ne $_ 
  276.             && !defined($A->{$pod}->{"Items"}->{$_}) && ($_ ne $1)) 
  277.         {
  278.             $A->{$pod}->{"Items"}->{$1} = $A->{$pod}->{"Items"}->{$_};
  279.             Debug("items", "item $1 REF TO $_");
  280.         } 
  281.         if ( m{^(tr|y|s|m|q[qwx])/.*[^/]} ) {
  282.             my $pf = $1 . '//';
  283.             $pf .= "/" if $1 eq "tr" || $1 eq "y" || $1 eq "s";
  284.             if ($pf ne $_) {
  285.                 $A->{$pod}->{"Items"}->{$pf} = $A->{$pod}->{"Items"}->{$_};
  286.                 Debug("items", "item $pf REF TO $_");
  287.             }
  288.     }
  289.     }
  290.     elsif ($cmd =~ /^head[12]/){                
  291.         return if defined($Headers{$_});
  292.         $A->{$pod}->{"Headers"}->{$_} = gensym($pod, $_);
  293.         Debug("headers", "header $_");
  294.     } 
  295.     else {
  296.         (warn "unrecognized header: $cmd") if $Debug;
  297.     } 
  298. }
  299.  
  300.  
  301. sub picrefs { 
  302.     my($char, $bigkey, $lilkey,$htype) = @_;
  303.     my($key,$ref,$podname);
  304.     for $podname ($pod,@inclusions){
  305.     for $ref ( "Items", "Headers" ) {
  306.         if (defined $A->{$podname}->{$ref}->{$bigkey}) {
  307.         $value = $A->{$podname}->{$ref}->{$key=$bigkey};
  308.         Debug("subs", "bigkey is $bigkey, value is $value\n");
  309.         } 
  310.         elsif (defined $A->{$podname}->{$ref}->{$lilkey}) {
  311.         $value = $A->{$podname}->{$ref}->{$key=$lilkey};
  312.         return "" if $lilkey eq '';
  313.         Debug("subs", "lilkey is $lilkey, value is $value\n");
  314.         } 
  315.     } 
  316.     if (length($key)) {
  317.             ($pod2,$num) = split(/_/,$value,2);
  318.         if($htype eq "NAME"){  
  319.         return "\n<A NAME=\"".$value."\">\n$bigkey</A>\n"
  320.         }
  321.         else{
  322.         return "\n$type$pod2.htm\#".$value."\">$bigkey<\/A>\n";
  323.         }
  324.     } 
  325.     }
  326.     if ($char =~ /[IF]/) {
  327.     return "<EM>$bigkey</EM>";
  328.     } elsif($char =~ /C/) {
  329.     return "<CODE>$bigkey</CODE>";
  330.     } else {
  331.     return "<STRONG>$bigkey</STRONG>";
  332.     }
  333.  
  334. sub find_refs { 
  335.     my($thing,$htype)=@_;
  336.     my($orig) = $$thing;
  337.     # LREF: a manpage(3f) we don't know about
  338.     $$thing=~s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))>:the I<$1>$2 manpage:g;
  339.     $$thing=~s/L<([^>]*)>/lrefs($1,$htype)/ge;
  340.     $$thing=~s/([CIBF])<(\W*?(-?\w*).*?)>/picrefs($1, $2, $3, $htype)/ge;
  341.     $$thing=~s/((\w+)\(\))/picrefs("I", $1, $2,$htype)/ge;
  342.     $$thing=~s/([\$\@%](?!&[gl]t)([\w:]+|\W\b))/varrefs($1,$htype)/ge;
  343.     (($$thing eq $orig) && ($htype eq "NAME")) && 
  344.     ($$thing=picrefs("I", $$thing, "", $htype));
  345. }
  346.  
  347. sub lrefs {
  348.     my($page, $item) = split(m#/#, $_[0], 2);
  349.     my($htype)=$_[1];
  350.     my($podname);
  351.     my($section) = $page =~ /\((.*)\)/;
  352.     my $selfref;
  353.     if ($page =~ /^[A-Z]/ && $item) {
  354.     $selfref++;
  355.     $item = "$page/$item";
  356.     $page = $pod;
  357.     }  elsif (!$item && $page =~ /[^a-z\-]/ && $page !~ /^\$.$/) {
  358.     $selfref++;
  359.     $item = $page;
  360.     $page = $pod;
  361.     } 
  362.     $item =~ s/\(\)$//;
  363.     if (!$item) {
  364.         if (!defined $section && defined $Podnames{$page}) {
  365.         return "\n$type$page.htm\">\nthe <EM>$page</EM> manpage<\/A>\n";
  366.     } else {
  367.         (warn "Bizarre entry $page/$item") if $Debug;
  368.         return "the <EM>$_[0]</EM>  manpage\n";
  369.     } 
  370.     } 
  371.  
  372.     if ($item =~ s/"(.*)"/$1/ || ($item =~ /[^\w\/\-]/ && $item !~ /^\$.$/)) {
  373.     $text = "<EM>$item</EM>";
  374.     $ref = "Headers";
  375.     } else {
  376.     $text = "<EM>$item</EM>";
  377.     $ref = "Items";
  378.     } 
  379.     for $podname ($pod, @inclusions){
  380.     undef $value;
  381.     if ($ref eq "Items") {
  382.         if (defined($value = $A->{$podname}->{$ref}->{$item})) {
  383.         ($pod2,$num) = split(/_/,$value,2);
  384.         return (($pod eq $pod2) && ($htype eq "NAME"))
  385.             ? "\n<A NAME=\"".$value."\">\n$text</A>\n"
  386.             : "\n$type$pod2.htm\#".$value."\">$text<\/A>\n";
  387.             }
  388.         } 
  389.     elsif($ref eq "Headers") {
  390.         if (defined($value = $A->{$podname}->{$ref}->{$item})) {
  391.         ($pod2,$num) = split(/_/,$value,2);
  392.         return (($pod eq $pod2) && ($htype eq "NAME")) 
  393.             ? "\n<A NAME=\"".$value."\">\n$text</A>\n"
  394.             : "\n$type$pod2.htm\#".$value."\">$text<\/A>\n";
  395.             }
  396.     }
  397.     }
  398.     (warn "No $ref reference for $item (@_)") if $Debug;
  399.     return $text;
  400.  
  401. sub varrefs {
  402.     my ($var,$htype) = @_;
  403.     for $podname ($pod,@inclusions){
  404.     if ($value = $A->{$podname}->{"Items"}->{$var}) {
  405.         ($pod2,$num) = split(/_/,$value,2);
  406.         Debug("vars", "way cool -- var ref on $var");
  407.         return (($pod eq $pod2) && ($htype eq "NAME"))  # INHERIT $_, $pod
  408.         ? "\n<A NAME=\"".$value."\">\n$var</A>\n"
  409.         : "\n$type$pod2.htm\#".$value."\">$var<\/A>\n";
  410.     }
  411.     }
  412.     Debug( "vars", "bummer, $var not a var");
  413.     return "<STRONG>$var</STRONG>";
  414.  
  415. sub gensym {
  416.     my ($podname, $key) = @_;
  417.     $key =~ s/\s.*//;
  418.     ($key = lc($key)) =~ tr/a-z/_/cs;
  419.     my $name = "${podname}_${key}_0";
  420.     $name =~ s/__/_/g;
  421.     while ($sawsym{$name}++) {
  422.         $name =~ s/_?(\d+)$/'_' . ($1 + 1)/e;
  423.     }
  424.     return $name;
  425.  
  426. sub pre_escapes {
  427.     my($thing)=@_;
  428.     $$thing=~s/&/noremap("&")/ge;
  429.     $$thing=~s/<</noremap("<<")/eg;
  430.     $$thing=~s/(?:[^ESIBLCF])</noremap("<")/eg;
  431.     $$thing=~s/E<([^\/][^<>]*)>/\&$1\;/g;              # embedded special
  432. }
  433.  
  434. sub noremap {
  435.     my $hide = $_[0];
  436.     $hide =~ tr/\000-\177/\200-\377/;
  437.     $hide;
  438.  
  439. sub post_escapes {
  440.     my($thing)=@_;
  441.     $$thing=~s/[^GM]>>/\>\;\>\;/g;
  442.     $$thing=~s/([^"MGAE])>/$1\>\;/g;
  443.     $$thing=~tr/\200-\377/\000-\177/;
  444. }
  445.  
  446. sub Debug {
  447.     my $level = shift;
  448.     print STDERR @_,"\n" if $Debug{$level};
  449.  
  450. sub dumptable  {
  451.     my $t = shift;
  452.     print STDERR "TABLE DUMP $t\n";
  453.     foreach $k (sort keys %$t) {
  454.     printf STDERR "%-20s <%s>\n", $t->{$k}, $k;
  455.     } 
  456. sub trim {
  457.     for (@_) {
  458.         s/^\s+//;
  459.         s/\s\n?$//;
  460.     }
  461. }
  462.  
  463.  
  464. __END__
  465. :endofperl
  466.