Home | History | Annotate | Line # | Download | only in mantools
postconf2html revision 1.1.1.2
      1      1.1      tron #!/usr/bin/perl
      2      1.1      tron 
      3      1.1      tron # postconf2html - add HTML paragraphs
      4      1.1      tron 
      5      1.1      tron # Basic operation:
      6      1.1      tron #
      7      1.1      tron # - Process input as text blocks separated by one or more empty
      8      1.1      tron # (or all whitespace) lines.
      9      1.1      tron #
     10  1.1.1.2  christos # - Remove text between <!-- and -->; each may be on a different line.
     11  1.1.1.2  christos #
     12  1.1.1.2  christos # - Optionally remove <nroffescape> pass-through requests (unless
     13  1.1.1.2  christos #   the -n option is specified).
     14      1.1      tron #
     15      1.1      tron # - Don't touch blocks that start with `<' in column zero.
     16      1.1      tron #
     17      1.1      tron # The only changes made are:
     18      1.1      tron #
     19      1.1      tron # - Emit "<DT><a name="parametername">parametername</a>...</DT><DD>" at
     20      1.1      tron #   the top of each parameter description.
     21      1.1      tron #
     22      1.1      tron # All other non-comment input is flagged as an error.
     23      1.1      tron 
     24  1.1.1.2  christos use Getopt::Std;
     25      1.1      tron 
     26  1.1.1.2  christos $opt_h = undef;
     27  1.1.1.2  christos $opt_v = undef;
     28  1.1.1.2  christos $opt_n = undef;
     29  1.1.1.2  christos getopts("hnv");
     30      1.1      tron 
     31  1.1.1.2  christos die "Usage: $0 [-nv]\n" if ($opt_h);
     32      1.1      tron 
     33      1.1      tron #push @ARGV, "/dev/null"; # XXX
     34      1.1      tron 
     35      1.1      tron while(<>) {
     36      1.1      tron 
     37      1.1      tron     # Skip comments.
     38      1.1      tron     next if /^#/;
     39      1.1      tron 
     40      1.1      tron     # Skip blank lines before text block.
     41      1.1      tron     next unless (/\S/);
     42      1.1      tron 
     43      1.1      tron     # Gobble up the next text block.
     44      1.1      tron     $block = "";
     45      1.1      tron     $comment = 0;
     46      1.1      tron     do {
     47      1.1      tron 	$_ =~ s/\s+\n$/\n/;
     48      1.1      tron 	$block .= $_;
     49      1.1      tron 	if ($_ =~ /<!--/)
     50      1.1      tron 	    { $comment = 1; } 
     51      1.1      tron 	if ($comment && $_ =~ /-->/)
     52      1.1      tron 	    { $comment = 0; $block =~ s/<!--.*-->//sg; }
     53      1.1      tron     } while((($_ = <>) && /\S/) || $comment);
     54      1.1      tron 
     55  1.1.1.2  christos     # Strip nroff escapes.
     56  1.1.1.2  christos     $block =~ s/<\s*nroffescape[^>]+>//g unless $opt_n;
     57  1.1.1.2  christos 
     58      1.1      tron     # Skip blanks after comment elimination.
     59      1.1      tron     if ($block =~ /^\s/) {
     60      1.1      tron 	$block =~ s/^\s+//s;
     61      1.1      tron 	next if ($block eq "");
     62      1.1      tron     }
     63      1.1      tron 
     64      1.1      tron     # Don't touch a text block starting with < in column zero.
     65      1.1      tron     if ($block =~ /^</) {
     66      1.1      tron 	print "$block\n";
     67      1.1      tron     }
     68      1.1      tron 
     69      1.1      tron     # Meta block. Emit upper case tags for html2man.
     70      1.1      tron     elsif ($block =~ /^%PARAM/) {
     71      1.1      tron 	print "\n</DD>\n\n" if ($param);
     72      1.1      tron 	print "\n<DL>\n\n" if ($need_dl);
     73      1.1      tron 	$need_dl = 0;
     74      1.1      tron 	($junk, $param, $defval) = split(/\s+/, $block, 3);
     75      1.1      tron 	$defval =~ s/\s+$//s;
     76      1.1      tron 	$defval = "empty" if ($defval eq "");
     77      1.1      tron 	$defval = "default: $defval" unless ($defval eq "read-only");
     78      1.1      tron 	print "<DT><b><a name=\"$param\">$param</a>\n($defval)</b></DT><DD>\n\n";
     79      1.1      tron     }
     80      1.1      tron 
     81      1.1      tron     # Meta block. Emit upper case tags for html2man.
     82      1.1      tron     elsif ($block =~ /^%CLASS/) {
     83      1.1      tron 	print "\n</DD>\n\n" if ($param);
     84      1.1      tron 	print "\n</DL>\n\n" if ($class);
     85      1.1      tron 	$param ="";
     86      1.1      tron 	($junk, $class, $text) = split(/\s+/, $block, 3);
     87      1.1      tron 	$text =~ s/\s+$//s;
     88      1.1      tron 	print "<H2><a name=\"$class\">$text</a></H2>\n\n";
     89      1.1      tron 	$need_dl = 1;
     90      1.1      tron     }
     91      1.1      tron 
     92      1.1      tron     # Can't happen.
     93      1.1      tron     else {
     94      1.1      tron 	die "Unrecognized text block:\n$block";
     95      1.1      tron     }
     96      1.1      tron }
     97      1.1      tron 
     98      1.1      tron print "\n</DD>\n\n" if ($param);
     99      1.1      tron print "\n</DL>\n\n" if ($class);
    100