#!/usr/local/bin/perl
#
#  roff-me - generate a summary of a roff -me file using semantic information
#
#  Interesting -me Macros
#  -----------------------
#  .bu			bulleted paragraph
#  .ip x i		indented paragraph with hanging tag
#  .np			numbered parapraph
#  .lp			parapraph
#  .pp			parapraph
#  .sh n x		section header x, depth n
#  .uh x		section header x
#
if ($#ARGV > 0) {
	print STDERR "Usage: roff-me filename\n";
	exit(1);
}
$tmpfile = "/tmp/roff-me.$$";
$file = shift(@ARGV);
$still_input_line = 0;
open(INPUT, "< $file") || die "Cannot read $file";
open(OUT, "| deroff > $tmpfile") || die "Cannot write $tmpfile";
while (($still_input_line == 1) || ($_ = <INPUT>)) {
	$still_input_line = 0;
	print "$2\n$3\n$4\n" if (/^\.(fo|he)\s+'([^']*)'([^']*)'([^']*)'\n$/o);
	print "$1\n" if (/^\.sh\s+\d+\s+(.*)\n$/o);
	print "$1\n" if (/^\.(uh|ip)\s+(.*)\n$/o);
	do print_paragraph() if (/^\.(bu|np)/o);
}
close(INPUT);
close(OUT);
open(RESULT, "< $tmpfile") || die "Cannot read $tmpfile";
print <RESULT>;
close(RESULT);
unlink($tmpfile);
exit(0);


sub print_paragraph {
	$npara = 0;
	while (<INPUT>) {
		next if (/^\.so/o);
		last if (/^\.(sh|uh)/o);
		last if (/^\.(pp|lp|ip)/o && $npara++ > 1);
		print OUT;
	}
	$still_input_line = 1;
}
