Annotation of embedaddon/bird2/doc/sbase/dist/fmt_html.pl, revision 1.1.1.1

1.1       misho       1: #
                      2: #  fmt_html.pl
                      3: #
                      4: #  $Id$
                      5: #
                      6: #  HTML-specific driver stuff
                      7: #
                      8: #  © Copyright 1996, Cees de Groot
                      9: #
                     10: package LinuxDocTools::fmt_html;
                     11: use strict;
                     12: 
                     13: use LinuxDocTools::CharEnts;
                     14: use LinuxDocTools::Vars;
                     15: 
                     16: use LinuxDocTools::FixRef;
                     17: my $fixref = $LinuxDocTools::FixRef::fixref;
                     18: 
                     19: use LinuxDocTools::Html2Html;
                     20: my $html2html = $LinuxDocTools::Html2Html::html2html;
                     21: 
                     22: my $html = {};
                     23: $html->{NAME} = "html";
                     24: $html->{HELP} = "";
                     25: $html->{OPTIONS} = [
                     26:    { option => "split", type => "l", 
                     27:      'values' => [ "0", "1", "2" ], short => "s" },
                     28:    { option => "toc", type => "l", 
                     29:      'values' => [ "0", "1", "2" ], short => "T" },
                     30:    { option => "dosnames", type => "f", short => "h" },
                     31:    { option => "imagebuttons", type => "f", short => "I"},
                     32:    { option => "header", type => "s", short => "H"},
                     33:    { option => "footer", type => "s", short => "F"}
                     34: ];
                     35: $html->{'split'}  = 1;
                     36: $html->{'toc'}  = -1;
                     37: $html->{dosnames}  = 0;
                     38: $html->{imagebuttons}  = 0;
                     39: $html->{header}  = "";
                     40: $html->{footer}  = "";
                     41: $html->{preNSGMLS} = sub {
                     42:   $global->{NsgmlsOpts} .= " -ifmthtml ";
                     43:   $global->{NsgmlsPrePipe} = "cat $global->{file}";
                     44: };
                     45: 
                     46: $Formats{$html->{NAME}} = $html;
                     47: 
                     48: # HTML escape sub.  this is called-back by `parse_data' below in
                     49: # `html_preASP' to properly escape `<' and `&' characters coming from
                     50: # the SGML source.
                     51: my %html_escapes;
                     52: $html_escapes{'&'} = '&amp;';
                     53: $html_escapes{'<'} = '&lt;';
                     54: 
                     55: my $html_escape = sub {
                     56:     my ($data) = @_;
                     57: 
                     58:     # replace the char with it's HTML equivalent
                     59:     $data =~ s|([&<])|$html_escapes{$1}|ge;
                     60: 
                     61:     return ($data);
                     62: };
                     63: 
                     64: #
                     65: #  Translate character entities and escape HTML special chars.
                     66: #
                     67: $html->{preASP} = sub
                     68: {
                     69:   my ($infile, $outfile) = @_;
                     70:   # note the conversion of `sdata_dirs' list to an anonymous array to
                     71:   # make a single argument
                     72:   my $char_maps = load_char_maps ('.2html', [ Text::EntityMap::sdata_dirs() ]);
                     73: 
                     74:   while (<$infile>)
                     75:     {
                     76:       if (/^-/)
                     77:         {
                     78:            my ($str) = $';
                     79:            chop ($str);
                     80:            print $outfile "-" . parse_data ($str, $char_maps, $html_escape) . "\n";
                     81:         }
                     82:       elsif (/^A/)
                     83:         {
                     84:          /^A(\S+) (IMPLIED|CDATA|NOTATION|ENTITY|TOKEN)( (.*))?$/
                     85:              || die "bad attribute data: $_\n";
                     86:          my ($name,$type,$value) = ($1,$2,$4);
                     87:          if ($type eq "CDATA")
                     88:            {
                     89:              # CDATA attributes get translated also
                     90:              $value = parse_data ($value, $char_maps, $html_escape);
                     91:            }
                     92:          print $outfile "A$name $type $value\n";
                     93:         }
                     94:       else
                     95:         {
                     96:          print $outfile $_;
                     97:         }
                     98:     }
                     99:   return 0;
                    100: };
                    101: 
                    102: #
                    103: #  Take the sgmlsasp output, and make something
                    104: #  useful from it.
                    105: #
                    106: $html->{postASP} = sub
                    107: {
                    108:   my $infile = shift;
                    109:   my $filename = $global->{filename};
                    110: 
                    111:   #
                    112:   #  Set various stuff as a result of option processing.
                    113:   #
                    114:   my $ext   = "html";
                    115:   $ext   = "htm"  if $html->{dosnames};
                    116:   my $img   = 0;
                    117:   $img   = 1 if $html->{imagebuttons};
                    118: 
                    119:   #
                    120:   # Bring in file
                    121:   #
                    122:   my @file = <$infile>;
                    123: 
                    124:   #
                    125:   #  Find references
                    126:   #
                    127:   &{$fixref->{init}}($html->{'split'});
                    128:   LINE: foreach (@file) {
                    129:       foreach my $pat (keys %{$fixref->{rules}}) {
                    130:           if (/$pat/) {
                    131:               # Call rule function then skip to next line
                    132:               &{$fixref->{rules}->{$pat}}; next LINE;
                    133:           }
                    134:       }
                    135:       &{$fixref->{defaultrule}};
                    136:   }
                    137:   &{$fixref->{finish}};
                    138: 
                    139:   #  
                    140:   #  Run through html2html, preserving stdout
                    141:   #  Also, handle prehtml.sed's tasks
                    142:   #
                    143:   my $filter = "";
                    144: #  $filter = "|$main::progs->{NKF} -e" if ($global->{language} eq "ja");
                    145:   open SAVEOUT, ">&STDOUT";
                    146:   open STDOUT, "$filter>$filename.$ext" or die qq(Cannot open "$filename.$ext");
                    147: 
                    148:   &{$html2html->{init}}($html->{'split'}, $ext, $img, $filename,
                    149:                         $fixref->{filenum}, $fixref->{lrec},
                    150:                        $html->{'header'}, $html->{'footer'}, $html->{'toc'},
                    151:                         $global->{tmpbase}, $global->{debug});
                    152:   LINE: foreach (@file) {
                    153:       s,<P></P>,,g;                    # remove empty <P></P> containers
                    154:       foreach my $pat (keys %{$html2html->{rules}}) {
                    155:           if (/$pat/) {
                    156:               # Call rule function then skip to next line
                    157:               &{$html2html->{rules}->{$pat}}; next LINE;
                    158:           }
                    159:       }
                    160:       &{$html2html->{defaultrule}};
                    161:   }
                    162:   &{$html2html->{finish}};
                    163: 
                    164:   close STDOUT;
                    165:   open STDOUT, ">&SAVEOUT";
                    166: 
                    167:   return 0;
                    168: };
                    169: 
                    170: 1;
                    171: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>