#!/usr/local/bin/perl -w
#
# Remove extranous "<para></para>" lines from the handbook, but keep nice
# linebreaks.
#

use strict;

my $toprint = '';
my $seen = 0;
my $prev = '';

while(<>) {
    # If this is a blank line, save it up, we might need it.
    if($_ =~ /^\s*$/) {
	$toprint .= $_;
	next;
    }

    # If this is <para></para>, mark the fact that we've seen it
    $seen = 1 if $_ =~ /<para><\/para>/;
     
    # If this is not a blank line, and we haven't seen <para></para>, then
    # print everything in $toprint, then print this line
    if(! $seen) {
	print $toprint;
	print $_;
	$toprint = '';
	next;
    }

    # If this is the <para></para> line then skip it
    next if $_ =~ /<para><\/para>/;

    # We need to print this line. It *might* need an initial newline,
    #
    # if
    #    This line opens a new para
    # and
    #    The previous line closed a para
    #
    # otherwise print as is
    if(($_ =~ /<para>/) && ($prev =~ /<\/para>/)) {
	print "\n$_";
    } else {
	print $_;
    }

    $toprint = '';
    $seen = 0;
} continue {
    if(($_ !~ /^\s*$/) && ($_ !~ /<para><\/para>/)) {
	$prev = $_;
    }
}
